#region Logging Defines // include this any class or method that required logging, and comment-out what is not needed #define LOGGING_ENABLED // this is only valid within the logger class #region Enabled logging levels #define LOGGING_ENABLE_INFO #define LOGGING_ENABLE_WARN #define LOGGING_ENABLE_DEBUG #define LOGGING_ENABLE_VERBOSEDEBUG #define LOGGING_ENABLE_ERROR #define LOGGING_ENABLE_VERBOSEERROR #define LOGGING_ENABLE_PROFILER #endregion #endregion using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; using System.Windows.Forms; using System.IO; using System.Diagnostics; using Enterprise.Logging; namespace RomCheater.Logging { //public interface ILogger //{ // void Write(string format, params object[] args); // void WriteLine(string format, params object[] args); //} #region loggerflags //[Obsolete("logging is being replaced by Enterprise.Logging.gLog")] internal 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 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(WARN); add_dict_entry(PROFILER); 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; List pairs = new List(); StringBuilder builder = new StringBuilder(); foreach (KeyValuePair> pair in value_pairs) { EnumNameValuePair enum_pair = pair.Value; if (HasFlag(pair.Value, t) && enum_pair != NONE) { pairs.Add(enum_pair); } } if (pairs.Count == 0) { return NONE; } else { int count = 0; foreach (string pair in pairs) { if (count == 0) { builder.AppendLine(pair); } else { builder.AppendFormat(" | {0}", pair); } count++; } loggerflagsbase rVal = new loggerflagsbase(new EnumNameValuePair(builder.ToString(), t)); return rVal; } } 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 PROFILER = new EnumNameValuePair("PROFILER", Binary.ToValue("1000000")); public static EnumNameValuePair DEFAULT = new EnumNameValuePair("DEFAULT", EnumNameValuePair.bitwise_or(INFO, ERROR, WARN)); public static EnumNameValuePair ALL = new EnumNameValuePair("ALL", EnumNameValuePair.bitwise_or(DEFAULT, DEBUG, VERBOSE_DEBUG, VERBOSE_ERROR, PROFILER)); #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(flag)) hasflag = true; return hasflag; } public static bool HasFlag(TValue flag, TValue compare) { bool hasflag = false; TValue value = compare; if ((EnumNameValuePair.bitwise_and(flag, value)).Equals(flag)) 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] //[Obsolete("logging is being replaced by Enterprise.Logging.gLog")] internal class loggerflags : loggerflagsbase { protected loggerflags() : base() { } 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 EnumNameValuePair(t.Name, 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 } #endregion #region public static class logger [Obsolete("logging is being replaced by Enterprise.Logging.gLog")] internal static class logger { private static string GetLoggingClass() { const int methodindex = 4; // get this method from stacktrace string c = string.Empty; StackTrace stackTrace = new StackTrace(); var frames = stackTrace.GetFrames().ToList(); List methods = new List(); frames.ForEach(s => methods.Add(s.GetMethod())); var method = methods[methodindex]; if (method.DeclaringType != null) { c = method.DeclaringType.Name; bool GetNextMethod = true; if (method.DeclaringType.FullName.ToLower().StartsWith("system") || method.DeclaringType.FullName.ToLower().StartsWith("microsoft") || c.ToLower() == "pluginbase" || c.ToLower() == "inputplugin" || c.ToLower() == "windowplugin" || c.ToLower() == "configplugin" || c.ToLower() == "usercontrolplugin" ) { int NextMethodCount = methodindex; while (GetNextMethod) { if (NextMethodCount == -1) { throw new InvalidOperationException("Unable to find calling class"); } method = methods[NextMethodCount--]; // walk backup the stack if (method.DeclaringType != null) { c = method.DeclaringType.Name; if (method.DeclaringType.FullName.ToLower().StartsWith("system") || method.DeclaringType.FullName.ToLower().StartsWith("microsoft")) { GetNextMethod = true; } else if (c.ToLower() == "pluginbase" || c.ToLower() == "inputplugin" || c.ToLower() == "windowplugin" || c.ToLower() == "configplugin" || c.ToLower() == "usercontrolplugin") { // ignore our abstract plugin classes GetNextMethod = true; } else { GetNextMethod = false; } } } } else { } } return string.Format("({0})", c); } 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 string.Format("{0}", timestamp); } public static class Info { private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(INFO) " + GetLoggingClass() + " " + format; } //[Conditional("LOGGING_ENABLE_INFO")] public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.INFO)) { xlogger.Write(CreateNewFormat(format), args); } } //[Conditional("LOGGING_ENABLE_INFO")] 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) " + GetLoggingClass() + " " + format; } //[Conditional("LOGGING_ENABLE_WARN")] public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.WARN)) { xlogger.Write(CreateNewFormat(format), args); } } //[Conditional("LOGGING_ENABLE_WARN")] 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) " + GetLoggingClass() + " " + format; } //[Conditional("LOGGING_ENABLE_DEBUG")] public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.DEBUG)) { xlogger.Write(CreateNewFormat(format), args); } } //[Conditional("LOGGING_ENABLE_DEBUG")] 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) " + GetLoggingClass() + " " + format; } //[Conditional("LOGGING_ENABLE_VERBOSEDEBUG")] public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.VERBOSE_DEBUG)) { xlogger.Write(CreateNewFormat(format), args); } } //[Conditional("LOGGING_ENABLE_VERBOSEDEBUG")] 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) " + GetLoggingClass() + " " + format; } //[Conditional("LOGGING_ENABLE_ERROR")] public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.ERROR)) { xlogger.Write(CreateNewFormat(format), args); } } //[Conditional("LOGGING_ENABLE_ERROR")] 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) " + GetLoggingClass() + " " + format; } //[Conditional("LOGGING_ENABLE_VERBOSEERROR")] public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.VERBOSE_ERROR)) { xlogger.Write(CreateNewFormat(format), args); } } //[Conditional("LOGGING_ENABLE_VERBOSEERROR")] public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.VERBOSE_ERROR)) { xlogger.WriteLine(CreateNewFormat(format), args); } } } public static class Profiler { private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(PROFILER) " + GetLoggingClass() + " " + format; } //[Conditional("LOGGING_ENABLE_PROFILER")] public static void Write(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.PROFILER)) { xlogger.Write(CreateNewFormat(format), args); } } //[Conditional("LOGGING_ENABLE_PROFILER")] public static void WriteLine(string format, params object[] args) { if (logging_flags.HasFlag(loggerflags.PROFILER)) { 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) " + GetLoggingClass() + " " + 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) " + GetLoggingClass() + " " + 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) " + GetLoggingClass() + " " + 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) " + GetLoggingClass() + " " + 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 Profiler { private static string CreateNewFormat(string format) { return " " + CreateTimeStamp() + "(FORCED PROFILER) " + GetLoggingClass() + " " + 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 [Obsolete("logging is being replaced by Enterprise.Logging.gLog")] internal static class xlogger { private static logwriter lh; static xlogger() { lh = new logwriter(); } #region ILogger Members //[Conditional("LOGGING_ENABLED")] public static void Write(string format, params object[] args) { init(); lh.Write(format, args); } //[Conditional("LOGGING_ENABLED")] 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 [Obsolete("logging is being replaced by Enterprise.Logging.gLog")] internal class logwriter //: ILogger { private LogWriter writer; public logwriter() : this(null) { } public logwriter(LogWriter writer) { this.writer = writer; } #region ILogger Members //[Conditional("LOGGING_ENABLED")] public void Write(string format, params object[] args) { WriteToLog(format, false, args); } //[Conditional("LOGGING_ENABLED")] public void WriteLine(string format, params object[] args) { WriteToLog(format, true, args); } private void WriteToLog(string format, bool newline, params object[] args) { //TextWriter tw = TextWriter.Synchronized(writer.Log); //if (writer != null) //{ // if (newline) // { // if (args.Length == 0) // { // tw.WriteLine(format.Replace("{", "{{").Replace("}", "}}"), args); // return; // } // tw.WriteLine(format, args); // } // else // { // if (args.Length == 0) // { // tw.Write(format.Replace("{", "{{").Replace("}", "}}"), args); // return; // } // tw.Write(format, args); // } //} //tw.Flush(); } #endregion } #endregion }