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

Diff of /trunk/RomCheater.Logging/logger.cs

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

--- trunk/RomCheater.Logging/logger.cs	2012/05/09 22:37:17	95
+++ trunk/RomCheater.Logging/logger.cs	2013/06/17 08:58:58	685
@@ -1,79 +1,360 @@
-´╗┐using System;
+´╗┐#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;
 
 namespace RomCheater.Logging
 {
-    public interface ILogger
+    //public interface ILogger
+    //{
+    //    void Write(string format, params object[] args);
+    //    void WriteLine(string format, params object[] args);
+    //}
+
+
+    #region loggerflags
+    public class loggerflagsbase<TValue>
+        where TValue : IConvertible
     {
-        void Write(string format, params object[] args);
-        void WriteLine(string format, params object[] args);
-    }
+        static loggerflagsbase()
+        {
+            value_pairs = new Dictionary<TValue, EnumNameValuePair<TValue>>();
+            name_pairs = new Dictionary<string, EnumNameValuePair<TValue>>();
+
+            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<TValue> t) { this.CurrentValue = t.CurrentValue; }
+        private static void init_dicts()
+        {
+            value_pairs = new Dictionary<TValue, EnumNameValuePair<TValue>>();
+            name_pairs = new Dictionary<string, EnumNameValuePair<TValue>>();
+            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<TValue> value)
+        {
+            try
+            {
+                value_pairs.Add(value, value);
+                name_pairs.Add(value, value);
+            }
+            catch { }
+        }
 
-    #region MesageLogger
-    [Flags]
-    public enum loggerflags
+        #region implicit operators
+        public static implicit operator loggerflagsbase<TValue>(EnumNameValuePair<TValue> t) { return new loggerflagsbase<TValue>(t); }
+        public static implicit operator EnumNameValuePair<TValue>(loggerflagsbase<TValue> t) { return new loggerflagsbase<TValue>(t); }
+        public static implicit operator loggerflagsbase<TValue>(TValue t)
+        {
+            //foreach (KeyValuePair<TValue, EnumNameValuePair<TValue>> pair in value_pairs) { EnumNameValuePair<TValue> l = pair.Value; if (l.Value.Equals(t)) { return new loggerflagsbase<TValue>(l); } }
+            //return loggerflagsbase<TValue>.NONE;
+            List<string> pairs = new List<string>();
+            StringBuilder builder = new StringBuilder();
+            foreach (KeyValuePair<TValue, EnumNameValuePair<TValue>> pair in value_pairs)
+            {
+                EnumNameValuePair<TValue> 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<TValue> rVal = new loggerflagsbase<TValue>(new EnumNameValuePair<TValue>(builder.ToString(), t));
+                return rVal;
+            }
+        }
+        public static implicit operator TValue(loggerflagsbase<TValue> t) { return t.CurrentValue.Value; }
+        public static implicit operator string(loggerflagsbase<TValue> t) { return t.CurrentValue.Name; }
+
+        #region operator overloads
+        public static bool operator ==(loggerflagsbase<TValue> x, loggerflagsbase<TValue> y) { return x.CurrentValue == y.CurrentValue; }
+        public static bool operator !=(loggerflagsbase<TValue> x, loggerflagsbase<TValue> 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<TValue> NONE = new EnumNameValuePair<TValue>("NONE", Binary<TValue>.ToValue("00000"));
+        public static EnumNameValuePair<TValue> INFO = new EnumNameValuePair<TValue>("INFO", Binary<TValue>.ToValue("00001"));
+        public static EnumNameValuePair<TValue> DEBUG = new EnumNameValuePair<TValue>("DEBUG", Binary<TValue>.ToValue("00010"));
+        public static EnumNameValuePair<TValue> ERROR = new EnumNameValuePair<TValue>("ERROR", Binary<TValue>.ToValue("00100"));
+        public static EnumNameValuePair<TValue> VERBOSE_DEBUG = new EnumNameValuePair<TValue>("VERBOSE_DEBUG", Binary<TValue>.ToValue("01000"));
+        public static EnumNameValuePair<TValue> VERBOSE_ERROR = new EnumNameValuePair<TValue>("VERBOSE_ERROR", Binary<TValue>.ToValue("10000"));
+        public static EnumNameValuePair<TValue> WARN = new EnumNameValuePair<TValue>("WARN", Binary<TValue>.ToValue("100000"));
+        public static EnumNameValuePair<TValue> PROFILER = new EnumNameValuePair<TValue>("PROFILER", Binary<TValue>.ToValue("1000000"));
+        public static EnumNameValuePair<TValue> DEFAULT = new EnumNameValuePair<TValue>("DEFAULT", EnumNameValuePair<TValue>.bitwise_or(INFO, DEBUG, ERROR, WARN));
+        public static EnumNameValuePair<TValue> ALL = new EnumNameValuePair<TValue>("ALL", EnumNameValuePair<TValue>.bitwise_or(DEFAULT, VERBOSE_DEBUG, VERBOSE_ERROR, PROFILER));
+        #endregion
+
+        protected static Dictionary<TValue, EnumNameValuePair<TValue>> value_pairs;
+        protected static Dictionary<string, EnumNameValuePair<TValue>> name_pairs;
+        private EnumNameValuePair<TValue> CurrentValue { get; set; }
+
+        public bool HasFlag(TValue flag)
+        {
+            bool hasflag = false;
+            TValue value = this.CurrentValue;
+            if ((EnumNameValuePair<TValue>.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<TValue>.bitwise_and(flag, value)).Equals(flag))
+                hasflag = true;
+            return hasflag;
+        }
+        public static List<TValue> GetValues() { return value_pairs.Keys.ToList(); }
+        public static List<string> 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<ushort>
     {
-        NONE = 0x0000,              // 000
-        INFO = 0x0001,              // 001
-        DEBUG = 0x0002,             // 010
-        ERROR= 0x0004,              // 100
-        DEFAULT = INFO | ERROR | DEBUG,
-        ALL = DEFAULT,
+        protected loggerflags() : base() { }
+        protected loggerflags(EnumNameValuePair<ushort> t) : base(t) { }
+        protected loggerflags(loggerflagsbase<ushort> t) : base(t) { }
+        #region implicit operators
+        public static implicit operator loggerflags(EnumNameValuePair<ushort> t) { return new loggerflags(t); }
+        public static implicit operator EnumNameValuePair<ushort>(loggerflags t) { return new EnumNameValuePair<ushort>(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
     public 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<MethodBase> methods = new List<MethodBase>();
+            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 timestamp;
+            return string.Format("{0}", timestamp);
         }
         public static class Info
         {
-            private static string CreateNewFormat(string format) { return "    " + CreateTimeStamp()+ "(INFO) " + format; }
+            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) " + format; }
+            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) " + format; }
+            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() + "(INFO) " + format; }
+                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() + "(DEBUG) " + format; }
+                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() + "(ERROR) " + format; }
+                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); }
             }
