public static void RegisterLogger(string format, string linebreakIndent, CallbackActionDelegate callback)
 {
     try
     {
         var validator = ParseAndValidate(format);
         RegisterLoggerUnsafe(validator, linebreakIndent, callback);
     }
     catch (ArgumentException) { throw; }
 }
Example #2
0
 public static void RegisterLogger(string format, string linebreakIndent, CallbackActionDelegate callback, Level logLevel)
 {
     try
     {
         var tokenList = ParseAndValidate(format);
         RegisterLoggerUnsafe(tokenList, linebreakIndent, callback, logLevel);
     }
     catch (ArgumentException) { throw; }
 }
Example #3
0
            public LogAction(string formatString, CallbackActionDelegate callback, Level logLevel,
                             bool usesLLS, bool usesETF, bool usesDF, bool usesSTF)
            {
                FormatString = formatString;
                Callback     = callback;
                LogLevel     = logLevel;

                UsesLogLevelSpaced      = usesLLS;
                UsesErrorTextFormatted  = usesETF;
                UsesDateFormatted       = usesDF;
                UsesStackTraceFormatted = usesSTF;
            }
Example #4
0
            public LogAction(string formatString, int formatIndent,
                             CallbackActionDelegate callback, Level logLevel,
                             bool usesLls, bool usesEtf, bool usesDf, bool usesStf)
            {
                FormatString = formatString;
                FormatIndent = formatIndent;
                Callback     = callback;
                LogLevel     = logLevel;

                UsesLogLevelSpaced      = usesLls;
                UsesErrorTextFormatted  = usesEtf;
                UsesDateFormatted       = usesDf;
                UsesStackTraceFormatted = usesStf;
            }
        private static void RegisterLoggerUnsafe(List <ParseToken> validator, string linebreakIndent, CallbackActionDelegate callback)
        {
            DynamicMethod dynLog    = new DynamicMethod("LogWrite" + callbackCount, typeof(void), new[] { typeof(LogHelper) }, typeof(Log), true);
            var           ilGen     = dynLog.GetILGenerator();
            var           localStrb = ilGen.DeclareLocal(typeof(StringBuilder));

            // common type arrays
            Type[] argsString = { typeof(string) };
            Type[] argsInt    = { typeof(int) };

            // common InfosTypes
            MethodInfo miStringBuilder_Append_String = typeof(StringBuilder).GetMethod(nameof(StringBuilder.Append), argsString);
            MethodInfo miLog_GenLogLevelSpaced       = typeof(LogHelper).GetMethod(nameof(LogHelper.GenLogLevelSpaced));
            MethodInfo miLog_GenDateFormatted        = typeof(LogHelper).GetMethod(nameof(LogHelper.GenDateFormatted));
            MethodInfo miLog_GenErrorTextFormatted   = typeof(LogHelper).GetMethod(nameof(LogHelper.GenErrorTextFormatted), argsInt);
            MethodInfo miLog_GenStackTraceFormatted  = typeof(LogHelper).GetMethod(nameof(LogHelper.GenStackTraceFormatted), argsInt);

            // prepare callback invoke
            ilGen.Emit(OpCodes.Ldsfld, typeof(Log).GetField(nameof(callbackAction), BindingFlags.NonPublic | BindingFlags.Static));
            ilGen.Emit(OpCodes.Ldc_I4, callbackCount);
            ilGen.Emit(OpCodes.Ldelem_Ref);

            // Load stringbuilder
            ilGen.Emit(OpCodes.Newobj, typeof(StringBuilder).GetConstructor(Type.EmptyTypes));
            ilGen.Emit(OpCodes.Stloc, localStrb);
            ilGen.Emit(OpCodes.Ldloc, localStrb);

            foreach (var part in validator)
            {
                switch (part.TokenType)
                {
                case MethodBuildToken.Text:
                    ilGen.Emit(OpCodes.Ldstr, (string)part.Value);
                    break;

                case MethodBuildToken.LogLevelSpaced:
                    ilGen.Emit(OpCodes.Ldarg_0);
                    ilGen.EmitCall(OpCodes.Callvirt, miLog_GenLogLevelSpaced, null);
                    break;

                case MethodBuildToken.ErrorTextFormatted:
                    ilGen.Emit(OpCodes.Ldarg_0);
                    ilGen.Emit(OpCodes.Ldc_I4_0);
                    ilGen.EmitCall(OpCodes.Callvirt, miLog_GenErrorTextFormatted, null);
                    break;

                case MethodBuildToken.DateFormatted:
                    ilGen.Emit(OpCodes.Ldarg_0);
                    ilGen.EmitCall(OpCodes.Callvirt, miLog_GenDateFormatted, null);
                    break;

                case MethodBuildToken.StackFormatted:
                    ilGen.Emit(OpCodes.Ldarg_0);
                    ilGen.Emit(OpCodes.Ldc_I4_M1);
                    ilGen.EmitCall(OpCodes.Callvirt, miLog_GenStackTraceFormatted, null);
                    break;

                default:
                    throw new InvalidProgramException("Undefined MethodBuildToken occoured");
                }
                ilGen.EmitCall(OpCodes.Callvirt, miStringBuilder_Append_String, null);
            }

            // call ToString and the callback method
            ilGen.EmitCall(OpCodes.Callvirt, typeof(StringBuilder).GetMethod(nameof(StringBuilder.ToString), Type.EmptyTypes), null);
            ilGen.EmitCall(OpCodes.Callvirt, typeof(CallbackActionDelegate).GetMethod(nameof(CallbackActionDelegate.Invoke), argsString), null);

            ilGen.Emit(OpCodes.Ret);

            lock (writeLock)
            {
                // Redim the calllist array
                if (callbackProcessor == null)
                {
                    callbackProcessor = new CallbackProcessorDelegate[1];
                    callbackAction    = new CallbackActionDelegate[1];
                }
                else
                {
                    CallbackProcessorDelegate[] tempProcessorArray = new CallbackProcessorDelegate[callbackCount + 1];
                    CallbackActionDelegate[]    tempActionArray    = new CallbackActionDelegate[callbackCount + 1];
                    Array.Copy(callbackProcessor, tempProcessorArray, callbackCount);
                    Array.Copy(callbackAction, tempActionArray, callbackCount);
                    callbackProcessor = tempProcessorArray;
                    callbackAction    = tempActionArray;
                }

                //Store event call in the calllist
                callbackProcessor[callbackCount] = (CallbackProcessorDelegate)dynLog.CreateDelegate(typeof(CallbackProcessorDelegate));
                callbackAction[callbackCount]    = callback;

                callbackCount++;
            }
        }
