From: Andy Wilkinson Date: Tue, 25 Nov 2014 05:48:07 +0000 Subject: VOID_DataLogger: Add a null gate in CloseFileIfOpen. X-Git-Tag: 0.16 X-Git-Url: http://git.toad.homelinux.net/projects/VOID.git/commitdiff/ef301cb --- VOID_DataLogger: Add a null gate in CloseFileIfOpen. --- --- a/VOIDEditorMaster.cs +++ b/VOIDEditorMaster.cs @@ -105,6 +105,26 @@ this.Core.OnGUI(); } + + public void OnDestroy() + { + if (this.Core == null) + { + return; + } + + this.Core.OnDestroy(); + } + + public void OnApplicationQuit() + { + if (this.Core == null) + { + return; + } + + this.Core.OnApplicationQuit(); + } } } --- a/VOIDFlightMaster.cs +++ b/VOIDFlightMaster.cs @@ -104,6 +104,26 @@ this.Core.OnGUI(); } + + public void OnDestroy() + { + if (this.Core == null) + { + return; + } + + this.Core.OnDestroy(); + } + + public void OnApplicationQuit() + { + if (this.Core == null) + { + return; + } + + this.Core.OnApplicationQuit(); + } } } --- a/VOID_Core.cs +++ b/VOID_Core.cs @@ -180,6 +180,12 @@ internal ApplicationLauncherButton AppLauncherButton; /* + * Events + * */ + public delegate void VOIDEventHandler(object sender); + public event VOIDEventHandler onApplicationQuit; + + /* * Properties * */ public bool factoryReset @@ -625,6 +631,11 @@ } } + public void OnApplicationQuit() + { + this.onApplicationQuit(this); + } + public void ResetGUI() { this.StopGUI(); --- a/VOID_DataLogger.cs +++ b/VOID_DataLogger.cs @@ -54,6 +54,7 @@ protected List csvBytes; + protected string _fileName; protected FileStream _outputFile; protected uint outstandingWrites; @@ -97,15 +98,20 @@ { get { - return KSP.IO.IOUtils.GetFilePathFor( - typeof(VOID_Core), - string.Format( - "{0}_{1}", - this.vessel.vesselName, - "data.csv" - ), - null - ); + if (this._fileName == null || this._fileName == string.Empty) + { + this._fileName = KSP.IO.IOUtils.GetFilePathFor( + typeof(VOID_Core), + string.Format( + "{0}_{1}", + this.vessel.vesselName, + "data.csv" + ), + null + ); + } + + return this._fileName; } } @@ -145,10 +151,13 @@ byte[] byteOrderMark = utf8Encoding.GetPreamble(); logger.Append(" and writing preamble"); - outputFile.Write(byteOrderMark, 0, byteOrderMark.Length); + this._outputFile.Write(byteOrderMark, 0, byteOrderMark.Length); } logger.Append('.'); + + logger.AppendFormat(" File is {0}opened asynchronously.", this._outputFile.IsAsync ? "" : "not "); + logger.Print(); } @@ -211,7 +220,7 @@ this.CloseFileIfOpen(); logger.Append(" Done."); - logger.Print(); + logger.Print(false); } #endregion @@ -222,7 +231,7 @@ { base.LoadConfig(); - this.logIntervalStr = this.logInterval.value.ToString("{0:0.0##}"); + this.logIntervalStr = this.logInterval.value.ToString("#.0##"); } public override void ModuleWindow(int _) @@ -266,7 +275,7 @@ if (float.TryParse(logIntervalStr, out newLogInterval)) { logInterval.value = newLogInterval; - this.logIntervalStr = this.logInterval.value.ToString("{0:0.0##}"); + this.logIntervalStr = this.logInterval.value.ToString("#.0##"); } GUILayout.EndVertical(); @@ -412,13 +421,20 @@ private void CloseFileIfOpen() { - if (this.csvBytes.Count > 0) - { + Tools.DebugLogger logger = Tools.DebugLogger.New(this); + + logger.AppendFormat("Cleaning up file {0}...", this.fileName); + + if (this.csvBytes != null && this.csvBytes.Count > 0) + { + logger.Append(" Writing remaining data..."); this.AsyncWriteData(); } + logger.Append(" Waiting for writes to finish."); while (this.outstandingWrites > 0) { + logger.Append('.'); System.Threading.Thread.Sleep(10); } @@ -426,8 +442,12 @@ { this._outputFile.Close(); this._outputFile = null; - } - } + logger.Append(" File closed."); + } + + logger.Print(false); + } + #endregion #region Constructors & Destructors @@ -446,6 +466,11 @@ this.WindowPos.x = Screen.width - 520f; this.WindowPos.y = 85f; + + this.core.onApplicationQuit += delegate(object sender) + { + this.CloseFileIfOpen(); + }; } ~VOID_DataLogger() --- a/VOID_Tools.cs +++ b/VOID_Tools.cs @@ -335,7 +335,7 @@ func(id); } #if DEBUG - catch (ArgumentException ex) + catch (ArgumentException) #else catch (ArgumentException) #endif