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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 64 - (hide annotations) (download)
Wed May 9 16:35:07 2012 UTC (8 years, 11 months ago) by william
File size: 5658 byte(s)
modify user settings dialog

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     #endregion
60    
61     }
62     #endregion
63    
64     #region internal static class Logger
65     internal static class xlogger
66     {
67     private static logwriter lh;
68     static xlogger() { lh = new logwriter(); }
69    
70     #region ILogger Members
71     public static void Write(string format, params object[] args)
72     {
73     init();
74     lh.Write(format, args);
75     }
76     public static void WriteLine(string format, params object[] args)
77     {
78     init();
79     lh.WriteLine(format, args);
80     }
81     #endregion
82    
83     #region Reflection Support
84     private static void init()
85     {
86     Assembly asm = Assembly.GetEntryAssembly();
87     Type[] types = asm.GetTypes();
88    
89     foreach (Type t in types)
90     {
91     if (t.BaseType == typeof(Form))
92     {
93     LogWriter lw = null;
94     PropertyInfo[] properties = t.GetProperties(BindingFlags.NonPublic | BindingFlags.Static);
95     foreach (PropertyInfo prop in properties)
96     {
97     if (prop.PropertyType == typeof(LogWriter))
98     {
99     try
100     {
101     lw = (LogWriter)prop.GetValue(null, null);
102     break;
103     }
104     catch (Exception)
105     {
106     throw;
107     }
108     }
109     }
110     lh = new logwriter(lw);
111     break;
112     }
113     }
114     }
115     #endregion
116     }
117     #endregion
118    
119     #region internal class LogHelper : ILogger
120     internal class logwriter : ILogger
121     {
122     private LogWriter writer;
123     public logwriter() : this(null) { }
124     public logwriter(LogWriter writer)
125     {
126     this.writer = writer;
127     }
128     #region ILogger Members
129     public void Write(string format, params object[] args)
130     {
131     WriteToLog(format,false, args);
132     }
133     public void WriteLine(string format, params object[] args)
134     {
135     WriteToLog(format,true, args);
136     }
137     private void WriteToLog(string format, bool newline, params object[] args)
138     {
139     if (writer != null)
140     {
141     if (newline)
142     {
143     writer.Log.WriteLine(format, args);
144     }
145     else
146     {
147     writer.Log.Write(format, args);
148     }
149     }
150     }
151     #endregion
152     }
153     #endregion
154     }

  ViewVC Help
Powered by ViewVC 1.1.22