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

Contents of /trunk/RomCheater.Logging/Logger.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 17 - (show annotations) (download)
Wed May 9 08:02:18 2012 UTC (8 years, 4 months ago) by william
File size: 3139 byte(s)
implement basic logging support

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 #region public static class Logger
17 public static class logger
18 {
19 private static logwriter lh;
20 static logger() { lh = new logwriter(); }
21
22 #region ILogger Members
23 public static void Write(string format, params object[] args)
24 {
25 init();
26 lh.Write(format, args);
27 }
28 public static void WriteLine(string format, params object[] args)
29 {
30 init();
31 lh.WriteLine(format, args);
32 }
33 #endregion
34
35 #region Reflection Support
36 private static void init()
37 {
38 Assembly asm = Assembly.GetEntryAssembly();
39 Type[] types = asm.GetTypes();
40
41 foreach (Type t in types)
42 {
43 if (t.BaseType == typeof(Form))
44 {
45 LogWriter lw = null;
46 PropertyInfo[] properties = t.GetProperties(BindingFlags.NonPublic | BindingFlags.Static);
47 foreach (PropertyInfo prop in properties)
48 {
49 if (prop.PropertyType == typeof(LogWriter))
50 {
51 try
52 {
53 lw = (LogWriter)prop.GetValue(null, null);
54 break;
55 }
56 catch (Exception ex)
57 {
58 throw;
59 }
60 }
61 }
62 lh = new logwriter(lw);
63 break;
64 }
65 }
66 }
67 #endregion
68 }
69 #endregion
70
71 #region internal class LogHelper : ILogger
72 internal class logwriter : ILogger
73 {
74 private LogWriter writer;
75 public logwriter() : this(null) { }
76 public logwriter(LogWriter writer)
77 {
78 this.writer = writer;
79 }
80 #region ILogger Members
81 public void Write(string format, params object[] args)
82 {
83 WriteToLog(format,false, args);
84 }
85 public void WriteLine(string format, params object[] args)
86 {
87 WriteToLog(format,true, args);
88 }
89 private void WriteToLog(string format, bool newline, params object[] args)
90 {
91 if (writer != null)
92 {
93 if (newline)
94 {
95 writer.Log.WriteLine(format, args);
96 }
97 else
98 {
99 writer.Log.Write(format, args);
100 }
101 }
102 }
103 #endregion
104 }
105 #endregion
106 }

  ViewVC Help
Powered by ViewVC 1.1.22