~nunit-core/nunitv2/2.5

« back to all changes in this revision

Viewing changes to src/NUnitCore/core/InternalTrace.cs

  • Committer: Charlie Poole
  • Date: 2010-12-21 00:53:07 UTC
  • Revision ID: charlie@nunit.org-20101221005307-m1qsj959lm1x2s52
Fix line endings

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// ****************************************************************
2
 
// Copyright 2008, Charlie Poole
3
 
// This is free software licensed under the NUnit license. You may
4
 
// obtain a copy of the license at http://nunit.org.
5
 
// ****************************************************************
6
 
 
7
 
using System;
8
 
 
9
 
namespace NUnit.Core
10
 
{
11
 
    /// <summary>
12
 
    /// InternalTraceLevel is an enumeration controlling the
13
 
    /// level of detailed presented in the internal log.
14
 
    /// </summary>
15
 
    public enum InternalTraceLevel
16
 
    {
17
 
        Default,
18
 
        Off,
19
 
        Error,
20
 
        Warning,
21
 
        Info,
22
 
        Verbose
23
 
    }
24
 
    
25
 
    /// <summary>
26
 
        /// Summary description for Logger.
27
 
        /// </summary>
28
 
        public class InternalTrace
29
 
        {
30
 
        private readonly static string TIME_FMT = "HH:mm:ss.fff";
31
 
 
32
 
                private static bool initialized;
33
 
 
34
 
        private static InternalTraceWriter writer;
35
 
        public static InternalTraceWriter Writer
36
 
        {
37
 
            get { return writer; }
38
 
        }
39
 
 
40
 
                public static InternalTraceLevel Level;
41
 
 
42
 
        public static void Initialize(string logName)
43
 
        {
44
 
            int lev = (int) new System.Diagnostics.TraceSwitch("NTrace", "NUnit internal trace").Level;
45
 
            Initialize(logName, (InternalTraceLevel)lev);
46
 
        }
47
 
 
48
 
        public static void Initialize(string logName, InternalTraceLevel level)
49
 
        {
50
 
                        if (!initialized)
51
 
                        {
52
 
                                Level = level;
53
 
 
54
 
                                if (writer == null && Level > InternalTraceLevel.Off)
55
 
                                {
56
 
                                        writer = new InternalTraceWriter(logName);
57
 
                                        writer.WriteLine("InternalTrace: Initializing at level " + Level.ToString());
58
 
                                }
59
 
 
60
 
                                initialized = true;
61
 
                        }
62
 
        }
63
 
 
64
 
        public static void Flush()
65
 
        {
66
 
            if (writer != null)
67
 
                writer.Flush();
68
 
        }
69
 
 
70
 
        public static void Close()
71
 
        {
72
 
            if (writer != null)
73
 
                writer.Close();
74
 
 
75
 
            writer = null;
76
 
        }
77
 
 
78
 
        public static Logger GetLogger(string name)
79
 
                {
80
 
                        return new Logger( name );
81
 
                }
82
 
 
83
 
                public static Logger GetLogger( Type type )
84
 
                {
85
 
                        return new Logger( type.FullName );
86
 
                }
87
 
 
88
 
        public static void Log(InternalTraceLevel level, string message, string category)
89
 
        {
90
 
            Log(level, message, category, null);
91
 
        }
92
 
 
93
 
        public static void Log(InternalTraceLevel level, string message, string category, Exception ex)
94
 
        {
95
 
            Writer.WriteLine("{0} {1,-5} [{2,2}] {3}: {4}",
96
 
                DateTime.Now.ToString(TIME_FMT),
97
 
                level == InternalTraceLevel.Verbose ? "Debug" : level.ToString(),
98
 
#if NET_2_0
99
 
                System.Threading.Thread.CurrentThread.ManagedThreadId,
100
 
#else
101
 
                AppDomain.GetCurrentThreadId(),
102
 
#endif
103
 
                category,
104
 
                message);
105
 
 
106
 
            if (ex != null)
107
 
                Writer.WriteLine(ex.ToString());
108
 
        }
109
 
    }
110
 
}
 
