using System; using System.Diagnostics; using System.IO; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Runtime.Versioning; using System.Text; using BepInEx.Logging; [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)] [assembly: AssemblyTitle("TurronishLogger")] [assembly: AssemblyDescription("DLL 6 — Logger / Diagnostics del Turronish Mod")] [assembly: AssemblyCompany("")] [assembly: AssemblyProduct("TurronishMod")] [assembly: AssemblyCopyright("")] [assembly: ComVisible(false)] [assembly: AssemblyFileVersion("1.0.0.0")] [assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")] [assembly: AssemblyVersion("1.0.0.0")] namespace TurronishMod.Logger; public enum TLogLevel { DEBUG, INFO, WARN, ERROR } public enum TLogMode { DEV, PROD, SILENT } public static class TurronishDiagLogger { internal const string SAL_SESION = "QmxpeA==SmF2"; private static ManualLogSource _bepLog; private static StreamWriter _file; private static readonly object _fileLock = new object(); public static TLogMode Mode { get; set; } = TLogMode.PROD; public static bool Verbose { get; set; } = false; public static string RutaArchivo { get; private set; } public static void Initialize(ManualLogSource bepLog, string logDirectory = null, TLogMode mode = TLogMode.PROD) { _bepLog = bepLog; Mode = mode; if (mode == TLogMode.SILENT || logDirectory == null) { WriteConsole(TLogLevel.INFO, "Logger", $"TurronishLogger iniciado. Modo={mode} | Sin archivo."); return; } try { Directory.CreateDirectory(logDirectory); string text = DateTime.Now.ToString("yyyyMMdd_HHmm"); RutaArchivo = Path.Combine(logDirectory, "turronish_" + text + ".txt"); _file = new StreamWriter(RutaArchivo, append: false, Encoding.UTF8) { AutoFlush = true }; string value = "╔══════════════════════════════════════════╗\n║ TURRONISH MOD — Sesion de Diagnostico ║\n" + $"║ Inicio : {DateTime.Now:yyyy-MM-dd HH:mm:ss} ║\n" + $"║ Modo : {mode,-10} ║\n" + "╚══════════════════════════════════════════╝"; lock (_fileLock) { _file.WriteLine(value); } WriteConsole(TLogLevel.INFO, "Logger", $"TurronishLogger iniciado. Modo={mode} | Archivo: {RutaArchivo}"); } catch (Exception ex) { _file = null; RutaArchivo = null; WriteConsole(TLogLevel.WARN, "Logger", "No se pudo crear archivo de log: " + ex.Message); } } public static void Info(string tag, string msg) { Write(TLogLevel.INFO, tag, msg); } public static void Warn(string tag, string msg) { Write(TLogLevel.WARN, tag, msg); } public static void Error(string tag, string msg) { Write(TLogLevel.ERROR, tag, msg); } public static void Debug(string tag, string msg) { if (Verbose) { Write(TLogLevel.DEBUG, tag, msg); } } public static void Sesion(string tag, string msg) { if (_file != null) { EscribirArchivo(TLogLevel.INFO, tag, "[SESION] " + msg); } } public static void CerrarSesion() { if (_file == null) { return; } EscribirArchivo(TLogLevel.INFO, "Logger", "Sesion cerrada."); lock (_fileLock) { try { _file.Flush(); _file.Close(); } catch { } finally { _file = null; } } } private static void Write(TLogLevel level, string tag, string msg) { bool flag = true; bool flag2 = true; switch (Mode) { case TLogMode.SILENT: flag = false; flag2 = level >= TLogLevel.WARN; break; case TLogMode.PROD: flag = level >= TLogLevel.WARN || (level == TLogLevel.INFO && Verbose); if (level == TLogLevel.DEBUG) { return; } break; } if (flag2) { WriteConsole(level, tag, msg); } if (flag) { EscribirArchivo(level, tag, msg); } } private static void WriteConsole(TLogLevel level, string tag, string msg) { if (_bepLog != null) { string text = "[" + tag + "] " + msg; switch (level) { case TLogLevel.INFO: _bepLog.LogInfo((object)text); break; case TLogLevel.WARN: _bepLog.LogWarning((object)text); break; case TLogLevel.ERROR: _bepLog.LogError((object)text); break; case TLogLevel.DEBUG: _bepLog.LogDebug((object)text); break; } } } private static void EscribirArchivo(TLogLevel level, string tag, string msg) { if (_file == null) { return; } string text = DateTime.Now.ToString("HH:mm:ss.fff"); string text2 = level.ToString().PadRight(5); string value = "[" + text + "][" + text2 + "][" + tag + "] " + msg; lock (_fileLock) { try { _file.WriteLine(value); } catch { } } } }