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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 116 - (hide annotations) (download)
Thu May 10 15:30:37 2012 UTC (8 years, 9 months ago) by william
File size: 14339 byte(s)

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     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     public static EnumNameValuePair<TValue> DEFAULT = new EnumNameValuePair<TValue>("DEFAULT", EnumNameValuePair<TValue>.bitwise_or(INFO,DEBUG,ERROR));
98     public static EnumNameValuePair<TValue> ALL = new EnumNameValuePair<TValue>("ALL", EnumNameValuePair<TValue>.bitwise_or(DEFAULT, VERBOSE_DEBUG, VERBOSE_ERROR));
99 william 111 #endregion
100    
101 william 116 protected static Dictionary<TValue, EnumNameValuePair<TValue>> value_pairs;
102     protected static Dictionary<string, EnumNameValuePair<TValue>> name_pairs;
103     private EnumNameValuePair<TValue> CurrentValue { get; set; }
104 william 111
105 william 116 public bool HasFlag(TValue flag)
106 william 111 {
107     bool hasflag = false;
108 william 116 TValue value = this.CurrentValue;
109     if (!(EnumNameValuePair<TValue>.bitwise_and(flag, value)).Equals(0))
110 william 111 hasflag = true;
111     return hasflag;
112     }
113    
114 william 116 public static List<TValue> GetValues() { return value_pairs.Keys.ToList(); }
115 william 111 public static List<string> GetNames() { return name_pairs.Keys.ToList(); }
116 william 112
117     public string Name { get { return CurrentValue.Name; } }
118 william 116 public TValue Value { get { return CurrentValue.Value; } }
119     }
120     //[Flags]
121     public class loggerflags : loggerflagsbase<ushort>
122     {
123     protected loggerflags() :base() { }
124     //protected loggerflags(ushort t) : this() { CurrentValue = new EnumNameValuePair< ushort>("", t); }
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     public static implicit operator EnumNameValuePair<ushort>(loggerflags t) { return new loggerflags(t.Value); }
130     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     public static class Debug
158     {
159     private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(DEBUG) " + format; }
160     public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.DEBUG)) { xlogger.Write(CreateNewFormat(format), args); } }
161     public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.DEBUG)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
162     }
163 william 99 public static class VerboseDebug
164     {
165     private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(VERBOSE DEBUG) " + format; }
166     public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.VERBOSE_DEBUG)) { xlogger.Write(CreateNewFormat(format), args); } }
167     public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.VERBOSE_DEBUG)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
168     }
169 william 28 public static class Error
170     {
171     private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(ERROR) " + format; }
172     public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.ERROR)) { xlogger.Write(CreateNewFormat(format), args); } }
173     public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.ERROR)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
174     }
175 william 99 public static class VerboseError
176     {
177     private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(VERBOSE ERROR) " + format; }
178     public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.VERBOSE_ERROR)) { xlogger.Write(CreateNewFormat(format), args); } }
179     public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.VERBOSE_ERROR)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
180     }
181 william 95 #region Force logging
182     public static class ForceLog
183     {
184     public static class Info
185     {
186 william 96 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(FORCED INFO) " + format; }
187 william 95 public static void Write(string format, params object[] args) { xlogger.Write(CreateNewFormat(format), args); }
188     public static void WriteLine(string format, params object[] args) { xlogger.WriteLine(CreateNewFormat(format), args); }
189     }
190     public static class Debug
191     {
192 william 96 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(FORCED DEBUG) " + 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     public static class Error
197     {
198 william 96 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(FORCED ERROR) " + format; }
199 william 95 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     }
203 william 28 #endregion
204 william 95 #endregion
205 william 28
206     }
207     #endregion
208    
209     #region internal static class Logger
210     internal static class xlogger
211     {
212     private static logwriter lh;
213     static xlogger() { lh = new logwriter(); }
214    
215     #region ILogger Members
216     public static void Write(string format, params object[] args)
217     {
218     init();
219     lh.Write(format, args);
220     }
221     public static void WriteLine(string format, params object[] args)
222     {
223     init();
224     lh.WriteLine(format, args);
225     }
226     #endregion
227    
228     #region Reflection Support
229     private static void init()
230     {
231     Assembly asm = Assembly.GetEntryAssembly();
232     Type[] types = asm.GetTypes();
233    
234     foreach (Type t in types)
235     {
236     if (t.BaseType == typeof(Form))
237     {
238     LogWriter lw = null;
239     PropertyInfo[] properties = t.GetProperties(BindingFlags.NonPublic | BindingFlags.Static);
240     foreach (PropertyInfo prop in properties)
241     {
242     if (prop.PropertyType == typeof(LogWriter))
243     {
244     try
245     {
246     lw = (LogWriter)prop.GetValue(null, null);
247 william 82 lh = new logwriter(lw);
248 william 28 break;
249     }
250     catch (Exception)
251     {
252     throw;
253     }
254     }
255 william 82 }
256 william 28 }
257     }
258     }
259     #endregion
260     }
261     #endregion
262    
263     #region internal class LogHelper : ILogger
264     internal class logwriter : ILogger
265     {
266     private LogWriter writer;
267     public logwriter() : this(null) { }
268     public logwriter(LogWriter writer)
269     {
270     this.writer = writer;
271     }
272     #region ILogger Members
273     public void Write(string format, params object[] args)
274     {
275     WriteToLog(format,false, args);
276     }
277     public void WriteLine(string format, params object[] args)
278     {
279     WriteToLog(format,true, args);
280     }
281     private void WriteToLog(string format, bool newline, params object[] args)
282     {
283     if (writer != null)
284     {
285     if (newline)
286     {
287     writer.Log.WriteLine(format, args);
288     }
289     else
290     {
291     writer.Log.Write(format, args);
292     }
293     }
294     }
295     #endregion
296     }
297     #endregion
298     }

  ViewVC Help
Powered by ViewVC 1.1.22