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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 269 - (hide annotations) (download)
Sun Jun 3 20:40:36 2012 UTC (9 years ago) by william
File size: 16871 byte(s)
+ overhaul logger and logging flags (to better handle turning flags off)

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

  ViewVC Help
Powered by ViewVC 1.1.22