Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Jul 5, 2024
1 parent 698b5d5 commit 9e834bc
Show file tree
Hide file tree
Showing 14 changed files with 254 additions and 179 deletions.
62 changes: 35 additions & 27 deletions Catel/Anotar.Catel.Fody/LogForwardingProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,38 +74,41 @@ void ProcessInstruction(Instruction instruction)
var messagePrefix = GetMessagePrefix(instruction);
if (parameters.Count == 0)
{
instructions.Replace(instruction,
new[]
{
instructions.Replace(
instruction,
[
Instruction.Create(OpCodes.Ldsfld, LoggerField),
Instruction.Create(OpCodes.Ldstr, messagePrefix),
Instruction.Create(OpCodes.Ldnull),
Instruction.Create(OpCodes.Ldc_I4, logEventLevel),
Instruction.Create(OpCodes.Callvirt, ModuleWeaver.WriteMethod)
});
]);
return;
}

if (methodReference.IsMatch("Exception", "String", "Object[]"))
{
if (messageVar == null)
{
messageVar = new(ModuleWeaver.TypeSystem.StringReference);
Method.Body.Variables.Add(messageVar);
}

if (exceptionVar == null)
{
exceptionVar = new(ModuleWeaver.ExceptionType);
Method.Body.Variables.Add(exceptionVar);
}

if (paramsVar == null)
{
paramsVar = new(ModuleWeaver.ObjectArray);
Method.Body.Variables.Add(paramsVar);
}

instructions.Replace(instruction,
new[]
{
instructions.Replace(
instruction,
[
Instruction.Create(OpCodes.Stloc, paramsVar),
Instruction.Create(OpCodes.Stloc, messageVar),
Instruction.Create(OpCodes.Stloc, exceptionVar),
Expand All @@ -118,27 +121,29 @@ void ProcessInstruction(Instruction instruction)
Instruction.Create(OpCodes.Call, ModuleWeaver.FormatMethod),
Instruction.Create(OpCodes.Ldnull),
Instruction.Create(OpCodes.Ldc_I4, logEventLevel),
ModuleWeaver.CreateLogExceptionCallInstruction(),
});
ModuleWeaver.CreateLogExceptionCallInstruction()
]);
return;
}

if (methodReference.IsMatch("Exception", "Func`1"))
{
if (funcVar == null)
{
funcVar = new(ModuleWeaver.GenericFunc);
Method.Body.Variables.Add(funcVar);
}

if (exceptionVar == null)
{
exceptionVar = new(ModuleWeaver.ExceptionType);
Method.Body.Variables.Add(exceptionVar);
}

var sectionNop = Instruction.Create(OpCodes.Nop);
instructions.Replace(instruction,
new[]
{
instructions.Replace(
instruction,
[
Instruction.Create(OpCodes.Stloc, funcVar),
Instruction.Create(OpCodes.Stloc, exceptionVar),
Instruction.Create(OpCodes.Call, isEnabledMethod),
Expand All @@ -153,25 +158,27 @@ void ProcessInstruction(Instruction instruction)
Instruction.Create(OpCodes.Ldc_I4, logEventLevel),
ModuleWeaver.CreateLogExceptionCallInstruction(),
sectionNop
});
]);
return;
}

if (methodReference.IsMatch("String", "Object[]"))
{
if (messageVar == null)
{
messageVar = new(ModuleWeaver.TypeSystem.StringReference);
Method.Body.Variables.Add(messageVar);
}

if (paramsVar == null)
{
paramsVar = new(ModuleWeaver.ObjectArray);
Method.Body.Variables.Add(paramsVar);
}

instructions.Replace(instruction,
new[]
{
instructions.Replace(
instruction,
[
Instruction.Create(OpCodes.Stloc, paramsVar),
Instruction.Create(OpCodes.Stloc, messageVar),
Instruction.Create(OpCodes.Ldsfld, LoggerField),
Expand All @@ -182,29 +189,30 @@ void ProcessInstruction(Instruction instruction)
Instruction.Create(OpCodes.Call, ModuleWeaver.FormatMethod),
Instruction.Create(OpCodes.Ldnull),
Instruction.Create(OpCodes.Ldc_I4, logEventLevel),
Instruction.Create(OpCodes.Callvirt, ModuleWeaver.WriteMethod),
});
Instruction.Create(OpCodes.Callvirt, ModuleWeaver.WriteMethod)
]);
return;
}

