Browse Source

Added unity logger

Documented new logging system
pull/46/head
Anairkoen Schno 6 years ago
parent
commit
935c52c633
6 changed files with 196 additions and 2 deletions
  1. +9
    -1
      IllusionInjector/Bootstrapper.cs
  2. +1
    -0
      IllusionInjector/IllusionInjector.csproj
  3. +34
    -0
      IllusionInjector/Logging/UnityLogInterceptor.cs
  4. +1
    -1
      IllusionInjector/obj/Debug/IllusionInjector.csproj.CoreCompileInputs.cache
  5. +21
    -0
      IllusionPlugin/Logging/LogPrinter.cs
  6. +130
    -0
      IllusionPlugin/Logging/Logger.cs

+ 9
- 1
IllusionInjector/Bootstrapper.cs View File

@ -1,4 +1,5 @@
using System;
using IllusionInjector.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -16,6 +17,13 @@ namespace IllusionInjector
{
Windows.GuiConsole.CreateConsole();
}
Application.logMessageReceived += delegate (string condition, string stackTrace, LogType type)
{
var level = UnityLogInterceptor.LogTypeToLevel(type);
UnityLogInterceptor.Unitylogger.Log(level, $"{condition.Trim()}");
UnityLogInterceptor.Unitylogger.Log(level, $"{stackTrace.Trim()}");
};
}
void Start()


+ 1
- 0
IllusionInjector/IllusionInjector.csproj View File

@ -59,6 +59,7 @@
<Compile Include="Logging\Printers\GlobalZFIlePrinter.cs" />
<Compile Include="Logging\Printers\PluginLogFilePrinter.cs" />
<Compile Include="Logging\StandardLogger.cs" />
<Compile Include="Logging\UnityLogInterceptor.cs" />
<Compile Include="PluginManager.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="PluginComponent.cs" />


+ 34
- 0
IllusionInjector/Logging/UnityLogInterceptor.cs View File

@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using LoggerBase = IllusionPlugin.Logging.Logger;
namespace IllusionInjector.Logging
{
public class UnityLogInterceptor
{
public static LoggerBase Unitylogger = new StandardLogger("UnityEngine");
public static LoggerBase.Level LogTypeToLevel(LogType type)
{
switch (type)
{
case LogType.Assert:
return LoggerBase.Level.Debug;
case LogType.Error:
return LoggerBase.Level.Error;
case LogType.Exception:
return LoggerBase.Level.Critical;
case LogType.Log:
return LoggerBase.Level.Info;
case LogType.Warning:
return LoggerBase.Level.Warning;
default:
return LoggerBase.Level.Info;
}
}
}
}

+ 1
- 1
IllusionInjector/obj/Debug/IllusionInjector.csproj.CoreCompileInputs.cache View File

@ -1 +1 @@
524c2a9e58911edfdaf36eb5bf255b181a39f814
8c1883c8697a3dc7189ee9ff1c9d85d453a46fe5

+ 21
- 0
IllusionPlugin/Logging/LogPrinter.cs View File

@ -6,11 +6,32 @@ using System.Threading.Tasks;
namespace IllusionPlugin.Logging
{
/// <summary>
/// The log printer's base class.
/// </summary>
public abstract class LogPrinter
{
/// <summary>
/// Provides a filter for which log levels to allow through.
/// </summary>
public abstract Logger.LogLevel Filter { get; set; }
/// <summary>
/// Prints a provided message from a given log at the specified time.
/// </summary>
/// <param name="level">the log level</param>
/// <param name="time">the time the message was composed</param>
/// <param name="logName">the name of the log that created this message</param>
/// <param name="message">the message</param>
public abstract void Print(Logger.Level level, DateTime time, string logName, string message);
/// <summary>
/// Called before the first print in a group. May be called multiple times.
/// Use this to create file handles and the like.
/// </summary>
public virtual void StartPrint() { }
/// <summary>
/// Called after the last print in a group. May be called multiple times.
/// Use this to dispose file handles and the like.
/// </summary>
public virtual void EndPrint() { }
}
}

+ 130
- 0
IllusionPlugin/Logging/Logger.cs View File

