Skip to content

Commit

Permalink
Minor improvements to logging and command line arguments.
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
Starkku committed Aug 6, 2020
1 parent 344202d commit 1c77848
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 21 deletions.
39 changes: 27 additions & 12 deletions MapTool.UI/MapTool.UI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public partial class MapToolUI : Form
private readonly List<ListBoxProfile> Profiles = new List<ListBoxProfile>();
private ListBoxProfile SelectedProfile = null;

private bool EnableWriteDebugLog = false;
private bool WriteLogFile = false;
private bool ShowDebugLogging = false;

public MapToolUI(string[] args)
{
Expand All @@ -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);
}
}

Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
14 changes: 7 additions & 7 deletions MapTool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -45,7 +46,7 @@ static void Main(string[] args)
ShowHelp();
return;
}
InitLogger(settings.DebugLogging);
InitLogger();

#if !DEBUG
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
Expand Down Expand Up @@ -132,15 +133,14 @@ private static void CurrentDomain_UnhandledException(object sender, UnhandledExc
/// <summary>
/// Initializes the logger.
/// </summary>
/// <param name="writeFile">Set to yes to write a log file.</param>
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);
}

/// <summary>
Expand Down
13 changes: 11 additions & 2 deletions MapTool/Utility/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,18 @@ public bool List
}

/// <summary>
/// If set, enable debug logging.
/// If set, writes a log file.
/// </summary>
public bool DebugLogging
public bool WriteLogFile
{
get;
set;
}

/// <summary>
/// If set, shows debug-level logging in console.
/// </summary>
public bool ShowDebugLogging
{
get;
set;
Expand Down

0 comments on commit 1c77848

Please sign in to comment.