ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/RomCheater/trunk/RomCheater.Logging/Logger.cs
Revision: 24
Committed: Wed May 9 08:59:52 2012 UTC (10 years, 10 months ago) by william
File size: 3244 byte(s)
Log Message:
logging support should be done now
logging includes:
* log to main application
* log to file

File Contents

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