-
Notifications
You must be signed in to change notification settings - Fork 279
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(log): inconsistent webview log target (#2021)
* Fix very inconsistent webview log target * Add change file * It's log-plugin not log * Lower rust version requirement * Use the third line instead of second
- Loading branch information
1 parent
52c093a
commit 371a2f7
Showing
4 changed files
with
80 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'log-plugin': 'patch' | ||
'log-js': 'patch' | ||
--- | ||
|
||
Make webview log target more consistent that it always starts with `webview` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,24 +44,78 @@ enum LogLevel { | |
Error | ||
} | ||
|
||
function getCallerLocation(stack?: string) { | ||
if (!stack) { | ||
return | ||
} | ||
|
||
if (stack.startsWith('Error')) { | ||
// Assume it's Chromium V8 | ||
// | ||
// Error | ||
// at baz (filename.js:10:15) | ||
// at bar (filename.js:6:3) | ||
// at foo (filename.js:2:3) | ||
// at filename.js:13:1 | ||
|
||
const lines = stack.split('\n') | ||
// Find the third line (caller's caller of the current location) | ||
const callerLine = lines[3].trim() | ||
|
||
const regex = | ||
/at\s+(?<functionName>.*?)\s+\((?<fileName>.*?):(?<lineNumber>\d+):(?<columnNumber>\d+)\)/ | ||
const match = callerLine.match(regex) | ||
|
||
if (match) { | ||
const { functionName, fileName, lineNumber, columnNumber } = | ||
match.groups as { | ||
functionName: string | ||
fileName: string | ||
lineNumber: string | ||
columnNumber: string | ||
} | ||
return `${functionName}@${fileName}:${lineNumber}:${columnNumber}` | ||
} else { | ||
// Handle cases where the regex does not match (e.g., last line without function name) | ||
const regexNoFunction = | ||
/at\s+(?<fileName>.*?):(?<lineNumber>\d+):(?<columnNumber>\d+)/ | ||
const matchNoFunction = callerLine.match(regexNoFunction) | ||
if (matchNoFunction) { | ||
const { fileName, lineNumber, columnNumber } = | ||
matchNoFunction.groups as { | ||
fileName: string | ||
lineNumber: string | ||
columnNumber: string | ||
} | ||
return `<anonymous>@${fileName}:${lineNumber}:${columnNumber}` | ||
} | ||
} | ||
} else { | ||
// Assume it's Webkit JavaScriptCore, example: | ||
// | ||
// [email protected]:10:24 | ||
// [email protected]:6:6 | ||
// [email protected]:2:6 | ||
// global [email protected]:13:4 | ||
|
||
const traces = stack.split('\n').map((line) => line.split('@')) | ||
const filtered = traces.filter(([name, location]) => { | ||
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('@') | ||
} | ||
} | ||
|
||
async function log( | ||
level: LogLevel, | ||
message: string, | ||
options?: LogOptions | ||
): Promise<void> { | ||
const traces = new Error().stack?.split('\n').map((line) => line.split('@')) | ||
|
||
const filtered = traces?.filter(([name, location]) => { | ||
return name.length > 0 && location !== '[native code]' | ||
}) | ||
const location = getCallerLocation(new Error().stack) | ||
|
||
const { file, line, keyValues } = options ?? {} | ||
|
||
let location = filtered?.[0]?.filter((v) => v.length > 0).join('@') | ||
if (location === 'Error') { | ||
location = 'webview::unknown' | ||
} | ||
|
||
await invoke('plugin:log|log', { | ||
level, | ||
message, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters