Skip to content

Commit dd8e241

Browse files
authored
Fix log patch spamming exceptions during startup (#605)
1 parent 3875c20 commit dd8e241

1 file changed

Lines changed: 10 additions & 23 deletions

File tree

‎Torch/Patches/KeenLogPatch.cs‎

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
42
using System.Reflection;
53
using System.Text;
64
using System.Threading;
7-
using System.Threading.Tasks;
85
using NLog;
9-
using Torch.API;
6+
using Sandbox.Engine.Multiplayer;
107
using Torch.Managers.PatchManager;
118
using Torch.Utils;
129
using VRage.Utils;
13-
using Sandbox.Engine.Multiplayer;
14-
using Sandbox.Game.World;
1510

1611
namespace Torch.Patches
1712
{
@@ -48,7 +43,6 @@ internal static class KeenLogPatch
4843
[ReflectedMethodInfo(typeof(MyMultiplayerServerBase), nameof(MyMultiplayerServerBase.ValidationFailed), Parameters = new[] { typeof(ulong), typeof(bool), typeof(string), typeof(bool) })]
4944
private static MethodInfo _logSuppressValidationFailed;
5045
#pragma warning restore 649
51-
5246

5347
public static void Patch(PatchContext context)
5448
{
@@ -62,7 +56,7 @@ public static void Patch(PatchContext context)
6256
context.GetPattern(_logWriteLineException).Prefixes.Add(Method(nameof(PrefixWriteLineException)));
6357
context.GetPattern(_logAppendToClosedLogException).Prefixes.Add(Method(nameof(PrefixAppendToClosedLogException)));
6458

65-
context.GetPattern(_logWriteLineOptions).Prefixes.Add(Method(nameof(PrefixWriteLineOptions)));
59+
context.GetPattern(_logWriteLineOptions).Prefixes.Add(Method(nameof(PrefixWriteLineOptions)));
6660
}
6761

6862
private static MethodInfo Method(string name)
@@ -75,26 +69,19 @@ private static MethodInfo Method(string name)
7569

7670
[ThreadStatic]
7771
private static StringBuilder _tmpStringBuilder;
78-
72+
7973
private static StringBuilder PrepareLog(MyLog log)
8074
{
8175
if (_tmpStringBuilder == null)
8276
_tmpStringBuilder = new StringBuilder();
83-
77+
8478
_tmpStringBuilder.Clear();
8579
var i = Thread.CurrentThread.ManagedThreadId;
8680
int t = 0;
87-
88-
try
89-
{
81+
82+
if (log.LogEnabled)
9083
t = _getIndentByThread(log, i);
91-
}
92-
catch (Exception e)
93-
{
94-
//Commented out as it eventually resolves once fully loaded.
95-
//_log.Debug(e, "Failed to get thread indent");
96-
}
97-
84+
9885
_tmpStringBuilder.Append(' ', t * 3);
9986
return _tmpStringBuilder;
10087
}
@@ -120,11 +107,11 @@ private static bool PrefixWriteLineOptions(MyLog __instance, string message, Log
120107
{
121108
var logFlagMethod = typeof(MyLog).GetMethod("LogFlag", BindingFlags.Instance | BindingFlags.NonPublic);
122109

123-
if(logFlagMethod == null)
110+
if (logFlagMethod == null)
124111
throw new Exception("Failed to find LogFlag method");
125-
112+
126113
var logFlag = (bool)logFlagMethod.Invoke(__instance, new object[] { option });
127-
114+
128115
if (logFlag)
129116
_log.Info(PrepareLog(__instance).Append(message));
130117
return false;

0 commit comments

Comments
 (0)