From 4b0ebbe59723351233482b27fc2ef019b33fc595 Mon Sep 17 00:00:00 2001 From: DrakiaXYZ Date: Fri, 27 Oct 2023 18:46:56 +0000 Subject: [PATCH] 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 https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/160 Co-authored-by: DrakiaXYZ <565558+TheDgtl@users.noreply.github.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Modules/pulls/35 Reviewed-by: Terkoiz Co-authored-by: DrakiaXYZ Co-committed-by: DrakiaXYZ --- .../Logging/EServerLogBackgroundColor.cs | 26 +++++++++ .../Models/Logging/EServerLogLevel.cs | 12 ++++ .../Models/Logging/EServerLogTextColor.cs | 26 +++++++++ .../Models/Logging/ServerLogRequest.cs | 15 +++++ project/Aki.Common/Utils/ServerLog.cs | 57 +++++++++++++++++++ .../MainMenu/PluginErrorNotifierPatch.cs | 11 ++-- 6 files changed, 142 insertions(+), 5 deletions(-) create mode 100644 project/Aki.Common/Models/Logging/EServerLogBackgroundColor.cs create mode 100644 project/Aki.Common/Models/Logging/EServerLogLevel.cs create mode 100644 project/Aki.Common/Models/Logging/EServerLogTextColor.cs create mode 100644 project/Aki.Common/Models/Logging/ServerLogRequest.cs create mode 100644 project/Aki.Common/Utils/ServerLog.cs diff --git a/project/Aki.Common/Models/Logging/EServerLogBackgroundColor.cs b/project/Aki.Common/Models/Logging/EServerLogBackgroundColor.cs new file mode 100644 index 0000000..56855ec --- /dev/null +++ b/project/Aki.Common/Models/Logging/EServerLogBackgroundColor.cs @@ -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 + } +} diff --git a/project/Aki.Common/Models/Logging/EServerLogLevel.cs b/project/Aki.Common/Models/Logging/EServerLogLevel.cs new file mode 100644 index 0000000..98a509e --- /dev/null +++ b/project/Aki.Common/Models/Logging/EServerLogLevel.cs @@ -0,0 +1,12 @@ +namespace Aki.Common.Models.Logging +{ + public enum EServerLogLevel + { + Error = 0, + Warn = 1, + Success = 2, + Info = 3, + Custom = 4, + Debug = 5 + } +} diff --git a/project/Aki.Common/Models/Logging/EServerLogTextColor.cs b/project/Aki.Common/Models/Logging/EServerLogTextColor.cs new file mode 100644 index 0000000..efc71f1 --- /dev/null +++ b/project/Aki.Common/Models/Logging/EServerLogTextColor.cs @@ -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 + } +} diff --git a/project/Aki.Common/Models/Logging/ServerLogRequest.cs b/project/Aki.Common/Models/Logging/ServerLogRequest.cs new file mode 100644 index 0000000..3bbcfe6 --- /dev/null +++ b/project/Aki.Common/Models/Logging/ServerLogRequest.cs @@ -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; } + } +} diff --git a/project/Aki.Common/Utils/ServerLog.cs b/project/Aki.Common/Utils/ServerLog.cs new file mode 100644 index 0000000..bd549dd --- /dev/null +++ b/project/Aki.Common/Utils/ServerLog.cs @@ -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)); + } + } +} diff --git a/project/Aki.SinglePlayer/Patches/MainMenu/PluginErrorNotifierPatch.cs b/project/Aki.SinglePlayer/Patches/MainMenu/PluginErrorNotifierPatch.cs index c8ea38b..20c81a5 100644 --- a/project/Aki.SinglePlayer/Patches/MainMenu/PluginErrorNotifierPatch.cs +++ b/project/Aki.SinglePlayer/Patches/MainMenu/PluginErrorNotifierPatch.cs @@ -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); } }