@@ -91,11 +372,13 @@
         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();
@@ -130,7 +413,7 @@
                                 throw;
                             }
                         }
-                    }                    
+                    }
                 }
             }
         }
@@ -139,7 +422,7 @@
     #endregion
 
     #region internal class LogHelper : ILogger
-    internal class logwriter : ILogger
+    internal class logwriter //: ILogger
     {
         private LogWriter writer;
         public logwriter() : this(null) { }
@@ -148,29 +431,43 @@
             this.writer = writer;
         }
         #region ILogger Members
+         [Conditional("LOGGING_ENABLED")]
         public void Write(string format, params object[] args)
         {
-            WriteToLog(format,false, args);
+            WriteToLog(format, false, args);
         }
+         [Conditional("LOGGING_ENABLED")]
         public void WriteLine(string format, params object[] args)
         {
-            WriteToLog(format,true, 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)
                 {
-                    writer.Log.WriteLine(format, args);
+                    if (args.Length == 0)
+                    {
+                        tw.WriteLine(format.Replace("{", "{{").Replace("}", "}}"), args);
+                        return;
+                    }
+                    tw.WriteLine(format, args);
                 }
                 else
                 {
-                    writer.Log.Write(format, args);
+                    if (args.Length == 0)
+                    {
+                        tw.Write(format.Replace("{", "{{").Replace("}", "}}"), args);
+                        return;
+                    }
+                    tw.Write(format, args);
                 }
             }
+            tw.Flush();
         }
         #endregion
     }
-#endregion
+    #endregion
 }

 

  ViewVC Help
Powered by ViewVC 1.1.22