diff --git a/Application/Core/ClientAuthentication.cs b/Application/Core/ClientAuthentication.cs index 4b73fd321..db3640f9e 100644 --- a/Application/Core/ClientAuthentication.cs +++ b/Application/Core/ClientAuthentication.cs @@ -67,7 +67,7 @@ namespace IW4MAdmin.Application.Core { if (AuthenticatedClients.Values.Count > 18) { - Program.ServerManager.GetLogger().WriteWarning($"auth client count is {AuthenticatedClients.Values.Count}, this is bad"); + Program.ServerManager.GetLogger(0).WriteError($"auth client count is {AuthenticatedClients.Values.Count}, this is bad"); return AuthenticatedClients.Values.Take(18).ToList(); } diff --git a/Application/IO/GameLogReader.cs b/Application/IO/GameLogReader.cs index 9405a0642..db35950a8 100644 --- a/Application/IO/GameLogReader.cs +++ b/Application/IO/GameLogReader.cs @@ -55,9 +55,9 @@ namespace IW4MAdmin.Application.IO catch (Exception e) { - Program.ServerManager.GetLogger().WriteWarning("Could not properly parse event line"); - Program.ServerManager.GetLogger().WriteDebug(e.Message); - Program.ServerManager.GetLogger().WriteDebug(eventLine); + server.Logger.WriteWarning("Could not properly parse event line"); + server.Logger.WriteDebug(e.Message); + server.Logger.WriteDebug(eventLine); } } } diff --git a/Application/IO/GameLogReaderHttp.cs b/Application/IO/GameLogReaderHttp.cs index e8a8dc3a5..3669ef0e3 100644 --- a/Application/IO/GameLogReaderHttp.cs +++ b/Application/IO/GameLogReaderHttp.cs @@ -59,9 +59,9 @@ namespace IW4MAdmin.Application.IO catch (Exception e) { - Program.ServerManager.GetLogger().WriteWarning("Could not properly parse event line"); - Program.ServerManager.GetLogger().WriteDebug(e.Message); - Program.ServerManager.GetLogger().WriteDebug(eventLine); + server.Logger.WriteWarning("Could not properly parse event line"); + server.Logger.WriteDebug(e.Message); + server.Logger.WriteDebug(eventLine); } } } diff --git a/Application/Localization/Configure.cs b/Application/Localization/Configure.cs index 27045824a..1329e01f9 100644 --- a/Application/Localization/Configure.cs +++ b/Application/Localization/Configure.cs @@ -59,7 +59,7 @@ namespace IW4MAdmin.Application.Localization { if (!localizationDict.TryAdd(item.Key, item.Value)) { - Program.ServerManager.GetLogger().WriteError($"Could not add locale string {item.Key} to localization"); + Program.ServerManager.GetLogger(0).WriteError($"Could not add locale string {item.Key} to localization"); } } } diff --git a/Application/Logger.cs b/Application/Logger.cs index 0f3597f1e..bbfc9874a 100644 --- a/Application/Logger.cs +++ b/Application/Logger.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Threading; namespace IW4MAdmin.Application { @@ -18,18 +19,18 @@ namespace IW4MAdmin.Application } readonly string FileName; - readonly object ThreadLock; + readonly SemaphoreSlim OnLogWriting; public Logger(string fn) { - FileName = Path.Join("Log", fn); - ThreadLock = new object(); - if (File.Exists(fn)) - File.Delete(fn); + FileName = Path.Join("Log", $"{fn}-{DateTime.Now.ToString("yyyyMMddHHmmssffff")}.log"); + OnLogWriting = new SemaphoreSlim(1,1); } void Write(string msg, LogType type) { + OnLogWriting.Wait(); + string stringType = type.ToString(); try @@ -40,8 +41,6 @@ namespace IW4MAdmin.Application catch (Exception) { } string LogLine = $"[{DateTime.Now.ToString("MM.dd.yyy HH:mm:ss.fff")}] - {stringType}: {msg}"; - lock (ThreadLock) - { #if DEBUG // lets keep it simple and dispose of everything quickly as logging wont be that much (relatively) @@ -53,7 +52,8 @@ namespace IW4MAdmin.Application //if (type != LogType.Debug) File.AppendAllText(FileName, $"{LogLine}{Environment.NewLine}"); #endif - } + + OnLogWriting.Release(1); } public void WriteVerbose(string msg) diff --git a/Application/Manager.cs b/Application/Manager.cs index d81cf80b9..453ec4c3b 100644 --- a/Application/Manager.cs +++ b/Application/Manager.cs @@ -27,7 +27,7 @@ namespace IW4MAdmin.Application private List _servers; public List Servers => _servers.OrderByDescending(s => s.ClientNum).ToList(); public Dictionary PrivilegedClients { get; set; } - public ILogger Logger { get; private set; } + public ILogger Logger => GetLogger(0); public bool Running { get; private set; } public bool IsInitialized { get; private set; } // define what the delagate function looks like @@ -49,11 +49,12 @@ namespace IW4MAdmin.Application ManualResetEventSlim OnQuit; readonly IPageList PageList; readonly SemaphoreSlim ProcessingEvent = new SemaphoreSlim(1, 1); + readonly Dictionary Loggers = new Dictionary(); private ApplicationManager() { - Logger = new Logger("IW4MAdmin.log"); // do any needed migrations + // todo: move out ConfigurationMigration.MoveConfigFolder10518(Logger); _servers = new List(); Commands = new List(); @@ -568,9 +569,29 @@ namespace IW4MAdmin.Application Running = false; } - public ILogger GetLogger() + public ILogger GetLogger(int serverId) { - return Logger; + if (Loggers.ContainsKey(serverId)) + { + return Loggers[serverId]; + } + + else + { + Logger newLogger; + + if (serverId == 0) + { + newLogger = new Logger("IW4MAdmin-Manager"); + } + else + { + newLogger = new Logger($"IW4MAdmin-Server-{serverId}"); + } + + Loggers.Add(serverId, newLogger); + return newLogger; + } } public IList GetMessageTokens() diff --git a/Application/Migration/ConfigurationMigration.cs b/Application/Migration/ConfigurationMigration.cs index 2745d1d13..ae20113b8 100644 --- a/Application/Migration/ConfigurationMigration.cs +++ b/Application/Migration/ConfigurationMigration.cs @@ -47,6 +47,13 @@ namespace IW4MAdmin.Application.Migration File.Move(configFile, destinationPath); } } + + if (!File.Exists(Path.Join("Database", "Database.db")) && + File.Exists("Database.db")) + { + log.WriteDebug("Moving database file"); + File.Move("Database.db", Path.Join("Database", "Database.db")); + } } } } diff --git a/Application/Server.cs b/Application/Server.cs index 211ec1d26..30a3481f6 100644 --- a/Application/Server.cs +++ b/Application/Server.cs @@ -201,7 +201,7 @@ namespace IW4MAdmin { player.Level = Player.Permission.User; } -#if DEBUG == false + if (currentBan != null) { Logger.WriteInfo($"Banned client {player} trying to connect..."); @@ -243,7 +243,6 @@ namespace IW4MAdmin Players[player.ClientNumber] = null; return false; } -#endif player.State = Player.ClientState.Connected; return true; @@ -251,9 +250,9 @@ namespace IW4MAdmin catch (Exception ex) { - Manager.GetLogger().WriteError($"{loc["SERVER_ERROR_ADDPLAYER"]} {polledPlayer.Name}::{polledPlayer.NetworkId}"); - Manager.GetLogger().WriteDebug(ex.Message); - Manager.GetLogger().WriteDebug(ex.StackTrace); + Logger.WriteError($"{loc["SERVER_ERROR_ADDPLAYER"]} {polledPlayer.Name}::{polledPlayer.NetworkId}"); + Logger.WriteDebug(ex.Message); + Logger.WriteDebug(ex.StackTrace); return false; } } @@ -335,7 +334,7 @@ namespace IW4MAdmin (canExecuteCommand || E.Origin?.Level == Player.Permission.Console)) { - var _ = (((Command)E.Extra).ExecuteAsync(E)); + await (((Command)E.Extra).ExecuteAsync(E)); } } diff --git a/Plugins/Stats/Helpers/StatManager.cs b/Plugins/Stats/Helpers/StatManager.cs index ad7f01501..fa12ca833 100644 --- a/Plugins/Stats/Helpers/StatManager.cs +++ b/Plugins/Stats/Helpers/StatManager.cs @@ -35,7 +35,7 @@ namespace IW4MAdmin.Plugins.Stats.Helpers { Servers = new ConcurrentDictionary(); ContextThreads = new ConcurrentDictionary(); - Log = mgr.GetLogger(); + Log = mgr.GetLogger(0); Manager = mgr; OnProcessingPenalty = new SemaphoreSlim(1, 1); } diff --git a/Plugins/Welcome/Plugin.cs b/Plugins/Welcome/Plugin.cs index 3506c0814..91119d617 100644 --- a/Plugins/Welcome/Plugin.cs +++ b/Plugins/Welcome/Plugin.cs @@ -108,7 +108,7 @@ namespace IW4MAdmin.Plugins.Welcome catch (Exception) { - joining.CurrentServer.Manager.GetLogger().WriteError("Could not open file Plugins\\GeoIP.dat for Welcome Plugin"); + joining.CurrentServer.Logger.WriteError("Could not open file Plugins\\GeoIP.dat for Welcome Plugin"); } msg = msg.Replace("{{TimesConnected}}", TimesConnected(joining)); diff --git a/SharedLibraryCore/Commands/NativeCommands.cs b/SharedLibraryCore/Commands/NativeCommands.cs index 99094f46c..889e669ce 100644 --- a/SharedLibraryCore/Commands/NativeCommands.cs +++ b/SharedLibraryCore/Commands/NativeCommands.cs @@ -1126,7 +1126,7 @@ namespace SharedLibraryCore.Commands { } - public override async Task ExecuteAsync(GameEvent E) + public override Task ExecuteAsync(GameEvent E) { var gameserverProcesses = System.Diagnostics.Process.GetProcessesByName("iw4x"); @@ -1180,10 +1180,12 @@ namespace SharedLibraryCore.Commands E.Origin.Tell("Could not kill server process"); E.Owner.Logger.WriteDebug("Unable to kill process"); E.Owner.Logger.WriteDebug($"Exception: {e.Message}"); - return; + return Task.CompletedTask; } } } + + return Task.CompletedTask; } } diff --git a/SharedLibraryCore/Interfaces/IManager.cs b/SharedLibraryCore/Interfaces/IManager.cs index 0345c44b9..fb95e3383 100644 --- a/SharedLibraryCore/Interfaces/IManager.cs +++ b/SharedLibraryCore/Interfaces/IManager.cs @@ -13,7 +13,7 @@ namespace SharedLibraryCore.Interfaces Task Init(); void Start(); void Stop(); - ILogger GetLogger(); + ILogger GetLogger(int serverId); IList GetServers(); IList GetCommands(); IList GetMessageTokens(); diff --git a/SharedLibraryCore/Objects/Player.cs b/SharedLibraryCore/Objects/Player.cs index 568f26839..955803928 100644 --- a/SharedLibraryCore/Objects/Player.cs +++ b/SharedLibraryCore/Objects/Player.cs @@ -339,7 +339,6 @@ namespace SharedLibraryCore.Objects if (sender.Level <= this.Level) { e.FailReason = GameEvent.EventFailReason.Permission; - return e; } sender.CurrentServer.Manager.GetEventHandler().AddEvent(e); @@ -367,7 +366,6 @@ namespace SharedLibraryCore.Objects if (sender.Level <= this.Level) { e.FailReason = GameEvent.EventFailReason.Permission; - return e; } sender.CurrentServer.Manager.GetEventHandler().AddEvent(e); diff --git a/SharedLibraryCore/PluginImporter.cs b/SharedLibraryCore/PluginImporter.cs index cf913e3bc..fd89ac535 100644 --- a/SharedLibraryCore/PluginImporter.cs +++ b/SharedLibraryCore/PluginImporter.cs @@ -34,7 +34,7 @@ namespace SharedLibraryCore.Plugins if (dllFileNames.Length == 0 && scriptFileNames.Length == 0) { - Manager.GetLogger().WriteDebug(Utilities.CurrentLocalization.LocalizationIndex["PLUGIN_IMPORTER_NOTFOUND"]); + Manager.GetLogger(0).WriteDebug(Utilities.CurrentLocalization.LocalizationIndex["PLUGIN_IMPORTER_NOTFOUND"]); return true; } @@ -43,7 +43,7 @@ namespace SharedLibraryCore.Plugins { var plugin = new ScriptPlugin(fileName); plugin.Initialize(Manager).Wait(); - Manager.GetLogger().WriteDebug($"Loaded script plugin \"{ plugin.Name }\" [{plugin.Version}]"); + Manager.GetLogger(0).WriteDebug($"Loaded script plugin \"{ plugin.Name }\" [{plugin.Version}]"); ActivePlugins.Add(plugin); } @@ -66,7 +66,7 @@ namespace SharedLibraryCore.Plugins Object commandObject = Activator.CreateInstance(assemblyType); Command newCommand = (Command)commandObject; ActiveCommands.Add(newCommand); - Manager.GetLogger().WriteDebug($"{Utilities.CurrentLocalization.LocalizationIndex["PLUGIN_IMPORTER_REGISTERCMD"]} \"{newCommand.Name}\""); + Manager.GetLogger(0).WriteDebug($"{Utilities.CurrentLocalization.LocalizationIndex["PLUGIN_IMPORTER_REGISTERCMD"]} \"{newCommand.Name}\""); LoadedCommands++; continue; } @@ -82,18 +82,18 @@ namespace SharedLibraryCore.Plugins { ActivePlugins.Add(newNotify); PluginAssemblies.Add(Plugin); - Manager.GetLogger().WriteDebug($"Loaded plugin \"{ newNotify.Name }\" [{newNotify.Version}]"); + Manager.GetLogger(0).WriteDebug($"Loaded plugin \"{ newNotify.Name }\" [{newNotify.Version}]"); } } catch (Exception E) { - Manager.GetLogger().WriteWarning($"{Utilities.CurrentLocalization.LocalizationIndex["PLUGIN_IMPORTER_ERROR"]} {Plugin.Location} - {E.Message}"); + Manager.GetLogger(0).WriteWarning($"{Utilities.CurrentLocalization.LocalizationIndex["PLUGIN_IMPORTER_ERROR"]} {Plugin.Location} - {E.Message}"); } } } } - Manager.GetLogger().WriteInfo($"Loaded {ActivePlugins.Count} plugins and registered {LoadedCommands} commands."); + Manager.GetLogger(0).WriteInfo($"Loaded {ActivePlugins.Count} plugins and registered {LoadedCommands} commands."); return true; } } diff --git a/SharedLibraryCore/ScriptPlugin.cs b/SharedLibraryCore/ScriptPlugin.cs index 585c7c7bd..3906a8592 100644 --- a/SharedLibraryCore/ScriptPlugin.cs +++ b/SharedLibraryCore/ScriptPlugin.cs @@ -42,8 +42,8 @@ namespace SharedLibraryCore } catch (Exception ex) { - Manager.GetLogger().WriteError($"{Utilities.CurrentLocalization.LocalizationIndex["PLUGIN_IMPORTER_ERROR"]} {Name}"); - Manager.GetLogger().WriteDebug(ex.Message); + Manager.GetLogger(0).WriteError($"{Utilities.CurrentLocalization.LocalizationIndex["PLUGIN_IMPORTER_ERROR"]} {Name}"); + Manager.GetLogger(0).WriteDebug(ex.Message); } } diff --git a/SharedLibraryCore/Server.cs b/SharedLibraryCore/Server.cs index c61d2eaf7..8dd2bcc70 100644 --- a/SharedLibraryCore/Server.cs +++ b/SharedLibraryCore/Server.cs @@ -32,7 +32,7 @@ namespace SharedLibraryCore IP = config.IPAddress; Port = config.Port; Manager = mgr; - Logger = Manager.GetLogger(); + Logger = Manager.GetLogger(this.GetHashCode()); ServerConfig = config; RemoteConnection = new RCon.Connection(IP, Port, Password, Logger); diff --git a/WebfrontCore/Controllers/HomeController.cs b/WebfrontCore/Controllers/HomeController.cs index 88bb79933..c5b92349f 100644 --- a/WebfrontCore/Controllers/HomeController.cs +++ b/WebfrontCore/Controllers/HomeController.cs @@ -23,9 +23,9 @@ namespace WebfrontCore.Controllers public IActionResult Error() { var exceptionFeature = HttpContext.Features.Get(); - Manager.GetLogger().WriteError($"[Webfront] {exceptionFeature.Error.Message}"); - Manager.GetLogger().WriteDebug(exceptionFeature.Path); - Manager.GetLogger().WriteDebug(exceptionFeature.Error.StackTrace); + Manager.GetLogger(0).WriteError($"[Webfront] {exceptionFeature.Error.Message}"); + Manager.GetLogger(0).WriteDebug(exceptionFeature.Path); + Manager.GetLogger(0).WriteDebug(exceptionFeature.Error.StackTrace); ViewBag.Description = Localization["WEBFRONT_ERROR_DESC"]; ViewBag.Title = Localization["WEBFRONT_ERROR_TITLE"];