From e61dc5ae5f00d339c0aa9d07b1b5152261d1a2f9 Mon Sep 17 00:00:00 2001 From: Joonas Rikkonen Date: Fri, 20 Apr 2018 20:11:01 +0300 Subject: [PATCH] Option to log debug console output to a file. Closes #318 --- .../BarotraumaClient/Source/DebugConsole.cs | 18 ++++- Barotrauma/BarotraumaClient/Source/Program.cs | 11 +-- .../BarotraumaShared/Source/DebugConsole.cs | 67 ++++++++++++++++++- .../BarotraumaShared/Source/GameSettings.cs | 3 + Barotrauma/BarotraumaShared/config.xml | 2 +- 5 files changed, 90 insertions(+), 11 deletions(-) diff --git a/Barotrauma/BarotraumaClient/Source/DebugConsole.cs b/Barotrauma/BarotraumaClient/Source/DebugConsole.cs index 1c69006aa..19d86a62f 100644 --- a/Barotrauma/BarotraumaClient/Source/DebugConsole.cs +++ b/Barotrauma/BarotraumaClient/Source/DebugConsole.cs @@ -67,7 +67,18 @@ namespace Barotrauma { while (queuedMessages.Count > 0) { - AddMessage(queuedMessages.Dequeue()); + var newMsg = queuedMessages.Dequeue(); + AddMessage(newMsg); + + if (GameSettings.SaveDebugConsoleLogs) + { + unsavedMessages.Add(newMsg); + if (unsavedMessages.Count >= messagesPerFile) + { + SaveLogs(); + unsavedMessages.Clear(); + } + } } } @@ -151,7 +162,10 @@ namespace Barotrauma { while (queuedMessages.Count > 0) { - AddMessage(queuedMessages.Dequeue()); + var newMsg = queuedMessages.Dequeue(); + AddMessage(newMsg); + + if (GameSettings.SaveDebugConsoleLogs) unsavedMessages.Add(newMsg); } } diff --git a/Barotrauma/BarotraumaClient/Source/Program.cs b/Barotrauma/BarotraumaClient/Source/Program.cs index ee47273d8..c4415750a 100644 --- a/Barotrauma/BarotraumaClient/Source/Program.cs +++ b/Barotrauma/BarotraumaClient/Source/Program.cs @@ -190,15 +190,16 @@ namespace Barotrauma sb.AppendLine("Last debug messages:"); for (int i = DebugConsole.Messages.Count - 1; i > 0; i--) { - sb.AppendLine(" " + DebugConsole.Messages[i].Time + " - " + DebugConsole.Messages[i].Text); + sb.AppendLine("[" + DebugConsole.Messages[i].Time + "] " + DebugConsole.Messages[i].Text); } - - + sw.WriteLine(sb.ToString()); sw.Close(); - CrashMessageBox( "A crash report (\"crashreport.txt\") was saved in the root folder of the game."+ - " If you'd like to help fix this bug, please post the report on the Undertow Games forums."); + if (GameSettings.SaveDebugConsoleLogs) DebugConsole.SaveLogs(); + + CrashMessageBox( "A crash report (\"crashreport.log\") was saved in the root folder of the game."+ + " If you'd like to help fix this bug, please post the report on Barotrauma's GitHub issue tracker: https://github.com/Regalis11/Barotrauma/issues/"); } } #endif diff --git a/Barotrauma/BarotraumaShared/Source/DebugConsole.cs b/Barotrauma/BarotraumaShared/Source/DebugConsole.cs index 728c688cb..f84fa55e1 100644 --- a/Barotrauma/BarotraumaShared/Source/DebugConsole.cs +++ b/Barotrauma/BarotraumaShared/Source/DebugConsole.cs @@ -128,6 +128,10 @@ namespace Barotrauma //used for keeping track of the message entered when pressing up/down static int selectedIndex; + private static List unsavedMessages = new List(); + private static int messagesPerFile = 800; + public const string SavePath = "ConsoleLogs"; + static DebugConsole() { commands.Add(new Command("help", "", (string[] args) => @@ -987,6 +991,7 @@ namespace Barotrauma commands.Add(new Command("togglekarma", "togglekarma: Toggles the karma system.", (string[] args) => { + throw new NotImplementedException(); if (GameMain.Server == null) return; GameMain.Server.KarmaEnabled = !GameMain.Server.KarmaEnabled; })); @@ -2170,18 +2175,28 @@ namespace Barotrauma if (string.IsNullOrEmpty((msg))) return; #if SERVER - Messages.Add(new ColoredText(msg, color, isCommand)); + var newMsg = new ColoredText(msg, color, isCommand); + Messages.Add(newMsg); //TODO: REMOVE Console.ForegroundColor = XnaToConsoleColor.Convert(color); Console.WriteLine(msg); Console.ForegroundColor = ConsoleColor.White; + if (GameSettings.SaveDebugConsoleLogs) + { + unsavedMessages.Add(newMsg); + if (unsavedMessages.Count >= messagesPerFile) + { + SaveLogs(); + unsavedMessages.Clear(); + } + } + if (Messages.Count > MaxMessages) { Messages.RemoveRange(0, Messages.Count - MaxMessages); - } - + } #elif CLIENT lock (queuedMessages) { @@ -2276,5 +2291,51 @@ namespace Barotrauma isOpen = true; #endif } + + + public static void SaveLogs() + { + if (unsavedMessages.Count == 0) return; + if (!Directory.Exists(SavePath)) + { + try + { + Directory.CreateDirectory(SavePath); + } + catch (Exception e) + { + ThrowError("Failed to create a folder for debug console logs", e); + return; + } + } + + string fileName = "DebugConsoleLog_" + DateTime.Now.ToShortDateString() + "_" + DateTime.Now.ToShortTimeString() + ".txt"; + var invalidChars = Path.GetInvalidFileNameChars(); + foreach (char invalidChar in invalidChars) + { + fileName = fileName.Replace(invalidChar.ToString(), ""); + } + + string filePath = Path.Combine(SavePath, fileName); + if (File.Exists(filePath)) + { + int fileNum = 2; + while (File.Exists(filePath + " (" + fileNum + ")")) + { + fileNum++; + } + filePath = filePath + " (" + fileNum + ")"; + } + + try + { + File.WriteAllLines(filePath, unsavedMessages.Select(l => "[" + l.Time + "] " + l.Text)); + } + catch (Exception e) + { + unsavedMessages.Clear(); + ThrowError("Saving debug console log to " + filePath + " failed", e); + } + } } } diff --git a/Barotrauma/BarotraumaShared/Source/GameSettings.cs b/Barotrauma/BarotraumaShared/Source/GameSettings.cs index a9a52caa2..e201d1d40 100644 --- a/Barotrauma/BarotraumaShared/Source/GameSettings.cs +++ b/Barotrauma/BarotraumaShared/Source/GameSettings.cs @@ -123,6 +123,7 @@ namespace Barotrauma } public static bool VerboseLogging { get; set; } + public static bool SaveDebugConsoleLogs { get; set; } public GameSettings(string filePath) { @@ -141,6 +142,7 @@ namespace Barotrauma WasGameUpdated = doc.Root.GetAttributeBool("wasgameupdated", false); VerboseLogging = doc.Root.GetAttributeBool("verboselogging", false); + SaveDebugConsoleLogs = doc.Root.GetAttributeBool("savedebugconsolelogs", false); if (doc == null) { @@ -284,6 +286,7 @@ namespace Barotrauma new XAttribute("musicvolume", musicVolume), new XAttribute("soundvolume", soundVolume), new XAttribute("verboselogging", VerboseLogging), + new XAttribute("savedebugconsolelogs", SaveDebugConsoleLogs), new XAttribute("enablesplashscreen", EnableSplashScreen)); if (WasGameUpdated) diff --git a/Barotrauma/BarotraumaShared/config.xml b/Barotrauma/BarotraumaShared/config.xml index 1f7e586a7..a5a185a0c 100644 --- a/Barotrauma/BarotraumaShared/config.xml +++ b/Barotrauma/BarotraumaShared/config.xml @@ -1,5 +1,5 @@  - +