Skip to content

Commit 625bb1c

Browse files
feat(log): re-export the log crate (tauri-apps#2965)
* feat(log): re-export the log crate * code review * Move emit_trace --------- Co-authored-by: Tony <[email protected]>
1 parent 6215afe commit 625bb1c

File tree

3 files changed

+83
-67
lines changed

3 files changed

+83
-67
lines changed

.changes/re-export-log.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"log": minor
3+
"log-js": minor
4+
---
5+
6+
Re-export the log crate.

plugins/log/src/commands.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
2+
// SPDX-License-Identifier: Apache-2.0
3+
// SPDX-License-Identifier: MIT
4+
5+
use std::collections::HashMap;
6+
7+
use log::RecordBuilder;
8+
9+
use crate::{LogLevel, WEBVIEW_TARGET};
10+
11+
#[tauri::command]
12+
pub fn log(
13+
level: LogLevel,
14+
message: String,
15+
location: Option<&str>,
16+
file: Option<&str>,
17+
line: Option<u32>,
18+
key_values: Option<HashMap<String, String>>,
19+
) {
20+
let level = log::Level::from(level);
21+
22+
let target = if let Some(location) = location {
23+
format!("{WEBVIEW_TARGET}:{location}")
24+
} else {
25+
WEBVIEW_TARGET.to_string()
26+
};
27+
28+
let mut builder = RecordBuilder::new();
29+
builder.level(level).target(&target).file(file).line(line);
30+
31+
let key_values = key_values.unwrap_or_default();
32+
let mut kv = HashMap::new();
33+
for (k, v) in key_values.iter() {
34+
kv.insert(k.as_str(), v.as_str());
35+
}
36+
builder.key_values(&kv);
37+
#[cfg(feature = "tracing")]
38+
emit_trace(level, &message, location, file, line, &kv);
39+
40+
log::logger().log(&builder.args(format_args!("{message}")).build());
41+
}
42+
43+
// Target becomes default and location is added as a parameter
44+
#[cfg(feature = "tracing")]
45+
fn emit_trace(
46+
level: log::Level,
47+
message: &String,
48+
location: Option<&str>,
49+
file: Option<&str>,
50+
line: Option<u32>,
51+
kv: &HashMap<&str, &str>,
52+
) {
53+
macro_rules! emit_event {
54+
($level:expr) => {
55+
tracing::event!(
56+
target: WEBVIEW_TARGET,
57+
$level,
58+
message = %message,
59+
location = location,
60+
file,
61+
line,
62+
?kv
63+
)
64+
};
65+
}
66+
match level {
67+
log::Level::Error => emit_event!(tracing::Level::ERROR),
68+
log::Level::Warn => emit_event!(tracing::Level::WARN),
69+
log::Level::Info => emit_event!(tracing::Level::INFO),
70+
log::Level::Debug => emit_event!(tracing::Level::DEBUG),
71+
log::Level::Trace => emit_event!(tracing::Level::TRACE),
72+
}
73+
}

plugins/log/src/lib.rs

Lines changed: 4 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@
1010
)]
1111

1212
use fern::{Filter, FormatCallback};
13-
use log::{logger, RecordBuilder};
1413
use log::{LevelFilter, Record};
1514
use serde::Serialize;
1615
use serde_repr::{Deserialize_repr, Serialize_repr};
1716
use std::borrow::Cow;
18-
use std::collections::HashMap;
1917
use std::{
2018
fmt::Arguments,
2119
fs::{self, File},
@@ -30,6 +28,9 @@ use tauri::{AppHandle, Emitter};
3028
use time::{macros::format_description, OffsetDateTime};
3129

3230
pub use fern;
31+
pub use log;
32+
33+
mod commands;
3334

3435
pub const WEBVIEW_TARGET: &str = "webview";
3536

@@ -206,70 +207,6 @@ impl Target {
206207
}
207208
}
208209

209-
// Target becomes default and location is added as a parameter
210-
#[cfg(feature = "tracing")]
211-
fn emit_trace(
212-
level: log::Level,
213-
message: &String,
214-
location: Option<&str>,
215-
file: Option<&str>,
216-
line: Option<u32>,
217-
kv: &HashMap<&str, &str>,
218-
) {
219-
macro_rules! emit_event {
220-
($level:expr) => {
221-
tracing::event!(
222-
target: WEBVIEW_TARGET,
223-
$level,
224-
message = %message,
225-
location = location,
226-
file,
227-
line,
228-
?kv
229-
)
230-
};
231-
}
232-
match level {
233-
log::Level::Error => emit_event!(tracing::Level::ERROR),
234-
log::Level::Warn => emit_event!(tracing::Level::WARN),
235-
log::Level::Info => emit_event!(tracing::Level::INFO),
236-
log::Level::Debug => emit_event!(tracing::Level::DEBUG),
237-
log::Level::Trace => emit_event!(tracing::Level::TRACE),
238-
}
239-
}
240-
241-
#[tauri::command]
242-
fn log(
243-
level: LogLevel,
244-
message: String,
245-
location: Option<&str>,
246-
file: Option<&str>,
247-
line: Option<u32>,
248-
key_values: Option<HashMap<String, String>>,
249-
) {
250-
let level = log::Level::from(level);
251-
252-
let target = if let Some(location) = location {
253-
format!("{WEBVIEW_TARGET}:{location}")
254-
} else {
255-
WEBVIEW_TARGET.to_string()
256-
};
257-
258-
let mut builder = RecordBuilder::new();
259-
builder.level(level).target(&target).file(file).line(line);
260-
261-
let key_values = key_values.unwrap_or_default();
262-
let mut kv = HashMap::new();
263-
for (k, v) in key_values.iter() {
264-
kv.insert(k.as_str(), v.as_str());
265-
}
266-
builder.key_values(&kv);
267-
#[cfg(feature = "tracing")]
268-
emit_trace(level, &message, location, file, line, &kv);
269-
270-
logger().log(&builder.args(format_args!("{message}")).build());
271-
}
272-
273210
pub struct Builder {
274211
dispatch: fern::Dispatch,
275212
rotation_strategy: RotationStrategy,
@@ -528,7 +465,7 @@ impl Builder {
528465
}
529466

530467
fn plugin_builder<R: Runtime>() -> plugin::Builder<R> {
531-
plugin::Builder::new("log").invoke_handler(tauri::generate_handler![log])
468+
plugin::Builder::new("log").invoke_handler(tauri::generate_handler![commands::log])
532469
}
533470

534471
#[allow(clippy::type_complexity)]

0 commit comments

Comments
 (0)