0
0
mirror of https://github.com/sp-tarkov/modules.git synced 2025-02-13 03:30:44 -05:00

Send plugin errors to the server console (!35)

- Implement the models/helpers required to send console messages to the server console
- Implement sending plugin errors on startup to the server console
- Remove an unused variable and incorrect comment from PluginErrorNotifierPatch

Example output:
![Example](https://i.imgur.com/c0XBYLm.png)

Depends on changes in SPT-AKI/Server#160

Co-authored-by: DrakiaXYZ <565558+TheDgtl@users.noreply.github.com>
Reviewed-on: SPT-AKI/Modules#35
Reviewed-by: Terkoiz <terkoiz@noreply.dev.sp-tarkov.com>
Co-authored-by: DrakiaXYZ <drakiaxyz@noreply.dev.sp-tarkov.com>
Co-committed-by: DrakiaXYZ <drakiaxyz@noreply.dev.sp-tarkov.com>
This commit is contained in:
DrakiaXYZ 2023-10-27 18:46:56 +00:00 committed by chomp
parent 697bc90c93
commit 4b0ebbe597
6 changed files with 142 additions and 5 deletions

View File

@ -0,0 +1,26 @@
using System.Runtime.Serialization;
namespace Aki.Common.Models.Logging
{
public enum EServerLogBackgroundColor
{
[EnumMember(Value = "")]
Default,
[EnumMember(Value = "blackBG")]
Black,
[EnumMember(Value = "redBG")]
Red,
[EnumMember(Value = "greenBG")]
Green,
[EnumMember(Value = "yellowBG")]
Yellow,
[EnumMember(Value = "blueBG")]
Blue,
[EnumMember(Value = "magentaBG")]
Magenta,
[EnumMember(Value = "cyanBG")]
Cyan,
[EnumMember(Value = "whiteBG")]
White
}
}

View File

@ -0,0 +1,12 @@
namespace Aki.Common.Models.Logging
{
public enum EServerLogLevel
{
Error = 0,
Warn = 1,
Success = 2,
Info = 3,
Custom = 4,
Debug = 5
}
}

View File

@ -0,0 +1,26 @@
using System.Runtime.Serialization;
namespace Aki.Common.Models.Logging
{
public enum EServerLogTextColor
{
[EnumMember(Value = "black")]
Black ,
[EnumMember(Value = "red")]
Red,
[EnumMember(Value = "green")]
Green,
[EnumMember(Value = "yellow")]
Yellow,
[EnumMember(Value = "blue")]
Blue,
[EnumMember(Value = "magenta")]
Magenta,
[EnumMember(Value = "cyan")]
Cyan,
[EnumMember(Value = "white")]
White,
[EnumMember(Value = "")]
Gray
}
}

View File

@ -0,0 +1,15 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.ComponentModel;
namespace Aki.Common.Models.Logging
{
public class ServerLogRequest
{
public string Source { get; set; }
public EServerLogLevel Level { get; set; }
public string Message { get; set; }
public EServerLogTextColor Color { get; set; }
public EServerLogBackgroundColor BackgroundColor { get; set; }
}
}

View File

@ -0,0 +1,57 @@
using Aki.Common.Http;
using Aki.Common.Models.Logging;
using System;
namespace Aki.Common.Utils
{
public static class ServerLog
{
public static void Custom(
string source,
string message,
EServerLogTextColor color = EServerLogTextColor.White,
EServerLogBackgroundColor backgroundColor = EServerLogBackgroundColor.Default)
{
Log(source, message, EServerLogLevel.Custom, color, backgroundColor);
}
public static void Error(string source, string message)
{
Log(source, message, EServerLogLevel.Error);
}
public static void Warn(string source, string message)
{
Log(source, message, EServerLogLevel.Warn);
}
public static void Success(string source, string message)
{
Log(source, message, EServerLogLevel.Success);
}
public static void Info(string source, string message)
{
Log(source, message, EServerLogLevel.Info);
}
public static void Log(
string source,
string message,
EServerLogLevel level = EServerLogLevel.Info,
EServerLogTextColor color = EServerLogTextColor.White,
EServerLogBackgroundColor backgroundColor = EServerLogBackgroundColor.Default)
{
ServerLogRequest request = new ServerLogRequest
{
Source = source,
Message = message,
Level = level,
Color = color,
BackgroundColor = backgroundColor
};
RequestHandler.PostJson("/singleplayer/log", Json.Serialize(request));
}
}
}

View File

@ -1,3 +1,4 @@
using Aki.Common.Utils;
using Aki.Reflection.Patching;
using Aki.Reflection.Utils;
using BepInEx.Bootstrap;
@ -13,18 +14,17 @@ namespace Aki.SinglePlayer.Patches.MainMenu
{
/***
* On the first show of the main menu, check if any BepInEx plugins have failed to load, and inform
* the user. This is done via a toast in the bottom right, with a more detailed console message
* the user. This is done via a toast in the bottom right, with a more detailed console message, as
* well as having the errors forwarded to the server console
**/
internal class PluginErrorNotifierPatch : ModulePatch
{
private static MethodInfo _displayMessageNotificationMethod;
private static MethodInfo _directLogMethod;
private static bool _messageShown = false;
protected override MethodBase GetTargetMethod()
{
_displayMessageNotificationMethod = AccessTools.Method(typeof(NotificationManagerClass), "DisplayMessageNotification");
_directLogMethod = AccessTools.Method(typeof(ConsoleScreen), "method_5");
var desiredType = typeof(MenuScreen);
var desiredMethod = desiredType.GetMethod("Show", PatchConstants.PrivateFlags);
@ -59,14 +59,15 @@ namespace Aki.SinglePlayer.Patches.MainMenu
// Show an error in the BepInEx console/log file
Logger.LogError(errorMessage);
// Show errors in the server console
ServerLog.Error("Aki.Singleplayer", errorMessage);
// Show an error in the in-game console, we have to write this in reverse order because the
// in-game console shows newer messages at the top
foreach (string line in errorMessage.Split('\n').Reverse())
{
if (line.Length > 0)
{
// Note: We directly call the internal Log method to work around a bug in 'LogError' that passes an empty string
// as the StackTrace parameter, which results in extra newlines being added to the console logs
ConsoleScreen.LogError(line);
}
}