diff --git a/SzCli/Logger.cs b/SzCli/Logger.cs new file mode 100644 index 0000000..b3523cd --- /dev/null +++ b/SzCli/Logger.cs @@ -0,0 +1,27 @@ +using SzLib; + +namespace SzCli; + +public class Logger(ISzFileManager fileManager) : ISzLogger +{ + public bool LogToFile { get; set; } = true; + public string LogFilePath { get; set; } = Path.Combine(fileManager.DataPath, "log.txt"); + + public void Log(string text) + { + Console.WriteLine($"{DateTime.UtcNow} : [SZ LOG] {text}"); + if (LogToFile) File.AppendAllText(LogFilePath, $"{DateTime.UtcNow} : [SZ LOG] {text}\n"); + } + + public void LogError(string text) + { + Console.WriteLine($"{DateTime.UtcNow} : [SZ ERR] {text}"); + if (LogToFile) File.AppendAllText(LogFilePath, $"{DateTime.UtcNow} : [SZ ERR] {text}\n"); + } + + public void LogWarning(string text) + { + Console.WriteLine($"{DateTime.UtcNow} : [SZ WARN] {text}"); + if (LogToFile) File.AppendAllText(LogFilePath, $"{DateTime.UtcNow} : [SZ WARN] {text}\n"); + } +} \ No newline at end of file diff --git a/SzCli/Program.cs b/SzCli/Program.cs index 5e2e612..4a8531e 100644 --- a/SzCli/Program.cs +++ b/SzCli/Program.cs @@ -5,22 +5,25 @@ namespace SzCli; public class Program { + private static readonly ISzFileManager _fileManager = new LocalFileManager(); + private static readonly ISzLogger _logger = new Logger(_fileManager); + public static void Main(string[] args) { - var parser = new SzParser(new LocalFileManager()); + var parser = new SzParser(_fileManager, _logger); - Console.WriteLine("Creating test dataset..."); + _logger.Log("Creating test dataset..."); var dataset = Test.CreateTestDataset(); - Console.WriteLine($"Successfully created test dataset with id {dataset.Id}"); + _logger.Log($"Successfully created test dataset with id {dataset.Id}"); - Console.WriteLine("Attempting to save dataset..."); + _logger.Log("Attempting to save dataset..."); var saved = parser.SaveDataset(dataset); - Console.WriteLine($"Successfully saved dataset? {saved}"); + _logger.Log($"Successfully saved dataset? {saved}"); - Console.WriteLine("Attempting to load dataset..."); + _logger.Log("Attempting to load dataset..."); var loadedDataset = parser.LoadDataset(dataset.Id); - Console.WriteLine($"Successfully loaded dataset? {loadedDataset != null}. Id is {loadedDataset?.Id}"); + _logger.Log($"Successfully loaded dataset? {loadedDataset != null}. Id is {loadedDataset?.Id}"); - Console.WriteLine(loadedDataset?.DataObjects["test"].Fields["value"].Value); + _logger.Log(loadedDataset?.DataObjects["test"].Fields["value"].Value); } } \ No newline at end of file diff --git a/SzLib/ISzLogger.cs b/SzLib/ISzLogger.cs new file mode 100644 index 0000000..8d8054a --- /dev/null +++ b/SzLib/ISzLogger.cs @@ -0,0 +1,10 @@ +namespace SzLib; + +public interface ISzLogger +{ + public bool LogToFile { get; set; } + public string LogFilePath { get; set; } + public void Log(string text); + public void LogError(string text); + public void LogWarning(string text); +} \ No newline at end of file diff --git a/SzLib/SzParser.cs b/SzLib/SzParser.cs index 3745be9..343f75e 100644 --- a/SzLib/SzParser.cs +++ b/SzLib/SzParser.cs @@ -3,7 +3,7 @@ using SzLib.DataObjects; namespace SzLib; -public class SzParser(ISzFileManager szFileManager) +public class SzParser(ISzFileManager szFileManager, ISzLogger szLogger) { private readonly JsonSerializerOptions _jsonOptions = new() { WriteIndented = true }; @@ -29,7 +29,8 @@ public class SzParser(ISzFileManager szFileManager) } catch (Exception e) { - throw new Exception("Could not deserialize JSON to type SzDataset: " + e.Message, e); + szLogger.LogError("Could not deserialize JSON to type SzDataset: " + e.Message); + return null; } } @@ -42,7 +43,7 @@ public class SzParser(ISzFileManager szFileManager) } catch (Exception e) { - Console.WriteLine("Error saving dataset: " + e.Message); + szLogger.LogError("Error saving dataset: " + e.Message); return false; } }