1
// ****************************************************************
 
2
// Copyright 2008, Charlie Poole
 
3
// This is free software licensed under the NUnit license. You may
 
4
// obtain a copy of the license at http://nunit.org.
 
5
// ****************************************************************
 
6
 
 
7
using System;
 
8
 
 
9
namespace NUnit.Core
 
10
{
 
11
    /// <summary>
 
12
    /// InternalTraceLevel is an enumeration controlling the
 
13
    /// level of detailed presented in the internal log.
 
14
    /// </summary>
 
15
    public enum InternalTraceLevel
 
16
    {
 
17
        Default,
 
18
        Off,
 
19
        Error,
 
20
        Warning,
 
21
        Info,
 
22
        Verbose
 
23
    }
 
24
    
 
25
    /// <summary>
 
26
        /// Summary description for Logger.
 
27
        /// </summary>
 
28
        public class InternalTrace
 
29
        {
 
30
        private readonly static string TIME_FMT = "HH:mm:ss.fff";
 
31
 
 
32
                private static bool initialized;
 
33
 
 
34
        private static InternalTraceWriter writer;
 
35
        public static InternalTraceWriter Writer
 
36
        {
 
37
            get { return writer; }
 
38
        }
 
39
 
 
40
                public static InternalTraceLevel Level;
 
41
 
 
42
        public static void Initialize(string logName)
 
43
        {
 
44
            int lev = (int) new System.Diagnostics.TraceSwitch("NTrace", "NUnit internal trace").Level;
 
45
            Initialize(logName, (InternalTraceLevel)lev);
 
46
        }
 
47
 
 
48
        public static void Initialize(string logName, InternalTraceLevel level)
 
49
        {
 
50
                        if (!initialized)
 
51
                        {
 
52
                                Level = level;
 
53
 
 
54
                                if (writer == null && Level > InternalTraceLevel.Off)
 
55
                                {
 
56
                                        writer = new InternalTraceWriter(logName);
 
57
                                        writer.WriteLine("InternalTrace: Initializing at level " + Level.ToString());
 
58
                                }
 
59
 
 
60
                                initialized = true;
 
61
                        }
 
62
        }
 
63
 
 
64
        public static void Flush()
 
65
        {
 
66
            if (writer != null)
 
67
                writer.Flush();
 
68
        }
 
69
 
 
70
        public static void Close()
 
71
        {
 
72
            if (writer != null)
 
73
                writer.Close();
 
74
 
 
75
            writer = null;
 
76
        }
 
77
 
 
78
        public static Logger GetLogger(string name)
 
79
                {
 
80
                        return new Logger( name );
 
81
                }
 
82
 
 
83
                public static Logger GetLogger( Type type )
 
84
                {
 
85
                        return new Logger( type.FullName );
 
86
                }
 
87
 
 
88
        public static void Log(InternalTraceLevel level, string message, string category)
 
89
        {
 
90
            Log(level, message, category, null);
 
91
        }
 
92
 
 
93
        public static void Log(InternalTraceLevel level, string message, string category, Exception ex)
 
94
        {
 
95
            Writer.WriteLine("{0} {1,-5} [{2,2}] {3}: {4}",
 
96
                DateTime.Now.ToString(TIME_FMT),
 
97
                level == InternalTraceLevel.Verbose ? "Debug" : level.ToString(),
 
98
#if NET_2_0
 
99
                System.Threading.Thread.CurrentThread.ManagedThreadId,
 
100
#else
 
101
                AppDomain.GetCurrentThreadId(),
 
102
#endif
 
103
                category,
 
104
                message);
 
105
 
 
106
            if (ex != null)
 
107
                Writer.WriteLine(ex.ToString());
 
108
        }
 
109
    }
 
110
}