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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 391 - (hide annotations) (download)
Wed Jun 20 10:02:14 2012 UTC (8 years, 11 months ago) by william
File size: 18567 byte(s)
+ add PROFILER to logging flags

1 william 28 using System;
2     using System.Collections.Generic;
3     using System.Linq;
4     using System.Text;
5     using System.Reflection;
6     using System.Windows.Forms;
7 william 302 using System.IO;
8 william 28
9     namespace RomCheater.Logging
10     {
11     public interface ILogger
12     {
13     void Write(string format, params object[] args);
14     void WriteLine(string format, params object[] args);
15     }
16    
17    
18 william 116 #region loggerflags
19     public class loggerflagsbase<TValue>
20     where TValue : IConvertible
21 william 28 {
22 william 116 static loggerflagsbase()
23 william 111 {
24 william 116 value_pairs = new Dictionary<TValue, EnumNameValuePair<TValue>>();
25     name_pairs = new Dictionary<string, EnumNameValuePair<TValue>>();
26 william 111
27     init_dicts();
28     }
29 william 116 protected loggerflagsbase()
30 william 111 {
31     CurrentValue = NONE;
32     }
33 william 116 //protected loggerflags(ushort t) : this() { CurrentValue = new EnumNameValuePair< ushort>("", t); }
34     protected loggerflagsbase(EnumNameValuePair< TValue> t) { CurrentValue = t; }
35     protected loggerflagsbase(loggerflagsbase<TValue> t) { this.CurrentValue = t.CurrentValue; }
36 william 111 private static void init_dicts()
37     {
38 william 116 value_pairs = new Dictionary<TValue, EnumNameValuePair< TValue>>();
39     name_pairs = new Dictionary<string, EnumNameValuePair< TValue>>();
40 william 111 add_dict_entry(NONE);
41     add_dict_entry(INFO);
42     add_dict_entry(DEBUG);
43     add_dict_entry(ERROR);
44     add_dict_entry(VERBOSE_DEBUG);
45     add_dict_entry(VERBOSE_ERROR);
46     add_dict_entry(DEFAULT);
47     add_dict_entry(ALL);
48     }
49    
50 william 116 private static void add_dict_entry(EnumNameValuePair<TValue> value)
51 william 111 {
52     try
53     {
54     value_pairs.Add(value, value);
55     name_pairs.Add(value, value);
56     }
57     catch { }
58     }
59    
60     #region implicit operators
61 william 116 public static implicit operator loggerflagsbase<TValue>(EnumNameValuePair<TValue> t) { return new loggerflagsbase<TValue>(t); }
62     public static implicit operator EnumNameValuePair<TValue>(loggerflagsbase<TValue> t) { return new loggerflagsbase<TValue>(t); }
63 william 265 public static implicit operator loggerflagsbase<TValue>(TValue t)
64 william 111 {
65 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); } }
66     //return loggerflagsbase<TValue>.NONE;
67     List<string> pairs = new List<string>();
68     StringBuilder builder = new StringBuilder();
69     foreach (KeyValuePair<TValue, EnumNameValuePair<TValue>> pair in value_pairs)
70     {
71     EnumNameValuePair<TValue> enum_pair = pair.Value;
72     if (HasFlag(pair.Value, t) && enum_pair != NONE)
73     {
74     pairs.Add(enum_pair);
75     }
76     }
77     if (pairs.Count == 0)
78     {
79     return NONE;
80     }
81     else
82     {
83     int count = 0;
84     foreach (string pair in pairs)
85     {
86     if (count == 0)
87     {
88     builder.AppendLine(pair);
89     }
90     else
91     {
92     builder.AppendFormat(" | {0}",pair);
93     }
94     count++;
95     }
96     loggerflagsbase<TValue> rVal = new loggerflagsbase<TValue>(new EnumNameValuePair<TValue>(builder.ToString(), t));
97     return rVal;
98     }
99 william 111 }
100 william 116 public static implicit operator TValue(loggerflagsbase<TValue> t) { return t.CurrentValue.Value; }
101     public static implicit operator string(loggerflagsbase<TValue> t) { return t.CurrentValue.Name; }
102 william 111
103     #region operator overloads
104 william 116 public static bool operator ==(loggerflagsbase<TValue> x, loggerflagsbase<TValue> y) { return x.CurrentValue == y.CurrentValue; }
105     public static bool operator !=(loggerflagsbase<TValue> x, loggerflagsbase<TValue> y) { return x.CurrentValue != y.CurrentValue; }
106 william 111 public override bool Equals(object obj)
107     {
108     loggerflags t = (obj as loggerflags);
109     if (t == null) return false;
110     return this.CurrentValue.Equals(t);
111     }
112     public override int GetHashCode()
113     {
114     return CurrentValue.GetHashCode();
115     }
116     public override string ToString()
117     {
118 william 116
119 william 111 return CurrentValue.ToString();
120     }
121     #endregion
122     #endregion
123     #region binary bit flags
124 william 116 public static EnumNameValuePair<TValue> NONE = new EnumNameValuePair<TValue>("NONE", Binary<TValue>.ToValue("00000"));
125     public static EnumNameValuePair<TValue> INFO = new EnumNameValuePair<TValue>("INFO", Binary<TValue>.ToValue("00001"));
126     public static EnumNameValuePair<TValue> DEBUG = new EnumNameValuePair<TValue>("DEBUG", Binary<TValue>.ToValue("00010"));
127     public static EnumNameValuePair<TValue> ERROR = new EnumNameValuePair<TValue>("ERROR", Binary<TValue>.ToValue("00100"));
128     public static EnumNameValuePair<TValue> VERBOSE_DEBUG = new EnumNameValuePair<TValue>("VERBOSE_DEBUG", Binary<TValue>.ToValue("01000"));
129     public static EnumNameValuePair<TValue> VERBOSE_ERROR = new EnumNameValuePair<TValue>("VERBOSE_ERROR", Binary<TValue>.ToValue("10000"));
130 william 163 public static EnumNameValuePair<TValue> WARN = new EnumNameValuePair<TValue>("WARN", Binary<TValue>.ToValue("100000"));
131 william 391 public static EnumNameValuePair<TValue> PROFILER = new EnumNameValuePair<TValue>("PROFILER", Binary<TValue>.ToValue("1000000"));
132 william 163 public static EnumNameValuePair<TValue> DEFAULT = new EnumNameValuePair<TValue>("DEFAULT", EnumNameValuePair<TValue>.bitwise_or(INFO, DEBUG, ERROR, WARN));
133 william 391 public static EnumNameValuePair<TValue> ALL = new EnumNameValuePair<TValue>("ALL", EnumNameValuePair<TValue>.bitwise_or(DEFAULT, VERBOSE_DEBUG, VERBOSE_ERROR, PROFILER));
134 william 111 #endregion
135    
136 william 116 protected static Dictionary<TValue, EnumNameValuePair<TValue>> value_pairs;
137     protected static Dictionary<string, EnumNameValuePair<TValue>> name_pairs;
138     private EnumNameValuePair<TValue> CurrentValue { get; set; }
139 william 111
140 william 116 public bool HasFlag(TValue flag)
141 william 111 {
142     bool hasflag = false;
143 william 116 TValue value = this.CurrentValue;
144 william 269 if ((EnumNameValuePair<TValue>.bitwise_and(flag, value)).Equals(flag))
145 william 111 hasflag = true;
146     return hasflag;
147     }
148 william 269 public static bool HasFlag(TValue flag, TValue compare)
149     {
150     bool hasflag = false;
151     TValue value = compare;
152     if ((EnumNameValuePair<TValue>.bitwise_and(flag, value)).Equals(flag))
153     hasflag = true;
154     return hasflag;
155     }
156 william 116 public static List<TValue> GetValues() { return value_pairs.Keys.ToList(); }
157 william 111 public static List<string> GetNames() { return name_pairs.Keys.ToList(); }
158 william 112
159     public string Name { get { return CurrentValue.Name; } }
160 william 116 public TValue Value { get { return CurrentValue.Value; } }
161     }
162     //[Flags]
163     public class loggerflags : loggerflagsbase<ushort>
164     {
165     protected loggerflags() :base() { }
166     protected loggerflags(EnumNameValuePair<ushort> t) : base(t) { }
167     protected loggerflags(loggerflagsbase<ushort> t) : base(t) { }
168     #region implicit operators
169     public static implicit operator loggerflags(EnumNameValuePair<ushort> t) { return new loggerflags(t); }
170 william 265 public static implicit operator EnumNameValuePair<ushort>(loggerflags t) { return new EnumNameValuePair<ushort>(t.Name,t.Value); }
171 william 116 public static implicit operator ushort(loggerflags t) { return t.Value; }
172     public static implicit operator loggerflags(ushort t) { return new loggerflags(t); }
173     public static implicit operator string(loggerflags t) { return t.Name; }
174     #endregion
175 william 112
176 william 28 }
177 william 116
178 william 28 public static class logger
179     {
180     private static loggerflags logging_flags;
181     static logger() { SetLoggingFlags(loggerflags.DEFAULT); }
182     public static void SetLoggingFlags(loggerflags flags) { logging_flags = flags; }
183 william 112 public static loggerflags GetLoggingFlags() { return logging_flags; }
184 william 28 #region sub-classes
185     private static string CreateTimeStamp()
186     {
187     string timestamp = "";
188     DateTime now = DateTime.Now;
189     timestamp = now.ToString("yyyy/MM/dd HH:mm:ss tt: ");
190     return timestamp;
191     }
192     public static class Info
193     {
194     private static string CreateNewFormat(string format) { return " " + CreateTimeStamp()+ "(INFO) " + format; }
195     public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.INFO)) { xlogger.Write(CreateNewFormat(format), args); } }
196     public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.INFO)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
197     }
198 william 163 public static class Warn
199     {
200     private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(WARN) " + format; }
201     public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.WARN)) { xlogger.Write(CreateNewFormat(format), args); } }
202     public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.WARN)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
203     }
204 william 28 public static class Debug
205     {
206     private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(DEBUG) " + format; }
207     public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.DEBUG)) { xlogger.Write(CreateNewFormat(format), args); } }
208     public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.DEBUG)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
209     }
210 william 99 public static class VerboseDebug
211     {
212     private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(VERBOSE DEBUG) " + format; }
213     public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.VERBOSE_DEBUG)) { xlogger.Write(CreateNewFormat(format), args); } }
214     public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.VERBOSE_DEBUG)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
215     }
216 william 28 public static class Error
217     {
218     private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(ERROR) " + format; }
219     public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.ERROR)) { xlogger.Write(CreateNewFormat(format), args); } }
220     public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.ERROR)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
221     }
222 william 99 public static class VerboseError
223     {
224     private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(VERBOSE ERROR) " + format; }
225     public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.VERBOSE_ERROR)) { xlogger.Write(CreateNewFormat(format), args); } }
226     public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.VERBOSE_ERROR)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
227     }
228 william 391 public static class Profiler
229     {
230     private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(PROFILER) " + format; }
231     public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.PROFILER)) { xlogger.Write(CreateNewFormat(format), args); } }
232     public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.PROFILER)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
233     }
234 william 95 #region Force logging
235     public static class ForceLog
236     {
237     public static class Info
238     {
239 william 96 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(FORCED INFO) " + format; }
240 william 95 public static void Write(string format, params object[] args) { xlogger.Write(CreateNewFormat(format), args); }
241     public static void WriteLine(string format, params object[] args) { xlogger.WriteLine(CreateNewFormat(format), args); }
242     }
243 william 163 public static class Warn
244     {
245     private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(FORCED Warn) " + format; }
246     public static void Write(string format, params object[] args) { xlogger.Write(CreateNewFormat(format), args); }
247     public static void WriteLine(string format, params object[] args) { xlogger.WriteLine(CreateNewFormat(format), args); }
248     }
249 william 95 public static class Debug
250     {
251 william 96 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(FORCED DEBUG) " + format; }
252 william 95 public static void Write(string format, params object[] args) { xlogger.Write(CreateNewFormat(format), args); }
253     public static void WriteLine(string format, params object[] args) { xlogger.WriteLine(CreateNewFormat(format), args); }
254     }
255     public static class Error
256     {
257 william 96 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(FORCED ERROR) " + format; }
258 william 95 public static void Write(string format, params object[] args) { xlogger.Write(CreateNewFormat(format), args); }
259     public static void WriteLine(string format, params object[] args) { xlogger.WriteLine(CreateNewFormat(format), args); }
260     }
261 william 391 public static class Profiler
262     {
263     private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(FORCED PROFILER) " + format; }
264     public static void Write(string format, params object[] args) { xlogger.Write(CreateNewFormat(format), args); }
265     public static void WriteLine(string format, params object[] args) { xlogger.WriteLine(CreateNewFormat(format), args); }
266     }
267 william 95 }
268 william 28 #endregion
269 william 95 #endregion
270 william 28
271     }
272     #endregion
273    
274     #region internal static class Logger
275     internal static class xlogger
276     {
277     private static logwriter lh;
278     static xlogger() { lh = new logwriter(); }
279    
280     #region ILogger Members
281     public static void Write(string format, params object[] args)
282     {
283     init();
284     lh.Write(format, args);
285     }
286     public static void WriteLine(string format, params object[] args)
287     {
288     init();
289     lh.WriteLine(format, args);
290     }
291     #endregion
292    
293     #region Reflection Support
294     private static void init()
295     {
296     Assembly asm = Assembly.GetEntryAssembly();
297     Type[] types = asm.GetTypes();
298    
299     foreach (Type t in types)
300     {
301     if (t.BaseType == typeof(Form))
302     {
303     LogWriter lw = null;
304     PropertyInfo[] properties = t.GetProperties(BindingFlags.NonPublic | BindingFlags.Static);
305     foreach (PropertyInfo prop in properties)
306     {
307     if (prop.PropertyType == typeof(LogWriter))
308     {
309     try
310     {
311     lw = (LogWriter)prop.GetValue(null, null);
312 william 82 lh = new logwriter(lw);
313 william 28 break;
314     }
315     catch (Exception)
316     {
317     throw;
318     }
319     }
320 william 82 }
321 william 28 }
322     }
323     }
324     #endregion
325     }
326     #endregion
327    
328     #region internal class LogHelper : ILogger
329     internal class logwriter : ILogger
330     {
331     private LogWriter writer;
332     public logwriter() : this(null) { }
333     public logwriter(LogWriter writer)
334     {
335     this.writer = writer;
336     }
337     #region ILogger Members
338     public void Write(string format, params object[] args)
339     {
340     WriteToLog(format,false, args);
341     }
342     public void WriteLine(string format, params object[] args)
343     {
344     WriteToLog(format,true, args);
345     }
346     private void WriteToLog(string format, bool newline, params object[] args)
347     {
348 william 302 TextWriter tw = TextWriter.Synchronized(writer.Log);
349 william 28 if (writer != null)
350     {
351     if (newline)
352     {
353 william 291 if (args.Length == 0)
354     {
355 william 302 tw.WriteLine(format.Replace("{", "{{").Replace("}", "}}"), args);
356 william 291 return;
357     }
358 william 302 tw.WriteLine(format, args);
359 william 28 }
360     else
361     {
362 william 291 if (args.Length == 0)
363     {
364 william 302 tw.Write(format.Replace("{", "{{").Replace("}", "}}"), args);
365 william 291 return;
366     }
367 william 302 tw.Write(format, args);
368 william 28 }
369     }
370 william 302 tw.Flush();
371 william 28 }
372     #endregion
373     }
374     #endregion
375     }

  ViewVC Help
Powered by ViewVC 1.1.22