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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 112 - (show annotations) (download)
Thu May 10 14:01:31 2012 UTC (9 years, 2 months ago) by william
File size: 13275 byte(s)
+ correct getting name and value from loggingflags
+ don't autosize loggingflags config control

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 MesageLogger
18 //[Flags]
19 public class loggerflags
20 {
21 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
116 public string Name { get { return CurrentValue.Name; } }
117 public ushort Value { get { return CurrentValue.Value; } }
118
119 }
120 public static class logger
121 {
122 private static loggerflags logging_flags;
123 static logger() { SetLoggingFlags(loggerflags.DEFAULT); }
124 public static void SetLoggingFlags(loggerflags flags) { logging_flags = flags; }
125 public static loggerflags GetLoggingFlags() { return logging_flags; }
126 #region sub-classes
127 private static string CreateTimeStamp()
128 {
129 string timestamp = "";
130 DateTime now = DateTime.Now;
131 timestamp = now.ToString("yyyy/MM/dd HH:mm:ss tt: ");
132 return timestamp;
133 }
134 public static class Info
135 {
136 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp()+ "(INFO) " + format; }
137 public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.INFO)) { xlogger.Write(CreateNewFormat(format), args); } }
138 public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.INFO)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
139 }
140 public static class Debug
141 {
142 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(DEBUG) " + format; }
143 public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.DEBUG)) { xlogger.Write(CreateNewFormat(format), args); } }
144 public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.DEBUG)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
145 }
146 public static class VerboseDebug
147 {
148 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(VERBOSE DEBUG) " + format; }
149 public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.VERBOSE_DEBUG)) { xlogger.Write(CreateNewFormat(format), args); } }
150 public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.VERBOSE_DEBUG)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
151 }
152 public static class Error
153 {
154 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(ERROR) " + format; }
155 public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.ERROR)) { xlogger.Write(CreateNewFormat(format), args); } }
156 public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.ERROR)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
157 }
158 public static class VerboseError
159 {
160 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(VERBOSE ERROR) " + format; }
161 public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.VERBOSE_ERROR)) { xlogger.Write(CreateNewFormat(format), args); } }
162 public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.VERBOSE_ERROR)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
163 }
164 #region Force logging
165 public static class ForceLog
166 {
167 public static class Info
168 {
169 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(FORCED INFO) " + format; }
170 public static void Write(string format, params object[] args) { xlogger.Write(CreateNewFormat(format), args); }
171 public static void WriteLine(string format, params object[] args) { xlogger.WriteLine(CreateNewFormat(format), args); }
172 }
173 public static class Debug
174 {
175 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(FORCED DEBUG) " + format; }
176 public static void Write(string format, params object[] args) { xlogger.Write(CreateNewFormat(format), args); }
177 public static void WriteLine(string format, params object[] args) { xlogger.WriteLine(CreateNewFormat(format), args); }
178 }
179 public static class Error
180 {
181 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(FORCED ERROR) " + format; }
182 public static void Write(string format, params object[] args) { xlogger.Write(CreateNewFormat(format), args); }
183 public static void WriteLine(string format, params object[] args) { xlogger.WriteLine(CreateNewFormat(format), args); }
184 }
185 }
186 #endregion
187 #endregion
188
189 }
190 #endregion
191
192 #region internal static class Logger
193 internal static class xlogger
194 {
195 private static logwriter lh;
196 static xlogger() { lh = new logwriter(); }
197
198 #region ILogger Members
199 public static void Write(string format, params object[] args)
200 {
201 init();
202 lh.Write(format, args);
203 }
204 public static void WriteLine(string format, params object[] args)
205 {
206 init();
207 lh.WriteLine(format, args);
208 }
209 #endregion
210
211 #region Reflection Support
212 private static void init()
213 {
214 Assembly asm = Assembly.GetEntryAssembly();
215 Type[] types = asm.GetTypes();
216
217 foreach (Type t in types)
218 {
219 if (t.BaseType == typeof(Form))
220 {
221 LogWriter lw = null;
222 PropertyInfo[] properties = t.GetProperties(BindingFlags.NonPublic | BindingFlags.Static);
223 foreach (PropertyInfo prop in properties)
224 {
225 if (prop.PropertyType == typeof(LogWriter))
226 {
227 try
228 {
229 lw = (LogWriter)prop.GetValue(null, null);
230 lh = new logwriter(lw);
231 break;
232 }
233 catch (Exception)
234 {
235 throw;
236 }
237 }
238 }
239 }
240 }
241 }
242 #endregion
243 }
244 #endregion
245
246 #region internal class LogHelper : ILogger
247 internal class logwriter : ILogger
248 {
249 private LogWriter writer;
250 public logwriter() : this(null) { }
251 public logwriter(LogWriter writer)
252 {
253 this.writer = writer;
254 }
255 #region ILogger Members
256 public void Write(string format, params object[] args)
257 {
258 WriteToLog(format,false, args);
259 }
260 public void WriteLine(string format, params object[] args)
261 {
262 WriteToLog(format,true, args);
263 }
264 private void WriteToLog(string format, bool newline, params object[] args)
265 {
266 if (writer != null)
267 {
268 if (newline)
269 {
270 writer.Log.WriteLine(format, args);
271 }
272 else
273 {
274 writer.Log.Write(format, args);
275 }
276 }
277 }
278 #endregion
279 }
280 #endregion
281 }

  ViewVC Help
Powered by ViewVC 1.1.22