Decompiled source of LoggingPlugin v1.0.1

LoggingPlugin.dll

Decompiled 6 months ago
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("LoggingPlugin")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Nth Dimension")]
[assembly: AssemblyProduct("LoggingPlugin")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("LoggingPlugin")]
[assembly: ComVisible(false)]
[assembly: Guid("c303405d-e66c-4316-9cdb-4e3ca15c6360")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace LordAshes;

[BepInPlugin("org.lordashes.plugins.logging", "Logging Plugin", "1.0.0.0")]
public class LoggingPlugin : BaseUnityPlugin
{
	public enum DiagnosticLevel
	{
		none,
		error,
		warning,
		info,
		debug,
		trace
	}

	public class Subscription
	{
		public DiagnosticLevel logLevel = DiagnosticLevel.info;

		public Action<string, DiagnosticLevel, string> callback = null;
	}

	public const string Name = "Logging Plugin";

	public const string Guid = "org.lordashes.plugins.logging";

	public const string Version = "1.0.0.0";

	public const string Author = "Lord Ashes";

	private static readonly ManualLogSource _logger = Logger.CreateLogSource("Lord Ashes.Logging Plugin");

	private static ConfigEntry<string> _level;

	private static ConfigEntry<string> _format;

	private static ConfigEntry<int> _paddingName;

	private static ConfigEntry<DiagnosticLevel> _ownDiagnostics;

	private static ConfigEntry<bool> _allowPluginDiagnosticsLevels;

	private static DictionaryList<string, DiagnosticLevel> logLevels = new DictionaryList<string, DiagnosticLevel>();

	private static DictionaryList<string, Subscription> subscriptions = new DictionaryList<string, Subscription>();

	private void Awake()
	{
		_ownDiagnostics = ((BaseUnityPlugin)this).Config.Bind<DiagnosticLevel>("Settings", "Logger Own Logging Level", DiagnosticLevel.error, (ConfigDescription)null);
		_level = ((BaseUnityPlugin)this).Config.Bind<string>("Settings", "Plugin Logging Level", "*:info", (ConfigDescription)null);
		_allowPluginDiagnosticsLevels = ((BaseUnityPlugin)this).Config.Bind<bool>("Settings", "Allow Plugins Logging Level", true, (ConfigDescription)null);
		_format = ((BaseUnityPlugin)this).Config.Bind<string>("Settings", "Log Entry Format", "{TIMESTAMP} - {LEVEL} - {NAME} - {CONTENT}", (ConfigDescription)null);
		_paddingName = ((BaseUnityPlugin)this).Config.Bind<int>("Settings", "Length Of Plugin Name", 20, (ConfigDescription)null);
		string[] array = _level.Value.Split(new char[1] { ',' });
		foreach (string text in array)
		{
			string[] array2 = text.Split(new char[1] { ':' });
			DiagnosticLevel result = DiagnosticLevel.none;
			Enum.TryParse<DiagnosticLevel>(array2[1], ignoreCase: true, out result);
			logLevels.Add(array2[0], result);
		}
		if (!logLevels.ContainsKey("*"))
		{
			logLevels.Add("*", DiagnosticLevel.info);
		}
		LogInfo("Logging Plugin: Active.");
	}

	public static void SetLogLevel(string pluginName, DiagnosticLevel level)
	{
		if (_allowPluginDiagnosticsLevels.Value)
		{
			if (logLevels.ContainsKey(pluginName))
			{
				logLevels.Add(pluginName, level);
			}
			else
			{
				logLevels[pluginName] = level;
			}
		}
		else if (_ownDiagnostics.Value >= DiagnosticLevel.debug)
		{
			Debug.Log((object)("Logging Plugin: Plugin Logging Levels Not Allowed. Ignoring " + pluginName + " SetLogLevel Request."));
		}
	}

	public static void SetLogLevel(DiagnosticLevel level)
	{
		logLevels.Add(Path.GetFileNameWithoutExtension(new StackTrace().GetFrame(1).GetMethod().Module.Name), level);
	}

	public static void LogError(string content)
	{
		Log(DiagnosticLevel.error, Path.GetFileNameWithoutExtension(new StackTrace().GetFrame(1).GetMethod().Module.Name), content);
	}

	public static void LogError(string friendlyName, string content)
	{
		Log(DiagnosticLevel.error, friendlyName, content);
	}

	public static void LogWarning(string content)
	{
		Log(DiagnosticLevel.warning, Path.GetFileNameWithoutExtension(new StackTrace().GetFrame(1).GetMethod().Module.Name), content);
	}

	public static void LogWarning(string friendlyName, string content)
	{
		Log(DiagnosticLevel.warning, friendlyName, content);
	}

	public static void LogInfo(string content)
	{
		Log(DiagnosticLevel.info, Path.GetFileNameWithoutExtension(new StackTrace().GetFrame(1).GetMethod().Module.Name), content);
	}

	public static void LogInfo(string friendlyName, string content)
	{
		Log(DiagnosticLevel.info, friendlyName, content);
	}

	public static void LogDebug(string content)
	{
		Log(DiagnosticLevel.debug, Path.GetFileNameWithoutExtension(new StackTrace().GetFrame(1).GetMethod().Module.Name), content);
	}

	public static void LogDebug(string friendlyName, string content)
	{
		Log(DiagnosticLevel.debug, friendlyName, content);
	}

	public static void LogTrace(string content)
	{
		Log(DiagnosticLevel.trace, Path.GetFileNameWithoutExtension(new StackTrace().GetFrame(1).GetMethod().Module.Name), content);
	}

	public static void LogTrace(string friendlyName, string content)
	{
		Log(DiagnosticLevel.trace, friendlyName, content);
	}

	public static void Log(DiagnosticLevel level, string content)
	{
		Log(DiagnosticLevel.trace, Path.GetFileNameWithoutExtension(new StackTrace().GetFrame(1).GetMethod().Module.Name), content);
	}

	public static DiagnosticLevel GetLogLevel()
	{
		string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(new StackTrace().GetFrame(1).GetMethod().Module.Name);
		if (logLevels.ContainsKey(fileNameWithoutExtension))
		{
			return logLevels[fileNameWithoutExtension];
		}
		return logLevels["*"];
	}

	public static void Log(DiagnosticLevel level, string name, string content)
	{
		DiagnosticLevel diagnosticLevel = (logLevels.ContainsKey(name) ? logLevels[name] : logLevels["*"]);
		if (_ownDiagnostics.Value >= DiagnosticLevel.trace)
		{
			Debug.Log((object)("Logging Plugin: Format = " + _format.Value));
			Debug.Log((object)("Logging Plugin: " + name + " Message Level = " + level));
			Debug.Log((object)("Logging Plugin: " + name + " Include Level = " + diagnosticLevel));
		}
		if (level <= diagnosticLevel)
		{
			string value = _format.Value;
			value = value.Replace("{NAME!}", name);
			value = value.Replace("{NAME}", (name + new string(' ', _paddingName.Value)).Substring(0, _paddingName.Value));
			value = value.Replace("{LEVEL!}", level.ToString().Substring(0, 1).ToUpper() + level.ToString().Substring(1).ToLower());
			value = value.Replace("{LEVEL}", (level.ToString().Substring(0, 1).ToUpper() + level.ToString().Substring(1).ToLower() + "       ").Substring(0, 7));
			string text = value;
			int num = (int)level;
			value = text.Replace("{LEVEL#}", num.ToString());
			value = value.Replace("{TIMESTAMP}", "{DATE} {TIME}");
			value = value.Replace("{DATE}", DateTime.Now.Year.ToString("0000") + "." + DateTime.Now.Month.ToString("00") + "." + DateTime.Now.Day.ToString("00"));
			value = value.Replace("{TIME}", DateTime.Now.Hour.ToString("00") + ":" + DateTime.Now.Minute.ToString("00") + ":" + DateTime.Now.Second.ToString("00") + "." + DateTime.Now.Millisecond.ToString("000"));
			value = value.Replace("{CONTENT}", content);
			Debug.Log((object)value);
		}
		if (subscriptions.ContainsKey(name))
		{
			if (_ownDiagnostics.Value >= DiagnosticLevel.trace)
			{
				Debug.Log((object)("Subscription Found For " + name));
			}
			if (level <= subscriptions[name].logLevel)
			{
				subscriptions[name].callback(name, level, content);
			}
		}
		else if (_ownDiagnostics.Value >= DiagnosticLevel.trace)
		{
			Debug.Log((object)("No Subscription Found For " + name));
		}
	}

	public static void Subscribe(Action<string, DiagnosticLevel, string> callback, DiagnosticLevel logLevel, string source)
	{
		subscriptions.Add(source, new Subscription
		{
			logLevel = logLevel,
			callback = callback
		});
	}
}