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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 99 - (show annotations) (download)
Wed May 9 23:23:38 2012 UTC (9 years, 6 months ago) by william
File size: 8570 byte(s)
+ add logging options:
* VERBOSE_DEBUG and VERBOSE_ERROR : for debug and error messages that are very 'chatty'

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 enum loggerflags :ushort
20 {
21 NONE = 0x0000, // 00000
22 INFO = 0x0001, // 00001
23 DEBUG = 0x0002, // 00010
24 ERROR= 0x0004, // 00100
25 VERBOSE_DEBUG = 0x0008, // 01000
26 VERBOSE_ERROR = 0x0010, // 10000
27 DEFAULT = INFO | ERROR | DEBUG,
28 ALL = DEFAULT | VERBOSE_DEBUG | VERBOSE_ERROR
29 }
30 public static class logger
31 {
32 private static loggerflags logging_flags;
33 static logger() { SetLoggingFlags(loggerflags.DEFAULT); }
34 public static void SetLoggingFlags(loggerflags flags) { logging_flags = flags; }
35 public static loggerflags GetLoggingFlags() { return logging_flags; }
36 #region sub-classes
37 private static string CreateTimeStamp()
38 {
39 string timestamp = "";
40 DateTime now = DateTime.Now;
41 timestamp = now.ToString("yyyy/MM/dd HH:mm:ss tt: ");
42 return timestamp;
43 }
44 public static class Info
45 {
46 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp()+ "(INFO) " + format; }
47 public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.INFO)) { xlogger.Write(CreateNewFormat(format), args); } }
48 public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.INFO)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
49 }
50 public static class Debug
51 {
52 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(DEBUG) " + format; }
53 public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.DEBUG)) { xlogger.Write(CreateNewFormat(format), args); } }
54 public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.DEBUG)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
55 }
56 public static class VerboseDebug
57 {
58 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(VERBOSE DEBUG) " + format; }
59 public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.VERBOSE_DEBUG)) { xlogger.Write(CreateNewFormat(format), args); } }
60 public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.VERBOSE_DEBUG)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
61 }
62 public static class Error
63 {
64 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(ERROR) " + format; }
65 public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.ERROR)) { xlogger.Write(CreateNewFormat(format), args); } }
66 public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.ERROR)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
67 }
68 public static class VerboseError
69 {
70 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(VERBOSE ERROR) " + format; }
71 public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.VERBOSE_ERROR)) { xlogger.Write(CreateNewFormat(format), args); } }
72 public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.VERBOSE_ERROR)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
73 }
74 #region Force logging
75 public static class ForceLog
76 {
77 public static class Info
78 {
79 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(FORCED INFO) " + format; }
80 public static void Write(string format, params object[] args) { xlogger.Write(CreateNewFormat(format), args); }
81 public static void WriteLine(string format, params object[] args) { xlogger.WriteLine(CreateNewFormat(format), args); }
82 }
83 public static class Debug
84 {
85 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(FORCED DEBUG) " + format; }
86 public static void Write(string format, params object[] args) { xlogger.Write(CreateNewFormat(format), args); }
87 public static void WriteLine(string format, params object[] args) { xlogger.WriteLine(CreateNewFormat(format), args); }
88 }
89 public static class Error
90 {
91 private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(FORCED ERROR) " + format; }
92 public static void Write(string format, params object[] args) { xlogger.Write(CreateNewFormat(format), args); }
93 public static void WriteLine(string format, params object[] args) { xlogger.WriteLine(CreateNewFormat(format), args); }
94 }
95 }
96 #endregion
97 #endregion
98
99 }
100 #endregion
101
102 #region internal static class Logger
103 internal static class xlogger
104 {
105 private static logwriter lh;
106 static xlogger() { lh = new logwriter(); }
107
108 #region ILogger Members
109 public static void Write(string format, params object[] args)
110 {
111 init();
112 lh.Write(format, args);
113 }
114 public static void WriteLine(string format, params object[] args)
115 {
116 init();
117 lh.WriteLine(format, args);
118 }
119 #endregion
120
121 #region Reflection Support
122 private static void init()
123 {
124 Assembly asm = Assembly.GetEntryAssembly();
125 Type[] types = asm.GetTypes();
126
127 foreach (Type t in types)
128 {
129 if (t.BaseType == typeof(Form))
130 {
131 LogWriter lw = null;
132 PropertyInfo[] properties = t.GetProperties(BindingFlags.NonPublic | BindingFlags.Static);
133 foreach (PropertyInfo prop in properties)
134 {
135 if (prop.PropertyType == typeof(LogWriter))
136 {
137 try
138 {
139 lw = (LogWriter)prop.GetValue(null, null);
140 lh = new logwriter(lw);
141 break;
142 }
143 catch (Exception)
144 {
145 throw;
146 }
147 }
148 }
149 }
150 }
151 }
152 #endregion
153 }
154 #endregion
155
156 #region internal class LogHelper : ILogger
157 internal class logwriter : ILogger
158 {
159 private LogWriter writer;
160 public logwriter() : this(null) { }
161 public logwriter(LogWriter writer)
162 {
163 this.writer = writer;
164 }
165 #region ILogger Members
166 public void Write(string format, params object[] args)
167 {
168 WriteToLog(format,false, args);
169 }
170 public void WriteLine(string format, params object[] args)
171 {
172 WriteToLog(format,true, args);
173 }
174 private void WriteToLog(string format, bool newline, params object[] args)
175 {
176 if (writer != null)
177 {
178 if (newline)
179 {
180 writer.Log.WriteLine(format, args);
181 }
182 else
183 {
184 writer.Log.Write(format, args);
185 }
186 }
187 }
188 #endregion
189 }
190 #endregion
191 }

  ViewVC Help
Powered by ViewVC 1.1.22