diff --git a/mocha-dotnet/src/app/Mocha.Oms.Server/Program.cs b/mocha-dotnet/src/app/Mocha.Oms.Server/Program.cs index 6ed1e90..25549da 100644 --- a/mocha-dotnet/src/app/Mocha.Oms.Server/Program.cs +++ b/mocha-dotnet/src/app/Mocha.Oms.Server/Program.cs @@ -8,6 +8,8 @@ using MBS.Core; using Mocha.Core; using Mocha.Core.UI.Server; +using Mocha.Core.Logging; +using Mocha.Core.Logging.Loggers; /// /// Provides the entry point for a simple application which starts a Web server @@ -33,8 +35,6 @@ public class Program : WebApplication private LibraryHandle l_System, l_Web; public Oms Oms { get; } - - private System.IO.StreamWriter? logSt = null; protected override WebServer CreateWebServer() { @@ -56,21 +56,15 @@ public class Program : WebApplication Console.Error.WriteLine("oms: error: failed to write PID file; graceful control will be unavailable"); } - string logFile = "/var/log/mocha/oms-dotnet.log"; - try - { - OpenLogFile(logFile); - } - catch (UnauthorizedAccessException ex) - { - logFile = System.IO.Path.Combine(new string[] { System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "oms-dotnet.log" }); - OpenLogFile(logFile); - } + DefaultFileLogger logger = new DefaultFileLogger(); + ConsoleLogger logger2 = new ConsoleLogger(); + Log.Loggers.Add(logger); + Log.Loggers.Add(logger2); - logSt?.WriteLine("**********"); - logSt?.WriteLine("Mocha .NET OMS, version {0}", System.Reflection.Assembly.GetExecutingAssembly().GetName().Version); - logSt?.WriteLine(String.Format("{0} {1}", DateTime.Now.ToLongDateString(), DateTime.Now.ToLongTimeString())); - logSt?.WriteLine("**********"); + Log.WriteLine("**********"); + Log.WriteLine("Mocha .NET OMS, version {0}", System.Reflection.Assembly.GetExecutingAssembly().GetName().Version); + Log.WriteLine(String.Format("{0} {1}", DateTime.Now.ToLongDateString(), DateTime.Now.ToLongTimeString())); + Log.WriteLine("**********"); // this all has to be done before the Web server is started... Oms.Initialize(); @@ -132,17 +126,6 @@ public class Program : WebApplication base.OnStartup(e); } - private void OpenLogFile(string logFile) - { - string logDir = System.IO.Path.GetDirectoryName(logFile); - if (!System.IO.Directory.Exists(logDir)) - { - System.IO.Directory.CreateDirectory(logDir); - } - logSt = new StreamWriter(System.IO.File.Open(logFile, FileMode.Append, FileAccess.Write, FileShare.Read), System.Text.Encoding.UTF8); - logSt.AutoFlush = true; - } - private bool UpdateMochafile() { bool enableMochaFile = true; @@ -151,11 +134,11 @@ public class Program : WebApplication // check to see if we have a Mochafile that we need to load // (this should only be done on development build) string Mochafilename = "/var/mocha/uploads/Mochafile.json"; - logSt.WriteLine(String.Format("oms-dotnet: looking for Mochafile '{0}'", Mochafilename)); + Log.WriteLine("oms-dotnet: looking for Mochafile '{0}'", Mochafilename); if (System.IO.File.Exists(Mochafilename)) { - logSt.WriteLine("oms-dotnet: Mochafile found"); + Log.WriteLine("oms-dotnet: Mochafile found"); if (LoadMochafile(Mochafilename)) { return true; @@ -163,24 +146,24 @@ public class Program : WebApplication } else { - logSt.WriteLine("oms-dotnet: Mochafile not found"); + Log.WriteLine("oms-dotnet: Mochafile not found"); } } else { - logSt.WriteLine("oms-dotnet: skipping Mochafile processing (not enabled)"); + Log.WriteLine("oms-dotnet: skipping Mochafile processing (not enabled)"); } return false; } private bool LoadMochafile(string mochafilename) { - logSt?.Write(String.Format("oms-dotnet: loading Mochafile at '{0}'", mochafilename)); + Log.WriteLine("oms-dotnet: loading Mochafile at '{0}'", mochafilename); JsonObject? json = JsonNode.Parse(System.IO.File.ReadAllText(mochafilename)) as JsonObject; if (json == null) { - logSt?.Write("oms-dotnet: Mochafile error: could not parse JSON"); + Log.WriteLine("oms-dotnet: Mochafile error: could not parse JSON"); return false; } @@ -193,16 +176,16 @@ public class Program : WebApplication if (jo.ContainsKey("name")) { string name = jo["name"].GetValue(); - logSt?.Write(String.Format("oms-dotnet: Mochafile says create tenant '{0}'", name)); + Log.WriteLine("oms-dotnet: Mochafile says create tenant '{0}'", name); TenantHandle th = Oms.CreateTenant(name); - logSt?.Write(String.Format("oms-dotnet: create tenant ok, handle is '{0}'", th)); + Log.WriteLine("oms-dotnet: create tenant ok, handle is '{0}'", th); } } } } - logSt?.Write("oms-dotnet: Mochafile processing complete"); + Log.WriteLine("oms-dotnet: Mochafile processing complete"); return true; } diff --git a/mocha-dotnet/src/lib/Mocha.Core/Logging/Log.cs b/mocha-dotnet/src/lib/Mocha.Core/Logging/Log.cs new file mode 100644 index 0000000..6ac5227 --- /dev/null +++ b/mocha-dotnet/src/lib/Mocha.Core/Logging/Log.cs @@ -0,0 +1,39 @@ +// Copyright (C) 2025 Michael Becker +// +// This file is part of Mocha.NET. +// +// Mocha.NET is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Mocha.NET is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Mocha.NET. If not, see . + +namespace Mocha.Core.Logging; + +public static class Log +{ + public static List Loggers { get; } = new List(); + + public static void WriteLine(string text) + { + foreach (Logger logger in Loggers) + { + logger.WriteLine(text); + } + } + public static void WriteLine(string text, params object[] format) + { + foreach (Logger logger in Loggers) + { + logger.WriteLine(text, format); + } + } + +} \ No newline at end of file diff --git a/mocha-dotnet/src/lib/Mocha.Core/Logging/Logger.cs b/mocha-dotnet/src/lib/Mocha.Core/Logging/Logger.cs new file mode 100644 index 0000000..e1bab67 --- /dev/null +++ b/mocha-dotnet/src/lib/Mocha.Core/Logging/Logger.cs @@ -0,0 +1,59 @@ +// Copyright (C) 2025 Michael Becker +// +// This file is part of Mocha.NET. +// +// Mocha.NET is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Mocha.NET is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Mocha.NET. If not, see . + +namespace Mocha.Core.Logging; + +public class Logger +{ + public Stream? Stream { get; protected set; } + + private StreamWriter? _sw; + protected StreamWriter? StreamWriter + { + get + { + if (_sw == null) + { + if (Stream != null) + { + _sw = new StreamWriter(Stream, System.Text.Encoding.UTF8); + _sw.AutoFlush = true; + } + } + return _sw; + } + } + + private System.IO.StreamWriter? sw = null; + + protected Logger() + { + } + public Logger(Stream st) + { + Stream = st; + } + + public void WriteLine(string text) + { + StreamWriter?.WriteLine(text); + } + public void WriteLine(string text, params object?[] format) + { + StreamWriter?.WriteLine(text, format); + } +} \ No newline at end of file diff --git a/mocha-dotnet/src/lib/Mocha.Core/Logging/Loggers/ConsoleLogger.cs b/mocha-dotnet/src/lib/Mocha.Core/Logging/Loggers/ConsoleLogger.cs new file mode 100644 index 0000000..84ebed0 --- /dev/null +++ b/mocha-dotnet/src/lib/Mocha.Core/Logging/Loggers/ConsoleLogger.cs @@ -0,0 +1,26 @@ +// Copyright (C) 2025 Michael Becker +// +// This file is part of Mocha.NET. +// +// Mocha.NET is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Mocha.NET is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Mocha.NET. If not, see . + +namespace Mocha.Core.Logging.Loggers; + +public class ConsoleLogger : Logger +{ + public ConsoleLogger() + { + Stream = Console.OpenStandardOutput(); + } +} \ No newline at end of file diff --git a/mocha-dotnet/src/lib/Mocha.Core/Logging/Loggers/DefaultFileLogger.cs b/mocha-dotnet/src/lib/Mocha.Core/Logging/Loggers/DefaultFileLogger.cs new file mode 100644 index 0000000..5762459 --- /dev/null +++ b/mocha-dotnet/src/lib/Mocha.Core/Logging/Loggers/DefaultFileLogger.cs @@ -0,0 +1,47 @@ +// Copyright (C) 2025 Michael Becker +// +// This file is part of Mocha.NET. +// +// Mocha.NET is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Mocha.NET is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Mocha.NET. If not, see . + +namespace Mocha.Core.Logging.Loggers; + +public class DefaultFileLogger : Logger +{ + public DefaultFileLogger() + { + string logFile = "/var/log/mocha/oms-dotnet.log"; + try + { + OpenLogFile(logFile); + } + catch (UnauthorizedAccessException ex) + { + logFile = System.IO.Path.Combine(new string[] { System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "oms-dotnet.log" }); + OpenLogFile(logFile); + } + } + + private void OpenLogFile(string logFile) + { + string logDir = System.IO.Path.GetDirectoryName(logFile); + if (!System.IO.Directory.Exists(logDir)) + { + System.IO.Directory.CreateDirectory(logDir); + } + + Stream st = System.IO.File.Open(logFile, FileMode.Append, FileAccess.Write, FileShare.Read); + } + +} \ No newline at end of file