Skip to content

Commit

Permalink
created CommandLogger class
Browse files Browse the repository at this point in the history
  • Loading branch information
p3nGu1nZz committed Apr 8, 2024
1 parent 6c132a2 commit fd896f1
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 0 deletions.
74 changes: 74 additions & 0 deletions Assets/CommandTerminal/CommandLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using System.IO;

namespace CommandTerminal
{
public class CommandLogger : IDisposable
{
private List<string> _logBuffer;
private readonly string _logFilePath;
private readonly int _bufferSize;
private StreamWriter _streamWriter;
private bool _disposed = false;

public CommandLogger(string fileName = "command_log.txt", int bufferSize = 100)
{
string _desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
_logFilePath = Path.Combine(_desktopPath, fileName);
_streamWriter = new StreamWriter(_logFilePath, true);
_logBuffer = new List<string>();
_bufferSize = bufferSize;
}

public void Log(string message)
{
lock (_logBuffer)
{
long _epochTime = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds;
_logBuffer.Add($"[{_epochTime}] {message}");

if (_logBuffer.Count >= _bufferSize)
{
FlushBuffer();
}
}
}

public void FlushBuffer()
{
lock (_logBuffer)
{
try
{
foreach (var _logEntry in _logBuffer)
{
_streamWriter.WriteLine(_logEntry);
}
_streamWriter.Flush();
_logBuffer.Clear();
}
catch (Exception ex)
{
// Handle the exception, e.g., log it to a separate file or rethrow
Console.WriteLine($"An error occurred while flushing the buffer: {ex.Message}");
}
}
}

public void Close()
{
FlushBuffer();
Dispose();
}

public void Dispose()
{
if (!_disposed)
{
_streamWriter?.Dispose();
_disposed = true;
}
}
}
}
2 changes: 2 additions & 0 deletions Assets/CommandTerminal/CommandLogger.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions Assets/CommandTerminal/Terminal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class Terminal : MonoBehaviour
public CommandEvents Events { get; private set; }
public CommandGUI GUI { get; private set; }
public CommandSystem System { get; private set; }
public CommandLogger Logger { get; private set; }

void Awake()
{
Expand All @@ -51,6 +52,7 @@ void Initialize()
Events = new CommandEvents();
GUI = new CommandGUI();
System = new CommandSystem();
Logger = new CommandLogger();
}

void Start()
Expand Down Expand Up @@ -274,5 +276,10 @@ public void LogError(string message)
{
Shell.IssueErrorMessage($"{message}");
}

public void LogToFile(string message)
{
Logger.Log(message);
}
}
}
1 change: 1 addition & 0 deletions Assets/Scripts/Agents/AgentUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace DialogosEngine
public static class AgentUtils
{
public const string k_EndOfSequence = "<eos>";
public const string k_AgentLogFile = "EchoAgentLog.txt";

public static float CalculateEchoReward(string expectedString, string guessedString)
{
Expand Down
3 changes: 3 additions & 0 deletions Assets/Scripts/Agents/EchoAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public void FixedUpdate()

SetReward(reward);
_CachedString = null;

// Testing
Terminal.Instance.LogToFile($"{GetCumulativeReward()} | {_CachedString} | {expectedString}");
}
}

Expand Down

0 comments on commit fd896f1

Please sign in to comment.