Skip to content

Commit

Permalink
Merge pull request #401 from chrispurnell/bug227
Browse files Browse the repository at this point in the history
Don't create log file when it is not needed.
  • Loading branch information
monkeyman192 authored Jul 3, 2022
2 parents 4cc5f17 + 41264d3 commit 478cc7a
Showing 1 changed file with 29 additions and 26 deletions.
55 changes: 29 additions & 26 deletions MBINCompiler/Source/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,31 +45,6 @@ public static int Main( string[] args )
}

NoLog = options.GetOptionSwitch( "nolog" );
if ( !NoLog ) {
Process running_proc = Process.GetCurrentProcess();
Process[] mbinc_procs = Process.GetProcessesByName(running_proc.ProcessName);

// If we only have one instance of MBINCompiler running create the usual log.
// If a process starts and there is already a running MBINCompiler process, then
// add the PID to the log file name so that there is no issue with two processes
// attempting to write to the same log file.
if (mbinc_procs.Length == 1) {
Logger.Open(Path.ChangeExtension(Utils.GetExecutablePath(), ".log"));
}
else {
Logger.Open(Path.ChangeExtension(Utils.GetExecutablePath(), $".{running_proc.Id}.log"));
}

Logger.EnableTraceLogging = true;

Logger.LogMessage("VERSION", $"MBINCompiler v{Version.GetVersionStringCompact()}");
Logger.LogMessage("ARGS", $"\"{string.Join("\" \"", args)}\"\n");
using (var indent = new Logger.IndentScope()) {
Logger.LogMessage("If you encounter any errors, please submit a bug report and include this log file.\n" +
"Please check that there isn't already a similar issue open before creating a new one.\n" +
"https://github.com/monkeyman192/MBINCompiler/issues\n");
}
}

// now we can emit an error if we need to
if ( invalidArguments ) return CommandLine.ShowInvalidCommandLineArg( options );
Expand All @@ -84,13 +59,41 @@ public static int Main( string[] args )
case "version": return VersionCommand.Execute( options );
case "list": return ListCommand.Execute( options );
case "register": return RegisterCommand.Execute( options );
default: return ConvertCommand.Execute( options );
default:
if ( !NoLog ) StartLogging( args );
return ConvertCommand.Execute( options );
}
} catch ( System.Exception e ) {
return CommandLine.ShowException( e );
}

}

private static void StartLogging( string[] args )
{
Process running_proc = Process.GetCurrentProcess();
Process[] mbinc_procs = Process.GetProcessesByName(running_proc.ProcessName);

// If we only have one instance of MBINCompiler running create the usual log.
// If a process starts and there is already a running MBINCompiler process, then
// add the PID to the log file name so that there is no issue with two processes
// attempting to write to the same log file.
if (mbinc_procs.Length == 1) {
Logger.Open(Path.ChangeExtension(Utils.GetExecutablePath(), ".log"));
}
else {
Logger.Open(Path.ChangeExtension(Utils.GetExecutablePath(), $".{running_proc.Id}.log"));
}

Logger.EnableTraceLogging = true;

Logger.LogMessage("VERSION", $"MBINCompiler v{Version.GetVersionStringCompact()}");
Logger.LogMessage("ARGS", $"\"{string.Join("\" \"", args)}\"\n");
using (var indent = new Logger.IndentScope()) {
Logger.LogMessage("If you encounter any errors, please submit a bug report and include this log file.\n" +
"Please check that there isn't already a similar issue open before creating a new one.\n" +
"https://github.com/monkeyman192/MBINCompiler/issues\n");
}
}
}
}

0 comments on commit 478cc7a

Please sign in to comment.