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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 814 - (hide annotations) (download)
Tue Apr 15 15:58:50 2014 UTC (7 years ago) by william
File size: 23290 byte(s)

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

  ViewVC Help
Powered by ViewVC 1.1.22