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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

1 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 using System.IO;
8
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 #region loggerflags
19 public class loggerflagsbase<TValue>
20 where TValue : IConvertible
21 {
22 static loggerflagsbase()
23 {
24 value_pairs = new Dictionary<TValue, EnumNameValuePair<TValue>>();
25 name_pairs = new Dictionary<string, EnumNameValuePair<TValue>>();
26
27 init_dicts();
28 }
29 protected loggerflagsbase()
30 {
31 CurrentValue = NONE;
32 }
33 //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 private static void init_dicts()
37 {
38 value_pairs = new Dictionary<TValue, EnumNameValuePair< TValue>>();
39 name_pairs = new Dictionary<string, EnumNameValuePair< TValue>>();
40 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 private static void add_dict_entry(EnumNameValuePair<TValue> value)
51 {
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 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 public static implicit operator loggerflagsbase<TValue>(TValue t)
64 {
65 //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 }
100 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
103 #region operator overloads
104 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 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
119 return CurrentValue.ToString();
120 }
121 #endregion
122 #endregion
123 #region binary bit flags
124 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 public static EnumNameValuePair<TValue> WARN = new EnumNameValuePair<TValue>("WARN", Binary<TValue>.ToValue("100000"));
131 public static EnumNameValuePair<TValue> PROFILER = new EnumNameValuePair<TValue>("PROFILER", Binary<TValue>.ToValue("1000000"));
132 public static EnumNameValuePair<TValue> DEFAULT = new EnumNameValuePair<TValue>("DEFAULT", EnumNameValuePair<TValue>.bitwise_or(INFO, DEBUG, ERROR, WARN));
133 public static EnumNameValuePair<TValue> ALL = new EnumNameValuePair<TValue>("ALL", EnumNameValuePair<TValue>.bitwise_or(DEFAULT, VERBOSE_DEBUG, VERBOSE_ERROR, PROFILER));
134 #endregion
135
136 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
140 public bool HasFlag(TValue flag)
141 {
142 bool hasflag = false;
143 TValue value = this.CurrentValue;
144 if ((EnumNameValuePair<TValue>.bitwise_and(flag, value)).Equals(flag))
145 hasflag = true;
146 return hasflag;
147 }
148 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 public static List<TValue> GetValues() { return value_pairs.Keys.ToList(); }
157 public static List<string> GetNames() { return name_pairs.Keys.ToList(); }
158
159 public string Name { get { return CurrentValue.Name; } }
160 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 public static implicit operator EnumNameValuePair<ushort>(loggerflags t) { return new EnumNameValuePair<ushort>(t.Name,t.Value); }
171 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
176 }
177
178 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 public static loggerflags GetLoggingFlags() { return logging_flags; }
184 #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 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 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 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 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 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 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 #region Force logging
235 public static class ForceLog
236 {
237 public static class Info
238 {
239 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(FORCED INFO) " + format; }
240 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 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 public static class Debug
250 {
251 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(FORCED DEBUG) " + format; }
252 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 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(FORCED ERROR) " + format; }
258 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 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 }
268 #endregion
269 #endregion
270
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 lh = new logwriter(lw);
313 break;
314 }
315 catch (Exception)
316 {
317 throw;
318 }
319 }
320 }
321 }
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 TextWriter tw = TextWriter.Synchronized(writer.Log);
349 if (writer != null)
350 {
351 if (newline)
352 {
353 if (args.Length == 0)
354 {
355 tw.WriteLine(format.Replace("{", "{{").Replace("}", "}}"), args);
356 return;
357 }
358 tw.WriteLine(format, args);
359 }
360 else
361 {
362 if (args.Length == 0)
363 {
364 tw.Write(format.Replace("{", "{{").Replace("}", "}}"), args);
365 return;
366 }
367 tw.Write(format, args);
368 }
369 }
370 tw.Flush();
371 }
372 #endregion
373 }
374 #endregion
375 }

  ViewVC Help
Powered by ViewVC 1.1.22