annotate Logging/Logger.cs @ 94:6bbcb3ef79d2

Re #47: Add translatable GTK# widgets * Remove "set" part of Name in translatable interface as not all GTK widgets support it and it isn't necessary
author IBBoard <dev@ibboard.co.uk>
date Tue, 28 Dec 2010 13:52:06 +0000
parents 90b9a3fe3c18
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
16
0352fa33ee8f Closes #8 - license code
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
1 // This file (Logger.cs) is a part of the IBBoard library and is copyright 2009 IBBoard.
0352fa33ee8f Closes #8 - license code
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
2 //
0352fa33ee8f Closes #8 - license code
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
3 // The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license.
0352fa33ee8f Closes #8 - license code
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
4
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
5 using System;
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
6 using System.IO;
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
7 using System.Text;
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
8 using System.Collections.Generic;
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
9
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
10 namespace IBBoard.Logging
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
11 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
12 public enum LogLevel {Debug = 1, Info = 2, Warning = 3, Error = 4, Critical = 5}
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
13 /// <summary>
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
14 /// Summary description for Logger.
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
15 /// </summary>
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
16 public abstract class Logger : TextWriter
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
17 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
18
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
19 protected LogLevel logLevel;
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
20 protected Stream stream;
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
21 protected UTF8Encoding encoding = new UTF8Encoding();
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
22 private List<LogItem> logMessages = new List<LogItem>();
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
23
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
24 public delegate void LogUpdatedDelegate(LogItem item);
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
25 public event LogUpdatedDelegate LogUpdatedEvent;
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
26
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
27 protected Logger(Stream logStream)
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
28 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
29 logLevel = LogLevel.Error;
0
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
30 stream = logStream;
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
31
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
32 //null stream means we're not actually logging out so we don't need to check it is writable
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
33 if (stream!=null)
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
34 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
35 if (!stream.CanWrite)
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
36 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
37 throw new ArgumentException("Log stream was not writable");
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
38 }
0
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
39 }
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
40
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
41 LogMessageString("Log started at " + String.Format("{0:HH:mm:ss, yyyy-MM-dd}", DateTime.Now));
0
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
42 }
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
43
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
44 ~Logger()
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
45 {
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
46 LogMessageString("Log closed at " + String.Format("{0:HH:mm:ss, yyyy-MM-dd}", DateTime.Now));
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
47 }
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
48
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
49 public LogLevel LogLevel
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
50 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
51 get { return logLevel; }
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
52 set { logLevel = value; }
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
53 }
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
54
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
55 public void Log(Exception ex, LogLevel level)
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
56 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
57 Log(ex.Message, ex.StackTrace, level);
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
58 }
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
59
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
60 public void Log(string message, LogLevel level)
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
61 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
62 Log(message, "", level);
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
63 }
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
64
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
65 private void Log(string message, string stacktrace, LogLevel level)
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
66 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
67 if (level >= LogLevel)
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
68 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
69 LogItem item = new LogItem(level, message, stacktrace);
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
70 LogMessage(item);
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
71 logMessages.Add(item);
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
72 OnLogUpdated(item);
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
73 }
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
74 }
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
75
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
76 private void OnLogUpdated(LogItem item)
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
77 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
78 if (LogUpdatedEvent!=null)
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
79 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
80 LogUpdatedEvent(item);
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
81 }
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
82 }
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
83
0
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
84 protected abstract void LogMessage(LogItem item);
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
85 protected abstract void LogMessageString(string str);
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
86
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
87 public void ResetInternalLog()
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
88 {
87
90b9a3fe3c18 * Correct licensing comments at top of files
IBBoard <dev@ibboard.co.uk>
parents: 37
diff changeset
89 logMessages = new List<LogItem>();
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
90 LogMessageString("Log reset at " + String.Format("{0:yyyy-MM-dd-HHmmss}", DateTime.Now));
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
91 }
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
92
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
93 public int LogLength
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
94 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
95 get { return logMessages.Count; }
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
96 }
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
97
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
98 public LogItem GetLogItem(int index)
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
99 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
100 if (index < LogLength)
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
101 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
102 return logMessages[index];
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
103 }
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
104 else
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
105 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
106 throw new IndexOutOfRangeException();
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
107 }
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
108 }
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
109
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
110 public LogItem[] GetLogItems(LogLevel minLogLevel)
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
111 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
112 List<LogItem> logItems = new List<LogItem>();
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
113
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
114 foreach (LogItem item in logMessages)
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
115 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
116 if (item.Level>=minLogLevel)
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
117 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
118 logItems.Add(item);
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
119 }
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
120 }
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
121
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
122 return logItems.ToArray();
0
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
123 }
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
124
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
125 //Allow the Logger to be used as the output point for System.Console.Error. In this case assume all messages are Error level
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
126 public override Encoding Encoding {
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
127 get { return encoding; }
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
128 }
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
129
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
130 public override void Write (object value)
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
131 {
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
132 if (value is Exception)
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
133 {
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
134 Log((Exception)value, LogLevel.Error);
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
135 }
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
136 }
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
137
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
138 public override void Write (string value)
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
139 {
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
140 Log(value, LogLevel.Error);
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
141 }
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
142
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
143 public override void WriteLine(object value)
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
144 {
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
145 Write(value);
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
146 }
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
147
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
148 public override void WriteLine (string value)
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
149 {
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
150 Write(value);
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
151 }
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
152
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
153 }
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
154 }