From fd896f1b7c64e57d7225f7100228d6cd5fd9b4c8 Mon Sep 17 00:00:00 2001 From: "K. R." Date: Mon, 8 Apr 2024 18:28:17 -0400 Subject: [PATCH] created CommandLogger class --- Assets/CommandTerminal/CommandLogger.cs | 74 ++++++++++++++++++++ Assets/CommandTerminal/CommandLogger.cs.meta | 2 + Assets/CommandTerminal/Terminal.cs | 7 ++ Assets/Scripts/Agents/AgentUtils.cs | 1 + Assets/Scripts/Agents/EchoAgent.cs | 3 + 5 files changed, 87 insertions(+) create mode 100644 Assets/CommandTerminal/CommandLogger.cs create mode 100644 Assets/CommandTerminal/CommandLogger.cs.meta diff --git a/Assets/CommandTerminal/CommandLogger.cs b/Assets/CommandTerminal/CommandLogger.cs new file mode 100644 index 0000000..c4ad6e3 --- /dev/null +++ b/Assets/CommandTerminal/CommandLogger.cs @@ -0,0 +1,74 @@ +using System; +using System.Collections.Generic; +using System.IO; + +namespace CommandTerminal +{ + public class CommandLogger : IDisposable + { + private List _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(); + _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; + } + } + } +} diff --git a/Assets/CommandTerminal/CommandLogger.cs.meta b/Assets/CommandTerminal/CommandLogger.cs.meta new file mode 100644 index 0000000..0f23308 --- /dev/null +++ b/Assets/CommandTerminal/CommandLogger.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 327837e947368ad47a747aebe40de9ff \ No newline at end of file diff --git a/Assets/CommandTerminal/Terminal.cs b/Assets/CommandTerminal/Terminal.cs index aa69113..57c788b 100644 --- a/Assets/CommandTerminal/Terminal.cs +++ b/Assets/CommandTerminal/Terminal.cs @@ -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() { @@ -51,6 +52,7 @@ void Initialize() Events = new CommandEvents(); GUI = new CommandGUI(); System = new CommandSystem(); + Logger = new CommandLogger(); } void Start() @@ -274,5 +276,10 @@ public void LogError(string message) { Shell.IssueErrorMessage($"{message}"); } + + public void LogToFile(string message) + { + Logger.Log(message); + } } } diff --git a/Assets/Scripts/Agents/AgentUtils.cs b/Assets/Scripts/Agents/AgentUtils.cs index 4ef6299..7a550cf 100644 --- a/Assets/Scripts/Agents/AgentUtils.cs +++ b/Assets/Scripts/Agents/AgentUtils.cs @@ -6,6 +6,7 @@ namespace DialogosEngine public static class AgentUtils { public const string k_EndOfSequence = ""; + public const string k_AgentLogFile = "EchoAgentLog.txt"; public static float CalculateEchoReward(string expectedString, string guessedString) { diff --git a/Assets/Scripts/Agents/EchoAgent.cs b/Assets/Scripts/Agents/EchoAgent.cs index 63a2719..14dc383 100644 --- a/Assets/Scripts/Agents/EchoAgent.cs +++ b/Assets/Scripts/Agents/EchoAgent.cs @@ -29,6 +29,9 @@ public void FixedUpdate() SetReward(reward); _CachedString = null; + + // Testing + Terminal.Instance.LogToFile($"{GetCumulativeReward()} | {_CachedString} | {expectedString}"); } }