5
// Aaron Bockover <abockover@novell.com>
7
// Copyright (C) 2005-2007 Novell, Inc.
9
// Permission is hereby granted, free of charge, to any person obtaining
10
// a copy of this software and associated documentation files (the
11
// "Software"), to deal in the Software without restriction, including
12
// without limitation the rights to use, copy, modify, merge, publish,
13
// distribute, sublicense, and/or sell copies of the Software, and to
14
// permit persons to whom the Software is furnished to do so, subject to
15
// the following conditions:
17
// The above copyright notice and this permission notice shall be
18
// included in all copies or substantial portions of the Software.
20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31
using System.Collections.Generic;
35
public class LogNotifyEventArgs : EventArgs
37
private LogEntry entry;
39
public LogNotifyEventArgs (LogEntry entry)
44
public LogEntry Entry {
49
public enum LogEntryType
60
private LogEntryType type;
61
private string message;
62
private string details;
63
private DateTime timestamp;
65
internal LogEntry (LogEntryType type, string message, string details)
68
this.message = message;
69
this.details = details;
70
this.timestamp = DateTime.Now;
73
public LogEntryType Type {
77
public string Message {
78
get { return message; }
81
public string Details {
82
get { return details; }
85
public DateTime TimeStamp {
86
get { return timestamp; }
90
public static class Log
92
public static event EventHandler<LogNotifyEventArgs> Notify;
94
private static Dictionary<uint, DateTime> timers = new Dictionary<uint, DateTime> ();
95
private static uint next_timer_id = 1;
97
private static bool debugging = false;
98
public static bool Debugging {
99
get { return debugging; }
100
set { debugging = value; }
103
private static bool tracing = false;
104
public static bool Tracing {
105
get { return tracing; }
106
set { tracing = value; }
109
public static void Commit (LogEntryType type, string message, string details, bool showUser)
111
if (type == LogEntryType.Debug && !Debugging) {
115
if (type == LogEntryType.Trace && !Tracing) {
119
if (type != LogEntryType.Information || (type == LogEntryType.Information && !showUser)) {
121
case LogEntryType.Error: ConsoleCrayon.ForegroundColor = ConsoleColor.Red; break;
122
case LogEntryType.Warning: ConsoleCrayon.ForegroundColor = ConsoleColor.Yellow; break;
123
case LogEntryType.Information: ConsoleCrayon.ForegroundColor = ConsoleColor.Green; break;
124
case LogEntryType.Debug: ConsoleCrayon.ForegroundColor = ConsoleColor.Blue; break;
125
case LogEntryType.Trace: ConsoleCrayon.ForegroundColor = ConsoleColor.Magenta; break;
128
Console.Write ("[{0} {1:00}:{2:00}:{3:00}.{4:000}]", TypeString (type), DateTime.Now.Hour,
129
DateTime.Now.Minute, DateTime.Now.Second, DateTime.Now.Millisecond);
131
ConsoleCrayon.ResetColor ();
133
if (details != null) {
134
Console.WriteLine (" {0} - {1}", message, details);
136
Console.WriteLine (" {0}", message);
141
OnNotify (new LogEntry (type, message, details));
144
if (type == LogEntryType.Trace) {
145
string str = String.Format ("MARK: {0}: {1}", message, details);
146
Mono.Unix.Native.Syscall.access(str, Mono.Unix.Native.AccessModes.F_OK);
150
private static string TypeString (LogEntryType type)
153
case LogEntryType.Debug: return "Debug";
154
case LogEntryType.Warning: return "Warn ";
155
case LogEntryType.Error: return "Error";
156
case LogEntryType.Information: return "Info ";
157
case LogEntryType.Trace: return "Trace";
162
private static void OnNotify (LogEntry entry)
164
EventHandler<LogNotifyEventArgs> handler = Notify;
165
if (handler != null) {
166
handler (null, new LogNotifyEventArgs (entry));
170
#region Timer Methods
172
public static uint DebugTimerStart (string message)
174
return TimerStart (message, false);
177
public static uint InformationTimerStart (string message)
179
return TimerStart (message, true);
182
private static uint TimerStart (string message, bool isInfo)
184
if (!Debugging && !isInfo) {
189
Information (message);
194
return TimerStart (isInfo);
197
public static uint DebugTimerStart ()
199
return TimerStart (false);
202
public static uint InformationTimerStart ()
204
return TimerStart (true);
207
private static uint TimerStart (bool isInfo)
209
if (!Debugging && !isInfo) {
213
uint timer_id = next_timer_id++;
214
timers.Add (timer_id, DateTime.Now);
218
public static void DebugTimerPrint (uint id)
224
TimerPrint (id, "Operation duration: {0}", false);
227
public static void DebugTimerPrint (uint id, string message)
233
TimerPrint (id, message, false);
236
public static void InformationTimerPrint (uint id)
238
TimerPrint (id, "Operation duration: {0}", true);
241
public static void InformationTimerPrint (uint id, string message)
243
TimerPrint (id, message, true);
246
private static void TimerPrint (uint id, string message, bool isInfo)
248
if (!Debugging && !isInfo) {
252
DateTime finish = DateTime.Now;
254
if (!timers.ContainsKey (id)) {
258
TimeSpan duration = finish - timers[id];
260
if (duration.TotalSeconds < 60) {
261
d_message = String.Format ("{0}s", duration.TotalSeconds);
263
d_message = duration.ToString ();
267
Information (message, d_message);
269
Debug (message, d_message);
275
#region Public Trace Methods
276
public static void Trace (string group, string format, params object [] args)
279
Commit (LogEntryType.Trace, group, String.Format (format, args), false);
284
#region Public Debug Methods
285
public static void Debug (string message)
288
Commit (LogEntryType.Debug, message, null, false);
292
public static void Debug (string format, params object [] args)
295
Debug (String.Format (format, args));
299
public static void DebugException (Exception e)
305
public static void DebugException (string message, Exception e)
308
Exception (message, e);
313
#region Public Information Methods
314
public static void Information (string message, string details, bool showUser)
316
Commit (LogEntryType.Information, message, details, showUser);
319
public static void Information (string message, bool showUser)
321
Information (message, null, showUser);
324
public static void Information (string format, params object [] args)
326
Information (String.Format (format, args), null, false);
330
#region Public Warning Methods
331
public static void Warning (string message, string details, bool showUser)
333
Commit (LogEntryType.Warning, message, details, showUser);
336
public static void Warning (string message, bool showUser)
338
Warning (message, null, showUser);
341
public static void Warning (string format, params object [] args)
343
Warning (String.Format (format, args), false);
347
#region Public Error Methods
348
public static void Error (string message, string details, bool showUser)
350
Commit (LogEntryType.Error, message, details, showUser);
353
public static void Error (string message, bool showUser)
355
Error (message, null, showUser);
358
public static void Error (string format, params object [] args)
360
Error (String.Format (format, args), null, false);
364
#region Public Exception Methods
366
public static void Exception (Exception e)
371
public static void Exception (string message, Exception e)
373
Stack<Exception> exception_chain = new Stack<Exception> ();
374
StringBuilder builder = new StringBuilder ();
377
exception_chain.Push (e);
378
e = e.InnerException;
381
while (exception_chain.Count > 0) {
382
e = exception_chain.Pop ();
383
builder.AppendFormat ("{0} (in `{1}')", e.Message, e.Source).AppendLine ();
384
builder.Append (e.StackTrace);
385
if (exception_chain.Count > 0) {
386
builder.AppendLine ();
390
// FIXME: We should save these to an actual log file
391
Log.Warning (message ?? "Caught an exception", builder.ToString (), false);
394
public static void Exception (Exception e, string format, params object [] args)
396
Exception (String.Format (format, args), e);