@ -6,47 +6,177 @@ using System.Threading.Tasks;
namespace IllusionPlugin.Logging
{
/// <summary>
/// The logger base class. Provides the format for console logs.
/// </summary>
public abstract class Logger
{
/// <summary>
/// The standard format for log messages.
/// </summary>
public static string LogFormat { get; protected internal set; } = "[{3} @ {2:HH:mm:ss} | {1}] {0}";
/// <summary>
/// An enum specifying the level of the message. Resembles Syslog.
/// </summary>
public enum Level : byte
{
/// <summary>
/// No associated level. These never get shown.
/// </summary>
None = 0,
/// <summary>
/// A debug message.
/// </summary>
Debug = 1,
/// <summary>
/// An informational message.
/// </summary>
Info = 2,
/// <summary>
/// A warning message.
/// </summary>
Warning = 4,
/// <summary>
/// An error message.
/// </summary>
Error = 8,
/// <summary>
/// A critical error message.
/// </summary>
Critical = 16
}
/// <summary>
/// An enum providing log level filters.
/// </summary>
[Flags]
public enum LogLevel : byte
{
/// <summary>
/// Allow no messages through.
/// </summary>
None = Level.None,
/// <summary>
/// Only shows Debug messages.
/// </summary>
DebugOnly = Level.Debug,
/// <summary>
/// Only shows info messages.
/// </summary>
InfoOnly = Level.Info,
/// <summary>
/// Only shows Warning messages.
/// </summary>
WarningOnly = Level.Warning,
/// <summary>
/// Only shows Error messages.
/// </summary>
ErrorOnly = Level.Error,
/// <summary>
/// Only shows Critical messages.
/// </summary>
CriticalOnly = Level.Critical,
/// <summary>
/// Shows all messages error and up.
/// </summary>
ErrorUp = ErrorOnly | CriticalOnly,
/// <summary>
/// Shows all messages warning and up.
/// </summary>
WarningUp = WarningOnly | ErrorUp,
/// <summary>
/// Shows all messages info and up.
/// </summary>
InfoUp = InfoOnly | WarningUp,
/// <summary>
/// Shows all messages.
/// </summary>
All = DebugOnly | InfoUp,
}
/// <summary>
/// A basic log function.
/// </summary>
/// <param name="level">the level of the message</param>
/// <param name="message">the message to log</param>
public abstract void Log(Level level, string message);
/// <summary>
/// A basic log function taking an exception to log.
/// </summary>
/// <param name="level">the level of the message</param>
/// <param name="exeption">the exception to log</param>
public void Log(Level level, Exception exeption) => Log(level, exeption.ToString());
/// <summary>
/// Sends a debug message.
/// Equivalent to Log(Level.Debug, message);
/// <see cref="Log(Level, string)"/>
/// </summary>
/// <param name="message">the message to log</param>
public void Debug(string message) => Log(Level.Debug, message);
/// <summary>
/// Sends an exception as a debug message.
/// Equivalent to Log(Level.Debug, e);
/// <see cref="Log(Level, Exception)"/>
/// </summary>
/// <param name="e">the exception to log</param>
public void Debug(Exception e) => Log(Level.Debug, e);
/// <summary>
/// Sends an info message.
/// Equivalent to Log(Level.Info, message).
/// <see cref="Log(Level, string)"/>
/// </summary>
/// <param name="message">the message to log</param>
public void Info(string message) => Log(Level.Info, message);
/// <summary>
/// Sends an exception as an info message.
/// Equivalent to Log(Level.Info, e);
/// <see cref="Log(Level, Exception)"/>
/// </summary>
/// <param name="e">the exception to log</param>
public void Info(Exception e) => Log(Level.Info, e);
/// <summary>
/// Sends a warning message.
/// Equivalent to Log(Level.Warning, message).
/// <see cref="Log(Level, string)"/>
/// </summary>
/// <param name="message">the message to log</param>
public void Warn(string message) => Log(Level.Warning, message);
/// <summary>
/// Sends an exception as a warning message.
/// Equivalent to Log(Level.Warning, e);
/// <see cref="Log(Level, Exception)"/>
/// </summary>
/// <param name="e">the exception to log</param>
public void Warn(Exception e) => Log(Level.Warning, e);
/// <summary>
/// Sends an error message.
/// Equivalent to Log(Level.Error, message).
/// <see cref="Log(Level, string)"/>
/// </summary>
/// <param name="message">the message to log</param>
public void Error(string message) => Log(Level.Error, message);
/// <summary>
/// Sends an exception as an error message.
/// Equivalent to Log(Level.Error, e);
/// <see cref="Log(Level, Exception)"/>
/// </summary>
/// <param name="e">the exception to log</param>
public void Error(Exception e) => Log(Level.Error, e);
/// <summary>
/// Sends a critical message.
/// Equivalent to Log(Level.Critical, message).
/// <see cref="Log(Level, string)"/>
/// </summary>
/// <param name="message">the message to log</param>
public void Critical(string message) => Log(Level.Critical, message);
/// <summary>
/// Sends an exception as a critical message.
/// Equivalent to Log(Level.Critical, e);
/// <see cref="Log(Level, Exception)"/>
/// </summary>
/// <param name="e">the exception to log</param>
public void Critical(Exception e) => Log(Level.Critical, e);
}
}

Loading…
Cancel
Save