ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/RomCheater/trunk/RomCheater.Logging/logger.cs
Revision: 82
Committed: Wed May 9 17:48:05 2012 UTC (11 years, 7 months ago) by william
File size: 5662 byte(s)
Log Message:
fix issues with logging control not being found

File Contents

# User Rev Content
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 william 82 lh = new logwriter(lw);
103 william 28 break;
104     }
105     catch (Exception)
106     {
107     throw;
108     }
109     }
110 william 82 }
111 william 28 }
112     }
113     }
114     #endregion
115     }
116     #endregion
117    
118     #region internal class LogHelper : ILogger
119     internal class logwriter : ILogger
120     {
121     private LogWriter writer;
122     public logwriter() : this(null) { }
123     public logwriter(LogWriter writer)
124     {
125     this.writer = writer;
126     }
127     #region ILogger Members
128     public void Write(string format, params object[] args)
129     {
130     WriteToLog(format,false, args);
131     }
132     public void WriteLine(string format, params object[] args)
133     {
134     WriteToLog(format,true, args);
135     }
136     private void WriteToLog(string format, bool newline, params object[] args)
137     {
138     if (writer != null)
139     {
140     if (newline)
141     {
142     writer.Log.WriteLine(format, args);
143     }
144     else
145     {
146     writer.Log.Write(format, args);
147     }
148     }
149     }
150     #endregion
151     }
152     #endregion
153     }