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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 302 - (hide annotations) (download)
Tue Jun 5 13:27:40 2012 UTC (9 years ago) by william
File size: 17386 byte(s)
use Synchronized TextWriter

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     public static EnumNameValuePair<TValue> DEFAULT = new EnumNameValuePair<TValue>("DEFAULT", EnumNameValuePair<TValue>.bitwise_or(INFO, DEBUG, ERROR, WARN));
132 william 116 public static EnumNameValuePair<TValue> ALL = new EnumNameValuePair<TValue>("ALL", EnumNameValuePair<TValue>.bitwise_or(DEFAULT, VERBOSE_DEBUG, VERBOSE_ERROR));
133 william 111 #endregion
134    
135 william 116 protected static Dictionary<TValue, EnumNameValuePair<TValue>> value_pairs;
136     protected static Dictionary<string, EnumNameValuePair<TValue>> name_pairs;
137     private EnumNameValuePair<TValue> CurrentValue { get; set; }
138 william 111
139 william 116 public bool HasFlag(TValue flag)
140 william 111 {
141     bool hasflag = false;
142 william 116 TValue value = this.CurrentValue;
143 william 269 if ((EnumNameValuePair<TValue>.bitwise_and(flag, value)).Equals(flag))
144 william 111 hasflag = true;
145     return hasflag;
146     }
147 william 269 public static bool HasFlag(TValue flag, TValue compare)
148     {
149     bool hasflag = false;
150     TValue value = compare;
151     if ((EnumNameValuePair<TValue>.bitwise_and(flag, value)).Equals(flag))
152     hasflag = true;
153     return hasflag;
154     }
155 william 116 public static List<TValue> GetValues() { return value_pairs.Keys.ToList(); }
156 william 111 public static List<string> GetNames() { return name_pairs.Keys.ToList(); }
157 william 112
158     public string Name { get { return CurrentValue.Name; } }
159 william 116 public TValue Value { get { return CurrentValue.Value; } }
160     }
161     //[Flags]
162     public class loggerflags : loggerflagsbase<ushort>
163     {
164     protected loggerflags() :base() { }
165     protected loggerflags(EnumNameValuePair<ushort> t) : base(t) { }
166     protected loggerflags(loggerflagsbase<ushort> t) : base(t) { }
167     #region implicit operators
168     public static implicit operator loggerflags(EnumNameValuePair<ushort> t) { return new loggerflags(t); }
169 william 265 public static implicit operator EnumNameValuePair<ushort>(loggerflags t) { return new EnumNameValuePair<ushort>(t.Name,t.Value); }
170 william 116 public static implicit operator ushort(loggerflags t) { return t.Value; }
171     public static implicit operator loggerflags(ushort t) { return new loggerflags(t); }
172     public static implicit operator string(loggerflags t) { return t.Name; }
173     #endregion
174 william 112
175 william 28 }
176 william 116
177 william 28 public static class logger
178     {
179     private static loggerflags logging_flags;
180     static logger() { SetLoggingFlags(loggerflags.DEFAULT); }
181     public static void SetLoggingFlags(loggerflags flags) { logging_flags = flags; }
182 william 112 public static loggerflags GetLoggingFlags() { return logging_flags; }
183 william 28 #region sub-classes
184     private static string CreateTimeStamp()
185     {
186     string timestamp = "";
187     DateTime now = DateTime.Now;
188     timestamp = now.ToString("yyyy/MM/dd HH:mm:ss tt: ");
189     return timestamp;
190     }
191     public static class Info
192     {
193     private static string CreateNewFormat(string format) { return " " + CreateTimeStamp()+ "(INFO) " + format; }
194     public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.INFO)) { xlogger.Write(CreateNewFormat(format), args); } }
195     public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.INFO)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
196     }
197 william 163 public static class Warn
198     {
199     private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(WARN) " + format; }
200     public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.WARN)) { xlogger.Write(CreateNewFormat(format), args); } }
201     public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.WARN)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
202     }
203 william 28 public static class Debug
204     {
205     private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(DEBUG) " + format; }
206     public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.DEBUG)) { xlogger.Write(CreateNewFormat(format), args); } }
207     public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.DEBUG)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
208     }
209 william 99 public static class VerboseDebug
210     {
211     private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(VERBOSE DEBUG) " + format; }
212     public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.VERBOSE_DEBUG)) { xlogger.Write(CreateNewFormat(format), args); } }
213     public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.VERBOSE_DEBUG)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
214     }
215 william 28 public static class Error
216     {
217     private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(ERROR) " + format; }
218     public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.ERROR)) { xlogger.Write(CreateNewFormat(format), args); } }
219     public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.ERROR)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
220     }
221 william 99 public static class VerboseError
222     {
223     private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(VERBOSE ERROR) " + format; }
224     public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.VERBOSE_ERROR)) { xlogger.Write(CreateNewFormat(format), args); } }
225     public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.VERBOSE_ERROR)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
226     }
227 william 95 #region Force logging
228     public static class ForceLog
229     {
230     public static class Info
231     {
232 william 96 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(FORCED INFO) " + format; }
233 william 95 public static void Write(string format, params object[] args) { xlogger.Write(CreateNewFormat(format), args); }
234     public static void WriteLine(string format, params object[] args) { xlogger.WriteLine(CreateNewFormat(format), args); }
235     }
236 william 163 public static class Warn
237     {
238     private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(FORCED Warn) " + format; }
239     public static void Write(string format, params object[] args) { xlogger.Write(CreateNewFormat(format), args); }
240     public static void WriteLine(string format, params object[] args) { xlogger.WriteLine(CreateNewFormat(format), args); }
241     }
242 william 95 public static class Debug
243     {
244 william 96 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(FORCED DEBUG) " + format; }
245 william 95 public static void Write(string format, params object[] args) { xlogger.Write(CreateNewFormat(format), args); }
246     public static void WriteLine(string format, params object[] args) { xlogger.WriteLine(CreateNewFormat(format), args); }
247     }
248     public static class Error
249     {
250 william 96 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(FORCED ERROR) " + format; }
251 william 95 public static void Write(string format, params object[] args) { xlogger.Write(CreateNewFormat(format), args); }
252     public static void WriteLine(string format, params object[] args) { xlogger.WriteLine(CreateNewFormat(format), args); }
253     }
254     }
255 william 28 #endregion
256 william 95 #endregion
257 william 28
258     }
259     #endregion
260    
261     #region internal static class Logger
262     internal static class xlogger
263     {
264     private static logwriter lh;
265     static xlogger() { lh = new logwriter(); }
266    
267     #region ILogger Members
268     public static void Write(string format, params object[] args)
269     {
270     init();
271     lh.Write(format, args);
272     }
273     public static void WriteLine(string format, params object[] args)
274     {
275     init();
276     lh.WriteLine(format, args);
277     }
278     #endregion
279    
280     #region Reflection Support
281     private static void init()
282     {
283     Assembly asm = Assembly.GetEntryAssembly();
284     Type[] types = asm.GetTypes();
285    
286     foreach (Type t in types)
287     {
288     if (t.BaseType == typeof(Form))
289     {
290     LogWriter lw = null;
291     PropertyInfo[] properties = t.GetProperties(BindingFlags.NonPublic | BindingFlags.Static);
292     foreach (PropertyInfo prop in properties)
293     {
294     if (prop.PropertyType == typeof(LogWriter))
295     {
296     try
297     {
298     lw = (LogWriter)prop.GetValue(null, null);
299 william 82 lh = new logwriter(lw);
300 william 28 break;
301     }
302     catch (Exception)
303     {
304     throw;
305     }
306     }
307 william 82 }
308 william 28 }
309     }
310     }
311     #endregion
312     }
313     #endregion
314    
315     #region internal class LogHelper : ILogger
316     internal class logwriter : ILogger
317     {
318     private LogWriter writer;
319     public logwriter() : this(null) { }
320     public logwriter(LogWriter writer)
321     {
322     this.writer = writer;
323     }
324     #region ILogger Members
325     public void Write(string format, params object[] args)
326     {
327     WriteToLog(format,false, args);
328     }
329     public void WriteLine(string format, params object[] args)
330     {
331     WriteToLog(format,true, args);
332     }
333     private void WriteToLog(string format, bool newline, params object[] args)
334     {
335 william 302 TextWriter tw = TextWriter.Synchronized(writer.Log);
336 william 28 if (writer != null)
337     {
338     if (newline)
339     {
340 william 291 if (args.Length == 0)
341     {
342 william 302 tw.WriteLine(format.Replace("{", "{{").Replace("}", "}}"), args);
343 william 291 return;
344     }
345 william 302 tw.WriteLine(format, args);
346 william 28 }
347     else
348     {
349 william 291 if (args.Length == 0)
350     {
351 william 302 tw.Write(format.Replace("{", "{{").Replace("}", "}}"), args);
352 william 291 return;
353     }
354 william 302 tw.Write(format, args);
355 william 28 }
356     }
357 william 302 tw.Flush();
358 william 28 }
359     #endregion
360     }
361     #endregion
362     }

  ViewVC Help
Powered by ViewVC 1.1.22