From a0b3fc8a6674b161d291c5a119444410e4c22ae2 Mon Sep 17 00:00:00 2001 From: Tony Date: Fri, 6 Dec 2024 20:13:10 +0800 Subject: [PATCH 1/7] Fix failed to get caller location throws --- plugins/log/guest-js/index.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/plugins/log/guest-js/index.ts b/plugins/log/guest-js/index.ts index 6287f913f..4233692a5 100644 --- a/plugins/log/guest-js/index.ts +++ b/plugins/log/guest-js/index.ts @@ -60,11 +60,11 @@ function getCallerLocation(stack?: string) { const lines = stack.split('\n') // Find the third line (caller's caller of the current location) - const callerLine = lines[3].trim() + const callerLine = lines[3]?.trim() const regex = /at\s+(?.*?)\s+\((?.*?):(?\d+):(?\d+)\)/ - const match = callerLine.match(regex) + const match = callerLine?.match(regex) if (match) { const { functionName, fileName, lineNumber, columnNumber } = @@ -79,7 +79,7 @@ function getCallerLocation(stack?: string) { // Handle cases where the regex does not match (e.g., last line without function name) const regexNoFunction = /at\s+(?.*?):(?\d+):(?\d+)/ - const matchNoFunction = callerLine.match(regexNoFunction) + const matchNoFunction = callerLine?.match(regexNoFunction) if (matchNoFunction) { const { fileName, lineNumber, columnNumber } = matchNoFunction.groups as { @@ -103,7 +103,7 @@ function getCallerLocation(stack?: string) { return name.length > 0 && location !== '[native code]' }) // Find the third line (caller's caller of the current location) - return filtered[2].filter((v) => v.length > 0).join('@') + return filtered[2]?.filter((v) => v.length > 0).join('@') } } From a2a29fd21e92ded90156bd6277a21a547e3b1d82 Mon Sep 17 00:00:00 2001 From: Tony Date: Fri, 6 Dec 2024 20:15:55 +0800 Subject: [PATCH 2/7] Add change file --- .changes/log-caller-location-throw.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changes/log-caller-location-throw.md diff --git a/.changes/log-caller-location-throw.md b/.changes/log-caller-location-throw.md new file mode 100644 index 000000000..ebdf7c1cd --- /dev/null +++ b/.changes/log-caller-location-throw.md @@ -0,0 +1,5 @@ +--- +"log-js": patch +--- + +Make log functions omit caller info when failed parsing it instead of throwing From 8aac008ebedcbf516cea2171a1333ea86caa4883 Mon Sep 17 00:00:00 2001 From: Tony Date: Fri, 6 Dec 2024 20:17:28 +0800 Subject: [PATCH 3/7] typo --- .changes/log-caller-location-throw.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changes/log-caller-location-throw.md b/.changes/log-caller-location-throw.md index ebdf7c1cd..37c25d897 100644 --- a/.changes/log-caller-location-throw.md +++ b/.changes/log-caller-location-throw.md @@ -2,4 +2,4 @@ "log-js": patch --- -Make log functions omit caller info when failed parsing it instead of throwing +Make log functions omit caller location when failed parsing it instead of throwing From 4de487935d1b50508f080dafad97fc5231332fa1 Mon Sep 17 00:00:00 2001 From: Tony Date: Fri, 6 Dec 2024 20:20:53 +0800 Subject: [PATCH 4/7] build --- plugins/log/api-iife.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/log/api-iife.js b/plugins/log/api-iife.js index 474375266..a959d45ba 100644 --- a/plugins/log/api-iife.js +++ b/plugins/log/api-iife.js @@ -1 +1 @@ -if("__TAURI__"in window){var __TAURI_PLUGIN_LOG__=function(e){"use strict";function n(e,n=!1){return window.__TAURI_INTERNALS__.transformCallback(e,n)}async function r(e,n={},r){return window.__TAURI_INTERNALS__.invoke(e,n,r)}var a,t;async function o(e,a,t){const o={kind:"Any"};return r("plugin:event|listen",{event:e,target:o,handler:n(a)}).then((n=>async()=>async function(e,n){await r("plugin:event|unlisten",{event:e,eventId:n})}(e,n)))}async function i(e,n,a){const t=function(e){if(e){if(!e.startsWith("Error"))return e.split("\n").map((e=>e.split("@"))).filter((([e,n])=>e.length>0&&"[native code]"!==n))[2].filter((e=>e.length>0)).join("@");{const n=e.split("\n")[3].trim(),r=/at\s+(?.*?)\s+\((?.*?):(?\d+):(?\d+)\)/,a=n.match(r);if(a){const{functionName:e,fileName:n,lineNumber:r,columnNumber:t}=a.groups;return`${e}@${n}:${r}:${t}`}{const e=/at\s+(?.*?):(?\d+):(?\d+)/,r=n.match(e);if(r){const{fileName:e,lineNumber:n,columnNumber:a}=r.groups;return`@${e}:${n}:${a}`}}}}}((new Error).stack),{file:o,line:i,keyValues:u}=a??{};await r("plugin:log|log",{level:e,message:n,location:t,file:o,line:i,keyValues:u})}async function u(e){return await o("log://log",(n=>{const{level:r}=n.payload;let{message:a}=n.payload;a=a.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g,""),e({message:a,level:r})}))}return"function"==typeof SuppressedError&&SuppressedError,function(e){e.WINDOW_RESIZED="tauri://resize",e.WINDOW_MOVED="tauri://move",e.WINDOW_CLOSE_REQUESTED="tauri://close-requested",e.WINDOW_DESTROYED="tauri://destroyed",e.WINDOW_FOCUS="tauri://focus",e.WINDOW_BLUR="tauri://blur",e.WINDOW_SCALE_FACTOR_CHANGED="tauri://scale-change",e.WINDOW_THEME_CHANGED="tauri://theme-changed",e.WINDOW_CREATED="tauri://window-created",e.WEBVIEW_CREATED="tauri://webview-created",e.DRAG_ENTER="tauri://drag-enter",e.DRAG_OVER="tauri://drag-over",e.DRAG_DROP="tauri://drag-drop",e.DRAG_LEAVE="tauri://drag-leave"}(a||(a={})),function(e){e[e.Trace=1]="Trace",e[e.Debug=2]="Debug",e[e.Info=3]="Info",e[e.Warn=4]="Warn",e[e.Error=5]="Error"}(t||(t={})),e.attachConsole=async function(){return await u((({level:e,message:n})=>{switch(e){case t.Trace:console.log(n);break;case t.Debug:console.debug(n);break;case t.Info:console.info(n);break;case t.Warn:console.warn(n);break;case t.Error:console.error(n);break;default:throw new Error(`unknown log level ${e}`)}}))},e.attachLogger=u,e.debug=async function(e,n){await i(t.Debug,e,n)},e.error=async function(e,n){await i(t.Error,e,n)},e.info=async function(e,n){await i(t.Info,e,n)},e.trace=async function(e,n){await i(t.Trace,e,n)},e.warn=async function(e,n){await i(t.Warn,e,n)},e}({});Object.defineProperty(window.__TAURI__,"log",{value:__TAURI_PLUGIN_LOG__})} +if("__TAURI__"in window){var __TAURI_PLUGIN_LOG__=function(e){"use strict";function n(e,n=!1){return window.__TAURI_INTERNALS__.transformCallback(e,n)}async function r(e,n={},r){return window.__TAURI_INTERNALS__.invoke(e,n,r)}var a,t;async function o(e,a,t){const o={kind:"Any"};return r("plugin:event|listen",{event:e,target:o,handler:n(a)}).then((n=>async()=>async function(e,n){await r("plugin:event|unlisten",{event:e,eventId:n})}(e,n)))}async function i(e,n,a){const t=function(e){if(e){if(!e.startsWith("Error")){const n=e.split("\n").map((e=>e.split("@"))).filter((([e,n])=>e.length>0&&"[native code]"!==n));return n[2]?.filter((e=>e.length>0)).join("@")}{const n=e.split("\n"),r=n[3]?.trim(),a=/at\s+(?.*?)\s+\((?.*?):(?\d+):(?\d+)\)/,t=r?.match(a);if(t){const{functionName:e,fileName:n,lineNumber:r,columnNumber:a}=t.groups;return`${e}@${n}:${r}:${a}`}{const e=/at\s+(?.*?):(?\d+):(?\d+)/,n=r?.match(e);if(n){const{fileName:e,lineNumber:r,columnNumber:a}=n.groups;return`@${e}:${r}:${a}`}}}}}((new Error).stack),{file:o,line:i,keyValues:u}=a??{};await r("plugin:log|log",{level:e,message:n,location:t,file:o,line:i,keyValues:u})}async function u(e){return await o("log://log",(n=>{const{level:r}=n.payload;let{message:a}=n.payload;a=a.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g,""),e({message:a,level:r})}))}return"function"==typeof SuppressedError&&SuppressedError,function(e){e.WINDOW_RESIZED="tauri://resize",e.WINDOW_MOVED="tauri://move",e.WINDOW_CLOSE_REQUESTED="tauri://close-requested",e.WINDOW_DESTROYED="tauri://destroyed",e.WINDOW_FOCUS="tauri://focus",e.WINDOW_BLUR="tauri://blur",e.WINDOW_SCALE_FACTOR_CHANGED="tauri://scale-change",e.WINDOW_THEME_CHANGED="tauri://theme-changed",e.WINDOW_CREATED="tauri://window-created",e.WEBVIEW_CREATED="tauri://webview-created",e.DRAG_ENTER="tauri://drag-enter",e.DRAG_OVER="tauri://drag-over",e.DRAG_DROP="tauri://drag-drop",e.DRAG_LEAVE="tauri://drag-leave"}(a||(a={})),function(e){e[e.Trace=1]="Trace",e[e.Debug=2]="Debug",e[e.Info=3]="Info",e[e.Warn=4]="Warn",e[e.Error=5]="Error"}(t||(t={})),e.attachConsole=async function(){return await u((({level:e,message:n})=>{switch(e){case t.Trace:console.log(n);break;case t.Debug:console.debug(n);break;case t.Info:console.info(n);break;case t.Warn:console.warn(n);break;case t.Error:console.error(n);break;default:throw new Error(`unknown log level ${e}`)}}))},e.attachLogger=u,e.debug=async function(e,n){await i(t.Debug,e,n)},e.error=async function(e,n){await i(t.Error,e,n)},e.info=async function(e,n){await i(t.Info,e,n)},e.trace=async function(e,n){await i(t.Trace,e,n)},e.warn=async function(e,n){await i(t.Warn,e,n)},e}({});Object.defineProperty(window.__TAURI__,"log",{value:__TAURI_PLUGIN_LOG__})} From f1624bd5d363a4e8cd3b73d6b220d63ed71edcaa Mon Sep 17 00:00:00 2001 From: Tony Date: Fri, 6 Dec 2024 20:21:59 +0800 Subject: [PATCH 5/7] Bump log rs --- .changes/log-caller-location-throw.md | 1 + 1 file changed, 1 insertion(+) diff --git a/.changes/log-caller-location-throw.md b/.changes/log-caller-location-throw.md index 37c25d897..014e065a3 100644 --- a/.changes/log-caller-location-throw.md +++ b/.changes/log-caller-location-throw.md @@ -1,4 +1,5 @@ --- +"log": patch "log-js": patch --- From ff381a5e9954cab7ad8cfbac8d29abfb60d90cf7 Mon Sep 17 00:00:00 2001 From: Tony Date: Fri, 6 Dec 2024 20:23:02 +0800 Subject: [PATCH 6/7] typo --- .changes/log-caller-location-throw.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changes/log-caller-location-throw.md b/.changes/log-caller-location-throw.md index 014e065a3..98ab68cd8 100644 --- a/.changes/log-caller-location-throw.md +++ b/.changes/log-caller-location-throw.md @@ -3,4 +3,4 @@ "log-js": patch --- -Make log functions omit caller location when failed parsing it instead of throwing +Make log functions omit caller location when failed to parse it instead of throwing From 6ac46f40fd52bfbd35f7a35ccfcfad7fc79e87c8 Mon Sep 17 00:00:00 2001 From: Tony Date: Fri, 6 Dec 2024 20:25:21 +0800 Subject: [PATCH 7/7] early return instead of using ? --- plugins/log/api-iife.js | 2 +- plugins/log/guest-js/index.ts | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/plugins/log/api-iife.js b/plugins/log/api-iife.js index a959d45ba..662117513 100644 --- a/plugins/log/api-iife.js +++ b/plugins/log/api-iife.js @@ -1 +1 @@ -if("__TAURI__"in window){var __TAURI_PLUGIN_LOG__=function(e){"use strict";function n(e,n=!1){return window.__TAURI_INTERNALS__.transformCallback(e,n)}async function r(e,n={},r){return window.__TAURI_INTERNALS__.invoke(e,n,r)}var a,t;async function o(e,a,t){const o={kind:"Any"};return r("plugin:event|listen",{event:e,target:o,handler:n(a)}).then((n=>async()=>async function(e,n){await r("plugin:event|unlisten",{event:e,eventId:n})}(e,n)))}async function i(e,n,a){const t=function(e){if(e){if(!e.startsWith("Error")){const n=e.split("\n").map((e=>e.split("@"))).filter((([e,n])=>e.length>0&&"[native code]"!==n));return n[2]?.filter((e=>e.length>0)).join("@")}{const n=e.split("\n"),r=n[3]?.trim(),a=/at\s+(?.*?)\s+\((?.*?):(?\d+):(?\d+)\)/,t=r?.match(a);if(t){const{functionName:e,fileName:n,lineNumber:r,columnNumber:a}=t.groups;return`${e}@${n}:${r}:${a}`}{const e=/at\s+(?.*?):(?\d+):(?\d+)/,n=r?.match(e);if(n){const{fileName:e,lineNumber:r,columnNumber:a}=n.groups;return`@${e}:${r}:${a}`}}}}}((new Error).stack),{file:o,line:i,keyValues:u}=a??{};await r("plugin:log|log",{level:e,message:n,location:t,file:o,line:i,keyValues:u})}async function u(e){return await o("log://log",(n=>{const{level:r}=n.payload;let{message:a}=n.payload;a=a.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g,""),e({message:a,level:r})}))}return"function"==typeof SuppressedError&&SuppressedError,function(e){e.WINDOW_RESIZED="tauri://resize",e.WINDOW_MOVED="tauri://move",e.WINDOW_CLOSE_REQUESTED="tauri://close-requested",e.WINDOW_DESTROYED="tauri://destroyed",e.WINDOW_FOCUS="tauri://focus",e.WINDOW_BLUR="tauri://blur",e.WINDOW_SCALE_FACTOR_CHANGED="tauri://scale-change",e.WINDOW_THEME_CHANGED="tauri://theme-changed",e.WINDOW_CREATED="tauri://window-created",e.WEBVIEW_CREATED="tauri://webview-created",e.DRAG_ENTER="tauri://drag-enter",e.DRAG_OVER="tauri://drag-over",e.DRAG_DROP="tauri://drag-drop",e.DRAG_LEAVE="tauri://drag-leave"}(a||(a={})),function(e){e[e.Trace=1]="Trace",e[e.Debug=2]="Debug",e[e.Info=3]="Info",e[e.Warn=4]="Warn",e[e.Error=5]="Error"}(t||(t={})),e.attachConsole=async function(){return await u((({level:e,message:n})=>{switch(e){case t.Trace:console.log(n);break;case t.Debug:console.debug(n);break;case t.Info:console.info(n);break;case t.Warn:console.warn(n);break;case t.Error:console.error(n);break;default:throw new Error(`unknown log level ${e}`)}}))},e.attachLogger=u,e.debug=async function(e,n){await i(t.Debug,e,n)},e.error=async function(e,n){await i(t.Error,e,n)},e.info=async function(e,n){await i(t.Info,e,n)},e.trace=async function(e,n){await i(t.Trace,e,n)},e.warn=async function(e,n){await i(t.Warn,e,n)},e}({});Object.defineProperty(window.__TAURI__,"log",{value:__TAURI_PLUGIN_LOG__})} +if("__TAURI__"in window){var __TAURI_PLUGIN_LOG__=function(e){"use strict";function n(e,n=!1){return window.__TAURI_INTERNALS__.transformCallback(e,n)}async function r(e,n={},r){return window.__TAURI_INTERNALS__.invoke(e,n,r)}var a,t;async function o(e,a,t){const o={kind:"Any"};return r("plugin:event|listen",{event:e,target:o,handler:n(a)}).then((n=>async()=>async function(e,n){await r("plugin:event|unlisten",{event:e,eventId:n})}(e,n)))}async function i(e,n,a){const t=function(e){if(e){if(!e.startsWith("Error")){const n=e.split("\n").map((e=>e.split("@"))).filter((([e,n])=>e.length>0&&"[native code]"!==n));return n[2]?.filter((e=>e.length>0)).join("@")}{const n=e.split("\n"),r=n[3]?.trim();if(!r)return;const a=/at\s+(?.*?)\s+\((?.*?):(?\d+):(?\d+)\)/,t=r.match(a);if(t){const{functionName:e,fileName:n,lineNumber:r,columnNumber:a}=t.groups;return`${e}@${n}:${r}:${a}`}{const e=/at\s+(?.*?):(?\d+):(?\d+)/,n=r.match(e);if(n){const{fileName:e,lineNumber:r,columnNumber:a}=n.groups;return`@${e}:${r}:${a}`}}}}}((new Error).stack),{file:o,line:i,keyValues:u}=a??{};await r("plugin:log|log",{level:e,message:n,location:t,file:o,line:i,keyValues:u})}async function u(e){return await o("log://log",(n=>{const{level:r}=n.payload;let{message:a}=n.payload;a=a.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g,""),e({message:a,level:r})}))}return"function"==typeof SuppressedError&&SuppressedError,function(e){e.WINDOW_RESIZED="tauri://resize",e.WINDOW_MOVED="tauri://move",e.WINDOW_CLOSE_REQUESTED="tauri://close-requested",e.WINDOW_DESTROYED="tauri://destroyed",e.WINDOW_FOCUS="tauri://focus",e.WINDOW_BLUR="tauri://blur",e.WINDOW_SCALE_FACTOR_CHANGED="tauri://scale-change",e.WINDOW_THEME_CHANGED="tauri://theme-changed",e.WINDOW_CREATED="tauri://window-created",e.WEBVIEW_CREATED="tauri://webview-created",e.DRAG_ENTER="tauri://drag-enter",e.DRAG_OVER="tauri://drag-over",e.DRAG_DROP="tauri://drag-drop",e.DRAG_LEAVE="tauri://drag-leave"}(a||(a={})),function(e){e[e.Trace=1]="Trace",e[e.Debug=2]="Debug",e[e.Info=3]="Info",e[e.Warn=4]="Warn",e[e.Error=5]="Error"}(t||(t={})),e.attachConsole=async function(){return await u((({level:e,message:n})=>{switch(e){case t.Trace:console.log(n);break;case t.Debug:console.debug(n);break;case t.Info:console.info(n);break;case t.Warn:console.warn(n);break;case t.Error:console.error(n);break;default:throw new Error(`unknown log level ${e}`)}}))},e.attachLogger=u,e.debug=async function(e,n){await i(t.Debug,e,n)},e.error=async function(e,n){await i(t.Error,e,n)},e.info=async function(e,n){await i(t.Info,e,n)},e.trace=async function(e,n){await i(t.Trace,e,n)},e.warn=async function(e,n){await i(t.Warn,e,n)},e}({});Object.defineProperty(window.__TAURI__,"log",{value:__TAURI_PLUGIN_LOG__})} diff --git a/plugins/log/guest-js/index.ts b/plugins/log/guest-js/index.ts index 4233692a5..5b097993a 100644 --- a/plugins/log/guest-js/index.ts +++ b/plugins/log/guest-js/index.ts @@ -61,10 +61,13 @@ function getCallerLocation(stack?: string) { const lines = stack.split('\n') // Find the third line (caller's caller of the current location) const callerLine = lines[3]?.trim() + if (!callerLine) { + return + } const regex = /at\s+(?.*?)\s+\((?.*?):(?\d+):(?\d+)\)/ - const match = callerLine?.match(regex) + const match = callerLine.match(regex) if (match) { const { functionName, fileName, lineNumber, columnNumber } = @@ -79,7 +82,7 @@ function getCallerLocation(stack?: string) { // Handle cases where the regex does not match (e.g., last line without function name) const regexNoFunction = /at\s+(?.*?):(?\d+):(?\d+)/ - const matchNoFunction = callerLine?.match(regexNoFunction) + const matchNoFunction = callerLine.match(regexNoFunction) if (matchNoFunction) { const { fileName, lineNumber, columnNumber } = matchNoFunction.groups as {