[NUI] Rebase develnui (DevelNUI only patches --> master) (#3910)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Devel.Tests.Ubuntu / nunit.framework / Common / Logging / Logger.cs
1 // ***********************************************************************
2 // Copyright (c) 2008-2013 Charlie Poole
3 //
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:
11 // 
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 // 
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 // ***********************************************************************
23 #define PORTABLE
24 #define TIZEN
25 #define NUNIT_FRAMEWORK
26 #define NUNITLITE
27 #define NET_4_5
28 #define PARALLEL
29 using System;
30 using System.IO;
31
32 #if NUNIT_ENGINE
33 namespace NUnit.Engine.Internal
34 #elif NUNIT_FRAMEWORK
35 namespace NUnit.Framework.Internal
36 #else
37 namespace NUnit.Common
38 #endif
39 {
40     /// <summary>
41     /// Provides internal logging to the NUnit framework
42     /// </summary>
43     public class Logger : ILogger
44     {
45         private readonly static string TIME_FMT = "HH:mm:ss.fff";
46         private readonly static string TRACE_FMT = "{0} {1,-5} [{2,2}] {3}: {4}";
47
48         private string name;
49         private string fullname;
50         private InternalTraceLevel maxLevel;
51         private TextWriter writer;
52
53         /// <summary>
54         /// Initializes a new instance of the <see cref="Logger"/> class.
55         /// </summary>
56         /// <param name="name">The name.</param>
57         /// <param name="level">The log level.</param>
58         /// <param name="writer">The writer where logs are sent.</param>
59         public Logger(string name, InternalTraceLevel level, TextWriter writer)
60         {
61             this.maxLevel = level;
62             this.writer = writer;
63             this.fullname = this.name = name;
64             int index = fullname.LastIndexOf('.');
65             if (index >= 0)
66                 this.name = fullname.Substring(index + 1);
67         }
68
69         #region Error
70         /// <summary>
71         /// Logs the message at error level.
72         /// </summary>
73         /// <param name="message">The message.</param>
74         public void Error(string message)
75         {
76             Log(InternalTraceLevel.Error, message);
77         }
78
79         /// <summary>
80         /// Logs the message at error level.
81         /// </summary>
82         /// <param name="message">The message.</param>
83         /// <param name="args">The message arguments.</param>
84         public void Error(string message, params object[] args)
85         {
86             Log(InternalTraceLevel.Error, message, args);
87         }
88
89         //public void Error(string message, Exception ex)
90         //{
91         //    if (service.Level >= InternalTraceLevel.Error)
92         //    {
93         //        service.Log(InternalTraceLevel.Error, message, name, ex);
94         //    }
95         //}
96         #endregion
97
98         #region Warning
99         /// <summary>
100         /// Logs the message at warm level.
101         /// </summary>
102         /// <param name="message">The message.</param>
103         public void Warning(string message)
104         {
105             Log(InternalTraceLevel.Warning, message);
106         }
107
108         /// <summary>
109         /// Logs the message at warning level.
110         /// </summary>
111         /// <param name="message">The message.</param>
112         /// <param name="args">The message arguments.</param>
113         public void Warning(string message, params object[] args)
114         {
115             Log(InternalTraceLevel.Warning, message, args);
116         }
117         #endregion
118
119         #region Info
120         /// <summary>
121         /// Logs the message at info level.
122         /// </summary>
123         /// <param name="message">The message.</param>
124         public void Info(string message)
125         {
126             Log(InternalTraceLevel.Info, message);
127         }
128
129         /// <summary>
130         /// Logs the message at info level.
131         /// </summary>
132         /// <param name="message">The message.</param>
133         /// <param name="args">The message arguments.</param>
134         public void Info(string message, params object[] args)
135         {
136             Log(InternalTraceLevel.Info, message, args);
137         }
138         #endregion
139
140         #region Debug
141         /// <summary>
142         /// Logs the message at debug level.
143         /// </summary>
144         /// <param name="message">The message.</param>
145         public void Debug(string message)
146         {
147             Log(InternalTraceLevel.Verbose, message);
148         }
149
150         /// <summary>
151         /// Logs the message at debug level.
152         /// </summary>
153         /// <param name="message">The message.</param>
154         /// <param name="args">The message arguments.</param>
155         public void Debug(string message, params object[] args)
156         {
157             Log(InternalTraceLevel.Verbose, message, args);
158         }
159         #endregion
160
161         #region Helper Methods
162         private void Log(InternalTraceLevel level, string message)
163         {
164             if (writer != null && this.maxLevel >= level)
165                 WriteLog(level, message);
166         }
167
168         private void Log(InternalTraceLevel level, string format, params object[] args)
169         {
170             if (this.maxLevel >= level)
171                 WriteLog(level, string.Format( format, args ) );
172         }
173
174         private void WriteLog(InternalTraceLevel level, string message)
175         {
176             writer.WriteLine(TRACE_FMT,
177                 DateTime.Now.ToString(TIME_FMT),
178                 level == InternalTraceLevel.Verbose ? "Debug" : level.ToString(),
179 #if PORTABLE
180                 System.Environment.CurrentManagedThreadId,
181 #else
182                 System.Threading.Thread.CurrentThread.ManagedThreadId,
183 #endif
184                 name,
185                 message);
186         }
187
188 #endregion
189     }
190 }