Example #6
0
 public CallbackAction(string name, string desc, Type[] argtypes, CallbackActionDelegate func)
     : base(name, desc, argtypes)
 {
     this.cbk = func;
 }
Example #7
0
        public static void RegisterLogger(string format, int linebreakIndent, CallbackActionDelegate callback, Level logLevel)
        {
            var tokenList = ParseAndValidate(format);

            RegisterLogger(tokenList, linebreakIndent, callback, logLevel);
        }
Example #8
0
 public static void RegisterLogger(string format, int linebreakIndent, CallbackActionDelegate callback)
 => RegisterLogger(format, linebreakIndent, callback, 0);
Example #9
0
        private static void RegisterLogger(List <ParseToken> tokenList, int linebreakIndent, CallbackActionDelegate callback, Level logLevel)
        {
            bool usesLls = false, usesEtf = false, usesDf = false, usesStf = false;
            var  formatString = new StringBuilder();

            foreach (var part in tokenList)
            {
                switch (part.TokenType)
                {
                case MethodBuildToken.Text:
                    formatString.Append(part.Value);
                    break;

                case MethodBuildToken.LogLevelSpaced:
                    formatString.Append(PlaceLogLevelSpaced);
                    usesLls = true;
                    break;

                case MethodBuildToken.ErrorTextFormatted:
                    formatString.Append(PlaceErrorTextFormatted);
                    usesEtf = true;
                    break;

                case MethodBuildToken.DateFormatted:
                    formatString.Append(PlaceDateFormatted);
                    usesDf = true;
                    break;

                case MethodBuildToken.StackFormatted:
                    formatString.Append(PlaceStackTraceFormatted);
                    usesStf = true;
                    break;

                default:
                    throw Util.UnhandledDefault(part.TokenType);
                }
            }

            lock (WriteLock)
            {
                LoggerFormats.Add(new LogAction(
                                      formatString.ToString(),
                                      linebreakIndent,
                                      callback,
                                      logLevel,
                                      usesLls, usesEtf, usesDf, usesStf));

                if (formatCache.Length <= linebreakIndent)
                {
                    formatCache = new string[linebreakIndent + 1];
                }
            }
        }
