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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 828 - (show annotations) (download)
Mon Sep 15 21:36:20 2014 UTC (6 years, 1 month ago) by william
File size: 23703 byte(s)
+ update logging usage

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

  ViewVC Help
Powered by ViewVC 1.1.22