diff --git a/ProjectLighthouse/Controllers/GameApi/LoginController.cs b/ProjectLighthouse/Controllers/GameApi/LoginController.cs index c8650a0a..e5575ee2 100644 --- a/ProjectLighthouse/Controllers/GameApi/LoginController.cs +++ b/ProjectLighthouse/Controllers/GameApi/LoginController.cs @@ -3,7 +3,6 @@ using System.IO; using System.Linq; using System.Net; using System.Threading.Tasks; -using Kettu; using LBPUnion.ProjectLighthouse.Helpers; using LBPUnion.ProjectLighthouse.Logging; using LBPUnion.ProjectLighthouse.Types; @@ -46,14 +45,14 @@ public class LoginController : ControllerBase if (npTicket == null) { - Logger.Log("npTicket was null, rejecting login", LoggerLevelLogin.Instance); + Logger.LogWarn("npTicket was null, rejecting login", "Login"); return this.BadRequest(); } IPAddress? remoteIpAddress = this.HttpContext.Connection.RemoteIpAddress; if (remoteIpAddress == null) { - Logger.Log("unable to determine ip, rejecting login", LoggerLevelLogin.Instance); + Logger.LogWarn("unable to determine ip, rejecting login", "Login"); return this.StatusCode(403, ""); // 403 probably isnt the best status code for this, but whatever } @@ -69,7 +68,7 @@ public class LoginController : ControllerBase token = await this.database.AuthenticateUser(npTicket, ipAddress); if (token == null) { - Logger.Log($"Unable to find/generate a token for username {npTicket.Username}", LoggerLevelLogin.Instance); + Logger.LogWarn($"Unable to find/generate a token for username {npTicket.Username}", "Login"); return this.StatusCode(403, ""); // If not, then 403. } } @@ -78,7 +77,7 @@ public class LoginController : ControllerBase if (user == null || user.Banned) { - Logger.Log($"Unable to find user {npTicket.Username} from token", LoggerLevelLogin.Instance); + Logger.LogError($"Unable to find user {npTicket.Username} from token", "Login"); return this.StatusCode(403, ""); } @@ -95,7 +94,7 @@ public class LoginController : ControllerBase DeniedAuthenticationHelper.AddAttempt(ipAddressAndName); await this.database.SaveChangesAsync(); - Logger.Log($"Too many recent denied logins from user {user.Username}, rejecting login", LoggerLevelLogin.Instance); + Logger.LogWarn($"Too many recent denied logins from user {user.Username}, rejecting login", "Login"); return this.StatusCode(403, ""); } } @@ -127,11 +126,11 @@ public class LoginController : ControllerBase if (!token.Approved) { - Logger.Log($"Token unapproved for user {user.Username}, rejecting login", LoggerLevelLogin.Instance); + Logger.LogWarn($"Token unapproved for user {user.Username}, rejecting login", "Login"); return this.StatusCode(403, ""); } - Logger.Log($"Successfully logged in user {user.Username} as {token.GameVersion} client", LoggerLevelLogin.Instance); + Logger.LogSuccess($"Successfully logged in user {user.Username} as {token.GameVersion} client", "Login"); // After this point we are now considering this session as logged in. // We just logged in with the token. Mark it as used so someone else doesnt try to use it, diff --git a/ProjectLighthouse/Controllers/GameApi/Matching/MatchController.cs b/ProjectLighthouse/Controllers/GameApi/Matching/MatchController.cs index a2124f02..3d6c13be 100644 --- a/ProjectLighthouse/Controllers/GameApi/Matching/MatchController.cs +++ b/ProjectLighthouse/Controllers/GameApi/Matching/MatchController.cs @@ -5,7 +5,6 @@ using System.IO; using System.Linq; using System.Text.Json; using System.Threading.Tasks; -using Kettu; using LBPUnion.ProjectLighthouse.Helpers; using LBPUnion.ProjectLighthouse.Helpers.Extensions; using LBPUnion.ProjectLighthouse.Logging; @@ -48,7 +47,7 @@ public class MatchController : ControllerBase if (bodyString.Length == 0 || bodyString[0] != '[') return this.BadRequest(); - Logger.Log("Received match data: " + bodyString, LoggerLevelMatch.Instance); + Logger.LogInfo("Received match data: " + bodyString, "Match"); IMatchData? matchData; try @@ -57,20 +56,19 @@ public class MatchController : ControllerBase } catch(Exception e) { - Logger.Log("Exception while parsing matchData: ", LoggerLevelMatch.Instance); - string[] lines = e.ToDetailedException().Split("\n"); - foreach (string line in lines) Logger.Log(line, LoggerLevelMatch.Instance); + Logger.LogError("Exception while parsing matchData: ", "Match"); + Logger.LogError(e.ToDetailedException(), "Match"); return this.BadRequest(); } if (matchData == null) { - Logger.Log("Could not parse match data: matchData is null", LoggerLevelMatch.Instance); + Logger.LogError($"Could not parse match data: {nameof(matchData)} is null", "Match"); return this.BadRequest(); } - Logger.Log($"Parsed match from {user.Username} (type: {matchData.GetType()})", LoggerLevelMatch.Instance); + Logger.LogError($"Parsed match from {user.Username} (type: {matchData.GetType()})", "Match"); #endregion diff --git a/ProjectLighthouse/Controllers/GameApi/MessageController.cs b/ProjectLighthouse/Controllers/GameApi/MessageController.cs index b51c632b..cef39b87 100644 --- a/ProjectLighthouse/Controllers/GameApi/MessageController.cs +++ b/ProjectLighthouse/Controllers/GameApi/MessageController.cs @@ -1,7 +1,6 @@ #nullable enable using System.IO; using System.Threading.Tasks; -using Kettu; using LBPUnion.ProjectLighthouse.Helpers; using LBPUnion.ProjectLighthouse.Logging; using LBPUnion.ProjectLighthouse.Types; @@ -99,7 +98,7 @@ along with this program. If not, see ."; string scannedText = CensorHelper.ScanMessage(response); - Logger.Log($"{user.Username}: {response} / {scannedText}", LoggerLevelFilter.Instance); + Logger.LogInfo($"{user.Username}: {response} / {scannedText}", "Filter"); return this.Ok(scannedText); } diff --git a/ProjectLighthouse/Controllers/GameApi/Resources/PhotosController.cs b/ProjectLighthouse/Controllers/GameApi/Resources/PhotosController.cs index aee276de..7ffe5116 100644 --- a/ProjectLighthouse/Controllers/GameApi/Resources/PhotosController.cs +++ b/ProjectLighthouse/Controllers/GameApi/Resources/PhotosController.cs @@ -6,7 +6,6 @@ using System.Linq; using System.Threading.Tasks; using System.Xml.Serialization; using Discord; -using Kettu; using LBPUnion.ProjectLighthouse.Helpers; using LBPUnion.ProjectLighthouse.Logging; using LBPUnion.ProjectLighthouse.Serialization; @@ -68,7 +67,7 @@ public class PhotosController : ControllerBase if (subject.User == null) continue; subject.UserId = subject.User.UserId; - Logger.Log($"Adding PhotoSubject (userid {subject.UserId}) to db", LoggerLevelPhotos.Instance); + Logger.LogDebug($"Adding PhotoSubject (userid {subject.UserId}) to db", "Photos"); this.database.PhotoSubjects.Add(subject); } @@ -88,7 +87,7 @@ public class PhotosController : ControllerBase // photo.Slot = await this.database.Slots.FirstOrDefaultAsync(s => s.SlotId == photo.SlotId); - Logger.Log($"Adding PhotoSubjectCollection ({photo.PhotoSubjectCollection}) to photo", LoggerLevelPhotos.Instance); + Logger.LogDebug($"Adding PhotoSubjectCollection ({photo.PhotoSubjectCollection}) to photo", "Photos"); this.database.Photos.Add(photo); @@ -168,4 +167,4 @@ public class PhotosController : ControllerBase await this.database.SaveChangesAsync(); return this.Ok(); } -} +} \ No newline at end of file diff --git a/ProjectLighthouse/Controllers/GameApi/Resources/ResourcesController.cs b/ProjectLighthouse/Controllers/GameApi/Resources/ResourcesController.cs index 75fc15dc..1ba1ee2e 100644 --- a/ProjectLighthouse/Controllers/GameApi/Resources/ResourcesController.cs +++ b/ProjectLighthouse/Controllers/GameApi/Resources/ResourcesController.cs @@ -3,7 +3,6 @@ using System.IO; using System.Linq; using System.Threading.Tasks; using System.Xml.Serialization; -using Kettu; using LBPUnion.ProjectLighthouse.Helpers; using LBPUnion.ProjectLighthouse.Logging; using LBPUnion.ProjectLighthouse.Serialization; @@ -97,27 +96,23 @@ public class ResourcesController : ControllerBase // lbp treats code 409 as success and as an indicator that the file is already present if (FileHelper.ResourceExists(hash)) this.Conflict(); - Logger.Log($"Processing resource upload (hash: {hash})", LoggerLevelResources.Instance); + Logger.LogInfo($"Processing resource upload (hash: {hash})", "Resources"); LbpFile file = new(await BinaryHelper.ReadFromPipeReader(this.Request.BodyReader)); if (!FileHelper.IsFileSafe(file)) { - Logger.Log($"File is unsafe (hash: {hash}, type: {file.FileType})", LoggerLevelResources.Instance); + Logger.LogWarn($"File is unsafe (hash: {hash}, type: {file.FileType})", "Resources"); return this.Conflict(); } string calculatedHash = file.Hash; if (calculatedHash != hash) { - Logger.Log - ( - $"File hash does not match the uploaded file! (hash: {hash}, calculatedHash: {calculatedHash}, type: {file.FileType})", - LoggerLevelResources.Instance - ); + Logger.LogWarn($"File hash does not match the uploaded file! (hash: {hash}, calculatedHash: {calculatedHash}, type: {file.FileType})", "Resources"); return this.Conflict(); } - Logger.Log($"File is OK! (hash: {hash}, type: {file.FileType})", LoggerLevelResources.Instance); + Logger.LogSuccess($"File is OK! (hash: {hash}, type: {file.FileType})", "Resources"); await IOFile.WriteAllBytesAsync(path, file.Data); return this.Ok(); } diff --git a/ProjectLighthouse/Controllers/GameApi/Slots/CollectionController.cs b/ProjectLighthouse/Controllers/GameApi/Slots/CollectionController.cs index 7c08add8..3386aabb 100644 --- a/ProjectLighthouse/Controllers/GameApi/Slots/CollectionController.cs +++ b/ProjectLighthouse/Controllers/GameApi/Slots/CollectionController.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -using Kettu; using LBPUnion.ProjectLighthouse.Helpers; using LBPUnion.ProjectLighthouse.Logging; using LBPUnion.ProjectLighthouse.Serialization; @@ -85,7 +84,7 @@ public class CollectionController : ControllerBase Category? category = CollectionHelper.Categories.FirstOrDefault(c => c.Endpoint == endpointName); if (category == null) return this.NotFound(); - Logger.Log("Found category " + category, LoggerLevelCategory.Instance); + Logger.LogDebug("Found category " + category, "Category"); List slots; int totalSlots; diff --git a/ProjectLighthouse/Controllers/Website/SlotPageController.cs b/ProjectLighthouse/Controllers/Website/SlotPageController.cs index f9ccfc91..1df0e966 100644 --- a/ProjectLighthouse/Controllers/Website/SlotPageController.cs +++ b/ProjectLighthouse/Controllers/Website/SlotPageController.cs @@ -1,6 +1,5 @@ #nullable enable using System.Threading.Tasks; -using Kettu; using LBPUnion.ProjectLighthouse.Helpers; using LBPUnion.ProjectLighthouse.Logging; using LBPUnion.ProjectLighthouse.Types; @@ -45,14 +44,14 @@ public class SlotPageController : ControllerBase if (msg == null) { - Logger.Log($"Refusing to post comment from {user.UserId} on user {id}, {nameof(msg)} is null", LoggerLevelComments.Instance); + Logger.LogError($"Refusing to post comment from {user.UserId} on user {id}, {nameof(msg)} is null", "Comments"); return this.Redirect("~/slot/" + id); } msg = SanitizationHelper.SanitizeString(msg); await this.database.PostComment(user, id, CommentType.Level, msg); - Logger.Log($"Posted comment from {user.UserId}: \"{msg}\" on user {id}", LoggerLevelComments.Instance); + Logger.LogSuccess($"Posted comment from {user.UserId}: \"{msg}\" on user {id}", "Comments"); return this.Redirect("~/slot/" + id); } diff --git a/ProjectLighthouse/Controllers/Website/UserPageController.cs b/ProjectLighthouse/Controllers/Website/UserPageController.cs index a5bb7969..fbb35518 100644 --- a/ProjectLighthouse/Controllers/Website/UserPageController.cs +++ b/ProjectLighthouse/Controllers/Website/UserPageController.cs @@ -1,9 +1,8 @@ #nullable enable using System.Threading.Tasks; -using Kettu; +using LBPUnion.ProjectLighthouse.Helpers; using LBPUnion.ProjectLighthouse.Logging; using LBPUnion.ProjectLighthouse.Types; -using LBPUnion.ProjectLighthouse.Helpers; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; @@ -39,14 +38,14 @@ public class UserPageController : ControllerBase if (msg == null) { - Logger.Log($"Refusing to post comment from {user.UserId} on user {id}, {nameof(msg)} is null", LoggerLevelComments.Instance); + Logger.LogError($"Refusing to post comment from {user.UserId} on user {id}, {nameof(msg)} is null", "Comments"); return this.Redirect("~/user/" + id); } msg = SanitizationHelper.SanitizeString(msg); await this.database.PostComment(user, id, CommentType.Profile, msg); - Logger.Log($"Posted comment from {user.UserId}: \"{msg}\" on user {id}", LoggerLevelComments.Instance); + Logger.LogSuccess($"Posted comment from {user.UserId}: \"{msg}\" on user {id}", "Comments"); return this.Redirect("~/user/" + id); } diff --git a/ProjectLighthouse/Helpers/Extensions/AspLogLevelExtensions.cs b/ProjectLighthouse/Helpers/Extensions/AspLogLevelExtensions.cs new file mode 100644 index 00000000..4164fd66 --- /dev/null +++ b/ProjectLighthouse/Helpers/Extensions/AspLogLevelExtensions.cs @@ -0,0 +1,22 @@ +using LBPUnion.ProjectLighthouse.Logging; +using AspLogLevel = Microsoft.Extensions.Logging.LogLevel; + +namespace LBPUnion.ProjectLighthouse.Helpers.Extensions; + +public static class AspLogLevelExtensions +{ + public static LogLevel ToLighthouseLevel(this AspLogLevel level) + { + return level switch + { + AspLogLevel.Trace => LogLevel.Debug, + AspLogLevel.Debug => LogLevel.Debug, + AspLogLevel.Information => LogLevel.Info, + AspLogLevel.Warning => LogLevel.Warning, + AspLogLevel.Error => LogLevel.Error, + AspLogLevel.Critical => LogLevel.Error, + AspLogLevel.None => LogLevel.Info, + _ => LogLevel.Info, + }; + } +} \ No newline at end of file diff --git a/ProjectLighthouse/Helpers/Extensions/ConsoleColorExtensions.cs b/ProjectLighthouse/Helpers/Extensions/ConsoleColorExtensions.cs new file mode 100644 index 00000000..68375db2 --- /dev/null +++ b/ProjectLighthouse/Helpers/Extensions/ConsoleColorExtensions.cs @@ -0,0 +1,20 @@ +using System; + +namespace LBPUnion.ProjectLighthouse.Helpers.Extensions; + +internal static class ConsoleColorExtensions +{ + internal static ConsoleColor ToDark(this ConsoleColor color) + => color switch + { + ConsoleColor.Blue => ConsoleColor.DarkBlue, + ConsoleColor.Cyan => ConsoleColor.DarkCyan, + ConsoleColor.Green => ConsoleColor.DarkGreen, + ConsoleColor.Gray => ConsoleColor.DarkGray, + ConsoleColor.Magenta => ConsoleColor.DarkMagenta, + ConsoleColor.Red => ConsoleColor.DarkRed, + ConsoleColor.White => ConsoleColor.Gray, + ConsoleColor.Yellow => ConsoleColor.DarkYellow, + _ => color, + }; +} \ No newline at end of file diff --git a/ProjectLighthouse/Helpers/Extensions/LogLevelExtensions.cs b/ProjectLighthouse/Helpers/Extensions/LogLevelExtensions.cs new file mode 100644 index 00000000..7296f416 --- /dev/null +++ b/ProjectLighthouse/Helpers/Extensions/LogLevelExtensions.cs @@ -0,0 +1,18 @@ +using System; +using LBPUnion.ProjectLighthouse.Logging; + +namespace LBPUnion.ProjectLighthouse.Helpers.Extensions; + +public static class LogLevelExtensions +{ + public static ConsoleColor ToColor(this LogLevel level) + => level switch + { + LogLevel.Debug => ConsoleColor.Magenta, + LogLevel.Error => ConsoleColor.Red, + LogLevel.Warning => ConsoleColor.Yellow, + LogLevel.Info => ConsoleColor.White, + LogLevel.Success => ConsoleColor.Green, + _ => ConsoleColor.White, + }; +} \ No newline at end of file diff --git a/ProjectLighthouse/Helpers/FileHelper.cs b/ProjectLighthouse/Helpers/FileHelper.cs index ede1d308..3d9b1f74 100644 --- a/ProjectLighthouse/Helpers/FileHelper.cs +++ b/ProjectLighthouse/Helpers/FileHelper.cs @@ -6,7 +6,6 @@ using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; -using Kettu; using LBPUnion.ProjectLighthouse.Logging; using LBPUnion.ProjectLighthouse.Types.Files; using LBPUnion.ProjectLighthouse.Types.Settings; @@ -115,8 +114,7 @@ public static class FileHelper EnsureDirectoryCreated(Path.Combine(Environment.CurrentDirectory, "png")); if (Directory.Exists("r")) { - Logger.Log - ("Converting all textures to PNG. This may take a while if this is the first time running this operation...", LoggerLevelStartup.Instance); + Logger.LogInfo("Converting all textures to PNG. This may take a while if this is the first time running this operation...", "Startup"); ConcurrentQueue fileQueue = new(); diff --git a/ProjectLighthouse/Helpers/InfluxHelper.cs b/ProjectLighthouse/Helpers/InfluxHelper.cs index 0799047d..07980b9c 100644 --- a/ProjectLighthouse/Helpers/InfluxHelper.cs +++ b/ProjectLighthouse/Helpers/InfluxHelper.cs @@ -5,7 +5,7 @@ using System.Threading; using System.Threading.Tasks; using InfluxDB.Client; using InfluxDB.Client.Writes; -using Kettu; +using LBPUnion.ProjectLighthouse.Helpers.Extensions; using LBPUnion.ProjectLighthouse.Logging; using LBPUnion.ProjectLighthouse.Types; using LBPUnion.ProjectLighthouse.Types.Settings; @@ -49,9 +49,8 @@ public static class InfluxHelper } catch(Exception e) { - Logger.Log("Exception while logging: ", LoggerLevelInflux.Instance); - - foreach (string line in e.ToString().Split("\n")) Logger.Log(line, LoggerLevelInflux.Instance); + Logger.LogError("Exception while logging: ", "InfluxDB"); + Logger.LogError(e.ToDetailedException(), "InfluxDB"); } } @@ -59,7 +58,7 @@ public static class InfluxHelper public static async Task StartLogging() { await Client.ReadyAsync(); - Logger.Log("InfluxDB is now ready.", LoggerLevelInflux.Instance); + Logger.LogSuccess("InfluxDB is now ready.", "InfluxDB"); Thread t = new ( delegate() @@ -72,9 +71,8 @@ public static class InfluxHelper } catch(Exception e) { - Logger.Log("Exception while running log thread: ", LoggerLevelInflux.Instance); - - foreach (string line in e.ToString().Split("\n")) Logger.Log(line, LoggerLevelInflux.Instance); + Logger.LogError("Exception while running log thread: ", "InfluxDB"); + Logger.LogError(e.ToDetailedException(), "InfluxDB"); } Thread.Sleep(60000); diff --git a/ProjectLighthouse/Helpers/RoomHelper.cs b/ProjectLighthouse/Helpers/RoomHelper.cs index 03a52a82..c54ee939 100644 --- a/ProjectLighthouse/Helpers/RoomHelper.cs +++ b/ProjectLighthouse/Helpers/RoomHelper.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Threading.Tasks; -using Kettu; using LBPUnion.ProjectLighthouse.Logging; using LBPUnion.ProjectLighthouse.Types; using LBPUnion.ProjectLighthouse.Types.Levels; @@ -46,7 +45,7 @@ public class RoomHelper { if (roomVersion == GameVersion.LittleBigPlanet1 || roomVersion == GameVersion.LittleBigPlanetPSP) { - Logger.Log($"Returning null for FindBestRoom, game ({roomVersion}) does not support dive in (should never happen?)", LoggerLevelMatch.Instance); + Logger.LogError($"Returning null for FindBestRoom, game ({roomVersion}) does not support dive in (should never happen?)", "Match"); return null; } @@ -140,7 +139,7 @@ public class RoomHelper }, }; - Logger.Log($"Found a room (id: {room.RoomId}) for user {user?.Username ?? "null"} (id: {user?.UserId ?? -1})", LoggerLevelMatch.Instance); + Logger.LogSuccess($"Found a room (id: {room.RoomId}) for user {user?.Username ?? "null"} (id: {user?.UserId ?? -1})", "Match"); return response; } @@ -173,7 +172,7 @@ public class RoomHelper CleanupRooms(room.Host, room); lock(Rooms) Rooms.Add(room); - Logger.Log($"Created room (id: {room.RoomId}) for host {room.Host.Username} (id: {room.Host.UserId})", LoggerLevelMatch.Instance); + Logger.LogInfo($"Created room (id: {room.RoomId}) for host {room.Host.Username} (id: {room.Host.UserId})", "Match"); return room; } @@ -238,7 +237,7 @@ public class RoomHelper if (roomCountBeforeCleanup != roomCountAfterCleanup) { - Logger.Log($"Cleaned up {roomCountBeforeCleanup - roomCountAfterCleanup} rooms.", LoggerLevelMatch.Instance); + Logger.LogDebug($"Cleaned up {roomCountBeforeCleanup - roomCountAfterCleanup} rooms.", "Match"); } } } diff --git a/ProjectLighthouse/Helpers/SwaggerFilter.cs b/ProjectLighthouse/Helpers/SwaggerFilter.cs index 90b333c9..8d1e7e4b 100644 --- a/ProjectLighthouse/Helpers/SwaggerFilter.cs +++ b/ProjectLighthouse/Helpers/SwaggerFilter.cs @@ -5,6 +5,14 @@ using Swashbuckle.AspNetCore.SwaggerGen; namespace LBPUnion.ProjectLighthouse.Helpers; +/// +/// +/// A filter for the swagger documentation endpoint. +/// +/// +/// Makes sure that only endpoints under /api/v1 show up. +/// +/// public class SwaggerFilter : IDocumentFilter { public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context) diff --git a/ProjectLighthouse/Helpers/VersionHelper.cs b/ProjectLighthouse/Helpers/VersionHelper.cs index c32d8aa8..6e91087e 100644 --- a/ProjectLighthouse/Helpers/VersionHelper.cs +++ b/ProjectLighthouse/Helpers/VersionHelper.cs @@ -1,5 +1,4 @@ using System.Linq; -using Kettu; using LBPUnion.ProjectLighthouse.Logging; using LBPUnion.ProjectLighthouse.Types.Settings; @@ -30,11 +29,11 @@ public static class VersionHelper } catch { - Logger.Log + Logger.LogError ( "Project Lighthouse was built incorrectly. Please make sure git is available when building. " + "Because of this, you will not be notified of updates.", - LoggerLevelStartup.Instance + "Startup" ); CommitHash = "invalid"; Branch = "invalid"; @@ -43,11 +42,11 @@ public static class VersionHelper if (IsDirty) { - Logger.Log + Logger.LogWarn ( "This is a modified version of Project Lighthouse. " + "Please make sure you are properly disclosing the source code to any users who may be using this instance.", - LoggerLevelStartup.Instance + "Startup" ); CanCheckForUpdates = false; } @@ -67,6 +66,6 @@ public static class VersionHelper #elif RELEASE "Release"; #else - "Unknown"; + "Unknown"; #endif } \ No newline at end of file diff --git a/ProjectLighthouse/Logging/AspNetToKettuLogger.cs b/ProjectLighthouse/Logging/AspNetToKettuLogger.cs deleted file mode 100644 index 113af70a..00000000 --- a/ProjectLighthouse/Logging/AspNetToKettuLogger.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System; -using Kettu; -using LBPUnion.ProjectLighthouse.Helpers.Extensions; -using Microsoft.Extensions.Logging; - -namespace LBPUnion.ProjectLighthouse.Logging; - -public class AspNetToKettuLogger : ILogger -{ - public IDisposable BeginScope(TState state) => NullScope.Instance; - public bool IsEnabled(LogLevel logLevel) => true; - - public void Log(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) - { - LoggerLevel loggerLevel = new LoggerLevelAspNet(logLevel); - - Logger.Log(state.ToString(), loggerLevel); - if (exception == null) return; - - string[] lines = exception.ToDetailedException().Replace("\r", "").Split("\n"); - foreach (string line in lines) Logger.Log(line, loggerLevel); - } -} \ No newline at end of file diff --git a/ProjectLighthouse/Logging/AspNetToKettuLoggerProvider.cs b/ProjectLighthouse/Logging/AspNetToKettuLoggerProvider.cs deleted file mode 100644 index ea1fd976..00000000 --- a/ProjectLighthouse/Logging/AspNetToKettuLoggerProvider.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using Microsoft.Extensions.Logging; - -namespace LBPUnion.ProjectLighthouse.Logging; - -[ProviderAlias("Kettu")] -public class AspNetToKettuLoggerProvider : ILoggerProvider, IDisposable -{ - public void Dispose() - { - GC.SuppressFinalize(this); - } - - public ILogger CreateLogger(string categoryName) => new AspNetToKettuLogger(); -} \ No newline at end of file diff --git a/ProjectLighthouse/Logging/LighthouseFileLogger.cs b/ProjectLighthouse/Logging/LighthouseFileLogger.cs deleted file mode 100644 index 4e881771..00000000 --- a/ProjectLighthouse/Logging/LighthouseFileLogger.cs +++ /dev/null @@ -1,31 +0,0 @@ -using System; -using System.IO; -using Kettu; -using LBPUnion.ProjectLighthouse.Helpers; -using LBPUnion.ProjectLighthouse.Helpers.Extensions; - -namespace LBPUnion.ProjectLighthouse.Logging; - -public class LighthouseFileLogger : LoggerBase -{ - private static readonly string logsDirectory = Path.Combine(Environment.CurrentDirectory, "logs"); - public override bool AllowMultiple => false; - - public override void Send(LoggerLine line) - { - FileHelper.EnsureDirectoryCreated(logsDirectory); - - string channel = string.IsNullOrEmpty(line.LoggerLevel.Channel) ? "" : $"[{line.LoggerLevel.Channel}] "; - - string contentFile = $"{channel}{line.LineData}\n"; - string contentAll = $"[{$"{line.LoggerLevel.Name} {channel}".TrimEnd()}] {line.LineData}\n"; - - try - { - File.AppendAllText(Path.Combine(logsDirectory, line.LoggerLevel.Name.ToFileName() + ".log"), contentFile); - File.AppendAllText(Path.Combine(logsDirectory, "all.log"), contentAll); - } - catch(IOException) {} // windows, ya goofed - - } -} \ No newline at end of file diff --git a/ProjectLighthouse/Logging/LogLevel.cs b/ProjectLighthouse/Logging/LogLevel.cs new file mode 100644 index 00000000..ea5b22b8 --- /dev/null +++ b/ProjectLighthouse/Logging/LogLevel.cs @@ -0,0 +1,10 @@ +namespace LBPUnion.ProjectLighthouse.Logging; + +public enum LogLevel +{ + Success = 0, + Info = 1, + Warning = 2, + Error = 3, + Debug = 4, +} \ No newline at end of file diff --git a/ProjectLighthouse/Logging/LogLine.cs b/ProjectLighthouse/Logging/LogLine.cs new file mode 100644 index 00000000..ec7bcda2 --- /dev/null +++ b/ProjectLighthouse/Logging/LogLine.cs @@ -0,0 +1,9 @@ +namespace LBPUnion.ProjectLighthouse.Logging; + +public struct LogLine +{ + public LogTrace Trace; + public LogLevel Level; + public string Area; + public string Message; +} \ No newline at end of file diff --git a/ProjectLighthouse/Logging/LogTrace.cs b/ProjectLighthouse/Logging/LogTrace.cs new file mode 100644 index 00000000..3e011551 --- /dev/null +++ b/ProjectLighthouse/Logging/LogTrace.cs @@ -0,0 +1,7 @@ +namespace LBPUnion.ProjectLighthouse.Logging; + +public struct LogTrace +{ + public string Name; + public short Line; +} \ No newline at end of file diff --git a/ProjectLighthouse/Logging/Logger.cs b/ProjectLighthouse/Logging/Logger.cs new file mode 100644 index 00000000..45dc99ec --- /dev/null +++ b/ProjectLighthouse/Logging/Logger.cs @@ -0,0 +1,174 @@ +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.IO; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace LBPUnion.ProjectLighthouse.Logging; + +// TODO: make into singleton, but with ability to also have instances +// Logger.LogSuccess() should still work and all, but ideally i'm also able to have another instance and do: +// Logger logger = new(); +// logger.LogSuccess(); +// I should also be able to access the log queue. +// This functionality is going to be used in the admin panel to get the output of commands. +public static class Logger +{ + + #region Internals + + /// + /// A list of custom loggers to use. + /// + private static readonly List loggers = new(); + + public static void AddLogger(ILogger logger) + { + loggers.Add(logger); + LogSuccess("Initialized " + logger.GetType().Name, "Logger"); + } + + private static LogTrace getTrace() + { + const int depth = 6; + const int skipDepth = depth - 2; + + // Get the stacktrace for logging... + string trace = Environment.StackTrace.Split('\n', depth, StringSplitOptions.RemoveEmptyEntries).Skip(skipDepth).First(); + + trace = trace.TrimEnd('\r'); + trace = trace.Substring(trace.LastIndexOf(Path.DirectorySeparatorChar) + 1); + trace = trace.Replace(".cs:line ", ":"); + + string[] traceSplit = trace.Split(':'); // Split for formatting! + + short lineNumber; + + try + { + lineNumber = short.Parse(traceSplit[1]); + } + catch + { + lineNumber = -1; + } + + return new LogTrace + { + Name = traceSplit[0], + Line = lineNumber, + }; + } + + #endregion + + #region Queue + + /// + /// A queue for the logger. + /// + /// We use a queue because if two threads try to log something at the time they'll mess each other's printing up. + /// + /// + private static readonly ConcurrentQueue logQueue = new(); + + /// + /// Adds a to the queue. Only used internally. + /// + /// The logLine to send to the queue. + private static void queueLog(LogLine logLine) + { + logQueue.Enqueue(logLine); + } + + [SuppressMessage("ReSharper", "FunctionNeverReturns")] + static Logger() // Start queue thread on first Logger access + { + Task.Factory.StartNew + ( + () => + { + while (true) + { + bool logged = queueLoop(); + Thread.Sleep(logged ? 10 : 100); + // We wait 100ms if we dont log since it's less likely that the program logged again. + // If we did log, wait 10ms before looping again. + + // This is all so we use as little CPU as possible. This is an endless while loop, after all. + } + } + ); + } + + /// + /// A function used by the queue thread + /// + /// + private static bool queueLoop() + { + bool logged = false; + if (logQueue.TryDequeue(out LogLine line)) + { + logged = true; + + foreach (ILogger logger in loggers) + { + logger.Log(line); + } + } + + return logged; + } + + #endregion + + #region Logging functions + + public static void LogDebug(string text, string area = "") + { + #if DEBUG + Log(text, area, LogLevel.Debug); + #endif + } + + public static void LogSuccess(string text, string area = "") + { + Log(text, area, LogLevel.Success); + } + + public static void LogInfo(string text, string area = "") + { + Log(text, area, LogLevel.Info); + } + + public static void LogWarn(string text, string area = "") + { + Log(text, area, LogLevel.Warning); + } + + public static void LogError(string text, string area = "") + { + Log(text, area, LogLevel.Error); + } + + public static void Log(string text, string area, LogLevel level) + { + queueLog + ( + new LogLine + { + Level = level, + Message = text, + Area = area, + Trace = getTrace(), + } + ); + } + + #endregion + +} \ No newline at end of file diff --git a/ProjectLighthouse/Logging/LoggerBase.cs b/ProjectLighthouse/Logging/LoggerBase.cs new file mode 100644 index 00000000..a44c6939 --- /dev/null +++ b/ProjectLighthouse/Logging/LoggerBase.cs @@ -0,0 +1,6 @@ +namespace LBPUnion.ProjectLighthouse.Logging; + +public interface ILogger +{ + public void Log(LogLine line); +} \ No newline at end of file diff --git a/ProjectLighthouse/Logging/LoggerLevels.cs b/ProjectLighthouse/Logging/LoggerLevels.cs deleted file mode 100644 index 303dda97..00000000 --- a/ProjectLighthouse/Logging/LoggerLevels.cs +++ /dev/null @@ -1,86 +0,0 @@ -using Kettu; -using Microsoft.Extensions.Logging; - -namespace LBPUnion.ProjectLighthouse.Logging; - -public class LoggerLevelStartup : LoggerLevel -{ - public static readonly LoggerLevelStartup Instance = new(); - public override string Name => "Startup"; -} - -public class LoggerLevelDatabase : LoggerLevel -{ - public static readonly LoggerLevelDatabase Instance = new(); - public override string Name => "Database"; -} - -public class LoggerLevelHttp : LoggerLevel -{ - public static readonly LoggerLevelHttp Instance = new(); - public override string Name => "HTTP"; -} - -public class LoggerLevelFilter : LoggerLevel -{ - public static readonly LoggerLevelFilter Instance = new(); - public override string Name => "Filter"; -} - -public class LoggerLevelLogin : LoggerLevel -{ - public static readonly LoggerLevelLogin Instance = new(); - public override string Name => "Login"; -} - -public class LoggerLevelResources : LoggerLevel -{ - public static readonly LoggerLevelResources Instance = new(); - public override string Name => "Resources"; -} - -public class LoggerLevelMatch : LoggerLevel -{ - public static readonly LoggerLevelMatch Instance = new(); - public override string Name => "Match"; -} - -public class LoggerLevelPhotos : LoggerLevel -{ - public static readonly LoggerLevelPhotos Instance = new(); - public override string Name => "Photos"; -} - -public class LoggerLevelConfig : LoggerLevel -{ - public static readonly LoggerLevelConfig Instance = new(); - public override string Name => "Config"; -} - -public class LoggerLevelInflux : LoggerLevel -{ - public static readonly LoggerLevelInflux Instance = new(); - public override string Name => "Influx"; -} - -public class LoggerLevelComments : LoggerLevel -{ - public static readonly LoggerLevelComments Instance = new(); - public override string Name => "Comments"; -} - -public class LoggerLevelAspNet : LoggerLevel -{ - - public LoggerLevelAspNet(LogLevel level) - { - this.Channel = level.ToString(); - } - public override string Name => "AspNet"; -} - -public class LoggerLevelCategory : LoggerLevel -{ - public static readonly LoggerLevelCategory Instance = new(); - public override string Name => "Category"; -} \ No newline at end of file diff --git a/ProjectLighthouse/Logging/Loggers/AspNet/AspNetToLighthouseLogger.cs b/ProjectLighthouse/Logging/Loggers/AspNet/AspNetToLighthouseLogger.cs new file mode 100644 index 00000000..1b3b9815 --- /dev/null +++ b/ProjectLighthouse/Logging/Loggers/AspNet/AspNetToLighthouseLogger.cs @@ -0,0 +1,30 @@ +using System; +using LBPUnion.ProjectLighthouse.Helpers.Extensions; +using Microsoft.Extensions.Logging; +using AspLogLevel = Microsoft.Extensions.Logging.LogLevel; + +namespace LBPUnion.ProjectLighthouse.Logging.Loggers.AspNet; + +public class AspNetToLighthouseLogger : Microsoft.Extensions.Logging.ILogger +{ + public IDisposable BeginScope(TState state) => NullScope.Instance; + public bool IsEnabled(AspLogLevel logLevel) => true; + + public string Category { get; init; } + + public AspNetToLighthouseLogger(string category) + { + this.Category = category; + } + + public void Log(AspLogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) + { + LogLevel level = logLevel.ToLighthouseLevel(); + + Logger.Log(state.ToString(), this.Category, level); + + if (exception == null) return; + + Logger.Log(exception.ToDetailedException(), this.Category, level); + } +} \ No newline at end of file diff --git a/ProjectLighthouse/Logging/Loggers/AspNet/AspNetToLighthouseLoggerProvider.cs b/ProjectLighthouse/Logging/Loggers/AspNet/AspNetToLighthouseLoggerProvider.cs new file mode 100644 index 00000000..9fc6eff6 --- /dev/null +++ b/ProjectLighthouse/Logging/Loggers/AspNet/AspNetToLighthouseLoggerProvider.cs @@ -0,0 +1,16 @@ +using System; +using Microsoft.Extensions.Logging; +using IAspLogger = Microsoft.Extensions.Logging.ILogger; + +namespace LBPUnion.ProjectLighthouse.Logging.Loggers.AspNet; + +[ProviderAlias("Kettu")] +public class AspNetToLighthouseLoggerProvider : ILoggerProvider, IDisposable +{ + public void Dispose() + { + GC.SuppressFinalize(this); + } + + public IAspLogger CreateLogger(string category) => new AspNetToLighthouseLogger(category); +} \ No newline at end of file diff --git a/ProjectLighthouse/Logging/Loggers/ConsoleLogger.cs b/ProjectLighthouse/Logging/Loggers/ConsoleLogger.cs new file mode 100644 index 00000000..f3ef83f4 --- /dev/null +++ b/ProjectLighthouse/Logging/Loggers/ConsoleLogger.cs @@ -0,0 +1,44 @@ +using System; +using LBPUnion.ProjectLighthouse.Helpers.Extensions; + +namespace LBPUnion.ProjectLighthouse.Logging.Loggers; + +public class ConsoleLogger : ILogger +{ + public void Log(LogLine logLine) + { + ConsoleColor oldBackgroundColor = Console.BackgroundColor; + ConsoleColor oldForegroundColor = Console.ForegroundColor; + + foreach (string line in logLine.Message.Split('\n')) + { + // The following is scuffed. Beware~ + + // Write the level! [Success] + Console.BackgroundColor = logLine.Level.ToColor().ToDark(); + Console.ForegroundColor = ConsoleColor.White; + Console.Write('['); + Console.ForegroundColor = logLine.Level.ToColor(); + Console.Write(logLine.Level); + Console.ForegroundColor = ConsoleColor.White; + Console.Write(']'); + Console.ForegroundColor = oldForegroundColor; + Console.BackgroundColor = oldBackgroundColor; + Console.Write(' '); + + Console.ForegroundColor = ConsoleColor.White; + Console.Write('<'); + Console.ForegroundColor = logLine.Level.ToColor(); + Console.Write(logLine.Trace.Name); + Console.ForegroundColor = ConsoleColor.White; + Console.Write(':'); + Console.ForegroundColor = logLine.Level.ToColor(); + Console.Write(logLine.Trace.Line); + Console.ForegroundColor = ConsoleColor.White; + Console.Write("> "); + Console.ForegroundColor = oldForegroundColor; + + Console.WriteLine(line); + } + } +} \ No newline at end of file diff --git a/ProjectLighthouse/Logging/InfluxLogger.cs b/ProjectLighthouse/Logging/Loggers/InfluxLogger.cs similarity index 51% rename from ProjectLighthouse/Logging/InfluxLogger.cs rename to ProjectLighthouse/Logging/Loggers/InfluxLogger.cs index bc9b870a..908ad38f 100644 --- a/ProjectLighthouse/Logging/InfluxLogger.cs +++ b/ProjectLighthouse/Logging/Loggers/InfluxLogger.cs @@ -1,21 +1,18 @@ using InfluxDB.Client; using InfluxDB.Client.Writes; -using Kettu; using LBPUnion.ProjectLighthouse.Helpers; using LBPUnion.ProjectLighthouse.Types.Settings; -namespace LBPUnion.ProjectLighthouse.Logging; +namespace LBPUnion.ProjectLighthouse.Logging.Loggers; -public class InfluxLogger : LoggerBase +public class InfluxLogger : ILogger { - public override bool AllowMultiple => false; - - public override void Send(LoggerLine line) + public void Log(LogLine line) { - string channel = string.IsNullOrEmpty(line.LoggerLevel.Channel) ? "" : $"[{line.LoggerLevel.Channel}] "; + string channel = string.IsNullOrEmpty(line.Area) ? "" : $"[{line.Area}] "; - string level = $"{$"{line.LoggerLevel.Name} {channel}".TrimEnd()}"; - string content = line.LineData; + string level = $"{$"{channel} {line}".TrimEnd()}"; + string content = line.Message; using WriteApi writeApi = InfluxHelper.Client.GetWriteApi(); diff --git a/ProjectLighthouse/Logging/Loggers/LighthouseFileLogger.cs b/ProjectLighthouse/Logging/Loggers/LighthouseFileLogger.cs new file mode 100644 index 00000000..010462c2 --- /dev/null +++ b/ProjectLighthouse/Logging/Loggers/LighthouseFileLogger.cs @@ -0,0 +1,28 @@ +using System; +using System.IO; +using LBPUnion.ProjectLighthouse.Helpers; + +namespace LBPUnion.ProjectLighthouse.Logging.Loggers; + +public class LighthouseFileLogger : ILogger +{ + private static readonly string logsDirectory = Path.Combine(Environment.CurrentDirectory, "logs"); + + public void Log(LogLine line) + { + FileHelper.EnsureDirectoryCreated(logsDirectory); + + string channel = string.IsNullOrEmpty(line.Area) ? "" : $"[{line.Area}] "; + + string contentFile = $"{channel}{line.Message}\n"; + string contentAll = $"[{$"{line.Level} {channel}".TrimEnd()}] {line.Message}\n"; + + try + { + File.AppendAllText(Path.Combine(logsDirectory, line.Level + ".log"), contentFile); + File.AppendAllText(Path.Combine(logsDirectory, "all.log"), contentAll); + } + catch(IOException) {} // windows, ya goofed + + } +} \ No newline at end of file diff --git a/ProjectLighthouse/Maintenance/Commands/CreateUserCommand.cs b/ProjectLighthouse/Maintenance/Commands/CreateUserCommand.cs index 390abe13..11bbd8b7 100644 --- a/ProjectLighthouse/Maintenance/Commands/CreateUserCommand.cs +++ b/ProjectLighthouse/Maintenance/Commands/CreateUserCommand.cs @@ -1,7 +1,6 @@ #nullable enable using System.Threading.Tasks; using JetBrains.Annotations; -using Kettu; using LBPUnion.ProjectLighthouse.Helpers; using LBPUnion.ProjectLighthouse.Logging; using LBPUnion.ProjectLighthouse.Types; @@ -25,17 +24,17 @@ public class CreateUserCommand : ICommand if (user == null) { user = await this._database.CreateUser(onlineId, CryptoHelper.BCryptHash(password)); - Logger.Log($"Created user {user.UserId} with online ID (username) {user.Username} and the specified password.", LoggerLevelLogin.Instance); + Logger.LogSuccess($"Created user {user.UserId} with online ID (username) {user.Username} and the specified password.", "Login"); user.PasswordResetRequired = true; - Logger.Log("This user will need to reset their password when they log in.", LoggerLevelLogin.Instance); + Logger.LogInfo("This user will need to reset their password when they log in.", "Login"); await this._database.SaveChangesAsync(); - Logger.Log("Database changes saved.", LoggerLevelDatabase.Instance); + Logger.LogInfo("Database changes saved.", "Database"); } else { - Logger.Log("A user with this username already exists.", LoggerLevelLogin.Instance); + Logger.LogError("A user with this username already exists.", "Login"); } } diff --git a/ProjectLighthouse/Pages/LoginForm.cshtml.cs b/ProjectLighthouse/Pages/LoginForm.cshtml.cs index 8af7237f..ac424b17 100644 --- a/ProjectLighthouse/Pages/LoginForm.cshtml.cs +++ b/ProjectLighthouse/Pages/LoginForm.cshtml.cs @@ -2,7 +2,6 @@ using System; using System.Threading.Tasks; using JetBrains.Annotations; -using Kettu; using LBPUnion.ProjectLighthouse.Helpers; using LBPUnion.ProjectLighthouse.Helpers.Extensions; using LBPUnion.ProjectLighthouse.Logging; @@ -47,28 +46,28 @@ public class LoginForm : BaseLayout User? user = await this.Database.Users.FirstOrDefaultAsync(u => u.Username == username); if (user == null) { - Logger.Log($"User {username} failed to login on web due to invalid username", LoggerLevelLogin.Instance); + Logger.LogWarn($"User {username} failed to login on web due to invalid username", "Login"); this.Error = "The username or password you entered is invalid."; return this.Page(); } if (!BCrypt.Net.BCrypt.Verify(password, user.Password)) { - Logger.Log($"User {user.Username} (id: {user.UserId}) failed to login on web due to invalid password", LoggerLevelLogin.Instance); + Logger.LogWarn($"User {user.Username} (id: {user.UserId}) failed to login on web due to invalid password", "Login"); this.Error = "The username or password you entered is invalid."; return this.Page(); } if (user.Banned) { - Logger.Log($"User {user.Username} (id: {user.UserId}) failed to login on web due to being banned", LoggerLevelLogin.Instance); + Logger.LogWarn($"User {user.Username} (id: {user.UserId}) failed to login on web due to being banned", "Login"); this.Error = "You have been banned. Please contact an administrator for more information.\nReason: " + user.BannedReason; return this.Page(); } if (user.EmailAddress == null && ServerSettings.Instance.SMTPEnabled) { - Logger.Log($"User {user.Username} (id: {user.UserId}) failed to login; email not set", LoggerLevelLogin.Instance); + Logger.LogWarn($"User {user.Username} (id: {user.UserId}) failed to login; email not set", "Login"); EmailSetToken emailSetToken = new() { @@ -102,7 +101,7 @@ public class LoginForm : BaseLayout } ); - Logger.Log($"User {user.Username} (id: {user.UserId}) successfully logged in on web", LoggerLevelLogin.Instance); + Logger.LogSuccess($"User {user.Username} (id: {user.UserId}) successfully logged in on web", "Login"); if (user.PasswordResetRequired) return this.Redirect("~/passwordResetRequired"); if (ServerSettings.Instance.SMTPEnabled && !user.EmailAddressVerified) return this.Redirect("~/login/sendVerificationEmail"); diff --git a/ProjectLighthouse/Pages/SetEmailForm.cshtml.cs b/ProjectLighthouse/Pages/SetEmailForm.cshtml.cs index 1e5dd041..9682157f 100644 --- a/ProjectLighthouse/Pages/SetEmailForm.cshtml.cs +++ b/ProjectLighthouse/Pages/SetEmailForm.cshtml.cs @@ -1,7 +1,6 @@ #nullable enable using System; using System.Threading.Tasks; -using Kettu; using LBPUnion.ProjectLighthouse.Helpers; using LBPUnion.ProjectLighthouse.Logging; using LBPUnion.ProjectLighthouse.Pages.Layouts; @@ -72,7 +71,7 @@ public class SetEmailForm : BaseLayout } ); - Logger.Log($"User {user.Username} (id: {user.UserId}) successfully logged in on web after setting an email address", LoggerLevelLogin.Instance); + Logger.LogSuccess($"User {user.Username} (id: {user.UserId}) successfully logged in on web after setting an email address", "Login"); this.Database.WebTokens.Add(webToken); await this.Database.SaveChangesAsync(); diff --git a/ProjectLighthouse/Program.cs b/ProjectLighthouse/Program.cs index 8f1a64d0..e896107f 100644 --- a/ProjectLighthouse/Program.cs +++ b/ProjectLighthouse/Program.cs @@ -1,9 +1,10 @@ #nullable enable using System; using System.Diagnostics; -using Kettu; using LBPUnion.ProjectLighthouse.Helpers; using LBPUnion.ProjectLighthouse.Logging; +using LBPUnion.ProjectLighthouse.Logging.Loggers; +using LBPUnion.ProjectLighthouse.Logging.Loggers.AspNet; using LBPUnion.ProjectLighthouse.Types.Settings; using Microsoft.AspNetCore.Hosting; using Microsoft.EntityFrameworkCore; @@ -23,46 +24,47 @@ public static class Program stopwatch.Start(); // Setup logging - - Logger.StartLogging(); - Logger.UpdateRate /= 2; - LoggerLine.LogFormat = "[{0}] {1}"; Logger.AddLogger(new ConsoleLogger()); Logger.AddLogger(new LighthouseFileLogger()); - Logger.Log("Welcome to Project Lighthouse!", LoggerLevelStartup.Instance); - Logger.Log($"You are running version {VersionHelper.FullVersion}", LoggerLevelStartup.Instance); + Logger.LogInfo("Welcome to Project Lighthouse!", "Startup"); + Logger.LogInfo($"You are running version {VersionHelper.FullVersion}", "Startup"); // Referencing ServerSettings.Instance here loads the config, see ServerSettings.cs for more information - Logger.Log("Loaded config file version " + ServerSettings.Instance.ConfigVersion, LoggerLevelStartup.Instance); + Logger.LogSuccess("Loaded config file version " + ServerSettings.Instance.ConfigVersion, "Startup"); - Logger.Log("Determining if the database is available...", LoggerLevelStartup.Instance); + Logger.LogInfo("Determining if the database is available...", "Startup"); bool dbConnected = ServerStatics.DbConnected; - Logger.Log(dbConnected ? "Connected to the database." : "Database unavailable! Exiting.", LoggerLevelStartup.Instance); + if (!dbConnected) + { + Logger.LogError("Database unavailable! Exiting.", "Startup"); + } + else + { + Logger.LogSuccess("Connected to the database.", "Startup"); + } if (!dbConnected) Environment.Exit(1); using Database database = new(); - Logger.Log("Migrating database...", LoggerLevelDatabase.Instance); + Logger.LogInfo("Migrating database...", "Database"); MigrateDatabase(database); if (ServerSettings.Instance.InfluxEnabled) { - Logger.Log("Influx logging is enabled. Starting influx logging...", LoggerLevelStartup.Instance); + Logger.LogInfo("Influx logging is enabled. Starting influx logging...", "Startup"); InfluxHelper.StartLogging().Wait(); if (ServerSettings.Instance.InfluxLoggingEnabled) Logger.AddLogger(new InfluxLogger()); } - #if DEBUG - Logger.Log + Logger.LogDebug ( "This is a debug build, so performance may suffer! " + "If you are running Lighthouse in a production environment, " + "it is highly recommended to run a release build. ", - LoggerLevelStartup.Instance + "Startup" ); - Logger.Log("You can do so by running any dotnet command with the flag: \"-c Release\". ", LoggerLevelStartup.Instance); - #endif + Logger.LogDebug("You can do so by running any dotnet command with the flag: \"-c Release\". ", "Startup"); if (args.Length != 0) { @@ -72,11 +74,11 @@ public static class Program if (ServerSettings.Instance.ConvertAssetsOnStartup) FileHelper.ConvertAllTexturesToPng(); - Logger.Log("Starting room cleanup thread...", LoggerLevelStartup.Instance); + Logger.LogInfo("Starting room cleanup thread...", "Startup"); RoomHelper.StartCleanupThread(); stopwatch.Stop(); - Logger.Log($"Ready! Startup took {stopwatch.ElapsedMilliseconds}ms. Passing off control to ASP.NET...", LoggerLevelStartup.Instance); + Logger.LogSuccess($"Ready! Startup took {stopwatch.ElapsedMilliseconds}ms. Passing off control to ASP.NET...", "Startup"); CreateHostBuilder(args).Build().Run(); } @@ -89,7 +91,7 @@ public static class Program database.Database.MigrateAsync().Wait(); stopwatch.Stop(); - Logger.Log($"Migration took {stopwatch.ElapsedMilliseconds}ms.", LoggerLevelDatabase.Instance); + Logger.LogSuccess($"Migration took {stopwatch.ElapsedMilliseconds}ms.", "Database"); } public static IHostBuilder CreateHostBuilder(string[] args) @@ -108,7 +110,7 @@ public static class Program logging => { logging.ClearProviders(); - logging.Services.TryAddEnumerable(ServiceDescriptor.Singleton()); + logging.Services.TryAddEnumerable(ServiceDescriptor.Singleton()); } ); } \ No newline at end of file diff --git a/ProjectLighthouse/ProjectLighthouse.csproj b/ProjectLighthouse/ProjectLighthouse.csproj index 68280754..19bf5488 100644 --- a/ProjectLighthouse/ProjectLighthouse.csproj +++ b/ProjectLighthouse/ProjectLighthouse.csproj @@ -17,7 +17,6 @@ - diff --git a/ProjectLighthouse/Startup/DebugWarmupLifetime.cs b/ProjectLighthouse/Startup/DebugWarmupLifetime.cs index bfe4e7c3..74372120 100644 --- a/ProjectLighthouse/Startup/DebugWarmupLifetime.cs +++ b/ProjectLighthouse/Startup/DebugWarmupLifetime.cs @@ -2,7 +2,7 @@ using System; using System.Net.Http; using System.Threading; using System.Threading.Tasks; -using Kettu; +using LBPUnion.ProjectLighthouse.Helpers.Extensions; using LBPUnion.ProjectLighthouse.Logging; using LBPUnion.ProjectLighthouse.Types.Settings; using Microsoft.Extensions.Hosting; @@ -40,18 +40,18 @@ public class DebugWarmupLifetime : IHostLifetime string url = ServerSettings.Instance.ServerListenUrl; url = url.Replace("0.0.0.0", "127.0.0.1"); - Logger.Log("Warming up Hot Reload...", LoggerLevelStartup.Instance); + Logger.LogDebug("Warming up Hot Reload...", "Startup"); try { client.GetAsync(url).Wait(); } catch(Exception e) { - Logger.Log("An error occurred while attempting to warm up hot reload. Initial page load will be delayed.", LoggerLevelStartup.Instance); - Logger.Log(e.Message, LoggerLevelStartup.Instance); + Logger.LogDebug("An error occurred while attempting to warm up hot reload. Initial page load will be delayed.", "Startup"); + Logger.LogDebug(e.ToDetailedException(), "Startup"); return; } - Logger.Log("Hot Reload is ready to go!", LoggerLevelStartup.Instance); + Logger.LogSuccess("Hot Reload is ready to go!", "Startup"); } public Task StopAsync(CancellationToken cancellationToken) => this.consoleLifetime.StopAsync(cancellationToken); diff --git a/ProjectLighthouse/Startup/Startup.cs b/ProjectLighthouse/Startup/Startup.cs index b41097e9..e86e509b 100644 --- a/ProjectLighthouse/Startup/Startup.cs +++ b/ProjectLighthouse/Startup/Startup.cs @@ -2,7 +2,6 @@ using System; using System.Diagnostics; using System.IO; using System.Reflection; -using Kettu; using LBPUnion.ProjectLighthouse.Helpers; using LBPUnion.ProjectLighthouse.Logging; using LBPUnion.ProjectLighthouse.Serialization; @@ -99,11 +98,11 @@ public class Startup if (string.IsNullOrEmpty(ServerSettings.Instance.ServerDigestKey)) { - Logger.Log + Logger.LogWarn ( "The serverDigestKey configuration option wasn't set, so digest headers won't be set or verified. This will also prevent LBP 1, LBP 2, and LBP Vita from working. " + "To increase security, it is recommended that you find and set this variable.", - LoggerLevelStartup.Instance + "Startup" ); computeDigests = false; } @@ -142,10 +141,10 @@ public class Startup requestStopwatch.Stop(); - Logger.Log + Logger.LogInfo ( $"{context.Response.StatusCode}, {requestStopwatch.ElapsedMilliseconds}ms: {context.Request.Method} {context.Request.Path}{context.Request.QueryString}", - LoggerLevelHttp.Instance + "HTTP" ); #if DEBUG @@ -153,7 +152,7 @@ public class Startup if (context.Request.Method == "POST") { context.Request.Body.Position = 0; - Logger.Log(await new StreamReader(context.Request.Body).ReadToEndAsync(), LoggerLevelHttp.Instance); + Logger.LogDebug(await new StreamReader(context.Request.Body).ReadToEndAsync(), "HTTP"); } #endif } diff --git a/ProjectLighthouse/Types/Categories/CategoryWithUser.cs b/ProjectLighthouse/Types/Categories/CategoryWithUser.cs index 1c7c6a0b..630d4b56 100644 --- a/ProjectLighthouse/Types/Categories/CategoryWithUser.cs +++ b/ProjectLighthouse/Types/Categories/CategoryWithUser.cs @@ -1,7 +1,6 @@ #nullable enable using System.Collections.Generic; using System.Diagnostics; -using Kettu; using LBPUnion.ProjectLighthouse.Logging; using LBPUnion.ProjectLighthouse.Serialization; using LBPUnion.ProjectLighthouse.Types.Levels; @@ -14,7 +13,7 @@ public abstract class CategoryWithUser : Category public override Slot? GetPreviewSlot(Database database) { #if DEBUG - Logger.Log("tried to get preview slot without user on CategoryWithUser", LoggerLevelCategory.Instance); + Logger.LogError("tried to get preview slot without user on CategoryWithUser", "Category"); if (Debugger.IsAttached) Debugger.Break(); #endif return null; @@ -24,7 +23,7 @@ public abstract class CategoryWithUser : Category public override int GetTotalSlots(Database database) { #if DEBUG - Logger.Log("tried to get total slots without user on CategoryWithUser", LoggerLevelCategory.Instance); + Logger.LogError("tried to get total slots without user on CategoryWithUser", "Category"); if (Debugger.IsAttached) Debugger.Break(); #endif return -1; @@ -34,7 +33,7 @@ public abstract class CategoryWithUser : Category public override IEnumerable GetSlots(Database database, int pageStart, int pageSize) { #if DEBUG - Logger.Log("tried to get slots without user on CategoryWithUser", LoggerLevelCategory.Instance); + Logger.LogError("tried to get slots without user on CategoryWithUser", "Category"); if (Debugger.IsAttached) Debugger.Break(); #endif return new List(); @@ -42,7 +41,7 @@ public abstract class CategoryWithUser : Category public new string Serialize(Database database) { - Logger.Log("tried to serialize without user on CategoryWithUser", LoggerLevelCategory.Instance); + Logger.LogError("tried to serialize without user on CategoryWithUser", "Category"); return string.Empty; } diff --git a/ProjectLighthouse/Types/Settings/ServerSettings.cs b/ProjectLighthouse/Types/Settings/ServerSettings.cs index d0f10b2a..7f0b5c6a 100644 --- a/ProjectLighthouse/Types/Settings/ServerSettings.cs +++ b/ProjectLighthouse/Types/Settings/ServerSettings.cs @@ -4,7 +4,6 @@ using System.IO; using System.Text.Json; using System.Text.Json.Serialization; using JetBrains.Annotations; -using Kettu; using LBPUnion.ProjectLighthouse.Logging; namespace LBPUnion.ProjectLighthouse.Types.Settings; @@ -20,7 +19,7 @@ public class ServerSettings { if (ServerStatics.IsUnitTesting) return; // Unit testing, we don't want to read configurations here since the tests will provide their own - Logger.Log("Loading config...", LoggerLevelConfig.Instance); + Logger.LogInfo("Loading config...", "Config"); if (File.Exists(ConfigFileName)) { @@ -30,7 +29,7 @@ public class ServerSettings if (Instance.ConfigVersion < CurrentConfigVersion) { - Logger.Log($"Upgrading config file from version {Instance.ConfigVersion} to version {CurrentConfigVersion}", LoggerLevelConfig.Instance); + Logger.LogInfo($"Upgrading config file from version {Instance.ConfigVersion} to version {CurrentConfigVersion}", "Config"); Instance.ConfigVersion = CurrentConfigVersion; configFile = JsonSerializer.Serialize ( @@ -59,12 +58,12 @@ public class ServerSettings File.WriteAllText(ConfigFileName, configFile); - Logger.Log + Logger.LogWarn ( "The configuration file was not found. " + "A blank configuration file has been created for you at " + $"{Path.Combine(Environment.CurrentDirectory, ConfigFileName)}", - LoggerLevelConfig.Instance + "Config" ); Environment.Exit(1); @@ -73,7 +72,7 @@ public class ServerSettings // Set up reloading if (Instance.ConfigReloading) { - Logger.Log("Setting up config reloading...", LoggerLevelConfig.Instance); + Logger.LogInfo("Setting up config reloading...", "Config"); fileWatcher = new FileSystemWatcher { Path = Environment.CurrentDirectory, @@ -90,8 +89,8 @@ public class ServerSettings private static void onConfigChanged(object sender, FileSystemEventArgs e) { Debug.Assert(e.Name == ConfigFileName); - Logger.Log("Configuration file modified, reloading config.", LoggerLevelConfig.Instance); - Logger.Log("Some changes may not apply, in which case may require a restart of Project Lighthouse.", LoggerLevelConfig.Instance); + Logger.LogInfo("Configuration file modified, reloading config.", "Config"); + Logger.LogWarn("Some changes may not apply, in which case may require a restart of Project Lighthouse.", "Config"); string configFile = File.ReadAllText(ConfigFileName); Instance = JsonSerializer.Deserialize(configFile) ?? throw new ArgumentNullException(nameof(ConfigFileName)); diff --git a/ProjectLighthouse/Types/Settings/ServerStatics.cs b/ProjectLighthouse/Types/Settings/ServerStatics.cs index dacc34f0..98703d11 100644 --- a/ProjectLighthouse/Types/Settings/ServerStatics.cs +++ b/ProjectLighthouse/Types/Settings/ServerStatics.cs @@ -1,7 +1,6 @@ #nullable enable using System; using System.Linq; -using Kettu; using LBPUnion.ProjectLighthouse.Logging; namespace LBPUnion.ProjectLighthouse.Types.Settings; @@ -20,7 +19,7 @@ public static class ServerStatics } catch(Exception e) { - Logger.Log(e.ToString(), LoggerLevelDatabase.Instance); + Logger.LogError(e.ToString(), "Database"); return false; } } diff --git a/ProjectLighthouse/Types/Tickets/NPTicket.cs b/ProjectLighthouse/Types/Tickets/NPTicket.cs index 91d76629..32d3c2e8 100644 --- a/ProjectLighthouse/Types/Tickets/NPTicket.cs +++ b/ProjectLighthouse/Types/Tickets/NPTicket.cs @@ -3,7 +3,6 @@ using System; using System.IO; using System.Text; using System.Text.Json; -using Kettu; using LBPUnion.ProjectLighthouse.Helpers; using LBPUnion.ProjectLighthouse.Helpers.Extensions; using LBPUnion.ProjectLighthouse.Logging; @@ -93,12 +92,8 @@ public class NPTicket reader.ReadUInt16BE(); // Ticket length, we don't care about this - #if DEBUG SectionHeader bodyHeader = reader.ReadSectionHeader(); - Logger.Log($"bodyHeader.Type is {bodyHeader.Type}", LoggerLevelLogin.Instance); - #else - reader.ReadSectionHeader(); - #endif + Logger.LogDebug($"bodyHeader.Type is {bodyHeader.Type}", "Login"); switch (npTicket.ticketVersion) { @@ -120,15 +115,13 @@ public class NPTicket npTicket.titleId = npTicket.titleId.Substring(0, npTicket.titleId.Length - 3); // Trim _00 at the end // Data now (hopefully): BCUS98245 - #if DEBUG - Logger.Log($"titleId is {npTicket.titleId}", LoggerLevelLogin.Instance); - #endif + Logger.LogDebug($"titleId is {npTicket.titleId}", "Login"); npTicket.GameVersion = GameVersionHelper.FromTitleId(npTicket.titleId); // Finally, convert it to GameVersion if (npTicket.GameVersion == GameVersion.Unknown) { - Logger.Log($"Could not determine game version from title id {npTicket.titleId}", LoggerLevelLogin.Instance); + Logger.LogWarn($"Could not determine game version from title id {npTicket.titleId}", "Login"); return null; } @@ -145,45 +138,34 @@ public class NPTicket if (npTicket.Platform == Platform.Unknown) { - Logger.Log($"Could not determine platform from IssuerId {npTicket.IssuerId} decimal", LoggerLevelLogin.Instance); + Logger.LogWarn($"Could not determine platform from IssuerId {npTicket.IssuerId} decimal", "Login"); return null; } #if DEBUG - Logger.Log("npTicket data:", LoggerLevelLogin.Instance); - foreach (string line in JsonSerializer.Serialize(npTicket).Split('\n')) - { - Logger.Log(line, LoggerLevelLogin.Instance); - } + Logger.LogDebug("npTicket data:", "Login"); + Logger.LogDebug(JsonSerializer.Serialize(npTicket), "Login"); #endif return npTicket; } catch(NotImplementedException) { - Logger.Log($"The ticket version {npTicket.ticketVersion} is not implemented yet.", LoggerLevelLogin.Instance); - Logger.Log + Logger.LogError($"The ticket version {npTicket.ticketVersion} is not implemented yet.", "Login"); + Logger.LogError ( "Please let us know that this is a ticket version that is actually used on our issue tracker at https://github.com/LBPUnion/project-lighthouse/issues !", - LoggerLevelLogin.Instance + "Login" ); return null; } catch(Exception e) { - Logger.Log("Failed to read npTicket!", LoggerLevelLogin.Instance); - Logger.Log("Either this is spam data, or the more likely that this is a bug.", LoggerLevelLogin.Instance); - Logger.Log - ( - "Please report the following exception to our issue tracker at https://github.com/LBPUnion/project-lighthouse/issues!", - LoggerLevelLogin.Instance - ); - - foreach (string line in e.ToDetailedException().Split('\n')) - { - Logger.Log(line, LoggerLevelLogin.Instance); - } + Logger.LogError("Failed to read npTicket!", "Login"); + Logger.LogError("Either this is spam data, or the more likely that this is a bug.", "Login"); + Logger.LogError("Please report the following exception to our issue tracker at https://github.com/LBPUnion/project-lighthouse/issues!", "Login"); + Logger.LogError(e.ToDetailedException(), "Login"); return null; } }