/[RomCheater]/trunk/RomCheater.Logging/logger.cs
ViewVC logotype

Annotation of /trunk/RomCheater.Logging/logger.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 692 - (hide annotations) (download)
Mon Jun 17 10:29:30 2013 UTC (8 years ago) by william
File size: 23281 byte(s)
+ re-enable configured logging flags, removed conditional flags

1 william 414 #region Logging Defines
2     // include this any class or method that required logging, and comment-out what is not needed
3 william 415 #define LOGGING_ENABLED // this is only valid within the logger class
4 william 414 #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 william 427 #define LOGGING_ENABLE_PROFILER
12 william 414 #endregion
13     #endregion
14    
15    
16     using System;
17 william 28 using System.Collections.Generic;
18     using System.Linq;
19     using System.Text;
20     using System.Reflection;
21     using System.Windows.Forms;
22 william 302 using System.IO;
23 william 414 using System.Diagnostics;
24 william 28
25     namespace RomCheater.Logging
26     {
27 william 415 //public interface ILogger
28     //{
29     // void Write(string format, params object[] args);
30     // void WriteLine(string format, params object[] args);
31     //}
32 william 28
33    
34 william 116 #region loggerflags
35 william 414 public class loggerflagsbase<TValue>
36 william 116 where TValue : IConvertible
37 william 28 {
38 william 414 static loggerflagsbase()
39 william 111 {
40 william 116 value_pairs = new Dictionary<TValue, EnumNameValuePair<TValue>>();
41     name_pairs = new Dictionary<string, EnumNameValuePair<TValue>>();
42 william 414
43 william 111 init_dicts();
44     }
45 william 414 protected loggerflagsbase()
46 william 111 {
47     CurrentValue = NONE;
48     }
49 william 116 //protected loggerflags(ushort t) : this() { CurrentValue = new EnumNameValuePair< ushort>("", t); }
50 william 414 protected loggerflagsbase(EnumNameValuePair<TValue> t) { CurrentValue = t; }
51 william 116 protected loggerflagsbase(loggerflagsbase<TValue> t) { this.CurrentValue = t.CurrentValue; }
52 william 111 private static void init_dicts()
53     {
54 william 414 value_pairs = new Dictionary<TValue, EnumNameValuePair<TValue>>();
55     name_pairs = new Dictionary<string, EnumNameValuePair<TValue>>();
56 william 111 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 william 692 add_dict_entry(WARN);
63     add_dict_entry(PROFILER);
64 william 111 add_dict_entry(DEFAULT);
65     add_dict_entry(ALL);
66     }
67    
68 william 116 private static void add_dict_entry(EnumNameValuePair<TValue> value)
69 william 111 {
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 william 116 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 william 265 public static implicit operator loggerflagsbase<TValue>(TValue t)
82 william 111 {
83 william 269 //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 william 414 builder.AppendFormat(" | {0}", pair);
111 william 269 }
112     count++;
113     }
114     loggerflagsbase<TValue> rVal = new loggerflagsbase<TValue>(new EnumNameValuePair<TValue>(builder.ToString(), t));
115     return rVal;
116     }
117 william 111 }
118 william 116 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 william 111
121     #region operator overloads
122 william 116 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 william 111 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 william 414
137 william 111 return CurrentValue.ToString();
138     }
139     #endregion
140     #endregion
141     #region binary bit flags
142 william 116 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 william 163 public static EnumNameValuePair<TValue> WARN = new EnumNameValuePair<TValue>("WARN", Binary<TValue>.ToValue("100000"));
149 william 391 public static EnumNameValuePair<TValue> PROFILER = new EnumNameValuePair<TValue>("PROFILER", Binary<TValue>.ToValue("1000000"));
150 william 692 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 william 111 #endregion
153    
154 william 116 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 william 111
158 william 116 public bool HasFlag(TValue flag)
159 william 111 {
160     bool hasflag = false;
161 william 116 TValue value = this.CurrentValue;
162 william 269 if ((EnumNameValuePair<TValue>.bitwise_and(flag, value)).Equals(flag))
163 william 111 hasflag = true;
164     return hasflag;
165     }
166 william 269 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 william 116 public static List<TValue> GetValues() { return value_pairs.Keys.ToList(); }
175 william 111 public static List<string> GetNames() { return name_pairs.Keys.ToList(); }
176 william 112
177     public string Name { get { return CurrentValue.Name; } }
178 william 116 public TValue Value { get { return CurrentValue.Value; } }
179     }
180     //[Flags]
181     public class loggerflags : loggerflagsbase<ushort>
182     {
183 william 414 protected loggerflags() : base() { }
184     protected loggerflags(EnumNameValuePair<ushort> t) : base(t) { }
185     protected loggerflags(loggerflagsbase<ushort> t) : base(t) { }
186 william 116 #region implicit operators
187 william 414 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 william 116 #endregion
193 william 112
194 william 28 }
195 william 414 #endregion
196    
197     #region public static class logger
198 william 28 public static class logger
199     {
200 william 668 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 william 685 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 william 668 {
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 william 685 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 william 668 else
248     {
249     GetNextMethod = false;
250     }
251     }
252     }
253     }
254 william 685 else
255     {
256     }
257 william 668
258     }
259     return string.Format("({0})",c);
260     }
261 william 28 private static loggerflags logging_flags;
262     static logger() { SetLoggingFlags(loggerflags.DEFAULT); }
263     public static void SetLoggingFlags(loggerflags flags) { logging_flags = flags; }
264 william 112 public static loggerflags GetLoggingFlags() { return logging_flags; }
265 william 28 #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 william 668 return string.Format("{0}", timestamp);
272 william 28 }
273     public static class Info
274     {
275 william 668 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(INFO) " + GetLoggingClass() + " " + format; }
276 william 692 //[Conditional("LOGGING_ENABLE_INFO")]
277 william 28 public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.INFO)) { xlogger.Write(CreateNewFormat(format), args); } }
278 william 692 //[Conditional("LOGGING_ENABLE_INFO")]
279 william 28 public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.INFO)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
280     }
281 william 163 public static class Warn
282     {
283 william 668 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(WARN) " + GetLoggingClass() + " " + format; }
284 william 692 //[Conditional("LOGGING_ENABLE_WARN")]
285 william 163 public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.WARN)) { xlogger.Write(CreateNewFormat(format), args); } }
286 william 692 //[Conditional("LOGGING_ENABLE_WARN")]
287 william 163 public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.WARN)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
288     }
289 william 28 public static class Debug
290     {
291 william 668 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(DEBUG) " + GetLoggingClass() + " " + format; }
292 william 692 //[Conditional("LOGGING_ENABLE_DEBUG")]
293 william 28 public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.DEBUG)) { xlogger.Write(CreateNewFormat(format), args); } }
294 william 692 //[Conditional("LOGGING_ENABLE_DEBUG")]
295 william 28 public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.DEBUG)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
296     }
297 william 99 public static class VerboseDebug
298     {
299 william 668 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(VERBOSE DEBUG) " + GetLoggingClass() + " " + format; }
300 william 692 //[Conditional("LOGGING_ENABLE_VERBOSEDEBUG")]
301 william 99 public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.VERBOSE_DEBUG)) { xlogger.Write(CreateNewFormat(format), args); } }
302 william 692 //[Conditional("LOGGING_ENABLE_VERBOSEDEBUG")]
303 william 415
304 william 99 public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.VERBOSE_DEBUG)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
305     }
306 william 28 public static class Error
307     {
308 william 668 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(ERROR) " + GetLoggingClass() + " " + format; }
309 william 692 //[Conditional("LOGGING_ENABLE_ERROR")]
310 william 28 public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.ERROR)) { xlogger.Write(CreateNewFormat(format), args); } }
311 william 692 //[Conditional("LOGGING_ENABLE_ERROR")]
312 william 28 public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.ERROR)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
313     }
314 william 99 public static class VerboseError
315     {
316 william 668 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(VERBOSE ERROR) " + GetLoggingClass() + " " + format; }
317 william 692 //[Conditional("LOGGING_ENABLE_VERBOSEERROR")]
318 william 99 public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.VERBOSE_ERROR)) { xlogger.Write(CreateNewFormat(format), args); } }
319 william 692 //[Conditional("LOGGING_ENABLE_VERBOSEERROR")]
320 william 99 public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.VERBOSE_ERROR)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
321     }
322 william 391 public static class Profiler
323     {
324 william 668 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(PROFILER) " + GetLoggingClass() + " " + format; }
325 william 692 //[Conditional("LOGGING_ENABLE_PROFILER")]
326 william 391 public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.PROFILER)) { xlogger.Write(CreateNewFormat(format), args); } }
327 william 692 //[Conditional("LOGGING_ENABLE_PROFILER")]
328 william 391 public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.PROFILER)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
329     }
330 william 95 #region Force logging
331     public static class ForceLog
332     {
333     public static class Info
334     {
335 william 668 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(FORCED INFO) " + GetLoggingClass() + " " + format; }
336 william 95 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 william 163 public static class Warn
340     {
341 william 668 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(FORCED Warn) " + GetLoggingClass() + " " + format; }
342 william 163 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 william 95 public static class Debug
346     {
347 william 668 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(FORCED DEBUG) " + GetLoggingClass() + " " + format; }
348 william 95 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 william 668 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(FORCED ERROR) " + GetLoggingClass() + " " + format; }
354 william 95 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 william 391 public static class Profiler
358     {
359 william 668 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(FORCED PROFILER) " + GetLoggingClass() + " " + format; }
360 william 391 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 william 95 }
364 william 28 #endregion
365 william 95 #endregion
366 william 28
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 william 692 //[Conditional("LOGGING_ENABLED")]
378 william 28 public static void Write(string format, params object[] args)
379 william 414 {
380 william 28 init();
381     lh.Write(format, args);
382     }
383 william 692 //[Conditional("LOGGING_ENABLED")]
384 william 28 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 william 82 lh = new logwriter(lw);
411 william 28 break;
412     }
413     catch (Exception)
414     {
415     throw;
416     }
417     }
418 william 414 }
419 william 28 }
420     }
421     }
422     #endregion
423     }
424     #endregion
425    
426     #region internal class LogHelper : ILogger
427 william 415 internal class logwriter //: ILogger
428 william 28 {
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 william 692 //[Conditional("LOGGING_ENABLED")]
437 william 28 public void Write(string format, params object[] args)
438     {
439 william 414 WriteToLog(format, false, args);
440 william 28 }
441 william 692 //[Conditional("LOGGING_ENABLED")]
442 william 28 public void WriteLine(string format, params object[] args)
443     {
444 william 414 WriteToLog(format, true, args);
445 william 28 }
446     private void WriteToLog(string format, bool newline, params object[] args)
447     {
448 william 302 TextWriter tw = TextWriter.Synchronized(writer.Log);
449 william 28 if (writer != null)
450     {
451     if (newline)
452     {
453 william 291 if (args.Length == 0)
454     {
455 william 302 tw.WriteLine(format.Replace("{", "{{").Replace("}", "}}"), args);
456 william 291 return;
457     }
458 william 302 tw.WriteLine(format, args);
459 william 28 }
460     else
461     {
462 william 291 if (args.Length == 0)
463     {
464 william 302 tw.Write(format.Replace("{", "{{").Replace("}", "}}"), args);
465 william 291 return;
466     }
467 william 302 tw.Write(format, args);
468 william 28 }
469     }
470 william 302 tw.Flush();
471 william 28 }
472     #endregion
473     }
474 william 414 #endregion
475 william 28 }

  ViewVC Help
Powered by ViewVC 1.1.22