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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 291 - (show annotations) (download)
Tue Jun 5 10:26:49 2012 UTC (8 years, 5 months ago) by william
File size: 17309 byte(s)
+ when args is a zero-length array (we don't need and format specifiers, change { and } to {{ }} respectively

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
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 #region loggerflags
18 public class loggerflagsbase<TValue>
19 where TValue : IConvertible
20 {
21 static loggerflagsbase()
22 {
23 value_pairs = new Dictionary<TValue, EnumNameValuePair<TValue>>();
24 name_pairs = new Dictionary<string, EnumNameValuePair<TValue>>();
25
26 init_dicts();
27 }
28 protected loggerflagsbase()
29 {
30 CurrentValue = NONE;
31 }
32 //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 private static void init_dicts()
36 {
37 value_pairs = new Dictionary<TValue, EnumNameValuePair< TValue>>();
38 name_pairs = new Dictionary<string, EnumNameValuePair< TValue>>();
39 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 private static void add_dict_entry(EnumNameValuePair<TValue> value)
50 {
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 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 public static implicit operator loggerflagsbase<TValue>(TValue t)
63 {
64 //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 }
99 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
102 #region operator overloads
103 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 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
118 return CurrentValue.ToString();
119 }
120 #endregion
121 #endregion
122 #region binary bit flags
123 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 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 public static EnumNameValuePair<TValue> ALL = new EnumNameValuePair<TValue>("ALL", EnumNameValuePair<TValue>.bitwise_or(DEFAULT, VERBOSE_DEBUG, VERBOSE_ERROR));
132 #endregion
133
134 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
138 public bool HasFlag(TValue flag)
139 {
140 bool hasflag = false;
141 TValue value = this.CurrentValue;
142 if ((EnumNameValuePair<TValue>.bitwise_and(flag, value)).Equals(flag))
143 hasflag = true;
144 return hasflag;
145 }
146 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 public static List<TValue> GetValues() { return value_pairs.Keys.ToList(); }
155 public static List<string> GetNames() { return name_pairs.Keys.ToList(); }
156
157 public string Name { get { return CurrentValue.Name; } }
158 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 public static implicit operator EnumNameValuePair<ushort>(loggerflags t) { return new EnumNameValuePair<ushort>(t.Name,t.Value); }
169 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
174 }
175
176 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 public static loggerflags GetLoggingFlags() { return logging_flags; }
182 #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 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 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 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 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 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 #region Force logging
227 public static class ForceLog
228 {
229 public static class Info
230 {
231 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(FORCED INFO) " + format; }
232 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 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 public static class Debug
242 {
243 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(FORCED DEBUG) " + format; }
244 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 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(FORCED ERROR) " + format; }
250 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 #endregion
255 #endregion
256
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 lh = new logwriter(lw);
299 break;
300 }
301 catch (Exception)
302 {
303 throw;
304 }
305 }
306 }
307 }
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 if (args.Length == 0)
339 {
340 writer.Log.WriteLine(format.Replace("{", "{{").Replace("}", "}}"), args);
341 return;
342 }
343 writer.Log.WriteLine(format, args);
344 }
345 else
346 {
347 if (args.Length == 0)
348 {
349 writer.Log.Write(format.Replace("{", "{{").Replace("}", "}}"), args);
350 return;
351 }
352 writer.Log.Write(format, args);
353 }
354 }
355 }
356 #endregion
357 }
358 #endregion
359 }

  ViewVC Help
Powered by ViewVC 1.1.22