From 2a85f63f025ed3674929c03ca5f4c0d35381636d Mon Sep 17 00:00:00 2001 From: LazuliKao Date: Sun, 30 Jul 2023 23:16:13 +0800 Subject: [PATCH] feat: optimize console in js show js filename in logger --- src/Hook/JsLog/JsLog.cs | 61 ++++++++++++++++++++++++++++------------- 1 file changed, 42 insertions(+), 19 deletions(-) diff --git a/src/Hook/JsLog/JsLog.cs b/src/Hook/JsLog/JsLog.cs index 2dd2dea..7e2cc0a 100644 --- a/src/Hook/JsLog/JsLog.cs +++ b/src/Hook/JsLog/JsLog.cs @@ -1,5 +1,7 @@ using System.Runtime.InteropServices; using System.Text; +using Hosihikari.Logging; +using Hosihikari.VanillaScript.Assets; using Hosihikari.VanillaScript.QuickJS; using Hosihikari.VanillaScript.QuickJS.Extensions; using Hosihikari.VanillaScript.QuickJS.Helper; @@ -9,6 +11,23 @@ namespace Hosihikari.VanillaScript.Hook.JsLog; internal class JsLog { + private static Dictionary _loggers = new(); + + private static unsafe Logger GetJsLogger(JsContext* ctx) + { + var file = Native.JS_GetScriptOrModuleName(ctx, 1); + if (string.IsNullOrWhiteSpace(file) || file == Prepare.EntryPointJsName) + { + return Log.Logger; + } + if (_loggers.TryGetValue(file, out var logger)) + return logger; + logger = new Logger(file); + logger.SetupConsole(); + _loggers.Add(file, logger); + return logger; + } + public static unsafe void Bind(JsContext* ctx, JsValue globalObject) { using var consoleInstance = Native.JS_NewObject(ctx); @@ -21,15 +40,16 @@ static JsValue PrintTrace(JsContext* ctx, JsValue thisObj, int argCount, JsValue try { var (file, line) = GetJsSourceInfo(ctx); - Log.Logger.Trace( - ParseLog(ctx, new ReadOnlySpan(argvIn, argCount)), - sourceFile: file, - sourceLine: line - ); + GetJsLogger(ctx) + .Trace( + ParseLog(ctx, new ReadOnlySpan(argvIn, argCount)), + sourceFile: file, + sourceLine: line + ); } catch (Exception ex) { - Log.Logger.Error("PrintTrace Invoke Failed", ex); + GetJsLogger(ctx).Error("PrintTrace Invoke Failed", ex); } return JsValueCreateHelper.Undefined; } @@ -43,11 +63,12 @@ static JsValue PrintInfo(JsContext* ctx, JsValue thisObj, int argCount, JsValue* { try { - Log.Logger.Information(ParseLog(ctx, new ReadOnlySpan(argvIn, argCount))); + GetJsLogger(ctx) + .Information(ParseLog(ctx, new ReadOnlySpan(argvIn, argCount))); } catch (Exception ex) { - Log.Logger.Error("PrintInfo Invoke Failed", ex); + GetJsLogger(ctx).Error("PrintInfo Invoke Failed", ex); } return JsValueCreateHelper.Undefined; } @@ -59,11 +80,11 @@ static JsValue PrintDebug(JsContext* ctx, JsValue thisObj, int argCount, JsValue { try { - Log.Logger.Debug(ParseLog(ctx, new ReadOnlySpan(argvIn, argCount))); + GetJsLogger(ctx).Debug(ParseLog(ctx, new ReadOnlySpan(argvIn, argCount))); } catch (Exception ex) { - Log.Logger.Error("PrintDebug Invoke Failed", ex); + GetJsLogger(ctx).Error("PrintDebug Invoke Failed", ex); } return JsValueCreateHelper.Undefined; } @@ -75,11 +96,12 @@ static JsValue PrintWarn(JsContext* ctx, JsValue thisObj, int argCount, JsValue* { try { - Log.Logger.Warning(ParseLog(ctx, new ReadOnlySpan(argvIn, argCount))); + GetJsLogger(ctx) + .Warning(ParseLog(ctx, new ReadOnlySpan(argvIn, argCount))); } catch (Exception ex) { - Log.Logger.Error("PrintWarn Invoke Failed", ex); + GetJsLogger(ctx).Error("PrintWarn Invoke Failed", ex); } return JsValueCreateHelper.Undefined; } @@ -92,15 +114,16 @@ static JsValue PrintError(JsContext* ctx, JsValue thisObj, int argCount, JsValue try { var (file, line) = GetJsSourceInfo(ctx); - Log.Logger.Error( - ParseLog(ctx, new ReadOnlySpan(argvIn, argCount)), - sourceFile: file, - sourceLine: line - ); + GetJsLogger(ctx) + .Error( + ParseLog(ctx, new ReadOnlySpan(argvIn, argCount)), + sourceFile: file, + sourceLine: line + ); } catch (Exception ex) { - Log.Logger.Error("PrintError Invoke Failed", ex); + GetJsLogger(ctx).Error("PrintError Invoke Failed", ex); } return JsValueCreateHelper.Undefined; } @@ -115,7 +138,7 @@ static JsValue PrintError(JsContext* ctx, JsValue thisObj, int argCount, JsValue // var argv = new ReadOnlySpan(argvIn, argCount); // if (argv[0].IsFalsey(ctx)) // { - // Log.Logger.Error(ParseLog(ctx, argv.Slice(1))); + // GetJsLogger(ctx).Error(ParseLog(ctx, argv.Slice(1))); // } // return JsValueCreateHelper.Undefined; //}