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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 111 - (hide annotations) (download)
Thu May 10 13:55:44 2012 UTC (9 years, 1 month ago) by william
File size: 13132 byte(s)
add support for implictly converting binary bit flags to enum-like values

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

  ViewVC Help
Powered by ViewVC 1.1.22