Example #10
0
        private static void RegisterLoggerUnsafe(List <ParseToken> tokenList, string linebreakIndent, CallbackActionDelegate callback, Level logLevel)
        {
            bool usesLLS = false, usesETF = false, usesDF = false, usesSTF = false;
            var  formatString = new StringBuilder();

            foreach (var part in tokenList)
            {
                switch (part.TokenType)
                {
                case MethodBuildToken.Text:
                    formatString.Append(part.Value);
                    break;

                case MethodBuildToken.LogLevelSpaced:
                    formatString.Append(PlaceLogLevelSpaced);
                    usesLLS = true;
                    break;

                case MethodBuildToken.ErrorTextFormatted:
                    formatString.Append(PlaceErrorTextFormatted);
                    usesETF = true;
                    break;

                case MethodBuildToken.DateFormatted:
                    formatString.Append(PlaceDateFormatted);
                    usesDF = true;
                    break;

                case MethodBuildToken.StackFormatted:
                    formatString.Append(PlaceStackTraceFormatted);
                    usesSTF = true;
                    break;

                default:
                    throw new InvalidProgramException("Undefined MethodBuildToken occoured");
                }
            }

            lock (writeLock)
            {
                loggerFormats.Add(new LogAction(
                                      formatString.ToString(),
                                      callback,
                                      logLevel,
                                      usesLLS, usesETF, usesDF, usesSTF));
            }
        }
