Browse Source

Added support for child loggers, moved to using them fore more differentiation within IPA

piracy-check
Anairkoen Schno 6 years ago
parent
commit
60fa80215c
9 changed files with 167 additions and 50 deletions
  1. +2
    -2
      IPA.Injector/Injector.cs
  2. +1
    -0
      IPA.Loader/IPA.Loader.csproj
  3. +3
    -3
      IPA.Loader/Loader/PluginManager.cs
  4. +3
    -0
      IPA.Loader/Logging/Logger.cs
  5. +1
    -1
      IPA.Loader/Logging/Printers/PluginLogFilePrinter.cs
  6. +60
    -0
      IPA.Loader/Logging/Printers/PluginSubLogPrinter.cs
  7. +54
    -1
      IPA.Loader/Logging/StandardLogger.cs
  8. +5
    -5
      IPA.Loader/Updating/Backup/BackupUnit.cs
  9. +38
    -38
      IPA.Loader/Updating/ModsaberML/Updater.cs

+ 2
- 2
IPA.Injector/Injector.cs View File

@ -44,7 +44,7 @@ namespace IPA.Injector
}
private static void Log(Level lvl, string message)
{ // multiple proxy methods to delay loading of assemblies until it's done
if (Logger.LogCreated)
if (LogCreated)
AssemblyLibLoaderCallLogger(lvl, message);
else
if (((byte)lvl & (byte)StandardLogger.PrintFilter) != 0)
@ -52,7 +52,7 @@ namespace IPA.Injector
}
private static void AssemblyLibLoaderCallLogger(Level lvl, string message)
{
Logger.log.Log(lvl, message);
libLoader.Log(lvl, message);
}
private static void Bootstrapper_Destroyed()


+ 1
- 0
IPA.Loader/IPA.Loader.csproj View File

@ -56,6 +56,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Loader\Composite\CompositeBSPlugin.cs" />
<Compile Include="Logging\Printers\PluginSubLogPrinter.cs" />
<Compile Include="PluginInterfaces\BeatSaber\IBeatSaberPlugin.cs" />
<Compile Include="PluginInterfaces\BeatSaber\IEnhancedBeatSaberPlugin.cs" />
<Compile Include="PluginInterfaces\BeatSaber\ModsaberModInfo.cs" />


+ 3
- 3
IPA.Loader/Loader/PluginManager.cs View File

@ -177,7 +177,7 @@ namespace IPA.Loader
}
catch (Exception e)
{
Logger.log.Error($"Could not load plugin {t.FullName} in {Path.GetFileName(file)}! {e}");
Logger.loader.Error($"Could not load plugin {t.FullName} in {Path.GetFileName(file)}! {e}");
}
}
@ -276,7 +276,7 @@ namespace IPA.Loader
}
catch (AmbiguousMatchException)
{
Logger.log.Error($"Only one Init allowed per plugin");
Logger.loader.Error($"Only one Init allowed per plugin");
}
}
else
@ -292,7 +292,7 @@ namespace IPA.Loader
}
catch (Exception e)
{
Logger.log.Error($"Could not load {Path.GetFileName(file)}! {e}");
Logger.loader.Error($"Could not load {Path.GetFileName(file)}! {e}");
}
return new Tuple<IEnumerable<BSPluginMeta>, IEnumerable<IPlugin>>(bsPlugins, ipaPlugins);


+ 3
- 0
IPA.Loader/Logging/Logger.cs View File

@ -21,6 +21,9 @@ namespace IPA.Logging
return _log;
}
}
internal static Logger updater => log.GetChildLogger("Upater");
internal static Logger libLoader => log.GetChildLogger("LibraryLoader");
internal static Logger loader => log.GetChildLogger("Loader");
internal static bool LogCreated => _log != null;
/// <summary>


+ 1
- 1
IPA.Loader/Logging/Printers/PluginLogFilePrinter.cs View File

@ -51,7 +51,7 @@ namespace IPA.Logging.Printers
public override void Print(Logger.Level level, DateTime time, string logName, string message)
{
foreach (var line in message.Split(new string[] { "\n", Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries))
fileWriter.WriteLine(string.Format("[{2} @ {1:HH:mm:ss}] {0}", line, time, level.ToString().ToUpper()));
fileWriter.WriteLine(string.Format(Logger.LogFormat, line, logName, time, level.ToString().ToUpper()));
}
}
}

+ 60
- 0
IPA.Loader/Logging/Printers/PluginSubLogPrinter.cs View File