if (methodReference.IsMatch("String"))
{
if (messageVar == null)
{
messageVar = new(ModuleWeaver.TypeSystem.StringReference);
Method.Body.Variables.Add(messageVar);
}
instructions.Replace(instruction,
new[]
{
instructions.Replace(
instruction,
[
Instruction.Create(OpCodes.Stloc, messageVar),
Instruction.Create(OpCodes.Ldsfld, LoggerField),
Instruction.Create(OpCodes.Ldstr, messagePrefix),
Instruction.Create(OpCodes.Ldloc, messageVar),
Instruction.Create(OpCodes.Call, ModuleWeaver.ConcatMethod),
Instruction.Create(OpCodes.Ldnull),
Instruction.Create(OpCodes.Ldc_I4, logEventLevel),
Instruction.Create(OpCodes.Callvirt, ModuleWeaver.WriteMethod),
});
Instruction.Create(OpCodes.Callvirt, ModuleWeaver.WriteMethod)
]);
return;
}
if (methodReference.IsMatch("Func`1"))
Expand All @@ -216,9 +224,9 @@ void ProcessInstruction(Instruction instruction)
}

var sectionNop = Instruction.Create(OpCodes.Nop);
instructions.Replace(instruction,
new[]
{
instructions.Replace(
instruction,
[
Instruction.Create(OpCodes.Stloc, funcVar),
Instruction.Create(OpCodes.Call, isEnabledMethod),
Instruction.Create(OpCodes.Brfalse_S, sectionNop),
Expand All @@ -231,7 +239,7 @@ void ProcessInstruction(Instruction instruction)
Instruction.Create(OpCodes.Ldc_I4, logEventLevel),
Instruction.Create(OpCodes.Callvirt, ModuleWeaver.WriteMethod),
sectionNop
});
]);
return;
}
throw new NotImplementedException();
Expand Down
2 changes: 1 addition & 1 deletion Catel/Tests/CatelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ static CatelTests()
var moduleWeaver = new ModuleWeaver();
assembly = moduleWeaver.ExecuteTestRun(
assemblyPath: "AssemblyToProcess.dll",
ignoreCodes: new[] { "0x80131869" }).Assembly;
ignoreCodes: ["0x80131869"]).Assembly;

LogManager.AddListener(new LogListener
{
Expand Down
62 changes: 31 additions & 31 deletions CommonLogging/Anotar.CommonLogging.Fody/LogForwardingProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,28 +67,28 @@ void ProcessInstruction(Instruction instruction)

if (parameters.Count == 0 && methodReference.Name.StartsWith("get_Is"))
{
instructions.Replace(instruction,
new[]
{
instructions.Replace(
instruction,
[
Instruction.Create(OpCodes.Ldsfld, LoggerField),
Instruction.Create(OpCodes.Callvirt, ModuleWeaver.GetLogEnabled(methodReference))
});
]);
return;
}

var isEnabledMethod = ModuleWeaver.GetLogEnabledForLog(methodReference);
var messagePrefix = GetMessagePrefix(instruction);
if (parameters.Count == 0)
{
instructions.Replace(instruction,
new[]
{
instructions.Replace(
instruction,
[
Instruction.Create(OpCodes.Ldsfld, LoggerField),
Instruction.Create(OpCodes.Ldstr, messagePrefix),
Instruction.Create(OpCodes.Ldc_I4_0),
Instruction.Create(OpCodes.Newarr, ModuleWeaver.TypeSystem.ObjectReference),
Instruction.Create(OpCodes.Callvirt, ModuleWeaver.GetFormatOperand(methodReference))
});
]);
return;
}

Expand All @@ -113,9 +113,9 @@ void ProcessInstruction(Instruction instruction)
Method.Body.Variables.Add(paramsVar);
}

instructions.Replace(instruction,
new[]
{
instructions.Replace(
instruction,
[
Instruction.Create(OpCodes.Stloc, paramsVar),
Instruction.Create(OpCodes.Stloc, exceptionVar),
Instruction.Create(OpCodes.Stloc, messageVar),
Expand All @@ -125,8 +125,8 @@ void ProcessInstruction(Instruction instruction)
Instruction.Create(OpCodes.Call, ModuleWeaver.ConcatMethod),
Instruction.Create(OpCodes.Ldloc, exceptionVar),
Instruction.Create(OpCodes.Ldloc, paramsVar),
Instruction.Create(OpCodes.Callvirt, ModuleWeaver.GetExceptionOperand(methodReference)),
});
Instruction.Create(OpCodes.Callvirt, ModuleWeaver.GetExceptionOperand(methodReference))
]);
return;
}

Expand All @@ -145,9 +145,9 @@ void ProcessInstruction(Instruction instruction)
}

var sectionNop = Instruction.Create(OpCodes.Nop);
instructions.Replace(instruction,
new[]
{
instructions.Replace(
instruction,
[
Instruction.Create(OpCodes.Stloc, exceptionVar),
Instruction.Create(OpCodes.Stloc, funcVar),
Instruction.Create(OpCodes.Ldsfld, LoggerField),
Expand All @@ -162,7 +162,7 @@ void ProcessInstruction(Instruction instruction)
Instruction.Create(OpCodes.Ldnull),
Instruction.Create(OpCodes.Callvirt, ModuleWeaver.GetExceptionOperand(methodReference)),
sectionNop
});
]);
return;
}

