ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/RomCheater/trunk/RomCheater.Logging/logger.cs
Revision: 163
Committed: Mon May 28 07:22:19 2012 UTC (11 years, 4 months ago) by william
File size: 15489 byte(s)
Log Message:
+ add warn log support

File Contents

# Content
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Reflection;
6 using System.Windows.Forms;
7
8 namespace RomCheater.Logging
9 {
10 public interface ILogger
11 {
12 void Write(string format, params object[] args);
13 void WriteLine(string format, params object[] args);
14 }
15
16
17 #region loggerflags
18 public class loggerflagsbase<TValue>
19 where TValue : IConvertible
20 {
21 static loggerflagsbase()
22 {
23 value_pairs = new Dictionary<TValue, EnumNameValuePair<TValue>>();
24 name_pairs = new Dictionary<string, EnumNameValuePair<TValue>>();
25
26 init_dicts();
27 }
28 protected loggerflagsbase()
29 {
30 CurrentValue = NONE;
31 }
32 //protected loggerflags(ushort t) : this() { CurrentValue = new EnumNameValuePair< ushort>("", t); }
33 protected loggerflagsbase(EnumNameValuePair< TValue> t) { CurrentValue = t; }
34 protected loggerflagsbase(loggerflagsbase<TValue> t) { this.CurrentValue = t.CurrentValue; }
35 private static void init_dicts()
36 {
37 value_pairs = new Dictionary<TValue, EnumNameValuePair< TValue>>();
38 name_pairs = new Dictionary<string, EnumNameValuePair< TValue>>();
39 add_dict_entry(NONE);
40 add_dict_entry(INFO);
41 add_dict_entry(DEBUG);
42 add_dict_entry(ERROR);
43 add_dict_entry(VERBOSE_DEBUG);
44 add_dict_entry(VERBOSE_ERROR);
45 add_dict_entry(DEFAULT);
46 add_dict_entry(ALL);
47 }
48
49 private static void add_dict_entry(EnumNameValuePair<TValue> value)
50 {
51 try
52 {
53 value_pairs.Add(value, value);
54 name_pairs.Add(value, value);
55 }
56 catch { }
57 }
58
59 #region implicit operators
60 public static implicit operator loggerflagsbase<TValue>(EnumNameValuePair<TValue> t) { return new loggerflagsbase<TValue>(t); }
61 public static implicit operator EnumNameValuePair<TValue>(loggerflagsbase<TValue> t) { return new loggerflagsbase<TValue>(t); }
62 public static implicit operator loggerflagsbase<TValue>(TValue t)
63 {
64 foreach (KeyValuePair<TValue, EnumNameValuePair<TValue>> pair in value_pairs) { EnumNameValuePair<TValue> l = pair.Value; if (l.Value.Equals(t)) { return new loggerflagsbase<TValue>(l); } }
65 return loggerflagsbase<TValue>.NONE;
66 }
67 public static implicit operator TValue(loggerflagsbase<TValue> t) { return t.CurrentValue.Value; }
68 public static implicit operator string(loggerflagsbase<TValue> t) { return t.CurrentValue.Name; }
69
70 #region operator overloads
71 public static bool operator ==(loggerflagsbase<TValue> x, loggerflagsbase<TValue> y) { return x.CurrentValue == y.CurrentValue; }
72 public static bool operator !=(loggerflagsbase<TValue> x, loggerflagsbase<TValue> y) { return x.CurrentValue != y.CurrentValue; }
73 public override bool Equals(object obj)
74 {
75 loggerflags t = (obj as loggerflags);
76 if (t == null) return false;
77 return this.CurrentValue.Equals(t);
78 }
79 public override int GetHashCode()
80 {
81 return CurrentValue.GetHashCode();
82 }
83 public override string ToString()
84 {
85
86 return CurrentValue.ToString();
87 }
88 #endregion
89 #endregion
90 #region binary bit flags
91 public static EnumNameValuePair<TValue> NONE = new EnumNameValuePair<TValue>("NONE", Binary<TValue>.ToValue("00000"));
92 public static EnumNameValuePair<TValue> INFO = new EnumNameValuePair<TValue>("INFO", Binary<TValue>.ToValue("00001"));
93 public static EnumNameValuePair<TValue> DEBUG = new EnumNameValuePair<TValue>("DEBUG", Binary<TValue>.ToValue("00010"));
94 public static EnumNameValuePair<TValue> ERROR = new EnumNameValuePair<TValue>("ERROR", Binary<TValue>.ToValue("00100"));
95 public static EnumNameValuePair<TValue> VERBOSE_DEBUG = new EnumNameValuePair<TValue>("VERBOSE_DEBUG", Binary<TValue>.ToValue("01000"));
96 public static EnumNameValuePair<TValue> VERBOSE_ERROR = new EnumNameValuePair<TValue>("VERBOSE_ERROR", Binary<TValue>.ToValue("10000"));
97 public static EnumNameValuePair<TValue> WARN = new EnumNameValuePair<TValue>("WARN", Binary<TValue>.ToValue("100000"));
98 public static EnumNameValuePair<TValue> DEFAULT = new EnumNameValuePair<TValue>("DEFAULT", EnumNameValuePair<TValue>.bitwise_or(INFO, DEBUG, ERROR, WARN));
99 public static EnumNameValuePair<TValue> ALL = new EnumNameValuePair<TValue>("ALL", EnumNameValuePair<TValue>.bitwise_or(DEFAULT, VERBOSE_DEBUG, VERBOSE_ERROR));
100 #endregion
101
102 protected static Dictionary<TValue, EnumNameValuePair<TValue>> value_pairs;
103 protected static Dictionary<string, EnumNameValuePair<TValue>> name_pairs;
104 private EnumNameValuePair<TValue> CurrentValue { get; set; }
105
106 public bool HasFlag(TValue flag)
107 {
108 bool hasflag = false;
109 TValue value = this.CurrentValue;
110 if (!(EnumNameValuePair<TValue>.bitwise_and(flag, value)).Equals(value))
111 hasflag = true;
112 return hasflag;
113 }
114
115 public static List<TValue> GetValues() { return value_pairs.Keys.ToList(); }
116 public static List<string> GetNames() { return name_pairs.Keys.ToList(); }
117
118 public string Name { get { return CurrentValue.Name; } }
119 public TValue Value { get { return CurrentValue.Value; } }
120 }
121 //[Flags]
122 public class loggerflags : loggerflagsbase<ushort>
123 {
124 protected loggerflags() :base() { }
125 //protected loggerflags(ushort t) : this() { CurrentValue = new EnumNameValuePair< ushort>("", t); }
126 protected loggerflags(EnumNameValuePair<ushort> t) : base(t) { }
127 protected loggerflags(loggerflagsbase<ushort> t) : base(t) { }
128 #region implicit operators
129 public static implicit operator loggerflags(EnumNameValuePair<ushort> t) { return new loggerflags(t); }
130 public static implicit operator EnumNameValuePair<ushort>(loggerflags t) { return new loggerflags(t.Value); }
131 public static implicit operator ushort(loggerflags t) { return t.Value; }
132 public static implicit operator loggerflags(ushort t) { return new loggerflags(t); }
133 public static implicit operator string(loggerflags t) { return t.Name; }
134 #endregion
135
136 }
137
138 public static class logger
139 {
140 private static loggerflags logging_flags;
141 static logger() { SetLoggingFlags(loggerflags.DEFAULT); }
142 public static void SetLoggingFlags(loggerflags flags) { logging_flags = flags; }
143 public static loggerflags GetLoggingFlags() { return logging_flags; }
144 #region sub-classes
145 private static string CreateTimeStamp()
146 {
147 string timestamp = "";
148 DateTime now = DateTime.Now;
149 timestamp = now.ToString("yyyy/MM/dd HH:mm:ss tt: ");
150 return timestamp;
151 }
152 public static class Info
153 {
154 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp()+ "(INFO) " + format; }
155 public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.INFO)) { xlogger.Write(CreateNewFormat(format), args); } }
156 public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.INFO)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
157 }
158 public static class Warn
159 {
160 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(WARN) " + format; }
161 public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.WARN)) { xlogger.Write(CreateNewFormat(format), args); } }
162 public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.WARN)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
163 }
164 public static class Debug
165 {
166 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(DEBUG) " + format; }
167 public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.DEBUG)) { xlogger.Write(CreateNewFormat(format), args); } }
168 public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.DEBUG)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
169 }
170 public static class VerboseDebug
171 {
172 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(VERBOSE DEBUG) " + format; }
173 public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.VERBOSE_DEBUG)) { xlogger.Write(CreateNewFormat(format), args); } }
174 public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.VERBOSE_DEBUG)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
175 }
176 public static class Error
177 {
178 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(ERROR) " + format; }
179 public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.ERROR)) { xlogger.Write(CreateNewFormat(format), args); } }
180 public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.ERROR)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
181 }
182 public static class VerboseError
183 {
184 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(VERBOSE ERROR) " + format; }
185 public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.VERBOSE_ERROR)) { xlogger.Write(CreateNewFormat(format), args); } }
186 public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.VERBOSE_ERROR)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
187 }
188 #region Force logging
189 public static class ForceLog
190 {
191 public static class Info
192 {
193 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(FORCED INFO) " + format; }
194 public static void Write(string format, params object[] args) { xlogger.Write(CreateNewFormat(format), args); }
195 public static void WriteLine(string format, params object[] args) { xlogger.WriteLine(CreateNewFormat(format), args); }
196 }
197 public static class Warn
198 {
199 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(FORCED Warn) " + format; }
200 public static void Write(string format, params object[] args) { xlogger.Write(CreateNewFormat(format), args); }
201 public static void WriteLine(string format, params object[] args) { xlogger.WriteLine(CreateNewFormat(format), args); }
202 }
203 public static class Debug
204 {
205 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(FORCED DEBUG) " + format; }
206 public static void Write(string format, params object[] args) { xlogger.Write(CreateNewFormat(format), args); }
207 public static void WriteLine(string format, params object[] args) { xlogger.WriteLine(CreateNewFormat(format), args); }
208 }
209 public static class Error
210 {
211 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(FORCED ERROR) " + format; }
212 public static void Write(string format, params object[] args) { xlogger.Write(CreateNewFormat(format), args); }
213 public static void WriteLine(string format, params object[] args) { xlogger.WriteLine(CreateNewFormat(format), args); }
214 }
215 }
216 #endregion
217 #endregion
218
219 }
220 #endregion
221
222 #region internal static class Logger
223 internal static class xlogger
224 {
225 private static logwriter lh;
226 static xlogger() { lh = new logwriter(); }
227
228 #region ILogger Members
229 public static void Write(string format, params object[] args)
230 {
231 init();
232 lh.Write(format, args);
233 }
234 public static void WriteLine(string format, params object[] args)
235 {
236 init();
237 lh.WriteLine(format, args);
238 }
239 #endregion
240
241 #region Reflection Support
242 private static void init()
243 {
244 Assembly asm = Assembly.GetEntryAssembly();
245 Type[] types = asm.GetTypes();
246
247 foreach (Type t in types)
248 {
249 if (t.BaseType == typeof(Form))
250 {
251 LogWriter lw = null;
252 PropertyInfo[] properties = t.GetProperties(BindingFlags.NonPublic | BindingFlags.Static);
253 foreach (PropertyInfo prop in properties)
254 {
255 if (prop.PropertyType == typeof(LogWriter))
256 {
257 try
258 {
259 lw = (LogWriter)prop.GetValue(null, null);
260 lh = new logwriter(lw);
261 break;
262 }
263 catch (Exception)
264 {
265 throw;
266 }
267 }
268 }
269 }
270 }
271 }
272 #endregion
273 }
274 #endregion
275
276 #region internal class LogHelper : ILogger
277 internal class logwriter : ILogger
278 {
279 private LogWriter writer;
280 public logwriter() : this(null) { }
281 public logwriter(LogWriter writer)
282 {
283 this.writer = writer;
284 }
285 #region ILogger Members
286 public void Write(string format, params object[] args)
287 {
288 WriteToLog(format,false, args);
289 }
290 public void WriteLine(string format, params object[] args)
291 {
292 WriteToLog(format,true, args);
293 }
294 private void WriteToLog(string format, bool newline, params object[] args)
295 {
296 if (writer != null)
297 {
298 if (newline)
299 {
300 writer.Log.WriteLine(format, args);
301 }
302 else
303 {
304 writer.Log.Write(format, args);
305 }
306 }
307 }
308 #endregion
309 }
310 #endregion
311 }