using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; using System.Windows.Forms; namespace RomCheater.Logging { public interface ILogger { void Write(string format, params object[] args); void WriteLine(string format, params object[] args); } #region public static class Logger public static class logger { private static logwriter lh; static logger() { lh = new logwriter(); } #region ILogger Members public static void Write(string format, params object[] args) { init(); lh.Write(format, args); } public static void WriteLine(string format, params object[] args) { init(); lh.WriteLine(format, args); } #endregion #region Reflection Support private static void init() { Assembly asm = Assembly.GetEntryAssembly(); Type[] types = asm.GetTypes(); foreach (Type t in types) { if (t.BaseType == typeof(Form)) { LogWriter lw = null; PropertyInfo[] properties = t.GetProperties(BindingFlags.NonPublic | BindingFlags.Static); foreach (PropertyInfo prop in properties) { if (prop.PropertyType == typeof(LogWriter)) { try { lw = (LogWriter)prop.GetValue(null, null); break; } catch (Exception ex) { throw; } } } lh = new logwriter(lw); break; } } } #endregion } #endregion #region internal class LogHelper : ILogger internal class logwriter : ILogger { private LogWriter writer; public logwriter() : this(null) { } public logwriter(LogWriter writer) { this.writer = writer; } #region ILogger Members public void Write(string format, params object[] args) { WriteToLog(format,false, args); } public void WriteLine(string format, params object[] args) { WriteToLog(format,true, args); } private void WriteToLog(string format, bool newline, params object[] args) { if (writer != null) { if (newline) { writer.Log.WriteLine(format, args); } else { writer.Log.Write(format, args); } } } #endregion } #endregion }