Skip to content

Commit

Permalink
UARTLogger now logs to text files, depending on how the config file i…
Browse files Browse the repository at this point in the history
…s set up.
  • Loading branch information
Threetwosevensixseven committed Dec 15, 2019
1 parent 2e8d0f4 commit 0f11009
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 2 deletions.
71 changes: 71 additions & 0 deletions UARTLogger/FileLogger.cs
Original file line number Diff line number Diff line change
@@ -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
}
}
14 changes: 12 additions & 2 deletions UARTLogger/UARTBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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)
Expand Down Expand Up @@ -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);
}
}
}
Expand All @@ -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.
Expand Down
1 change: 1 addition & 0 deletions UARTLogger/UARTLogger.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="FileLogger.cs" />
<Compile Include="Settings.cs" />
<Compile Include="SpectrumCharset.cs" />
<Compile Include="UARTBuffer.cs" />
Expand Down

0 comments on commit 0f11009

Please sign in to comment.