/[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	2012/06/03 20:40:36	269
@@ -14,22 +14,171 @@
     }
 
 
-    #region MesageLogger
-    [Flags]
-    public enum loggerflags
+    #region loggerflags
+    public class loggerflagsbase<TValue> 
+        where TValue : IConvertible
     {
-        NONE = 0x0000,              // 000
-        INFO = 0x0001,              // 001
-        DEBUG = 0x0002,             // 010
-        ERROR= 0x0004,              // 100
-        DEFAULT = INFO | ERROR | DEBUG,
-        ALL = DEFAULT,
+         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 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> 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));
+        #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>
+    {
+          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
+
+    }
+    
     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()
         {
@@ -44,36 +193,60 @@
             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() + "(INFO) " + format; }
+                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() + "(DEBUG) " + format; }
+                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() + "(ERROR) " + format; }
+                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); }
             }

 

  ViewVC Help
Powered by ViewVC 1.1.22