ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/RomCheater/trunk/RomCheater.Logging/Logger.cs
Revision: 26
Committed: Wed May 9 09:28:57 2012 UTC (11 years ago) by william
File size: 5323 byte(s)
Log Message:
info, debug, and error log messages

File Contents

# Content
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
20 {
21 NONE = 0x0000, // 000
22 INFO = 0x0001, // 001
23 DEBUG = 0x0002, // 010
24 ERROR= 0x0004, // 100
25 DEFAULT = INFO | ERROR,
26 ALL = 0x07,
27 }
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 public static class Info
35 {
36 private static string CreateNewFormat(string format) { return " INFO: " + format; }
37 public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.INFO)) { xlogger.Write(CreateNewFormat(format), args); } }
38 public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.INFO)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
39 }
40 public static class Debug
41 {
42 private static string CreateNewFormat(string format) { return " DEBUG: " + format; }
43 public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.DEBUG)) { xlogger.Write(CreateNewFormat(format), args); } }
44 public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.DEBUG)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
45 }
46 public static class Error
47 {
48 private static string CreateNewFormat(string format) { return " ERROR: " + format; }
49 public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.ERROR)) { xlogger.Write(CreateNewFormat(format), args); } }
50 public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.ERROR)) { xlogger.WriteLine(CreateNewFormat(format), args); } }
51 }
52 #endregion
53
54 }
55 #endregion
56
57 #region internal static class Logger
58 internal static class xlogger
59 {
60 private static logwriter lh;
61 static xlogger() { lh = new logwriter(); }
62
63 #region ILogger Members
64 public static void Write(string format, params object[] args)
65 {
66 init();
67 lh.Write(format, args);
68 }
69 public static void WriteLine(string format, params object[] args)
70 {
71 init();
72 lh.WriteLine(format, args);
73 }
74 #endregion
75
76 #region Reflection Support
77 private static void init()
78 {
79 Assembly asm = Assembly.GetEntryAssembly();
80 Type[] types = asm.GetTypes();
81
82 foreach (Type t in types)
83 {
84 if (t.BaseType == typeof(Form))
85 {
86 LogWriter lw = null;
87 PropertyInfo[] properties = t.GetProperties(BindingFlags.NonPublic | BindingFlags.Static);
88 foreach (PropertyInfo prop in properties)
89 {
90 if (prop.PropertyType == typeof(LogWriter))
91 {
92 try
93 {
94 lw = (LogWriter)prop.GetValue(null, null);
95 break;
96 }
97 catch (Exception)
98 {
99 throw;
100 }
101 }
102 }
103 lh = new logwriter(lw);
104 break;
105 }
106 }
107 }
108 #endregion
109 }
110 #endregion
111
112 #region internal class LogHelper : ILogger
113 internal class logwriter : ILogger
114 {
115 private LogWriter writer;
116 public logwriter() : this(null) { }
117 public logwriter(LogWriter writer)
118 {
119 this.writer = writer;
120 }
121 #region ILogger Members
122 public void Write(string format, params object[] args)
123 {
124 WriteToLog(format,false, args);
125 }
126 public void WriteLine(string format, params object[] args)
127 {
128 WriteToLog(format,true, args);
129 }
130 private void WriteToLog(string format, bool newline, params object[] args)
131 {
132 if (writer != null)
133 {
134 if (newline)
135 {
136 writer.Log.WriteLine(format, args);
137 }
138 else
139 {
140 writer.Log.Write(format, args);
141 }
142 }
143 }
144 #endregion
145 }
146 #endregion
147 }