Skip to content

Commit 10670bf

Browse files
Legend-Mastergezihuzi
authored andcommitted
fix(log): inconsistent webview log target (tauri-apps#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
1 parent cabbc01 commit 10670bf

File tree

4 files changed

+80
-26
lines changed

4 files changed

+80
-26
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'log-plugin': 'patch'
3+
'log-js': 'patch'
4+
---
5+
6+
Make webview log target more consistent that it always starts with `webview`

plugins/log/api-iife.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/log/guest-js/index.ts

Lines changed: 64 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,78 @@ enum LogLevel {
4444
Error
4545
}
4646

47+
function getCallerLocation(stack?: string) {
48+
if (!stack) {
49+
return
50+
}
51+
52+
if (stack.startsWith('Error')) {
53+
// Assume it's Chromium V8
54+
//
55+
// Error
56+
// at baz (filename.js:10:15)
57+
// at bar (filename.js:6:3)
58+
// at foo (filename.js:2:3)
59+
// at filename.js:13:1
60+
61+
const lines = stack.split('\n')
62+
// Find the third line (caller's caller of the current location)
63+
const callerLine = lines[3].trim()
64+
65+
const regex =
66+
/at\s+(?<functionName>.*?)\s+\((?<fileName>.*?):(?<lineNumber>\d+):(?<columnNumber>\d+)\)/
67+
const match = callerLine.match(regex)
68+
69+
if (match) {
70+
const { functionName, fileName, lineNumber, columnNumber } =
71+
match.groups as {
72+
functionName: string
73+
fileName: string
74+
lineNumber: string
75+
columnNumber: string
76+
}
77+
return `${functionName}@${fileName}:${lineNumber}:${columnNumber}`
78+
} else {
79+
// Handle cases where the regex does not match (e.g., last line without function name)
80+
const regexNoFunction =
81+
/at\s+(?<fileName>.*?):(?<lineNumber>\d+):(?<columnNumber>\d+)/
82+
const matchNoFunction = callerLine.match(regexNoFunction)
83+
if (matchNoFunction) {
84+
const { fileName, lineNumber, columnNumber } =
85+
matchNoFunction.groups as {
86+
fileName: string
87+
lineNumber: string
88+
columnNumber: string
89+
}
90+
return `<anonymous>@${fileName}:${lineNumber}:${columnNumber}`
91+
}
92+
}
93+
} else {
94+
// Assume it's Webkit JavaScriptCore, example:
95+
//
96+
// baz@filename.js:10:24
97+
// bar@filename.js:6:6
98+
// foo@filename.js:2:6
99+
// global code@filename.js:13:4
100+
101+
const traces = stack.split('\n').map((line) => line.split('@'))
102+
const filtered = traces.filter(([name, location]) => {
103+
return name.length > 0 && location !== '[native code]'
104+
})
105+
// Find the third line (caller's caller of the current location)
106+
return filtered[2].filter((v) => v.length > 0).join('@')
107+
}
108+
}
109+
47110
async function log(
48111
level: LogLevel,
49112
message: string,
50113
options?: LogOptions
51114
): Promise<void> {
52-
const traces = new Error().stack?.split('\n').map((line) => line.split('@'))
53-
54-
const filtered = traces?.filter(([name, location]) => {
55-
return name.length > 0 && location !== '[native code]'
56-
})
115+
const location = getCallerLocation(new Error().stack)
57116

58117
const { file, line, keyValues } = options ?? {}
59118

60-
let location = filtered?.[0]?.filter((v) => v.length > 0).join('@')
61-
if (location === 'Error') {
62-
location = 'webview::unknown'
63-
}
64-
65119
await invoke('plugin:log|log', {
66120
level,
67121
message,

plugins/log/src/lib.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use tauri::{AppHandle, Emitter};
3333
pub use fern;
3434
use time::OffsetDateTime;
3535

36-
pub const WEBVIEW_TARGET: &str = "Webview";
36+
pub const WEBVIEW_TARGET: &str = "webview";
3737

3838
#[cfg(target_os = "ios")]
3939
mod ios {
@@ -230,22 +230,16 @@ fn log(
230230
line: Option<u32>,
231231
key_values: Option<HashMap<String, String>>,
232232
) {
233-
let location = location.unwrap_or("webview");
234-
235233
let level = log::Level::from(level);
236234

237-
let metadata = log::MetadataBuilder::new()
238-
.level(level)
239-
.target(WEBVIEW_TARGET)
240-
.build();
235+
let target = if let Some(location) = location {
236+
format!("{WEBVIEW_TARGET}:{location}")
237+
} else {
238+
WEBVIEW_TARGET.to_string()
239+
};
241240

242241
let mut builder = RecordBuilder::new();
243-
builder
244-
.level(level)
245-
.metadata(metadata)
246-
.target(location)
247-
.file(file)
248-
.line(line);
242+
builder.level(level).target(&target).file(file).line(line);
249243

250244
let key_values = key_values.unwrap_or_default();
251245
let mut kv = HashMap::new();
@@ -380,8 +374,8 @@ impl Builder {
380374
/// .clear_targets()
381375
/// .targets([
382376
/// Target::new(TargetKind::Webview),
383-
/// Target::new(TargetKind::LogDir { file_name: Some("webview".into()) }).filter(|metadata| metadata.target() == WEBVIEW_TARGET),
384-
/// Target::new(TargetKind::LogDir { file_name: Some("rust".into()) }).filter(|metadata| metadata.target() != WEBVIEW_TARGET),
377+
/// Target::new(TargetKind::LogDir { file_name: Some("webview".into()) }).filter(|metadata| metadata.target().starts_with(WEBVIEW_TARGET)),
378+
/// Target::new(TargetKind::LogDir { file_name: Some("rust".into()) }).filter(|metadata| !metadata.target().starts_with(WEBVIEW_TARGET)),
385379
/// ]);
386380
/// ```
387381
pub fn targets(mut self, targets: impl IntoIterator<Item = Target>) -> Self {

0 commit comments

Comments
 (0)