1 // ***********************************************************************
2 // Copyright (c) 2008-2013 Charlie Poole
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 // ***********************************************************************
25 #define NUNIT_FRAMEWORK
33 namespace NUnit.Engine.Internal
35 namespace NUnit.Framework.Internal
37 namespace NUnit.Common
41 /// InternalTrace provides facilities for tracing the execution
42 /// of the NUnit framework. Tests and classes under test may make use
43 /// of Console writes, System.Diagnostics.Trace or various loggers and
44 /// NUnit itself traps and processes each of them. For that reason, a
45 /// separate internal trace is needed.
48 /// InternalTrace uses a global lock to allow multiple threads to write
49 /// trace messages. This can easily make it a bottleneck so it must be
50 /// used sparingly. Keep the trace Level as low as possible and only
51 /// insert InternalTrace writes where they are needed.
52 /// TODO: add some buffering and a separate writer thread as an option.
53 /// TODO: figure out a way to turn on trace in specific classes only.
55 public static class InternalTrace
57 private static InternalTraceLevel traceLevel;
58 private static InternalTraceWriter traceWriter;
61 /// Gets a flag indicating whether the InternalTrace is initialized
63 public static bool Initialized { get; private set; }
67 /// Initialize the internal trace facility using the name of the log
68 /// to be written to and the trace level.
70 /// <param name="logName">The log name</param>
71 /// <param name="level">The trace level</param>
72 public static void Initialize(string logName, InternalTraceLevel level)
78 if (traceWriter == null && traceLevel > InternalTraceLevel.Off)
80 traceWriter = new InternalTraceWriter(logName);
81 traceWriter.WriteLine("InternalTrace: Initializing at level {0}", traceLevel);
87 traceWriter.WriteLine("InternalTrace: Ignoring attempted re-initialization at level {0}", level);
92 /// Initialize the internal trace using a provided TextWriter and level
94 /// <param name="writer">A TextWriter</param>
95 /// <param name="level">The InternalTraceLevel</param>
96 public static void Initialize(TextWriter writer, InternalTraceLevel level)
102 if (traceWriter == null && traceLevel > InternalTraceLevel.Off)
104 traceWriter = new InternalTraceWriter(writer);
105 traceWriter.WriteLine("InternalTrace: Initializing at level " + traceLevel.ToString());
113 /// Get a named Logger
115 /// <returns></returns>
116 public static Logger GetLogger(string name)
118 return new Logger(name, traceLevel, traceWriter);
122 /// Get a logger named for a particular Type.
124 public static Logger GetLogger(Type type)
126 return GetLogger(type.FullName);