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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 668 - (hide annotations) (download)
Mon Jun 17 04:49:09 2013 UTC (8 years ago) by william
File size: 22247 byte(s)

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

  ViewVC Help
Powered by ViewVC 1.1.22