@ -0,0 +1,60 @@
using IPA.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IPA.Logging.Printers
{
/// <summary>
/// Prints log messages to the file specified by the name.
/// </summary>
public class PluginSubLogPrinter : GZFilePrinter
{
/// <summary>
/// Provides a filter for this specific printer.
/// </summary>
public override Logger.LogLevel Filter { get; set; } = Logger.LogLevel.All;
private string name;
private string mainName;
/// <summary>
/// Gets the <see cref="FileInfo"/> for the target file.
/// </summary>
/// <returns></returns>
protected override FileInfo GetFileInfo()
{
var logsDir = new DirectoryInfo(Path.Combine("Logs", mainName, name));
logsDir.Create();
var finfo = new FileInfo(Path.Combine(logsDir.FullName, $"{DateTime.Now:yyyy.MM.dd.HH.mm}.log"));
return finfo;
}
/// <summary>
/// Creates a new printer with the given name.
/// </summary>
/// <param name="mainname">the name of the main logger</param>
/// <param name="name">the name of the logger</param>
public PluginSubLogPrinter(string mainname, string name)
{
this.name = name;
mainName = mainname;
}
/// <summary>
/// Prints an entry to the associated file.
/// </summary>
/// <param name="level">the <see cref="Logger.Level"/> of the message</param>
/// <param name="time">the <see cref="DateTime"/> the message was recorded at</param>
/// <param name="logName">the name of the log that sent the message</param>
/// <param name="message">the message to print</param>
public override void Print(Logger.Level level, DateTime time, string logName, string message)
{
foreach (var line in message.Split(new string[] { "\n", Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries))
fileWriter.WriteLine(string.Format("[{3} @ {2:HH:mm:ss}] {0}", line, logName, time, level.ToString().ToUpper()));
}
}
}

+ 54
- 1
IPA.Loader/Logging/StandardLogger.cs View File

@ -48,13 +48,15 @@ namespace IPA.Logging
};
private string logName;
private static bool showSourceClass = true;
private static readonly bool showSourceClass = true;
/// <summary>
/// All levels defined by this filter will be sent to loggers. All others will be ignored.
/// </summary>
public static LogLevel PrintFilter { get; set; } = LogLevel.InfoUp;
private List<LogPrinter> printers = new List<LogPrinter>(defaultPrinters);
private Dictionary<string, StandardLogger> children = new Dictionary<string, StandardLogger>();
static StandardLogger()
{
if (ModPrefs.GetBool("IPA", "PrintDebug", false, true))
@ -62,6 +64,22 @@ namespace IPA.Logging
showSourceClass = ModPrefs.GetBool("IPA", "DebugShowCallSource", false, true);
}
private StandardLogger(string mainName, string subName, params LogPrinter[] inherited)
{
logName = $"{mainName}/{subName}";
printers = new List<LogPrinter>(inherited)
{
new PluginSubLogPrinter(mainName, subName)
};
if (_logThread == null || !_logThread.IsAlive)
{
_logThread = new Thread(LogThread);
_logThread.Start();
}
}
internal StandardLogger(string name)
{
logName = name;
@ -75,6 +93,17 @@ namespace IPA.Logging
}
}
internal StandardLogger GetChild(string name)
{
if (!children.TryGetValue(name, out StandardLogger chld))
{
chld = new StandardLogger(logName, name, printers.ToArray());
children.Add(name, chld);
}
return chld;
}
/// <summary>
/// Logs a specific message at a given level.
/// </summary>
@ -164,4 +193,28 @@ namespace IPA.Logging
_logThread.Join();
}
}
/// <summary>
/// A class providing extensions for various loggers.
/// </summary>
public static class LoggerExtensions
{
/// <summary>
/// Gets a child logger, if supported.
/// </summary>
/// <param name="logger">the parent <see cref="Logger"/></param>
/// <param name="name">the name of the child</param>
/// <returns>the child logger</returns>
public static Logger GetChildLogger(this Logger logger, string name)
{
if (logger is StandardLogger)
{
return (logger as StandardLogger).GetChild(name);
}
else
{
throw new InvalidOperationException();
}
}
}
}

+ 5
- 5
IPA.Loader/Updating/Backup/BackupUnit.cs View File

