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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 265 - (hide annotations) (download)
Sun Jun 3 19:47:47 2012 UTC (9 years, 5 months ago) by william
File size: 15399 byte(s)
+ fix stackoverflow exceptions in config and logger

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 116 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 william 111 }
67 william 116 public static implicit operator TValue(loggerflagsbase<TValue> t) { return t.CurrentValue.Value; }
68     public static implicit operator string(loggerflagsbase<TValue> t) { return t.CurrentValue.Name; }
69 william 111
70     #region operator overloads
71 william 116 public static bool operator ==(loggerflagsbase<TValue> x, loggerflagsbase<TValue> y) { return x.CurrentValue == y.CurrentValue; }
72     public static bool operator !=(loggerflagsbase<TValue> x, loggerflagsbase<TValue> y) { return x.CurrentValue != y.CurrentValue; }
73 william 111 public override bool Equals(object obj)
74     {
75     loggerflags t = (obj as loggerflags);
76     if (t == null) return false;
77     return this.CurrentValue.Equals(t);
78     }
79     public override int GetHashCode()
80     {
81     return CurrentValue.GetHashCode();
82     }
83     public override string ToString()
84     {
85 william 116
86 william 111 return CurrentValue.ToString();
87     }
88     #endregion
89     #endregion
90     #region binary bit flags
91 william 116 public static EnumNameValuePair<TValue> NONE = new EnumNameValuePair<TValue>("NONE", Binary<TValue>.ToValue("00000"));
92     public static EnumNameValuePair<TValue> INFO = new EnumNameValuePair<TValue>("INFO", Binary<TValue>.ToValue("00001"));
93     public static EnumNameValuePair<TValue> DEBUG = new EnumNameValuePair<TValue>("DEBUG", Binary<TValue>.ToValue("00010"));
94     public static EnumNameValuePair<TValue> ERROR = new EnumNameValuePair<TValue>("ERROR", Binary<TValue>.ToValue("00100"));
95     public static EnumNameValuePair<TValue> VERBOSE_DEBUG = new EnumNameValuePair<TValue>("VERBOSE_DEBUG", Binary<TValue>.ToValue("01000"));
96     public static EnumNameValuePair<TValue> VERBOSE_ERROR = new EnumNameValuePair<TValue>("VERBOSE_ERROR", Binary<TValue>.ToValue("10000"));
97 william 163 public static EnumNameValuePair<TValue> WARN = new EnumNameValuePair<TValue>("WARN", Binary<TValue>.ToValue("100000"));
98     public static EnumNameValuePair<TValue> DEFAULT = new EnumNameValuePair<TValue>("DEFAULT", EnumNameValuePair<TValue>.bitwise_or(INFO, DEBUG, ERROR, WARN));
99 william 116 public static EnumNameValuePair<TValue> ALL = new EnumNameValuePair<TValue>("ALL", EnumNameValuePair<TValue>.bitwise_or(DEFAULT, VERBOSE_DEBUG, VERBOSE_ERROR));
100 william 111 #endregion
101    
102 william 116 protected static Dictionary<TValue, EnumNameValuePair<TValue>> value_pairs;
103     protected static Dictionary<string, EnumNameValuePair<TValue>> name_pairs;
104     private EnumNameValuePair<TValue> CurrentValue { get; set; }
105 william 111
106 william 116 public bool HasFlag(TValue flag)
107 william 111 {
108     bool hasflag = false;
109 william 116 TValue value = this.CurrentValue;
110 william 151 if (!(EnumNameValuePair<TValue>.bitwise_and(flag, value)).Equals(value))
111 william 111 hasflag = true;
112     return hasflag;
113     }
114    
115 william 116 public static List<TValue> GetValues() { return value_pairs.Keys.ToList(); }
116 william 111 public static List<string> GetNames() { return name_pairs.Keys.ToList(); }
117 william 112
118     public string Name { get { return CurrentValue.Name; } }
119 william 116 public TValue Value { get { return CurrentValue.Value; } }
120     }
121     //[Flags]
122     public class loggerflags : loggerflagsbase<ushort>
123     {
124     protected loggerflags() :base() { }
125     protected loggerflags(EnumNameValuePair<ushort> t) : base(t) { }
126     protected loggerflags(loggerflagsbase<ushort> t) : base(t) { }
127     #region implicit operators
128     public static implicit operator loggerflags(EnumNameValuePair<ushort> t) { return new loggerflags(t); }
129 william 265 public static implicit operator EnumNameValuePair<ushort>(loggerflags t) { return new EnumNameValuePair<ushort>(t.Name,t.Value); }
130 william 116 public static implicit operator ushort(loggerflags t) { return t.Value; }
131     public static implicit operator loggerflags(ushort t) { return new loggerflags(t); }
132     public static implicit operator string(loggerflags t) { return t.Name; }
133     #endregion
134 william 112
135 william 28 }
136 william 116
137 william 28 public static class logger
138     {
139     private static loggerflags logging_flags;
140     static logger() { SetLoggingFlags(loggerflags.DEFAULT); }
141     public static void SetLoggingFlags(loggerflags flags) { logging_flags = flags; }
142 william 112 public static loggerflags GetLoggingFlags() { return logging_flags; }
143 william 28 #region sub-classes
144     private static string CreateTimeStamp()
145     {
146     string timestamp = "";
147     DateTime now = DateTime.Now;
148     timestamp = now.ToString("yyyy/MM/dd HH:mm:ss tt: ");
149     return timestamp;
150     }
151     public static class Info
152     {
153     private static string CreateNewFormat(string format) { return " " + CreateTimeStamp()+ "(INFO) " + format; }
154     public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.INFO)) { xlogger.Write(CreateNewFormat(format), args); } }
155     public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.INFO)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
156     }
157 william 163 public static class Warn
158     {
159     private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(WARN) " + format; }
160     public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.WARN)) { xlogger.Write(CreateNewFormat(format), args); } }
161     public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.WARN)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
162     }
163 william 28 public static class Debug
164     {
165     private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(DEBUG) " + format; }
166     public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.DEBUG)) { xlogger.Write(CreateNewFormat(format), args); } }
167     public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.DEBUG)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
168     }
169 william 99 public static class VerboseDebug
170     {
171     private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(VERBOSE DEBUG) " + format; }
172     public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.VERBOSE_DEBUG)) { xlogger.Write(CreateNewFormat(format), args); } }
173     public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.VERBOSE_DEBUG)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
174     }
175 william 28 public static class Error
176     {
177     private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(ERROR) " + format; }
178     public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.ERROR)) { xlogger.Write(CreateNewFormat(format), args); } }
179     public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.ERROR)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
180     }
181 william 99 public static class VerboseError
182     {
183     private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(VERBOSE ERROR) " + format; }
184     public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.VERBOSE_ERROR)) { xlogger.Write(CreateNewFormat(format), args); } }
185     public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.VERBOSE_ERROR)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
186     }
187 william 95 #region Force logging
188     public static class ForceLog
189     {
190     public static class Info
191     {
192 william 96 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(FORCED INFO) " + format; }
193 william 95 public static void Write(string format, params object[] args) { xlogger.Write(CreateNewFormat(format), args); }
194     public static void WriteLine(string format, params object[] args) { xlogger.WriteLine(CreateNewFormat(format), args); }
195     }
196 william 163 public static class Warn
197     {
198     private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(FORCED Warn) " + format; }
199     public static void Write(string format, params object[] args) { xlogger.Write(CreateNewFormat(format), args); }
200     public static void WriteLine(string format, params object[] args) { xlogger.WriteLine(CreateNewFormat(format), args); }
201     }
202 william 95 public static class Debug
203     {
204 william 96 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(FORCED DEBUG) " + format; }
205 william 95 public static void Write(string format, params object[] args) { xlogger.Write(CreateNewFormat(format), args); }
206     public static void WriteLine(string format, params object[] args) { xlogger.WriteLine(CreateNewFormat(format), args); }
207     }
208     public static class Error
209     {
210 william 96 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(FORCED ERROR) " + format; }
211 william 95 public static void Write(string format, params object[] args) { xlogger.Write(CreateNewFormat(format), args); }
212     public static void WriteLine(string format, params object[] args) { xlogger.WriteLine(CreateNewFormat(format), args); }
213     }
214     }
215 william 28 #endregion
216 william 95 #endregion
217 william 28
218     }
219     #endregion
220    
221     #region internal static class Logger
222     internal static class xlogger
223     {
224     private static logwriter lh;
225     static xlogger() { lh = new logwriter(); }
226    
227     #region ILogger Members
228     public static void Write(string format, params object[] args)
229     {
230     init();
231     lh.Write(format, args);
232     }
233     public static void WriteLine(string format, params object[] args)
234     {
235     init();
236     lh.WriteLine(format, args);
237     }
238     #endregion
239    
240     #region Reflection Support
241     private static void init()
242     {
243     Assembly asm = Assembly.GetEntryAssembly();
244     Type[] types = asm.GetTypes();
245    
246     foreach (Type t in types)
247     {
248     if (t.BaseType == typeof(Form))
249     {
250     LogWriter lw = null;
251     PropertyInfo[] properties = t.GetProperties(BindingFlags.NonPublic | BindingFlags.Static);
252     foreach (PropertyInfo prop in properties)
253     {
254     if (prop.PropertyType == typeof(LogWriter))
255     {
256     try
257     {
258     lw = (LogWriter)prop.GetValue(null, null);
259 william 82 lh = new logwriter(lw);
260 william 28 break;
261     }
262     catch (Exception)
263     {
264     throw;
265     }
266     }
267 william 82 }
268 william 28 }
269     }
270     }
271     #endregion
272     }
273     #endregion
274    
275     #region internal class LogHelper : ILogger
276     internal class logwriter : ILogger
277     {
278     private LogWriter writer;
279     public logwriter() : this(null) { }
280     public logwriter(LogWriter writer)
281     {
282     this.writer = writer;
283     }
284     #region ILogger Members
285     public void Write(string format, params object[] args)
286     {
287     WriteToLog(format,false, args);
288     }
289     public void WriteLine(string format, params object[] args)
290     {
291     WriteToLog(format,true, args);
292     }
293     private void WriteToLog(string format, bool newline, params object[] args)
294     {
295     if (writer != null)
296     {
297     if (newline)
298     {
299     writer.Log.WriteLine(format, args);
300     }
301     else
302     {
303     writer.Log.Write(format, args);
304     }
305     }
306     }
307     #endregion
308     }
309     #endregion
310     }

  ViewVC Help
Powered by ViewVC 1.1.22