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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 685 - (show annotations) (download)
Mon Jun 17 08:58:58 2013 UTC (8 years, 4 months ago) by william
File size: 23172 byte(s)

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(DEFAULT);
63 add_dict_entry(ALL);
64 }
65
66 private static void add_dict_entry(EnumNameValuePair<TValue> value)
67 {
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 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 public static implicit operator loggerflagsbase<TValue>(TValue t)
80 {
81 //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 builder.AppendFormat(" | {0}", pair);
109 }
110 count++;
111 }
112 loggerflagsbase<TValue> rVal = new loggerflagsbase<TValue>(new EnumNameValuePair<TValue>(builder.ToString(), t));
113 return rVal;
114 }
115 }
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
119 #region operator overloads
120 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 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
135 return CurrentValue.ToString();
136 }
137 #endregion
138 #endregion
139 #region binary bit flags
140 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 public static EnumNameValuePair<TValue> WARN = new EnumNameValuePair<TValue>("WARN", Binary<TValue>.ToValue("100000"));
147 public static EnumNameValuePair<TValue> PROFILER = new EnumNameValuePair<TValue>("PROFILER", Binary<TValue>.ToValue("1000000"));
148 public static EnumNameValuePair<TValue> DEFAULT = new EnumNameValuePair<TValue>("DEFAULT", EnumNameValuePair<TValue>.bitwise_or(INFO, DEBUG, ERROR, WARN));
149 public static EnumNameValuePair<TValue> ALL = new EnumNameValuePair<TValue>("ALL", EnumNameValuePair<TValue>.bitwise_or(DEFAULT, VERBOSE_DEBUG, VERBOSE_ERROR, PROFILER));
150 #endregion
151
152 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
156 public bool HasFlag(TValue flag)
157 {
158 bool hasflag = false;
159 TValue value = this.CurrentValue;
160 if ((EnumNameValuePair<TValue>.bitwise_and(flag, value)).Equals(flag))
161 hasflag = true;
162 return hasflag;
163 }
164 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 public static List<TValue> GetValues() { return value_pairs.Keys.ToList(); }
173 public static List<string> GetNames() { return name_pairs.Keys.ToList(); }
174
175 public string Name { get { return CurrentValue.Name; } }
176 public TValue Value { get { return CurrentValue.Value; } }
177 }
178 //[Flags]
179 public class loggerflags : loggerflagsbase<ushort>
180 {
181 protected loggerflags() : base() { }
182 protected loggerflags(EnumNameValuePair<ushort> t) : base(t) { }
183 protected loggerflags(loggerflagsbase<ushort> t) : base(t) { }
184 #region implicit operators
185 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 #endregion
191
192 }
193 #endregion
194
195 #region public static class logger
196 public static class logger
197 {
198 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 c.ToLower() == "pluginbase" ||
215 c.ToLower() == "inputplugin" ||
216 c.ToLower() == "windowplugin" ||
217 c.ToLower() == "configplugin" ||
218 c.ToLower() == "usercontrolplugin"
219 )
220 {
221 int NextMethodCount = methodindex;
222 while (GetNextMethod)
223 {
224 if (NextMethodCount == -1)
225 {
226 throw new InvalidOperationException("Unable to find calling class");
227 }
228 method = methods[NextMethodCount--]; // walk backup the stack
229 if (method.DeclaringType != null)
230 {
231 c = method.DeclaringType.Name;
232 if (method.DeclaringType.FullName.ToLower().StartsWith("system") || method.DeclaringType.FullName.ToLower().StartsWith("microsoft"))
233 {
234 GetNextMethod = true;
235 }
236 else if (c.ToLower() == "pluginbase" ||
237 c.ToLower() == "inputplugin" ||
238 c.ToLower() == "windowplugin" ||
239 c.ToLower() == "configplugin" ||
240 c.ToLower() == "usercontrolplugin")
241 {
242 // ignore our abstract plugin classes
243 GetNextMethod = true;
244 }
245 else
246 {
247 GetNextMethod = false;
248 }
249 }
250 }
251 }
252 else
253 {
254 }
255
256 }
257 return string.Format("({0})",c);
258 }
259 private static loggerflags logging_flags;
260 static logger() { SetLoggingFlags(loggerflags.DEFAULT); }
261 public static void SetLoggingFlags(loggerflags flags) { logging_flags = flags; }
262 public static loggerflags GetLoggingFlags() { return logging_flags; }
263 #region sub-classes
264 private static string CreateTimeStamp()
265 {
266 string timestamp = "";
267 DateTime now = DateTime.Now;
268 timestamp = now.ToString("yyyy/MM/dd HH:mm:ss tt: ");
269 return string.Format("{0}", timestamp);
270 }
271 public static class Info
272 {
273 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(INFO) " + GetLoggingClass() + " " + format; }
274 [Conditional("LOGGING_ENABLE_INFO")]
275 public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.INFO)) { xlogger.Write(CreateNewFormat(format), args); } }
276 [Conditional("LOGGING_ENABLE_INFO")]
277 public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.INFO)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
278 }
279 public static class Warn
280 {
281 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(WARN) " + GetLoggingClass() + " " + format; }
282 [Conditional("LOGGING_ENABLE_WARN")]
283 public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.WARN)) { xlogger.Write(CreateNewFormat(format), args); } }
284 [Conditional("LOGGING_ENABLE_WARN")]
285 public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.WARN)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
286 }
287 public static class Debug
288 {
289 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(DEBUG) " + GetLoggingClass() + " " + format; }
290 [Conditional("LOGGING_ENABLE_DEBUG")]
291 public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.DEBUG)) { xlogger.Write(CreateNewFormat(format), args); } }
292 [Conditional("LOGGING_ENABLE_DEBUG")]
293 public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.DEBUG)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
294 }
295 public static class VerboseDebug
296 {
297 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(VERBOSE DEBUG) " + GetLoggingClass() + " " + format; }
298 [Conditional("LOGGING_ENABLE_VERBOSEDEBUG")]
299 public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.VERBOSE_DEBUG)) { xlogger.Write(CreateNewFormat(format), args); } }
300 [Conditional("LOGGING_ENABLE_VERBOSEDEBUG")]
301
302 public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.VERBOSE_DEBUG)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
303 }
304 public static class Error
305 {
306 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(ERROR) " + GetLoggingClass() + " " + format; }
307 [Conditional("LOGGING_ENABLE_ERROR")]
308 public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.ERROR)) { xlogger.Write(CreateNewFormat(format), args); } }
309 [Conditional("LOGGING_ENABLE_ERROR")]
310 public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.ERROR)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
311 }
312 public static class VerboseError
313 {
314 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(VERBOSE ERROR) " + GetLoggingClass() + " " + format; }
315 [Conditional("LOGGING_ENABLE_VERBOSEERROR")]
316 public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.VERBOSE_ERROR)) { xlogger.Write(CreateNewFormat(format), args); } }
317 [Conditional("LOGGING_ENABLE_VERBOSEERROR")]
318 public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.VERBOSE_ERROR)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
319 }
320 public static class Profiler
321 {
322 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(PROFILER) " + GetLoggingClass() + " " + format; }
323 [Conditional("LOGGING_ENABLE_PROFILER")]
324 public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.PROFILER)) { xlogger.Write(CreateNewFormat(format), args); } }
325 [Conditional("LOGGING_ENABLE_PROFILER")]
326 public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.PROFILER)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
327 }
328 #region Force logging
329 public static class ForceLog
330 {
331 public static class Info
332 {
333 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(FORCED INFO) " + GetLoggingClass() + " " + format; }
334 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 public static class Warn
338 {
339 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(FORCED Warn) " + GetLoggingClass() + " " + format; }
340 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 public static class Debug
344 {
345 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(FORCED DEBUG) " + GetLoggingClass() + " " + format; }
346 public static void Write(string format, params object[] args) { xlogger.Write(CreateNewFormat(format), args); }
347 public static void WriteLine(string format, params object[] args) { xlogger.WriteLine(CreateNewFormat(format), args); }
348 }
349 public static class Error
350 {
351 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(FORCED ERROR) " + GetLoggingClass() + " " + format; }
352 public static void Write(string format, params object[] args) { xlogger.Write(CreateNewFormat(format), args); }
353 public static void WriteLine(string format, params object[] args) { xlogger.WriteLine(CreateNewFormat(format), args); }
354 }
355 public static class Profiler
356 {
357 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(FORCED PROFILER) " + GetLoggingClass() + " " + format; }
358 public static void Write(string format, params object[] args) { xlogger.Write(CreateNewFormat(format), args); }
359 public static void WriteLine(string format, params object[] args) { xlogger.WriteLine(CreateNewFormat(format), args); }
360 }
361 }
362 #endregion
363 #endregion
364
365 }
366 #endregion
367
368 #region internal static class Logger
369 internal static class xlogger
370 {
371 private static logwriter lh;
372 static xlogger() { lh = new logwriter(); }
373
374 #region ILogger Members
375 [Conditional("LOGGING_ENABLED")]
376 public static void Write(string format, params object[] args)
377 {
378 init();
379 lh.Write(format, args);
380 }
381 [Conditional("LOGGING_ENABLED")]
382 public static void WriteLine(string format, params object[] args)
383 {
384 init();
385 lh.WriteLine(format, args);
386 }
387 #endregion
388
389 #region Reflection Support
390 private static void init()
391 {
392 Assembly asm = Assembly.GetEntryAssembly();
393 Type[] types = asm.GetTypes();
394
395 foreach (Type t in types)
396 {
397 if (t.BaseType == typeof(Form))
398 {
399 LogWriter lw = null;
400 PropertyInfo[] properties = t.GetProperties(BindingFlags.NonPublic | BindingFlags.Static);
401 foreach (PropertyInfo prop in properties)
402 {
403 if (prop.PropertyType == typeof(LogWriter))
404 {
405 try
406 {
407 lw = (LogWriter)prop.GetValue(null, null);
408 lh = new logwriter(lw);
409 break;
410 }
411 catch (Exception)
412 {
413 throw;
414 }
415 }
416 }
417 }
418 }
419 }
420 #endregion
421 }
422 #endregion
423
424 #region internal class LogHelper : ILogger
425 internal class logwriter //: ILogger
426 {
427 private LogWriter writer;
428 public logwriter() : this(null) { }
429 public logwriter(LogWriter writer)
430 {
431 this.writer = writer;
432 }
433 #region ILogger Members
434 [Conditional("LOGGING_ENABLED")]
435 public void Write(string format, params object[] args)
436 {
437 WriteToLog(format, false, args);
438 }
439 [Conditional("LOGGING_ENABLED")]
440 public void WriteLine(string format, params object[] args)
441 {
442 WriteToLog(format, true, args);
443 }
444 private void WriteToLog(string format, bool newline, params object[] args)
445 {
446 TextWriter tw = TextWriter.Synchronized(writer.Log);
447 if (writer != null)
448 {
449 if (newline)
450 {
451 if (args.Length == 0)
452 {
453 tw.WriteLine(format.Replace("{", "{{").Replace("}", "}}"), args);
454 return;
455 }
456 tw.WriteLine(format, args);
457 }
458 else
459 {
460 if (args.Length == 0)
461 {
462 tw.Write(format.Replace("{", "{{").Replace("}", "}}"), args);
463 return;
464 }
465 tw.Write(format, args);
466 }
467 }
468 tw.Flush();
469 }
470 #endregion
471 }
472 #endregion
473 }

  ViewVC Help
Powered by ViewVC 1.1.22