-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters