Decompiled source of RotatingLogs v1.0.0

RotatingLogs.dll

Decompiled a week ago
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using Microsoft.CodeAnalysis;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("RotatingLogs")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+6acc394ca5bf1a68dd6324b553573f5102e444b9")]
[assembly: AssemblyProduct("RotatingLogs")]
[assembly: AssemblyTitle("RotatingLogs")]
[assembly: AssemblyMetadata("RepositoryUrl", "https://github.com/flibber-hk/Silksong.RotatingLogs")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace BepInEx
{
	[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
	[Conditional("CodeGeneration")]
	internal sealed class BepInAutoPluginAttribute : Attribute
	{
		public BepInAutoPluginAttribute(string? id = null, string? name = null, string? version = null)
		{
		}
	}
}
namespace BepInEx.Preloader.Core.Patching
{
	[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
	[Conditional("CodeGeneration")]
	internal sealed class PatcherAutoPluginAttribute : Attribute
	{
		public PatcherAutoPluginAttribute(string? id = null, string? name = null, string? version = null)
		{
		}
	}
}
namespace RotatingLogs
{
	[BepInPlugin("io.github.flibber-hk.rotatinglogs", "RotatingLogs", "1.0.0")]
	public class RotatingLogsPlugin : BaseUnityPlugin
	{
		private const string MAX_LOGS_DESC = "At startup, delete all but the most recent X logs.\nSet the value to -1 to ignore this setting.\nThis value is only used at startup.";

		private const string MAX_DAYS_DESC = "At startup, delete logs older than X days.\nSet the value to -1 to ignore this setting.\nThis value is only used at startup.";

		private const string datetimeFormat = "yyyy-MM-dd_HH-mm-ss";

		private string logPath = Path.Combine(Paths.BepInExRootPath, "LogOutput.log");

		private string backupDir = Path.Combine(Paths.BepInExRootPath, "OldLogs");

		private string? logBackup;

		private ConfigEntry<int> MaxLogs;

		private ConfigEntry<int> MaxDays;

		public const string Id = "io.github.flibber-hk.rotatinglogs";

		public static string Name => "RotatingLogs";

		public static string Version => "1.0.0";

		private void Awake()
		{
			MaxLogs = ((BaseUnityPlugin)this).Config.Bind<int>("General", "MaxLogs", 20, "At startup, delete all but the most recent X logs.\nSet the value to -1 to ignore this setting.\nThis value is only used at startup.");
			MaxDays = ((BaseUnityPlugin)this).Config.Bind<int>("General", "MaxDays", 7, "At startup, delete logs older than X days.\nSet the value to -1 to ignore this setting.\nThis value is only used at startup.");
			if (!File.Exists(logPath))
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Not rotating logs: Log path not found");
				return;
			}
			Directory.CreateDirectory(backupDir);
			string text = DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss");
			logBackup = Path.Combine(backupDir, "LogOutput_" + text + ".log");
			CleanupOldLogs();
			BackupLog();
			AddDiskLog();
			((BaseUnityPlugin)this).Logger.LogInfo((object)("Plugin " + Name + " (io.github.flibber-hk.rotatinglogs) has loaded!"));
		}

		private void AddDiskLog()
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0014: Expected O, but got Unknown
			LogLevel val = (LogLevel)63;
			bool flag = true;
			DiskLogListener item = new DiskLogListener(logBackup, val, true, flag);
			Logger.Listeners.Add((ILogListener)(object)item);
		}

		private void CleanupOldLogs()
		{
			try
			{
				CleanupOldLogsInternal();
			}
			catch
			{
			}
		}

		private void CleanupOldLogsInternal()
		{
			foreach (var (num, fileInfo) in (from f in Directory.GetFiles(backupDir, "LogOutput_*.log")
				select new FileInfo(f) into f
				orderby f.CreationTime descending
				select f).ToList().Select((FileInfo file, int idx) => (idx, file)))
			{
				if (num == 0)
				{
					continue;
				}
				if (MaxLogs.Value != -1 && num >= MaxLogs.Value)
				{
					try
					{
						fileInfo.Delete();
					}
					catch
					{
					}
					continue;
				}
				DateTime dateTime;
				try
				{
					dateTime = DateTime.ParseExact(fileInfo.Name.Substring(10, 19), "yyyy-MM-dd_HH-mm-ss", CultureInfo.InvariantCulture);
				}
				catch (Exception)
				{
					continue;
				}
				TimeSpan timeSpan = DateTime.Now - dateTime;
				if (MaxDays.Value != -1 && timeSpan.Days > MaxDays.Value)
				{
					try
					{
						fileInfo.Delete();
					}
					catch
					{
					}
				}
			}
		}

		private void FlushLogs()
		{
			try
			{
				foreach (DiskLogListener item in Logger.Listeners.OfType<DiskLogListener>())
				{
					item.LogWriter.Flush();
				}
			}
			catch
			{
			}
		}

		private void BackupLog()
		{
			if (string.IsNullOrEmpty(logBackup))
			{
				return;
			}
			try
			{
				FlushLogs();
				File.Copy(logPath, logBackup, overwrite: true);
			}
			catch
			{
			}
		}
	}
}