PageRenderTime 30ms CodeModel.GetById 14ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NUnit/core/InternalTrace.cs

#
C# | 110 lines | 80 code | 18 blank | 12 comment | 11 complexity | 57a00c24d23178ced080db67510604be MD5 | raw file
  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
  7using System;
  8
  9namespace 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}