ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/RomCheater/trunk/RomCheater.Logging/logger.cs
Revision: 692
Committed: Mon Jun 17 10:29:30 2013 UTC (10 years, 5 months ago) by william
File size: 23281 byte(s)
Log Message:
+ re-enable configured logging flags, removed conditional flags

File Contents

# Content
1 #region Logging Defines
2 // include this any class or method that required logging, and comment-out what is not needed
3 #define LOGGING_ENABLED // this is only valid within the logger class
4 #region Enabled logging levels
5 #define LOGGING_ENABLE_INFO
6 #define LOGGING_ENABLE_WARN
7 #define LOGGING_ENABLE_DEBUG
8 #define LOGGING_ENABLE_VERBOSEDEBUG
9 #define LOGGING_ENABLE_ERROR
10 #define LOGGING_ENABLE_VERBOSEERROR
11 #define LOGGING_ENABLE_PROFILER
12 #endregion
13 #endregion
14
15
16 using System;
17 using System.Collections.Generic;
18 using System.Linq;
19 using System.Text;
20 using System.Reflection;
21 using System.Windows.Forms;
22 using System.IO;
23 using System.Diagnostics;
24
25 namespace RomCheater.Logging
26 {
27 //public interface ILogger
28 //{
29 // void Write(string format, params object[] args);
30 // void WriteLine(string format, params object[] args);
31 //}
32
33
34 #region loggerflags
35 public class loggerflagsbase<TValue>
36 where TValue : IConvertible
37 {
38 static loggerflagsbase()
39 {
40 value_pairs = new Dictionary<TValue, EnumNameValuePair<TValue>>();
41 name_pairs = new Dictionary<string, EnumNameValuePair<TValue>>();
42
43 init_dicts();
44 }
45 protected loggerflagsbase()
46 {
47 CurrentValue = NONE;
48 }
49 //protected loggerflags(ushort t) : this() { CurrentValue = new EnumNameValuePair< ushort>("", t); }
50 protected loggerflagsbase(EnumNameValuePair<TValue> t) { CurrentValue = t; }
51 protected loggerflagsbase(loggerflagsbase<TValue> t) { this.CurrentValue = t.CurrentValue; }
52 private static void init_dicts()
53 {
54 value_pairs = new Dictionary<TValue, EnumNameValuePair<TValue>>();
55 name_pairs = new Dictionary<string, EnumNameValuePair<TValue>>();
56 add_dict_entry(NONE);
57 add_dict_entry(INFO);
58 add_dict_entry(DEBUG);
59 add_dict_entry(ERROR);
60 add_dict_entry(VERBOSE_DEBUG);
61 add_dict_entry(VERBOSE_ERROR);
62 add_dict_entry(WARN);
63 add_dict_entry(PROFILER);
64 add_dict_entry(DEFAULT);
65 add_dict_entry(ALL);
66 }
67
68 private static void add_dict_entry(EnumNameValuePair<TValue> value)
69 {
70 try
71 {
72 value_pairs.Add(value, value);
73 name_pairs.Add(value, value);
74 }
75 catch { }
76 }
77
78 #region implicit operators
79 public static implicit operator loggerflagsbase<TValue>(EnumNameValuePair<TValue> t) { return new loggerflagsbase<TValue>(t); }
80 public static implicit operator EnumNameValuePair<TValue>(loggerflagsbase<TValue> t) { return new loggerflagsbase<TValue>(t); }
81 public static implicit operator loggerflagsbase<TValue>(TValue t)
82 {
83 //foreach (KeyValuePair<TValue, EnumNameValuePair<TValue>> pair in value_pairs) { EnumNameValuePair<TValue> l = pair.Value; if (l.Value.Equals(t)) { return new loggerflagsbase<TValue>(l); } }
84 //return loggerflagsbase<TValue>.NONE;
85 List<string> pairs = new List<string>();
86 StringBuilder builder = new StringBuilder();
87 foreach (KeyValuePair<TValue, EnumNameValuePair<TValue>> pair in value_pairs)
88 {
89 EnumNameValuePair<TValue> enum_pair = pair.Value;
90 if (HasFlag(pair.Value, t) && enum_pair != NONE)
91 {
92 pairs.Add(enum_pair);
93 }
94 }
95 if (pairs.Count == 0)
96 {
97 return NONE;
98 }
99 else
100 {
101 int count = 0;
102 foreach (string pair in pairs)
103 {
104 if (count == 0)
105 {
106 builder.AppendLine(pair);
107 }
108 else
109 {
110 builder.AppendFormat(" | {0}", pair);
111 }
112 count++;
113 }
114 loggerflagsbase<TValue> rVal = new loggerflagsbase<TValue>(new EnumNameValuePair<TValue>(builder.ToString(), t));
115 return rVal;
116 }
117 }
118 public static implicit operator TValue(loggerflagsbase<TValue> t) { return t.CurrentValue.Value; }
119 public static implicit operator string(loggerflagsbase<TValue> t) { return t.CurrentValue.Name; }
120
121 #region operator overloads
122 public static bool operator ==(loggerflagsbase<TValue> x, loggerflagsbase<TValue> y) { return x.CurrentValue == y.CurrentValue; }
123 public static bool operator !=(loggerflagsbase<TValue> x, loggerflagsbase<TValue> y) { return x.CurrentValue != y.CurrentValue; }
124 public override bool Equals(object obj)
125 {
126 loggerflags t = (obj as loggerflags);
127 if (t == null) return false;
128 return this.CurrentValue.Equals(t);
129 }
130 public override int GetHashCode()
131 {
132 return CurrentValue.GetHashCode();
133 }
134 public override string ToString()
135 {
136
137 return CurrentValue.ToString();
138 }
139 #endregion
140 #endregion
141 #region binary bit flags
142 public static EnumNameValuePair<TValue> NONE = new EnumNameValuePair<TValue>("NONE", Binary<TValue>.ToValue("00000"));
143 public static EnumNameValuePair<TValue> INFO = new EnumNameValuePair<TValue>("INFO", Binary<TValue>.ToValue("00001"));
144 public static EnumNameValuePair<TValue> DEBUG = new EnumNameValuePair<TValue>("DEBUG", Binary<TValue>.ToValue("00010"));
145 public static EnumNameValuePair<TValue> ERROR = new EnumNameValuePair<TValue>("ERROR", Binary<TValue>.ToValue("00100"));
146 public static EnumNameValuePair<TValue> VERBOSE_DEBUG = new EnumNameValuePair<TValue>("VERBOSE_DEBUG", Binary<TValue>.ToValue("01000"));
147 public static EnumNameValuePair<TValue> VERBOSE_ERROR = new EnumNameValuePair<TValue>("VERBOSE_ERROR", Binary<TValue>.ToValue("10000"));
148 public static EnumNameValuePair<TValue> WARN = new EnumNameValuePair<TValue>("WARN", Binary<TValue>.ToValue("100000"));
149 public static EnumNameValuePair<TValue> PROFILER = new EnumNameValuePair<TValue>("PROFILER", Binary<TValue>.ToValue("1000000"));
150 public static EnumNameValuePair<TValue> DEFAULT = new EnumNameValuePair<TValue>("DEFAULT", EnumNameValuePair<TValue>.bitwise_or(INFO, ERROR, WARN));
151 public static EnumNameValuePair<TValue> ALL = new EnumNameValuePair<TValue>("ALL", EnumNameValuePair<TValue>.bitwise_or(DEFAULT,DEBUG, VERBOSE_DEBUG, VERBOSE_ERROR, PROFILER));
152 #endregion
153
154 protected static Dictionary<TValue, EnumNameValuePair<TValue>> value_pairs;
155 protected static Dictionary<string, EnumNameValuePair<TValue>> name_pairs;
156 private EnumNameValuePair<TValue> CurrentValue { get; set; }
157
158 public bool HasFlag(TValue flag)
159 {
160 bool hasflag = false;
161 TValue value = this.CurrentValue;
162 if ((EnumNameValuePair<TValue>.bitwise_and(flag, value)).Equals(flag))
163 hasflag = true;
164 return hasflag;
165 }
166 public static bool HasFlag(TValue flag, TValue compare)
167 {
168 bool hasflag = false;
169 TValue value = compare;
170 if ((EnumNameValuePair<TValue>.bitwise_and(flag, value)).Equals(flag))
171 hasflag = true;
172 return hasflag;
173 }
174 public static List<TValue> GetValues() { return value_pairs.Keys.ToList(); }
175 public static List<string> GetNames() { return name_pairs.Keys.ToList(); }
176
177 public string Name { get { return CurrentValue.Name; } }
178 public TValue Value { get { return CurrentValue.Value; } }
179 }
180 //[Flags]
181 public class loggerflags : loggerflagsbase<ushort>
182 {
183 protected loggerflags() : base() { }
184 protected loggerflags(EnumNameValuePair<ushort> t) : base(t) { }
185 protected loggerflags(loggerflagsbase<ushort> t) : base(t) { }
186 #region implicit operators
187 public static implicit operator loggerflags(EnumNameValuePair<ushort> t) { return new loggerflags(t); }
188 public static implicit operator EnumNameValuePair<ushort>(loggerflags t) { return new EnumNameValuePair<ushort>(t.Name, t.Value); }
189 public static implicit operator ushort(loggerflags t) { return t.Value; }
190 public static implicit operator loggerflags(ushort t) { return new loggerflags(t); }
191 public static implicit operator string(loggerflags t) { return t.Name; }
192 #endregion
193
194 }
195 #endregion
196
197 #region public static class logger
198 public static class logger
199 {
200 private static string GetLoggingClass()
201 {
202 const int methodindex = 4; // get this method from stacktrace
203 string c = string.Empty;
204 StackTrace stackTrace = new StackTrace();
205 var frames = stackTrace.GetFrames().ToList();
206 List<MethodBase> methods = new List<MethodBase>();
207 frames.ForEach(s => methods.Add(s.GetMethod()));
208
209 var method = methods[methodindex];
210 if (method.DeclaringType != null)
211 {
212 c = method.DeclaringType.Name;
213
214 bool GetNextMethod = true;
215 if (method.DeclaringType.FullName.ToLower().StartsWith("system") || method.DeclaringType.FullName.ToLower().StartsWith("microsoft") ||
216 c.ToLower() == "pluginbase" ||
217 c.ToLower() == "inputplugin" ||
218 c.ToLower() == "windowplugin" ||
219 c.ToLower() == "configplugin" ||
220 c.ToLower() == "usercontrolplugin"
221 )
222 {
223 int NextMethodCount = methodindex;
224 while (GetNextMethod)
225 {
226 if (NextMethodCount == -1)
227 {
228 throw new InvalidOperationException("Unable to find calling class");
229 }
230 method = methods[NextMethodCount--]; // walk backup the stack
231 if (method.DeclaringType != null)
232 {
233 c = method.DeclaringType.Name;
234 if (method.DeclaringType.FullName.ToLower().StartsWith("system") || method.DeclaringType.FullName.ToLower().StartsWith("microsoft"))
235 {
236 GetNextMethod = true;
237 }
238 else if (c.ToLower() == "pluginbase" ||
239 c.ToLower() == "inputplugin" ||
240 c.ToLower() == "windowplugin" ||
241 c.ToLower() == "configplugin" ||
242 c.ToLower() == "usercontrolplugin")
243 {
244 // ignore our abstract plugin classes
245 GetNextMethod = true;
246 }
247 else
248 {
249 GetNextMethod = false;
250 }
251 }
252 }
253 }
254 else
255 {
256 }
257
258 }
259 return string.Format("({0})",c);
260 }
261 private static loggerflags logging_flags;
262 static logger() { SetLoggingFlags(loggerflags.DEFAULT); }
263 public static void SetLoggingFlags(loggerflags flags) { logging_flags = flags; }
264 public static loggerflags GetLoggingFlags() { return logging_flags; }
265 #region sub-classes
266 private static string CreateTimeStamp()
267 {
268 string timestamp = "";
269 DateTime now = DateTime.Now;
270 timestamp = now.ToString("yyyy/MM/dd HH:mm:ss tt: ");
271 return string.Format("{0}", timestamp);
272 }
273 public static class Info
274 {
275 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(INFO) " + GetLoggingClass() + " " + format; }
276 //[Conditional("LOGGING_ENABLE_INFO")]
277 public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.INFO)) { xlogger.Write(CreateNewFormat(format), args); } }
278 //[Conditional("LOGGING_ENABLE_INFO")]
279 public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.INFO)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
280 }
281 public static class Warn
282 {
283 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(WARN) " + GetLoggingClass() + " " + format; }
284 //[Conditional("LOGGING_ENABLE_WARN")]
285 public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.WARN)) { xlogger.Write(CreateNewFormat(format), args); } }
286 //[Conditional("LOGGING_ENABLE_WARN")]
287 public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.WARN)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
288 }
289 public static class Debug
290 {
291 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(DEBUG) " + GetLoggingClass() + " " + format; }
292 //[Conditional("LOGGING_ENABLE_DEBUG")]
293 public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.DEBUG)) { xlogger.Write(CreateNewFormat(format), args); } }
294 //[Conditional("LOGGING_ENABLE_DEBUG")]
295 public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.DEBUG)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
296 }
297 public static class VerboseDebug
298 {
299 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(VERBOSE DEBUG) " + GetLoggingClass() + " " + format; }
300 //[Conditional("LOGGING_ENABLE_VERBOSEDEBUG")]
301 public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.VERBOSE_DEBUG)) { xlogger.Write(CreateNewFormat(format), args); } }
302 //[Conditional("LOGGING_ENABLE_VERBOSEDEBUG")]
303
304 public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.VERBOSE_DEBUG)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
305 }
306 public static class Error
307 {
308 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(ERROR) " + GetLoggingClass() + " " + format; }
309 //[Conditional("LOGGING_ENABLE_ERROR")]
310 public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.ERROR)) { xlogger.Write(CreateNewFormat(format), args); } }
311 //[Conditional("LOGGING_ENABLE_ERROR")]
312 public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.ERROR)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
313 }
314 public static class VerboseError
315 {
316 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(VERBOSE ERROR) " + GetLoggingClass() + " " + format; }
317 //[Conditional("LOGGING_ENABLE_VERBOSEERROR")]
318 public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.VERBOSE_ERROR)) { xlogger.Write(CreateNewFormat(format), args); } }
319 //[Conditional("LOGGING_ENABLE_VERBOSEERROR")]
320 public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.VERBOSE_ERROR)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
321 }
322 public static class Profiler
323 {
324 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(PROFILER) " + GetLoggingClass() + " " + format; }
325 //[Conditional("LOGGING_ENABLE_PROFILER")]
326 public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.PROFILER)) { xlogger.Write(CreateNewFormat(format), args); } }
327 //[Conditional("LOGGING_ENABLE_PROFILER")]
328 public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.PROFILER)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
329 }
330 #region Force logging
331 public static class ForceLog
332 {
333 public static class Info
334 {
335 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(FORCED INFO) " + GetLoggingClass() + " " + format; }
336 public static void Write(string format, params object[] args) { xlogger.Write(CreateNewFormat(format), args); }
337 public static void WriteLine(string format, params object[] args) { xlogger.WriteLine(CreateNewFormat(format), args); }
338 }
339 public static class Warn
340 {
341 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(FORCED Warn) " + GetLoggingClass() + " " + format; }
342 public static void Write(string format, params object[] args) { xlogger.Write(CreateNewFormat(format), args); }
343 public static void WriteLine(string format, params object[] args) { xlogger.WriteLine(CreateNewFormat(format), args); }
344 }
345 public static class Debug
346 {
347 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(FORCED DEBUG) " + GetLoggingClass() + " " + format; }
348 public static void Write(string format, params object[] args) { xlogger.Write(CreateNewFormat(format), args); }
349 public static void WriteLine(string format, params object[] args) { xlogger.WriteLine(CreateNewFormat(format), args); }
350 }
351 public static class Error
352 {
353 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(FORCED ERROR) " + GetLoggingClass() + " " + format; }
354 public static void Write(string format, params object[] args) { xlogger.Write(CreateNewFormat(format), args); }
355 public static void WriteLine(string format, params object[] args) { xlogger.WriteLine(CreateNewFormat(format), args); }
356 }
357 public static class Profiler
358 {
359 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(FORCED PROFILER) " + GetLoggingClass() + " " + format; }
360 public static void Write(string format, params object[] args) { xlogger.Write(CreateNewFormat(format), args); }
361 public static void WriteLine(string format, params object[] args) { xlogger.WriteLine(CreateNewFormat(format), args); }
362 }
363 }
364 #endregion
365 #endregion
366
367 }
368 #endregion
369
370 #region internal static class Logger
371 internal static class xlogger
372 {
373 private static logwriter lh;
374 static xlogger() { lh = new logwriter(); }
375
376 #region ILogger Members
377 //[Conditional("LOGGING_ENABLED")]
378 public static void Write(string format, params object[] args)
379 {
380 init();
381 lh.Write(format, args);
382 }
383 //[Conditional("LOGGING_ENABLED")]
384 public static void WriteLine(string format, params object[] args)
385 {
386 init();
387 lh.WriteLine(format, args);
388 }
389 #endregion
390
391 #region Reflection Support
392 private static void init()
393 {
394 Assembly asm = Assembly.GetEntryAssembly();
395 Type[] types = asm.GetTypes();
396
397 foreach (Type t in types)
398 {
399 if (t.BaseType == typeof(Form))
400 {
401 LogWriter lw = null;
402 PropertyInfo[] properties = t.GetProperties(BindingFlags.NonPublic | BindingFlags.Static);
403 foreach (PropertyInfo prop in properties)
404 {
405 if (prop.PropertyType == typeof(LogWriter))
406 {
407 try
408 {
409 lw = (LogWriter)prop.GetValue(null, null);
410 lh = new logwriter(lw);
411 break;
412 }
413 catch (Exception)
414 {
415 throw;
416 }
417 }
418 }
419 }
420 }
421 }
422 #endregion
423 }
424 #endregion
425
426 #region internal class LogHelper : ILogger
427 internal class logwriter //: ILogger
428 {
429 private LogWriter writer;
430 public logwriter() : this(null) { }
431 public logwriter(LogWriter writer)
432 {
433 this.writer = writer;
434 }
435 #region ILogger Members
436 //[Conditional("LOGGING_ENABLED")]
437 public void Write(string format, params object[] args)
438 {
439 WriteToLog(format, false, args);
440 }
441 //[Conditional("LOGGING_ENABLED")]
442 public void WriteLine(string format, params object[] args)
443 {
444 WriteToLog(format, true, args);
445 }
446 private void WriteToLog(string format, bool newline, params object[] args)
447 {
448 TextWriter tw = TextWriter.Synchronized(writer.Log);
449 if (writer != null)
450 {
451 if (newline)
452 {
453 if (args.Length == 0)
454 {
455 tw.WriteLine(format.Replace("{", "{{").Replace("}", "}}"), args);
456 return;
457 }
458 tw.WriteLine(format, args);
459 }
460 else
461 {
462 if (args.Length == 0)
463 {
464 tw.Write(format.Replace("{", "{{").Replace("}", "}}"), args);
465 return;
466 }
467 tw.Write(format, args);
468 }
469 }
470 tw.Flush();
471 }
472 #endregion
473 }
474 #endregion
475 }