Example #11
0
        private static void RegisterLoggerUnsafe(List<ParseToken> validator, string linebreakIndent, CallbackActionDelegate callback)
        {
            DynamicMethod dynLog = new DynamicMethod("LogWrite" + callbackCount, typeof(void), new[] { typeof(LogHelper) }, typeof(Log), true);
            var ilGen = dynLog.GetILGenerator();
            var localStrb = ilGen.DeclareLocal(typeof(StringBuilder));

            // common type arrays
            Type[] argsString = { typeof(string) };
            Type[] argsInt = { typeof(int) };

            // common InfosTypes
            MethodInfo miStringBuilder_Append_String = typeof(StringBuilder).GetMethod(nameof(StringBuilder.Append), argsString);
            MethodInfo miLog_GenLogLevelSpaced = typeof(LogHelper).GetMethod(nameof(LogHelper.GenLogLevelSpaced));
            MethodInfo miLog_GenDateFormatted = typeof(LogHelper).GetMethod(nameof(LogHelper.GenDateFormatted));
            MethodInfo miLog_GenErrorTextFormatted = typeof(LogHelper).GetMethod(nameof(LogHelper.GenErrorTextFormatted), argsInt);
            MethodInfo miLog_GenStackTraceFormatted = typeof(LogHelper).GetMethod(nameof(LogHelper.GenStackTraceFormatted), argsInt);

            // prepare callback invoke
            ilGen.Emit(OpCodes.Ldsfld, typeof(Log).GetField(nameof(callbackAction), BindingFlags.NonPublic | BindingFlags.Static));
            ilGen.Emit(OpCodes.Ldc_I4, callbackCount);
            ilGen.Emit(OpCodes.Ldelem_Ref);

            // Load stringbuilder
            ilGen.Emit(OpCodes.Newobj, typeof(StringBuilder).GetConstructor(Type.EmptyTypes));
            ilGen.Emit(OpCodes.Stloc, localStrb);
            ilGen.Emit(OpCodes.Ldloc, localStrb);

            foreach (var part in validator)
            {
                switch (part.TokenType)
                {
                case MethodBuildToken.Text:
                    ilGen.Emit(OpCodes.Ldstr, (string)part.Value);
                    break;
                case MethodBuildToken.LogLevelSpaced:
                    ilGen.Emit(OpCodes.Ldarg_0);
                    ilGen.EmitCall(OpCodes.Callvirt, miLog_GenLogLevelSpaced, null);
                    break;
                case MethodBuildToken.ErrorTextFormatted:
                    ilGen.Emit(OpCodes.Ldarg_0);
                    ilGen.Emit(OpCodes.Ldc_I4_0);
                    ilGen.EmitCall(OpCodes.Callvirt, miLog_GenErrorTextFormatted, null);
                    break;
                case MethodBuildToken.DateFormatted:
                    ilGen.Emit(OpCodes.Ldarg_0);
                    ilGen.EmitCall(OpCodes.Callvirt, miLog_GenDateFormatted, null);
                    break;
                case MethodBuildToken.StackFormatted:
                    ilGen.Emit(OpCodes.Ldarg_0);
                    ilGen.Emit(OpCodes.Ldc_I4_M1);
                    ilGen.EmitCall(OpCodes.Callvirt, miLog_GenStackTraceFormatted, null);
                    break;
                default:
                    throw new InvalidProgramException("Undefined MethodBuildToken occoured");
                }
                ilGen.EmitCall(OpCodes.Callvirt, miStringBuilder_Append_String, null);
            }

            // call ToString and the callback method
            ilGen.EmitCall(OpCodes.Callvirt, typeof(StringBuilder).GetMethod(nameof(StringBuilder.ToString), Type.EmptyTypes), null);
            ilGen.EmitCall(OpCodes.Callvirt, typeof(CallbackActionDelegate).GetMethod(nameof(CallbackActionDelegate.Invoke), argsString), null);

            ilGen.Emit(OpCodes.Ret);

            lock (writeLock)
            {
                // Redim the calllist array
                if (callbackProcessor == null)
                {
                    callbackProcessor = new CallbackProcessorDelegate[1];
                    callbackAction = new CallbackActionDelegate[1];
                }
                else
                {
                    CallbackProcessorDelegate[] tempProcessorArray = new CallbackProcessorDelegate[callbackCount + 1];
                    CallbackActionDelegate[] tempActionArray = new CallbackActionDelegate[callbackCount + 1];
                    Array.Copy(callbackProcessor, tempProcessorArray, callbackCount);
                    Array.Copy(callbackAction, tempActionArray, callbackCount);
                    callbackProcessor = tempProcessorArray;
                    callbackAction = tempActionArray;
                }

                //Store event call in the calllist
                callbackProcessor[callbackCount] = (CallbackProcessorDelegate)dynLog.CreateDelegate(typeof(CallbackProcessorDelegate));
                callbackAction[callbackCount] = callback;

                callbackCount++;
            }
        }
Example #12
0
 public static void RegisterLogger(string format, string linebreakIndent, CallbackActionDelegate callback)
 {
     try
     {
         var validator = ParseAndValidate(format);
         RegisterLoggerUnsafe(validator, linebreakIndent, callback);
     }
     catch (ArgumentException) { throw; }
 }