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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 97 - (hide annotations) (download)
Wed May 9 22:48:59 2012 UTC (8 years, 9 months ago) by william
File size: 7277 byte(s)
get logging flags directly from logger class

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

  ViewVC Help
Powered by ViewVC 1.1.22