/// <summary> /// Writes a message to a logfile instance, if it exists /// </summary> /// <param name="message">The message to write</param> /// <param name="logfiles">The logfile to write to</param> /// <param name="logLevel">The level of severity of the message. If not Application log, this parameter is ignored</param> public static void WriteToLog(string message, Logfiles logfiles, LogLevel logLevel) { Logfile fileToWriteTo = null; switch (logfiles) { case Logfiles.Application: fileToWriteTo = ApplicationLogfile; break; case Logfiles.Updater: fileToWriteTo = UpdaterLogfile; break; case Logfiles.Editor: fileToWriteTo = EditorLogfile; break; case Logfiles.PatchDesigner: fileToWriteTo = PatcherLogfile; break; case Logfiles.Installer: fileToWriteTo = InstallLogfile; break; case Logfiles.Uninstaller: fileToWriteTo = UninstallLogfile; break; } //check if the application logfile is null and the application is now in a new mode if (fileToWriteTo == null && CommandLineSettings.ApplicationMode != ApplicationMode.Default) { switch (CommandLineSettings.ApplicationMode) { case ApplicationMode.Editor: fileToWriteTo = EditorLogfile; break; case ApplicationMode.PatchDesigner: fileToWriteTo = PatcherLogfile; break; case ApplicationMode.Updater: fileToWriteTo = UpdaterLogfile; break; } } //check if logfile is null else if (fileToWriteTo == null) { //check if it's the application logfile if (fileToWriteTo == ApplicationLogfile) { if (!FailedToWriteToLogWindowShown) { MessageBox.Show("Failed to write to application log: instance is null!"); FailedToWriteToLogWindowShown = true; } } else { WriteToLog(string.Format("Tried to write to null log instance: {0}", logfiles.ToString()), Logfiles.Application, LogLevel.Error); } return; } if (logfiles == Logfiles.PatchDesigner || logfiles == Logfiles.Editor) { string temp = fileToWriteTo.Write(message, logLevel); OnLoggingUIThreadReport?.Invoke(temp); } else { fileToWriteTo.Write(message, logLevel); } }
#pragma warning restore CA1009 /// <summary> /// Initialize the logging system for the application /// </summary> /// <param name="logfile">The log file to initialize</param> /// <param name="logfilePath">The custom path of where to initialize the file</param> /// <returns>True if successful initialization, false otherwise</returns> public static bool Init(Logfiles logfile, string logfilePath = null) { Logfile fileToWriteTo = null; //assign it here first to make sure it's null switch (logfile) { case Logfiles.Application: fileToWriteTo = ApplicationLogfile; logfilePath = ApplicationLogFilename; break; case Logfiles.Installer: fileToWriteTo = InstallLogfile; break; case Logfiles.Uninstaller: fileToWriteTo = UninstallLogfile; break; case Logfiles.Editor: fileToWriteTo = EditorLogfile; logfilePath = ApplicationEditorLogFilename; break; case Logfiles.PatchDesigner: fileToWriteTo = PatcherLogfile; logfilePath = ApplicationPatchDesignerLogFilename; break; case Logfiles.Updater: fileToWriteTo = UpdaterLogfile; logfilePath = ApplicationUpdaterLogFilename; break; } if (fileToWriteTo != null) { throw new BadMemeException("only do this once jackass"); } #pragma warning disable IDE0068 // Use recommended dispose pattern fileToWriteTo = new Logfile(logfilePath, ApplicationLogfileTimestamp); #pragma warning restore IDE0068 // Use recommended dispose pattern //now that it's newed, the reference needs to be reverse assigned switch (logfile) { case Logfiles.Application: ApplicationLogfile = fileToWriteTo; break; case Logfiles.Installer: InstallLogfile = fileToWriteTo; break; case Logfiles.Uninstaller: UninstallLogfile = fileToWriteTo; break; case Logfiles.Editor: EditorLogfile = fileToWriteTo; break; case Logfiles.PatchDesigner: PatcherLogfile = fileToWriteTo; break; case Logfiles.Updater: UpdaterLogfile = fileToWriteTo; break; } if (!fileToWriteTo.Init()) { MessageBox.Show(string.Format("Failed to initialize logfile {0}, check file permissions", logfilePath)); return(false); } return(true); }