Mercurial > repos > IBBoard
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 |
rev | line source |
---|---|
16 | 1 // This file (Logger.cs) is a part of the IBBoard library and is copyright 2009 IBBoard. |
2 // | |
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. | |
4 | |
37 | 5 using System; |
6 using System.IO; | |
7 using System.Text; | |
8 using System.Collections.Generic; | |
9 | |
10 namespace IBBoard.Logging | |
11 { | |
12 public enum LogLevel {Debug = 1, Info = 2, Warning = 3, Error = 4, Critical = 5} | |
13 /// <summary> | |
14 /// Summary description for Logger. | |
15 /// </summary> | |
16 public abstract class Logger : TextWriter | |
17 { | |
18 | |
19 protected LogLevel logLevel; | |
20 protected Stream stream; | |
21 protected UTF8Encoding encoding = new UTF8Encoding(); | |
22 private List<LogItem> logMessages = new List<LogItem>(); | |
23 | |
24 public delegate void LogUpdatedDelegate(LogItem item); | |
25 public event LogUpdatedDelegate LogUpdatedEvent; | |
26 | |
27 protected Logger(Stream logStream) | |
28 { | |
29 logLevel = LogLevel.Error; | |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
30 stream = logStream; |
37 | 31 |
32 //null stream means we're not actually logging out so we don't need to check it is writable | |
33 if (stream!=null) | |
34 { | |
35 if (!stream.CanWrite) | |
36 { | |
37 throw new ArgumentException("Log stream was not writable"); | |
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 | 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 | 47 } |
48 | |
49 public LogLevel LogLevel | |
50 { | |
51 get { return logLevel; } | |
52 set { logLevel = value; } | |
53 } | |
54 | |
55 public void Log(Exception ex, LogLevel level) | |
56 { | |
57 Log(ex.Message, ex.StackTrace, level); | |
58 } | |
59 | |
60 public void Log(string message, LogLevel level) | |
61 { | |
62 Log(message, "", level); | |
63 } | |
64 | |
65 private void Log(string message, string stacktrace, LogLevel level) | |
66 { | |
67 if (level >= LogLevel) | |
68 { | |
69 LogItem item = new LogItem(level, message, stacktrace); | |
70 LogMessage(item); | |
71 logMessages.Add(item); | |
72 OnLogUpdated(item); | |
73 } | |
74 } | |
75 | |
76 private void OnLogUpdated(LogItem item) | |
77 { | |
78 if (LogUpdatedEvent!=null) | |
79 { | |
80 LogUpdatedEvent(item); | |
81 } | |
82 } | |
83 | |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
84 protected abstract void LogMessage(LogItem item); |
37 | 85 protected abstract void LogMessageString(string str); |
86 | |
87 public void ResetInternalLog() | |
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 | 90 LogMessageString("Log reset at " + String.Format("{0:yyyy-MM-dd-HHmmss}", DateTime.Now)); |
91 } | |
92 | |
93 public int LogLength | |
94 { | |
95 get { return logMessages.Count; } | |
96 } | |
97 | |
98 public LogItem GetLogItem(int index) | |
99 { | |
100 if (index < LogLength) | |
101 { | |
102 return logMessages[index]; | |
103 } | |
104 else | |
105 { | |
106 throw new IndexOutOfRangeException(); | |
107 } | |
108 } | |
109 | |
110 public LogItem[] GetLogItems(LogLevel minLogLevel) | |
111 { | |
112 List<LogItem> logItems = new List<LogItem>(); | |
113 | |
114 foreach (LogItem item in logMessages) | |
115 { | |
116 if (item.Level>=minLogLevel) | |
117 { | |
118 logItems.Add(item); | |
119 } | |
120 } | |
121 | |
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 | 152 |
153 } | |
154 } |