Esempio n. 1
0
 public void Log(object obj, int reqLevel)
 {
     Console.Write("{0} {{ ", name);
     foreach (FieldInfo f in type.GetFields(LogOps.PUBLIC_INSTANCE))
     {
         if (LogOps.IsLogAllowed(f))
         {
             int defLevel = LogOps.GetLogLevel(f);
             if (defLevel <= reqLevel)
             {
                 Console.Write("{0}: {1}; ", f.Name, f.GetValue(obj));
             }
         }
     }
     Console.WriteLine('}');
 }
Esempio n. 2
0
        private static void ImplementLogMethod(MethodBuilder metBuilder, Type type)
        {
            ILGenerator  il   = metBuilder.GetILGenerator();
            LocalBuilder tobj = il.DeclareLocal(type);

            il.Emit(OpCodes.Ldarg_1);
            il.Emit(OpCodes.Castclass, type);
            il.Emit(OpCodes.Stloc, tobj);

            il.Emit(OpCodes.Ldstr, type.Name + " { ");
            il.Emit(OpCodes.Call, typeof(System.Console).GetMethod("Write", new Type[] { typeof(string) }));

            foreach (FieldInfo f in type.GetFields(LogOps.PUBLIC_INSTANCE))
            {
                if (LogOps.IsLogAllowed(f))
                {
                    int   defLevel = LogOps.GetLogLevel(f);
                    Label noLog    = il.DefineLabel();
                    if (defLevel > 0)
                    {
                        il.Emit(OpCodes.Ldc_I4, defLevel);
                        il.Emit(OpCodes.Ldarg_2);
                        il.Emit(OpCodes.Cgt);
                        il.Emit(OpCodes.Brtrue, noLog);
                    }
                    il.Emit(OpCodes.Ldstr, f.Name + ": {0}; ");
                    il.Emit(OpCodes.Ldloc, tobj);
                    il.Emit(OpCodes.Ldfld, f);
                    if (f.FieldType.IsValueType)
                    {
                        il.Emit(OpCodes.Box, f.FieldType);
                    }
                    il.Emit(OpCodes.Call, typeof(System.Console).GetMethod("Write", new Type[] { typeof(string), typeof(object) }));
                    il.MarkLabel(noLog);
                }
            }

            il.Emit(OpCodes.Ldc_I4_S, '}');
            il.Emit(OpCodes.Call, typeof(System.Console).GetMethod("WriteLine", new Type[] { typeof(char) }));

            il.Emit(OpCodes.Ret);
        }