// This file is provided under The MIT License as part of RiptideNetworking.
// Copyright (c) Tom Weiland
// For additional information please see the included LICENSE.md file or view it on GitHub:
// https://github.com/RiptideNetworking/Riptide/blob/main/LICENSE.md
using System;
using System.Collections.Generic;
namespace Riptide.Utils
{
/// Defines log message types.
public enum LogType
{
/// Logs that are used for investigation during development.
Debug,
/// Logs that provide general information about application flow.
Info,
/// Logs that highlight abnormal or unexpected events in the application flow.
Warning,
/// Logs that highlight problematic events in the application flow which will cause unexpected behavior if not planned for.
Error
}
/// Provides functionality for logging messages.
public class RiptideLogger
{
/// Whether or not messages will be logged.
public static bool IsDebugLoggingEnabled => logMethods.ContainsKey(LogType.Debug);
/// Whether or not messages will be logged.
public static bool IsInfoLoggingEnabled => logMethods.ContainsKey(LogType.Info);
/// Whether or not messages will be logged.
public static bool IsWarningLoggingEnabled => logMethods.ContainsKey(LogType.Warning);
/// Whether or not messages will be logged.
public static bool IsErrorLoggingEnabled => logMethods.ContainsKey(LogType.Error);
/// Encapsulates a method used to log messages.
/// The message to log.
public delegate void LogMethod(string log);
/// Log methods, accessible by their
private static readonly Dictionary logMethods = new Dictionary(4);
/// Whether or not to include timestamps when logging messages.
private static bool includeTimestamps;
/// The format to use for timestamps.
private static string timestampFormat;
/// Initializes with all log types enabled.
/// The method to use when logging all types of messages.
/// Whether or not to include timestamps when logging messages.
/// The format to use for timestamps.
public static void Initialize(LogMethod logMethod, bool includeTimestamps, string timestampFormat = "HH:mm:ss") => Initialize(logMethod, logMethod, logMethod, logMethod, includeTimestamps, timestampFormat);
/// Initializes with the supplied log methods.
/// The method to use when logging debug messages. Set to to disable debug logs.
/// The method to use when logging info messages. Set to to disable info logs.
/// The method to use when logging warning messages. Set to to disable warning logs.
/// The method to use when logging error messages. Set to to disable error logs.
/// Whether or not to include timestamps when logging messages.
/// The format to use for timestamps.
public static void Initialize(LogMethod debugMethod, LogMethod infoMethod, LogMethod warningMethod, LogMethod errorMethod, bool includeTimestamps, string timestampFormat = "HH:mm:ss")
{
logMethods.Clear();
if (debugMethod != null)
logMethods.Add(LogType.Debug, debugMethod);
if (infoMethod != null)
logMethods.Add(LogType.Info, infoMethod);
if (warningMethod != null)
logMethods.Add(LogType.Warning, warningMethod);
if (errorMethod != null)
logMethods.Add(LogType.Error, errorMethod);
RiptideLogger.includeTimestamps = includeTimestamps;
RiptideLogger.timestampFormat = timestampFormat;
}
/// Enables logging for messages of the given .
/// The type of message to enable logging for.
/// The method to use when logging this type of message.
public static void EnableLoggingFor(LogType logType, LogMethod logMethod)
{
if (logMethods.ContainsKey(logType))
logMethods[logType] = logMethod;
else
logMethods.Add(logType, logMethod);
}
/// Disables logging for messages of the given .
/// The type of message to enable logging for.
public static void DisableLoggingFor(LogType logType) => logMethods.Remove(logType);
/// Logs a message.
/// The type of log message that is being logged.
/// The message to log.
public static void Log(LogType logType, string message)
{
if (logMethods.TryGetValue(logType, out LogMethod logMethod))
{
if (includeTimestamps)
logMethod($"[{GetTimestamp(DateTime.Now)}]: {message}");
else
logMethod(message);
}
}
/// Logs a message.
/// The type of log message that is being logged.
/// Who is logging this message.
/// The message to log.
public static void Log(LogType logType, string logName, string message)
{
if (logMethods.TryGetValue(logType, out LogMethod logMethod))
{
if (includeTimestamps)
logMethod($"[{GetTimestamp(DateTime.Now)}] ({logName}): {message}");
else
logMethod($"({logName}): {message}");
}
}
/// Converts a object to a formatted timestamp string.
/// The time to format.
/// The formatted timestamp.
private static string GetTimestamp(DateTime time)
{
return time.ToString(timestampFormat);
}
}
}