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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 95 - (hide annotations) (download)
Wed May 9 22:37:17 2012 UTC (9 years, 1 month ago) by william
File size: 7169 byte(s)
logger add a forced log class (which ignores logging flags)

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

  ViewVC Help
Powered by ViewVC 1.1.22