-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using Elements.Core; | ||
using MonkeyLoader.Logging; | ||
using MonkeyLoader.Patching; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace MonkeyLoader.Resonite | ||
{ | ||
internal sealed class ResoniteLogToConsole : Monkey<ResoniteLogToConsole> | ||
{ | ||
public override bool CanBeDisabled => true; | ||
|
||
protected override IEnumerable<IFeaturePatch> GetFeaturePatches() => []; | ||
|
||
protected override bool OnLoaded() | ||
{ | ||
UniLog.OnError += message => ToConsoleLoggingHandler(LoggingLevel.Error, message); | ||
UniLog.OnWarning += message => ToConsoleLoggingHandler(LoggingLevel.Warn, message); | ||
UniLog.OnLog += message => ToConsoleLoggingHandler(LoggingLevel.Info, message); | ||
|
||
return true; | ||
} | ||
|
||
private static void ToConsoleLoggingHandler(LoggingLevel level, string message) | ||
{ | ||
Task.Run(() => | ||
{ | ||
// Log when enabled, Console is open, level is supported, and message isn't from the ModLoader | ||
if (!Enabled || !ConsoleLoggingHandler.Instance.Connected || !Logger.ShouldLog(level) || message.Length <= 25 || message.Contains("[MonkeyLoader")) | ||
return; | ||
|
||
string Producer() | ||
{ | ||
var fpsIndex = message.IndexOf(" FPS)"); | ||
if (fpsIndex >= 0) | ||
{ | ||
var openIndex = message.LastIndexOf('(', fpsIndex - 4); | ||
message = message[openIndex..]; | ||
} | ||
|
||
return message.TrimEnd('\r', '\n'); | ||
} | ||
|
||
switch (level) | ||
{ | ||
case LoggingLevel.Error: | ||
ConsoleLoggingHandler.Instance.Error(Producer); | ||
break; | ||
|
||
case LoggingLevel.Warn: | ||
ConsoleLoggingHandler.Instance.Warn(Producer); | ||
break; | ||
|
||
default: | ||
ConsoleLoggingHandler.Instance.Info(Producer); | ||
break; | ||
} | ||
}); | ||
} | ||
} | ||
} |