@ -77,7 +77,7 @@ namespace IPA.Updating.Backup
if (_Files.Contains(relativePath))
{
Logger.log.Debug($"Skipping backup of {relativePath}");
Logger.updater.Debug($"Skipping backup of {relativePath}");
return;
}
@ -110,7 +110,7 @@ namespace IPA.Updating.Backup
{
foreach (var relativePath in _Files)
{
Logger.log.Debug($"Restoring {relativePath}");
Logger.updater.Debug($"Restoring {relativePath}");
// Original version
var backupFile = new FileInfo(Path.Combine(_BackupPath.FullName, relativePath));
var target = new FileInfo(Path.Combine(Environment.CurrentDirectory, relativePath));
@ -119,13 +119,13 @@ namespace IPA.Updating.Backup
{
if (backupFile.Length > 0)
{
Logger.log.Debug($" {backupFile.FullName} => {target.FullName}");
Logger.updater.Debug($" {backupFile.FullName} => {target.FullName}");
target.Directory.Create();
backupFile.CopyTo(target.FullName, true);
}
else
{
Logger.log.Debug($" x {target.FullName}");
Logger.updater.Debug($" x {target.FullName}");
if (target.Exists)
{
target.Delete();
@ -134,7 +134,7 @@ namespace IPA.Updating.Backup
}
else
{
Logger.log.Error("Backup not found!");
Logger.updater.Error("Backup not found!");
}
}
}


+ 38
- 38
IPA.Loader/Updating/ModsaberML/Updater.cs View File

@ -38,7 +38,7 @@ namespace IPA.Updating.ModsaberML
}
catch (Exception e)
{
Logger.log.Error(e);
Logger.updater.Error(e);
}
}
@ -55,7 +55,7 @@ namespace IPA.Updating.ModsaberML
IEnumerator CheckForUpdatesCoroutine()
{
Logger.log.Info("Checking for mod updates...");
Logger.updater.Info("Checking for mod updates...");
var toUpdate = new List<UpdateStruct>();
var GameVersion = new Version(Application.version);
@ -71,20 +71,20 @@ namespace IPA.Updating.ModsaberML
if (request.isNetworkError)
{
Logger.log.Error("Network error while trying to update mods");
Logger.log.Error(request.error);
Logger.updater.Error("Network error while trying to update mods");
Logger.updater.Error(request.error);
continue;
}
if (request.isHttpError)
{
if (request.responseCode == 404)
{
Logger.log.Error($"Mod {plugin.Plugin.Name} not found under name {info.InternalName}");
Logger.updater.Error($"Mod {plugin.Plugin.Name} not found under name {info.InternalName}");
continue;
}
Logger.log.Error($"Server returned an error code while trying to update mod {plugin.Plugin.Name}");
Logger.log.Error(request.error);
Logger.updater.Error($"Server returned an error code while trying to update mod {plugin.Plugin.Name}");
Logger.updater.Error(request.error);
continue;
}
@ -94,23 +94,23 @@ namespace IPA.Updating.ModsaberML
try
{
modRegistry = JsonConvert.DeserializeObject<ApiEndpoint.Mod>(json);
Logger.log.Debug(modRegistry.ToString());
Logger.updater.Debug(modRegistry.ToString());
}
catch (Exception e)
{
Logger.log.Error($"Parse error while trying to update mods");
Logger.log.Error(e);
Logger.updater.Error($"Parse error while trying to update mods");
Logger.updater.Error(e);
continue;
}
Logger.log.Debug($"Found Modsaber.ML registration for {plugin.Plugin.Name} ({info.InternalName})");
Logger.log.Debug($"Installed version: {info.CurrentVersion}; Latest version: {modRegistry.Version}");
Logger.updater.Debug($"Found Modsaber.ML registration for {plugin.Plugin.Name} ({info.InternalName})");
Logger.updater.Debug($"Installed version: {info.CurrentVersion}; Latest version: {modRegistry.Version}");
if (modRegistry.Version > info.CurrentVersion)
{
Logger.log.Debug($"{plugin.Plugin.Name} needs an update!");
Logger.updater.Debug($"{plugin.Plugin.Name} needs an update!");
if (modRegistry.GameVersion == GameVersion)
{
Logger.log.Debug($"Queueing update...");
Logger.updater.Debug($"Queueing update...");
toUpdate.Add(new UpdateStruct
{
plugin = plugin,
@ -119,13 +119,13 @@ namespace IPA.Updating.ModsaberML
}
else
{
Logger.log.Warn($"Update avaliable for {plugin.Plugin.Name}, but for a different Beat Saber version!");
Logger.updater.Warn($"Update avaliable for {plugin.Plugin.Name}, but for a different Beat Saber version!");
}
}
}
}
Logger.log.Info($"{toUpdate.Count} mods need updating");
Logger.updater.Info($"{toUpdate.Count} mods need updating");
if (toUpdate.Count == 0) yield break;
@ -149,19 +149,19 @@ namespace IPA.Updating.ModsaberML
protected override void ReceiveContentLength(int contentLength)
{
Stream.Capacity = contentLength;
Logger.log.Debug($"Got content length: {contentLength}");
Logger.updater.Debug($"Got content length: {contentLength}");
}
protected override void CompleteContent()
{
Logger.log.Debug("Download complete");
Logger.updater.Debug("Download complete");
}
protected override bool ReceiveData(byte[] data, int dataLength)
{
if (data == null || data.Length < 1)
{
Logger.log.Debug("CustomWebRequest :: ReceiveData - received a null/empty buffer");
Logger.updater.Debug("CustomWebRequest :: ReceiveData - received a null/empty buffer");
return false;
}
@ -184,7 +184,7 @@ namespace IPA.Updating.ModsaberML
private void ExtractPluginAsync(MemoryStream stream, UpdateStruct item, ApiEndpoint.Mod.PlatformFile fileInfo, string tempDirectory)
{
Logger.log.Debug($"Extracting ZIP file for {item.plugin.Plugin.Name}");
Logger.updater.Debug($"Extracting ZIP file for {item.plugin.Plugin.Name}");
var data = stream.GetBuffer();
SHA1 sha = new SHA1CryptoServiceProvider();
@ -201,12 +201,12 @@ namespace IPA.Updating.ModsaberML
using (var zipFile = ZipFile.Read(stream))
{
Logger.log.Debug("Streams opened");
Logger.updater.Debug("Streams opened");
foreach (var entry in zipFile)
{
if (entry.IsDirectory)
{
Logger.log.Debug($"Creating directory {entry.FileName}");
Logger.updater.Debug($"Creating directory {entry.FileName}");
Directory.CreateDirectory(Path.Combine(Environment.CurrentDirectory, entry.FileName));
}
else
@ -233,7 +233,7 @@ namespace IPA.Updating.ModsaberML
else
newFiles.Add(targetFile);
Logger.log.Debug($"Extracting file {targetFile.FullName}");
Logger.updater.Debug($"Extracting file {targetFile.FullName}");
var fstream = targetFile.Create();
ostream.CopyTo(fstream);
@ -266,12 +266,12 @@ namespace IPA.Updating.ModsaberML
backup.Delete();
Logger.log.Debug("Downloader exited");
Logger.updater.Debug("Downloader exited");
}
IEnumerator UpdateModCoroutine(UpdateStruct item, string tempDirectory)
{
Logger.log.Debug($"Steam avaliable: {SteamCheck.IsAvailable}");
Logger.updater.Debug($"Steam avaliable: {SteamCheck.IsAvailable}");
ApiEndpoint.Mod.PlatformFile platformFile;
if (SteamCheck.IsAvailable || item.externInfo.Files.Oculus == null)
@ -281,14 +281,14 @@ namespace IPA.Updating.ModsaberML
string url = platformFile.DownloadPath;
Logger.log.Debug($"URL = {url}");
Logger.updater.Debug($"URL = {url}");
const int MaxTries = 3;
int maxTries = MaxTries;
while (maxTries > 0)
{
if (maxTries-- != MaxTries)
Logger.log.Info($"Re-trying download...");
Logger.updater.Info($"Re-trying download...");
using (var stream = new MemoryStream())
using (var request = UnityWebRequest.Get(url))
@ -297,22 +297,22 @@ namespace IPA.Updating.ModsaberML
var dlh = new StreamDownloadHandler(stream);
request.downloadHandler = dlh;
Logger.log.Debug("Sending request");
//Logger.log.Debug(request?.downloadHandler?.ToString() ?? "DLH==NULL");
Logger.updater.Debug("Sending request");
//Logger.updater.Debug(request?.downloadHandler?.ToString() ?? "DLH==NULL");
yield return request.SendWebRequest();
Logger.log.Debug("Download finished");
Logger.updater.Debug("Download finished");
if (request.isNetworkError)
{
Logger.log.Error("Network error while trying to update mod");
Logger.log.Error(request.error);
Logger.updater.Error("Network error while trying to update mod");
Logger.updater.Error(request.error);
taskTokenSource.Cancel();
continue;
}
if (request.isHttpError)
{
Logger.log.Error($"Server returned an error code while trying to update mod");
Logger.log.Error(request.error);
Logger.updater.Error($"Server returned an error code while trying to update mod");
Logger.updater.Error(request.error);
taskTokenSource.Cancel();
continue;
}
@ -329,8 +329,8 @@ namespace IPA.Updating.ModsaberML
if (downloadTask.IsFaulted)
{
Logger.log.Error($"Error downloading mod {item.plugin.Plugin.Name}");
Logger.log.Error(downloadTask.Exception);
Logger.updater.Error($"Error downloading mod {item.plugin.Plugin.Name}");
Logger.updater.Error(downloadTask.Exception);
continue;
}
@ -339,9 +339,9 @@ namespace IPA.Updating.ModsaberML
}
if (maxTries == 0)
Logger.log.Warn($"Plugin download failed {MaxTries} times, not re-trying");
Logger.updater.Warn($"Plugin download failed {MaxTries} times, not re-trying");
else
Logger.log.Debug("Download complete");
Logger.updater.Debug("Download complete");
}
}
}

Loading…
Cancel
Save