From 1c77848efff3e6277f34c6f5293a79cb7e4586b2 Mon Sep 17 00:00:00 2001 From: Starkku Date: Thu, 6 Aug 2020 23:30:29 +0300 Subject: [PATCH] Minor improvements to logging and command line arguments. - Added separate command line argument to enable debug-level logging in console and/or UI textbox. - Enabling writing of log file for MapTool.UI will now print debug-level logging in the file. --- MapTool.UI/MapTool.UI.cs | 39 +++++++++++++++++++++++++------------ MapTool/Program.cs | 14 ++++++------- MapTool/Utility/Settings.cs | 13 +++++++++++-- 3 files changed, 45 insertions(+), 21 deletions(-) diff --git a/MapTool.UI/MapTool.UI.cs b/MapTool.UI/MapTool.UI.cs index f01724e..c64fd14 100644 --- a/MapTool.UI/MapTool.UI.cs +++ b/MapTool.UI/MapTool.UI.cs @@ -29,7 +29,8 @@ public partial class MapToolUI : Form private readonly List Profiles = new List(); private ListBoxProfile SelectedProfile = null; - private bool EnableWriteDebugLog = false; + private bool WriteLogFile = false; + private bool ShowDebugLogging = false; public MapToolUI(string[] args) { @@ -52,10 +53,10 @@ public MapToolUI(string[] args) ext2 += delim2 + "*" + ValidMapExts[i]; } openFileDialog.Filter = "Map files (" + ext1 + ")|" + ext2; - if (EnableWriteDebugLog) + if (WriteLogFile) { string logfile = AppDomain.CurrentDomain.BaseDirectory + Path.ChangeExtension(AppDomain.CurrentDomain.FriendlyName, ".log"); - Logger.Initialize(logfile, true, false); + Logger.Initialize(logfile, true, ShowDebugLogging); } } @@ -66,7 +67,14 @@ private void ParseArguments(string[] args) switch (arg.ToLower()) { case "-log": - EnableWriteDebugLog = true; + case "--log": + case "-g": + WriteLogFile = true; + continue; + case "-debug": + case "--debug": + case "-d": + ShowDebugLogging = true; continue; default: continue; @@ -239,7 +247,8 @@ private void ProcessMap(string filename) string outputfilename = Path.GetDirectoryName(filename) + "\\" + Path.GetFileNameWithoutExtension(filename) + "_altered" + Path.GetExtension(filename); if (cbOverwrite.Checked) outputfilename = filename; string extra = ""; - //if (EnableWriteDebugLog) extra += " -log"; + if (WriteLogFile) + extra += " -debug"; string cmd = "-i=\"" + filename + "\" -o=\"" + outputfilename + "\" -p=\"" + SelectedProfile.FileName + "\"" + extra; try @@ -277,20 +286,26 @@ private void ConsoleDataReceived(object sender, DataReceivedEventArgs e) AppendToLog(e.Data); } - private delegate void LogDelegate(string s); - private void AppendToLog(string s) + private delegate void LogDelegate(string str); + private void AppendToLog(string str) { - if (s == null) return; + if (str == null) return; if (InvokeRequired) { - Invoke(new LogDelegate(AppendToLog), s); + Invoke(new LogDelegate(AppendToLog), str); return; } - textBoxLogger.AppendText(s + "\r\n"); - if (EnableWriteDebugLog) + bool skipTextBoxLogging = false; + if (!ShowDebugLogging && str.Contains("[Debug]") && str.Length >= 8) { - Logger.LogToFileOnly(s); + string strTrimTime = str.Substring(8); + if (strTrimTime.StartsWith("[Debug]")) + skipTextBoxLogging = true; } + if (!skipTextBoxLogging) + textBoxLogger.AppendText(str + "\r\n"); + if (WriteLogFile) + Logger.LogToFileOnly(str); } private delegate void ControlStateDelegate(bool enable); diff --git a/MapTool/Program.cs b/MapTool/Program.cs index 078b39e..72587da 100644 --- a/MapTool/Program.cs +++ b/MapTool/Program.cs @@ -25,12 +25,13 @@ static void Main(string[] args) { options = new OptionSet { - { "h|help", "Show help", v => settings.ShowHelp = true}, + { "h|?|help", "Show help", v => settings.ShowHelp = true}, { "i|infile=", "Input file.", v => settings.FileInput = v}, { "o|outfile=", "Output file.", v => settings.FileOutput = v}, { "l|list", "List theater data based on input theater config file.", v => settings.List = true}, { "p|profilefile=", "Conversion profile file. This also enables the conversion logic.", v => settings.FileConfig = v}, - { "log|debug-logging", "If set, writes a log to a file in program directory.", v => settings.DebugLogging = true} + { "g|log", "If set, writes a log to a file in program directory.", v => settings.WriteLogFile = true}, + { "d|debug", "If set, shows debug-level logging in console window.", v => settings.ShowDebugLogging = true} }; try { @@ -45,7 +46,7 @@ static void Main(string[] args) ShowHelp(); return; } - InitLogger(settings.DebugLogging); + InitLogger(); #if !DEBUG AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); @@ -132,15 +133,14 @@ private static void CurrentDomain_UnhandledException(object sender, UnhandledExc /// /// Initializes the logger. /// - /// Set to yes to write a log file. - private static void InitLogger(bool writeFile = false) + private static void InitLogger() { string filename = AppDomain.CurrentDomain.BaseDirectory + Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location) + ".log"; - bool enableDebugLogging = false; + bool enableDebugLogging = settings.ShowDebugLogging; #if DEBUG enableDebugLogging = true; #endif - Logger.Initialize(filename, writeFile, enableDebugLogging); + Logger.Initialize(filename, settings.WriteLogFile, enableDebugLogging); } /// diff --git a/MapTool/Utility/Settings.cs b/MapTool/Utility/Settings.cs index e5d9fb1..08b16f5 100644 --- a/MapTool/Utility/Settings.cs +++ b/MapTool/Utility/Settings.cs @@ -57,9 +57,18 @@ public bool List } /// - /// If set, enable debug logging. + /// If set, writes a log file. /// - public bool DebugLogging + public bool WriteLogFile + { + get; + set; + } + + /// + /// If set, shows debug-level logging in console. + /// + public bool ShowDebugLogging { get; set;