Strip down the original logger class
This commit is contained in:
@@ -985,8 +985,6 @@ namespace Barotrauma
|
||||
|
||||
Screen.Selected.AddToGUIUpdateList();
|
||||
|
||||
LuaCsLogger.AddToGUIUpdateList();
|
||||
|
||||
Client?.AddToGUIUpdateList();
|
||||
|
||||
SubmarinePreview.AddToGUIUpdateList();
|
||||
|
||||
@@ -13,14 +13,6 @@ namespace Barotrauma
|
||||
{
|
||||
partial class LuaCsSetup
|
||||
{
|
||||
public void AddToGUIUpdateList()
|
||||
{
|
||||
if (!DisableErrorGUIOverlay)
|
||||
{
|
||||
LuaCsLogger.AddToGUIUpdateList();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
|
||||
@@ -74,27 +74,27 @@ namespace Barotrauma.LuaCs
|
||||
public static void PrintMessage(string s)
|
||||
{
|
||||
#if SERVER
|
||||
LuaCsLogger.LogMessage($"[Server] {s}");
|
||||
GameMain.LuaCs.Logger.LogMessage($"{s}");
|
||||
#else
|
||||
LuaCsLogger.LogMessage($"[Client] {s}");
|
||||
GameMain.LuaCs.Logger.LogMessage($"{s}");
|
||||
#endif
|
||||
}
|
||||
|
||||
public static void PrintWarning(string s)
|
||||
{
|
||||
#if SERVER
|
||||
LuaCsLogger.Log($"[Server] {s}", Color.Yellow);
|
||||
GameMain.LuaCs.Logger.Log($"{s}", Color.Yellow);
|
||||
#else
|
||||
LuaCsLogger.Log($"[Client] {s}", Color.Yellow);
|
||||
GameMain.LuaCs.Logger.Log($"{s}", Color.Yellow);
|
||||
#endif
|
||||
}
|
||||
|
||||
public static void PrintError(string s)
|
||||
{
|
||||
#if SERVER
|
||||
LuaCsLogger.LogError($"[Server] {s}");
|
||||
GameMain.LuaCs.Logger.LogError($"{s}");
|
||||
#else
|
||||
LuaCsLogger.LogError($"[Client] {s}");
|
||||
GameMain.LuaCs.Logger.LogError($"{s}");
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ namespace Barotrauma
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
LuaCsLogger.HandleException(e, LuaCsMessageOrigin.CSharpMod);
|
||||
GameMain.LuaCs.Logger.HandleException(e);
|
||||
}
|
||||
IsDisposed = true;
|
||||
}
|
||||
|
||||
@@ -191,7 +191,7 @@ class LuaScriptManagementService : ILuaScriptManagementService, ILuaDataService
|
||||
_script = new Script(CoreModules.Preset_SoftSandbox | CoreModules.Debug | CoreModules.IO | CoreModules.OS_System);
|
||||
_script.Options.DebugPrint = (string msg) =>
|
||||
{
|
||||
_loggerService.Log(msg);
|
||||
_loggerService.LogMessage($"[Lua] {msg}");
|
||||
};
|
||||
_script.Options.ScriptLoader = _luaScriptLoader;
|
||||
_script.Options.CheckThreadAccess = false;
|
||||
@@ -221,7 +221,7 @@ class LuaScriptManagementService : ILuaScriptManagementService, ILuaDataService
|
||||
_script.Globals["loadfile"] = (Func<string, Table, string, DynValue>)LoadFile;
|
||||
_script.Globals["require"] = (Func<string, Table, DynValue>)luaRequire.Require;
|
||||
|
||||
_script.Globals["printerror"] = (DynValue o) => { LuaCsLogger.LogError(o.ToString()); };
|
||||
_script.Globals["printerror"] = (DynValue o) => { _loggerService.LogError($"[Lua] {o.ToString()}"); };
|
||||
|
||||
_script.Globals["dostring"] = (Func<string, Table, string, DynValue>)_script.DoString;
|
||||
_script.Globals["load"] = (Func<string, Table, string, DynValue>)_script.LoadString;
|
||||
|
||||
@@ -15,167 +15,43 @@ namespace Barotrauma
|
||||
|
||||
partial class LuaCsLogger
|
||||
{
|
||||
public static bool HideUserNames = true;
|
||||
|
||||
#if SERVER
|
||||
private const string LogPrefix = "SV";
|
||||
private const int NetMaxLength = 1024; // character limit of vanilla Barotrauma's chat system.
|
||||
private const int NetMaxMessages = 60;
|
||||
|
||||
// This is used so its possible to call logging functions inside the serverLog
|
||||
// hook without creating an infinite loop
|
||||
private static bool lockLog = false;
|
||||
#else
|
||||
private const string LogPrefix = "CL";
|
||||
#endif
|
||||
|
||||
public static LuaCsMessageLogger MessageLogger;
|
||||
public static LuaCsExceptionHandler ExceptionHandler;
|
||||
|
||||
public static void HandleException(Exception ex, LuaCsMessageOrigin origin)
|
||||
{
|
||||
string errorString = "";
|
||||
switch (ex)
|
||||
{
|
||||
case NetRuntimeException netRuntimeException:
|
||||
if (netRuntimeException.DecoratedMessage == null)
|
||||
{
|
||||
errorString = netRuntimeException.ToString();
|
||||
}
|
||||
else
|
||||
{
|
||||
// FIXME: netRuntimeException.ToString() doesn't print the InnerException's stack trace...
|
||||
errorString = $"{netRuntimeException.DecoratedMessage}: {netRuntimeException}";
|
||||
}
|
||||
break;
|
||||
case InterpreterException interpreterException:
|
||||
if (interpreterException.DecoratedMessage == null)
|
||||
{
|
||||
errorString = interpreterException.ToString();
|
||||
}
|
||||
else
|
||||
{
|
||||
errorString = interpreterException.DecoratedMessage;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
errorString = ex.StackTrace != null
|
||||
? ex.ToString()
|
||||
: $"{ex}\n{Environment.StackTrace}";
|
||||
break;
|
||||
}
|
||||
|
||||
LogError(Environment.UserName + " " + errorString, origin);
|
||||
GameMain.LuaCs.Logger.HandleException(ex);
|
||||
}
|
||||
|
||||
public static void LogError(string message, LuaCsMessageOrigin origin)
|
||||
{
|
||||
if (HideUserNames && !Environment.UserName.IsNullOrEmpty())
|
||||
{
|
||||
message = message.Replace(Environment.UserName, "USERNAME");
|
||||
}
|
||||
|
||||
switch (origin)
|
||||
{
|
||||
case LuaCsMessageOrigin.LuaCs:
|
||||
case LuaCsMessageOrigin.Unknown:
|
||||
LogError($"[{LogPrefix} ERROR] {message}");
|
||||
break;
|
||||
case LuaCsMessageOrigin.LuaMod:
|
||||
LogError($"[{LogPrefix} LUA ERROR] {message}");
|
||||
break;
|
||||
case LuaCsMessageOrigin.CSharpMod:
|
||||
LogError($"[{LogPrefix} CS ERROR] {message}");
|
||||
break;
|
||||
}
|
||||
GameMain.LuaCs.Logger.LogError(message);
|
||||
}
|
||||
|
||||
public static void LogError(string message)
|
||||
{
|
||||
Log($"{message}", Color.Red, ServerLog.MessageType.Error);
|
||||
GameMain.LuaCs.Logger.LogError(message);
|
||||
}
|
||||
|
||||
public static void LogMessage(string message, Color? serverColor = null, Color? clientColor = null)
|
||||
{
|
||||
if (serverColor == null) { serverColor = Color.MediumPurple; }
|
||||
if (clientColor == null) { clientColor = Color.Purple; }
|
||||
|
||||
#if SERVER
|
||||
Log(message, serverColor);
|
||||
#else
|
||||
Log(message, clientColor);
|
||||
#endif
|
||||
GameMain.LuaCs.Logger.LogMessage(message, serverColor, clientColor);
|
||||
}
|
||||
|
||||
public static void Log(string message, Color? color = null, ServerLog.MessageType messageType = ServerLog.MessageType.ServerMessage)
|
||||
{
|
||||
MessageLogger?.Invoke(message);
|
||||
|
||||
DebugConsole.NewMessage(message, color);
|
||||
|
||||
#if SERVER
|
||||
void broadcastMessage(string m)
|
||||
{
|
||||
foreach (var client in GameMain.Server.ConnectedClients)
|
||||
{
|
||||
//if (client.ChatMsgQueue.Count > NetMaxMessages)
|
||||
//{
|
||||
// If there's an error or message happening many times per second (inside Update loop for example)
|
||||
// we will need to discart some messages so the client doesn't get overloaded by all
|
||||
// those net messages.
|
||||
// continue;
|
||||
//}
|
||||
|
||||
ChatMessage consoleMessage = ChatMessage.Create("", m, ChatMessageType.Console, null, textColor: color);
|
||||
GameMain.Server.SendDirectChatMessage(consoleMessage, client);
|
||||
|
||||
if (!GameMain.Server.ServerSettings.SaveServerLogs || !client.HasPermission(ClientPermissions.ServerLog))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
ChatMessage logMessage = ChatMessage.Create(messageType.ToString(), "[LuaCs] " + m, ChatMessageType.ServerLog, null);
|
||||
GameMain.Server.SendDirectChatMessage(logMessage, client);
|
||||
}
|
||||
}
|
||||
|
||||
if (GameMain.Server != null)
|
||||
{
|
||||
if (GameMain.Server.ServerSettings.SaveServerLogs)
|
||||
{
|
||||
string logMessage = "[LuaCs] " + message;
|
||||
GameMain.Server.ServerSettings.ServerLog.WriteLine(logMessage, messageType, false);
|
||||
|
||||
if (!lockLog)
|
||||
{
|
||||
lockLog = true;
|
||||
GameMain.LuaCs?.Hook?.Call("serverLog", logMessage, messageType);
|
||||
lockLog = false;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < message.Length; i += NetMaxLength)
|
||||
{
|
||||
string subStr = message.Substring(i, Math.Min(1024, message.Length - i));
|
||||
|
||||
broadcastMessage(subStr);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
GameMain.LuaCs.Logger.Log(message, color, messageType);
|
||||
}
|
||||
}
|
||||
|
||||
partial class LuaCsSetup
|
||||
{
|
||||
// Compatibility with cs mods that use this method.
|
||||
public static void PrintLuaError(object message) => LuaCsLogger.LogError($"{message}", LuaCsMessageOrigin.LuaMod);
|
||||
public static void PrintCsError(object message) => LuaCsLogger.LogError($"{message}", LuaCsMessageOrigin.CSharpMod);
|
||||
public static void PrintGenericError(object message) => LuaCsLogger.LogError($"{message}", LuaCsMessageOrigin.LuaCs);
|
||||
public static void PrintLuaError(object message) => GameMain.LuaCs.Logger.LogError($"{message}");
|
||||
public static void PrintCsError(object message) => GameMain.LuaCs.Logger.LogError($"{message}");
|
||||
public static void PrintGenericError(object message) => GameMain.LuaCs.Logger.LogError($"{message}");
|
||||
|
||||
internal void PrintMessage(object message) => LuaCsLogger.LogMessage($"{message}");
|
||||
internal void PrintMessage(object message) => GameMain.LuaCs.Logger.LogMessage($"{message}");
|
||||
|
||||
public static void PrintCsMessage(object message) => LuaCsLogger.LogMessage($"{message}");
|
||||
public static void PrintCsMessage(object message) => GameMain.LuaCs.Logger.LogMessage($"{message}");
|
||||
|
||||
internal void HandleException(Exception ex, LuaCsMessageOrigin origin) => LuaCsLogger.HandleException(ex, origin);
|
||||
internal void HandleException(Exception ex, LuaCsMessageOrigin origin) => GameMain.LuaCs.Logger.HandleException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,10 +57,12 @@ namespace Barotrauma
|
||||
private List<TimedAction> timedActions = new List<TimedAction>();
|
||||
|
||||
private readonly IEventService _eventService;
|
||||
private readonly ILoggerService _loggerService;
|
||||
|
||||
public LuaCsTimer(IEventService eventService)
|
||||
public LuaCsTimer(IEventService eventService, ILoggerService loggerService)
|
||||
{
|
||||
_eventService = eventService;
|
||||
_loggerService = loggerService;
|
||||
SubscribeToEvents();
|
||||
}
|
||||
|
||||
@@ -117,7 +119,7 @@ namespace Barotrauma
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
LuaCsLogger.HandleException(e, LuaCsMessageOrigin.CSharpMod);
|
||||
_loggerService.HandleException(e);
|
||||
}
|
||||
|
||||
timedActions.Remove(timedAction);
|
||||
|
||||
Reference in New Issue
Block a user