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 loggerflags public class loggerflagsbase where TValue : IConvertible { static loggerflagsbase() { value_pairs = new Dictionary>(); name_pairs = new Dictionary>(); init_dicts(); } protected loggerflagsbase() { CurrentValue = NONE; } //protected loggerflags(ushort t) : this() { CurrentValue = new EnumNameValuePair< ushort>("", t); } protected loggerflagsbase(EnumNameValuePair< TValue> t) { CurrentValue = t; } protected loggerflagsbase(loggerflagsbase t) { this.CurrentValue = t.CurrentValue; } private static void init_dicts() { value_pairs = new Dictionary>(); name_pairs = new Dictionary>(); add_dict_entry(NONE); add_dict_entry(INFO); add_dict_entry(DEBUG); add_dict_entry(ERROR); add_dict_entry(VERBOSE_DEBUG); add_dict_entry(VERBOSE_ERROR); add_dict_entry(DEFAULT); add_dict_entry(ALL); } private static void add_dict_entry(EnumNameValuePair value) { try { value_pairs.Add(value, value); name_pairs.Add(value, value); } catch { } } #region implicit operators public static implicit operator loggerflagsbase(EnumNameValuePair t) { return new loggerflagsbase(t); } public static implicit operator EnumNameValuePair(loggerflagsbase t) { return new loggerflagsbase(t); } public static implicit operator loggerflagsbase(TValue t) { foreach (KeyValuePair> pair in value_pairs) { EnumNameValuePair l = pair.Value; if (l.Value.Equals(t)) { return new loggerflagsbase(l); } } return loggerflagsbase.NONE; } public static implicit operator TValue(loggerflagsbase t) { return t.CurrentValue.Value; } public static implicit operator string(loggerflagsbase t) { return t.CurrentValue.Name; } #region operator overloads public static bool operator ==(loggerflagsbase x, loggerflagsbase y) { return x.CurrentValue == y.CurrentValue; } public static bool operator !=(loggerflagsbase x, loggerflagsbase y) { return x.CurrentValue != y.CurrentValue; } public override bool Equals(object obj) { loggerflags t = (obj as loggerflags); if (t == null) return false; return this.CurrentValue.Equals(t); } public override int GetHashCode() { return CurrentValue.GetHashCode(); } public override string ToString() { return CurrentValue.ToString(); } #endregion #endregion #region binary bit flags public static EnumNameValuePair NONE = new EnumNameValuePair("NONE", Binary.ToValue("00000")); public static EnumNameValuePair INFO = new EnumNameValuePair("INFO", Binary.ToValue("00001")); public static EnumNameValuePair DEBUG = new EnumNameValuePair("DEBUG", Binary.ToValue("00010")); public static EnumNameValuePair ERROR = new EnumNameValuePair("ERROR", Binary.ToValue("00100")); public static EnumNameValuePair VERBOSE_DEBUG = new EnumNameValuePair("VERBOSE_DEBUG", Binary.ToValue("01000")); public static EnumNameValuePair VERBOSE_ERROR = new EnumNameValuePair("VERBOSE_ERROR", Binary.ToValue("10000")); public static EnumNameValuePair WARN = new EnumNameValuePair("WARN", Binary.ToValue("100000")); public static EnumNameValuePair DEFAULT = new EnumNameValuePair("DEFAULT", EnumNameValuePair.bitwise_or(INFO, DEBUG, ERROR, WARN)); public static EnumNameValuePair ALL = new EnumNameValuePair("ALL", EnumNameValuePair.bitwise_or(DEFAULT, VERBOSE_DEBUG, VERBOSE_ERROR)); #endregion protected static Dictionary> value_pairs; protected static Dictionary> name_pairs; private EnumNameValuePair CurrentValue { get; set; } public bool HasFlag(TValue flag) { bool hasflag = false; TValue value = this.CurrentValue; if (!(EnumNameValuePair.bitwise_and(flag, value)).Equals(value)) hasflag = true; return hasflag; } public static List GetValues() { return value_pairs.Keys.ToList(); } public static List GetNames() { return name_pairs.Keys.ToList(); } public string Name { get { return CurrentValue.Name; } } public TValue Value { get { return CurrentValue.Value; } } } //[Flags] public class loggerflags : loggerflagsbase { protected loggerflags() :base() { } //protected loggerflags(ushort t) : this() { CurrentValue = new EnumNameValuePair< ushort>("", t); } protected loggerflags(EnumNameValuePair t) : base(t) { } protected loggerflags(loggerflagsbase t) : base(t) { } #region implicit operators public static implicit operator loggerflags(EnumNameValuePair t) { return new loggerflags(t); } public static implicit operator EnumNameValuePair(loggerflags t) { return new loggerflags(t.Value); } public static implicit operator ushort(loggerflags t) { return t.Value; } public static implicit operator loggerflags(ushort t) { return new loggerflags(t); } public static implicit operator string(loggerflags t) { return t.Name; } #endregion } public static class logger { private static loggerflags logging_flags; static logger() { SetLoggingFlags(loggerflags.DEFAULT); } public static void SetLoggingFlags(loggerflags flags) { logging_flags = flags; } public static loggerflags GetLoggingFlags() { return logging_flags; } #region sub-classes private static string CreateTimeStamp() { string timestamp = ""; DateTime now = DateTime.Now; timestamp = now.ToString("yyyy/MM/dd HH:mm:ss tt: "); return timestamp; } public static class Info { private static string CreateNewFormat(string format) { return " " + CreateTimeStamp()+ "(INFO) " + format; } public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.INFO)) { xlogger.Write(CreateNewFormat(format), args); } } public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.INFO)) { xlogger.WriteLine(CreateNewFormat(format), args); } } } public static class Warn { private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(WARN) " + format; } public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.WARN)) { xlogger.Write(CreateNewFormat(format), args); } } public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.WARN)) { xlogger.WriteLine(CreateNewFormat(format), args); } } } public static class Debug { private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(DEBUG) " + format; } public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.DEBUG)) { xlogger.Write(CreateNewFormat(format), args); } } public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.DEBUG)) { xlogger.WriteLine(CreateNewFormat(format), args); } } } public static class VerboseDebug { private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(VERBOSE DEBUG) " + format; } public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.VERBOSE_DEBUG)) { xlogger.Write(CreateNewFormat(format), args); } } public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.VERBOSE_DEBUG)) { xlogger.WriteLine(CreateNewFormat(format), args); } } } public static class Error { private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(ERROR) " + format; } public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.ERROR)) { xlogger.Write(CreateNewFormat(format), args); } } public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.ERROR)) { xlogger.WriteLine(CreateNewFormat(format), args); } } } public static class VerboseError { private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(VERBOSE ERROR) " + format; } public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.VERBOSE_ERROR)) { xlogger.Write(CreateNewFormat(format), args); } } public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.VERBOSE_ERROR)) { xlogger.WriteLine(CreateNewFormat(format), args); } } } #region Force logging public static class ForceLog { public static class Info { private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(FORCED INFO) " + format; } public static void Write(string format, params object[] args) { xlogger.Write(CreateNewFormat(format), args); } public static void WriteLine(string format, params object[] args) { xlogger.WriteLine(CreateNewFormat(format), args); } } public static class Warn { private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(FORCED Warn) " + format; } public static void Write(string format, params object[] args) { xlogger.Write(CreateNewFormat(format), args); } public static void WriteLine(string format, params object[] args) { xlogger.WriteLine(CreateNewFormat(format), args); } } public static class Debug { private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(FORCED DEBUG) " + format; } public static void Write(string format, params object[] args) { xlogger.Write(CreateNewFormat(format), args); } public static void WriteLine(string format, params object[] args) { xlogger.WriteLine(CreateNewFormat(format), args); } } public static class Error { private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(FORCED ERROR) " + format; } public static void Write(string format, params object[] args) { xlogger.Write(CreateNewFormat(format), args); } public static void WriteLine(string format, params object[] args) { xlogger.WriteLine(CreateNewFormat(format), args); } } } #endregion #endregion } #endregion #region internal static class Logger internal static class xlogger { private static logwriter lh; static xlogger() { 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); lh = new logwriter(lw); break; } catch (Exception) { throw; } } } } } } #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 }