diff --git a/UARTLogger/FileLogger.cs b/UARTLogger/FileLogger.cs
new file mode 100644
index 0000000..e8633f9
--- /dev/null
+++ b/UARTLogger/FileLogger.cs
@@ -0,0 +1,71 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace UARTLogger
+{
+ public class FileLogger : IDisposable
+ {
+ private FileStream fileStream;
+ private StreamWriter fileWriter;
+
+ public FileLogger(Settings Settings, UARTTargets Target)
+ {
+ if (Target == UARTTargets.ESP && (Settings == null || !Settings.EnableESPLogging))
+ return;
+ if (Target == UARTTargets.Pi && (Settings == null || !Settings.EnablePiLogging))
+ return;
+ string fn = Target == UARTTargets.ESP ? Settings.ESPLogFile : Settings.PiLogFile;
+ var mode = Settings.TruncateLogsOnStartup ? FileMode.Create : FileMode.Append;
+ fileStream = File.Open(fn, mode, FileAccess.Write, FileShare.ReadWrite);
+ fileWriter = new StreamWriter(fileStream);
+ fileWriter.AutoFlush = true;
+ }
+
+ public void Write(String Value)
+ {
+ if (fileWriter != null && Value != null)
+ fileWriter.Write(Value);
+ }
+
+ #region IDisposable Support
+ private bool disposedValue = false; // To detect redundant calls
+
+ protected virtual void Dispose(bool disposing)
+ {
+ if (!disposedValue)
+ {
+ if (disposing)
+ {
+ // Dispose managed state (managed objects).
+ fileWriter.Dispose();
+ fileStream.Dispose();
+ }
+
+ // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
+ // TODO: set large fields to null.
+
+ disposedValue = true;
+ }
+ }
+
+ // TODO: override a finalizer only if Dispose(bool disposing) above has code to free unmanaged resources.
+ // ~FileLogger() {
+ // // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
+ // Dispose(false);
+ // }
+
+ // This code added to correctly implement the disposable pattern.
+ public void Dispose()
+ {
+ // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
+ Dispose(true);
+ // TODO: uncomment the following line if the finalizer is overridden above.
+ // GC.SuppressFinalize(this);
+ }
+ #endregion
+ }
+}
diff --git a/UARTLogger/UARTBuffer.cs b/UARTLogger/UARTBuffer.cs
index 41d8872..760dcc6 100644
--- a/UARTLogger/UARTBuffer.cs
+++ b/UARTLogger/UARTBuffer.cs
@@ -16,6 +16,8 @@ public class UARTBuffer : IDisposable
private UARTTargets target;
private object sync;
private Timer timer;
+ private FileLogger espLogger;
+ private FileLogger piLogger;
public UARTBuffer(Settings Settings)
{
@@ -24,7 +26,9 @@ public UARTBuffer(Settings Settings)
state = UARTStates.Reading;
target = UARTTargets.ESP;
sync = new object();
- timer = new Timer(timerElapsed, this, settings.FlushLogsAfterSecs * 1000, Timeout.Infinite);
+ timer = new Timer(timerElapsed, this, settings.FlushLogsAfterSecs * 1000, Timeout.Infinite);
+ espLogger = new FileLogger(Settings, UARTTargets.ESP);
+ piLogger = new FileLogger(Settings, UARTTargets.Pi);
}
public void Log(byte Value, UARTStates NewState)
@@ -111,7 +115,11 @@ private void Flush()
}
// Log result
- Debug.Write(sb.ToString());
+ var text = sb.ToString();
+ if (target == UARTTargets.ESP)
+ espLogger.Write(text);
+ else if (target == UARTTargets.Pi)
+ piLogger.Write(text);
}
}
}
@@ -134,6 +142,8 @@ protected virtual void Dispose(bool disposing)
{
// Dispose managed state (managed objects).
Flush();
+ espLogger.Dispose();
+ piLogger.Dispose();
}
// TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
diff --git a/UARTLogger/UARTLogger.csproj b/UARTLogger/UARTLogger.csproj
index c51a3c2..165697c 100644
--- a/UARTLogger/UARTLogger.csproj
+++ b/UARTLogger/UARTLogger.csproj
@@ -43,6 +43,7 @@
+