Expand All @@ -174,16 +174,16 @@ void ProcessInstruction(Instruction instruction)
Method.Body.Variables.Add(messageVar);
}

instructions.Replace(instruction,
new[]
{
instructions.Replace(
instruction,
[
Instruction.Create(OpCodes.Stloc, messageVar),
Instruction.Create(OpCodes.Ldsfld, LoggerField),
Instruction.Create(OpCodes.Ldstr, messagePrefix),
Instruction.Create(OpCodes.Ldloc, messageVar),
Instruction.Create(OpCodes.Call, ModuleWeaver.ConcatMethod),
Instruction.Create(OpCodes.Callvirt, ModuleWeaver.GetNormalOperand(methodReference)),
});
Instruction.Create(OpCodes.Callvirt, ModuleWeaver.GetNormalOperand(methodReference))
]);
return;
}

Expand All @@ -201,18 +201,18 @@ void ProcessInstruction(Instruction instruction)
Method.Body.Variables.Add(paramsVar);
}

instructions.Replace(instruction,
new[]
{
instructions.Replace(
instruction,
[
Instruction.Create(OpCodes.Stloc, paramsVar),
Instruction.Create(OpCodes.Stloc, messageVar),
Instruction.Create(OpCodes.Ldsfld, LoggerField),
Instruction.Create(OpCodes.Ldstr, messagePrefix),
Instruction.Create(OpCodes.Ldloc, messageVar),
Instruction.Create(OpCodes.Call, ModuleWeaver.ConcatMethod),
Instruction.Create(OpCodes.Ldloc, paramsVar),
Instruction.Create(OpCodes.Callvirt, ModuleWeaver.GetFormatOperand(methodReference)),
});
Instruction.Create(OpCodes.Callvirt, ModuleWeaver.GetFormatOperand(methodReference))
]);
return;
}

Expand All @@ -225,9 +225,9 @@ void ProcessInstruction(Instruction instruction)
}

var sectionNop = Instruction.Create(OpCodes.Nop);
instructions.Replace(instruction,
new[]
{
instructions.Replace(
instruction,
[
Instruction.Create(OpCodes.Stloc, funcVar),
Instruction.Create(OpCodes.Ldsfld, LoggerField),
Instruction.Create(OpCodes.Callvirt, isEnabledMethod),
Expand All @@ -240,7 +240,7 @@ void ProcessInstruction(Instruction instruction)
Instruction.Create(OpCodes.Ldnull),
Instruction.Create(OpCodes.Callvirt, ModuleWeaver.GetFormatOperand(methodReference)),
sectionNop
});
]);
return;
}

Expand Down
12 changes: 6 additions & 6 deletions CommonLogging/Tests/ActionLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void Trace(object message)
actionAdapter.Traces.Add(new()
{
Format = message.ToString(),
Args = new object[] { }
Args = []
});
}

Expand Down Expand Up @@ -78,7 +78,7 @@ public void Debug(object message)
actionAdapter.Debugs.Add(new()
{
Format = message.ToString(),
Args = new object[] { }
Args = []
});
}

Expand Down Expand Up @@ -141,7 +141,7 @@ public void Info(object message)
actionAdapter.Informations.Add(new()
{
Format = message.ToString(),
Args = new object[] {}
Args = []
});
}

Expand Down Expand Up @@ -204,7 +204,7 @@ public void Warn(object message)
actionAdapter.Warnings.Add(new()
{
Format = message.ToString(),
Args = new object[] { }
Args = []
});
}

Expand Down Expand Up @@ -267,7 +267,7 @@ public void Error(object message)
actionAdapter.Errors.Add(new()
{
Format = message.ToString(),
Args = new object[] { }
Args = []
});
}

Expand Down Expand Up @@ -330,7 +330,7 @@ public void Fatal(object message)
actionAdapter.Fatals.Add(new()
{
Format = message.ToString(),
Args = new object[] { }
Args = []
});
}

Expand Down
2 changes: 1 addition & 1 deletion CommonLogging/Tests/CommonLoggingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ static CommonLoggingTests()
var moduleWeaver = new ModuleWeaver();
assembly = moduleWeaver.ExecuteTestRun(
assemblyPath: "AssemblyToProcess.dll",
ignoreCodes: new[] { "0x80131869" }).Assembly;
ignoreCodes: ["0x80131869"]).Assembly;
actionAdapter = new();
LogManager.Adapter = actionAdapter;
}
Expand Down
Loading

0 comments on commit 9e834bc

Please sign in to comment.