VOID_DataLogger: Changed atmDensity format to a raw number format.
[VOID.git] / VOID_DataLogger.cs
blob:a/VOID_DataLogger.cs -> blob:b/VOID_DataLogger.cs
--- a/VOID_DataLogger.cs
+++ b/VOID_DataLogger.cs
@@ -54,6 +54,7 @@
 
 		protected List<byte> 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(VOIDCore),
+						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,15 +442,19 @@
 			{
 				this._outputFile.Close();
 				this._outputFile = null;
-			}
-		}
+				logger.Append(" File closed.");
+			}
+
+			logger.Print(false);
+		}
+
 		#endregion
 
 		#region Constructors & Destructors
 
 		public VOID_DataLogger()
 		{
-			this._Name = "CSV Data Logger";
+			this.Name = "CSV Data Logger";
 
 			this.loggingActive = false;
 			this.firstWrite = true;
@@ -446,6 +466,11 @@
 
 			this.WindowPos.x = Screen.width - 520f;
 			this.WindowPos.y = 85f;
+
+			this.core.onApplicationQuit += delegate(object sender)
+			{
+				this.CloseFileIfOpen();
+			};
 		}
 
 		~VOID_DataLogger()