Skip to content

Commit c52ee6b

Browse files
committed
feat(log): replace log with tracing
1 parent b73eedd commit c52ee6b

18 files changed

Lines changed: 63 additions & 92 deletions

Cargo.toml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,10 @@ wasmbind = []
3333
chrono = { version = "0.4.44", features = ["serde", "wasmbind"] }
3434
futures-util = { version = "0.3.32", default-features = false }
3535
getrandom = { version = "0.4.2", features = ["wasm_js"] }
36-
log = "0.4.29"
3736
serde = { version = "1.0.228", features = ["derive"] }
3837
serde_json = "1.0.149"
3938
wasm-bindgen = "0.2.114"
40-
wasm-logger = "0.2.0"
39+
wasm-tracing = "2.1.0"
4140

4241
[dependencies]
4342
brotli = { version = "8.0.2", optional = true }
@@ -48,24 +47,22 @@ flate2 = { version = "1.1.9", optional = true }
4847
heck = "0.5.0"
4948
html-to-markdown-rs = "3.0.2"
5049
http = "1.4.0"
51-
lazy_static = "1.5.0"
5250
linked_hash_set = { version = "0.1.6", features = ["serde"] }
5351
linked-hash-map = "0.5.6"
54-
log = { version = "0.4.29", features = ["std"] }
5552
lol_html = "2.7.2"
5653
percent-encoding = "2.3.2"
5754
rand = "0.10.0"
5855
regex = "1.12.3"
5956
serde = { version = "1.0.228", features = ["derive", "rc"] }
6057
serde_json = "1.0.149"
6158
tracing = "0.1.44"
59+
tracing-subscriber = "0.3.23"
6260
trusted-proxies = "0.3.0"
6361
url = "2.5.8"
6462

6563
[dev-dependencies]
6664
pprof = { version = "0.15.0", features = ["flamegraph"] }
6765
criterion = { version = "0.8.2", default-features = false }
68-
tracing-subscriber = "0.3.17"
6966

7067
[[bench]]
7168
name = "match_rule_benchmark"

src/action/ffi.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub extern "C" fn redirectionio_action_json_deserialize(str: *mut c_char) -> *co
2121

2222
let action = match json_decode(action_str) {
2323
Err(error) => {
24-
log::error!("Unable to deserialize \"{action_str}\" to action: {error}");
24+
tracing::error!("unable to deserialize \"{action_str}\" to action: {error}");
2525

2626
return null();
2727
}
@@ -44,7 +44,7 @@ pub extern "C" fn redirectionio_action_json_serialize(_action: *mut Action) -> *
4444
let action = unsafe { &*_action };
4545
let action_serialized = match json_encode(action) {
4646
Err(error) => {
47-
log::error!("Unable to serialize to action: {error}");
47+
tracing::error!("unable to serialize to action: {error}");
4848

4949
return null();
5050
}

src/api/redirection_loop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ impl RedirectionLoop {
5959
let request = match Request::from_example(&router.config, &new_example) {
6060
Ok(request) => request,
6161
Err(err) => {
62-
log::warn!("cannot create request from new target: {new_example:?} : {err}");
62+
tracing::warn!("cannot create request from new target: {new_example:?} : {err}");
6363

6464
break;
6565
}

src/api/rule.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl Rule {
7474
let rule_result = json_decode(rule_str);
7575

7676
if rule_result.is_err() {
77-
log::error!("Unable to create rule from string {}: {}", rule_str, rule_result.err().unwrap());
77+
tracing::error!("unable to create rule from string {}: {}", rule_str, rule_result.err().unwrap());
7878

7979
return None;
8080
}
@@ -140,21 +140,21 @@ impl Rule {
140140
IpConstraint::InRange(range) => match range.parse::<AnyIpCidr>() {
141141
Ok(cidr) => route_ips.push(RouteIp::InRange(cidr)),
142142
Err(err) => {
143-
log::error!("cannot parse cidr {range}: {err}");
143+
tracing::error!("cannot parse cidr {range}: {err}");
144144
}
145145
},
146146
IpConstraint::NotInRange(range) => match range.parse::<AnyIpCidr>() {
147147
Ok(cidr) => route_ips.push(RouteIp::NotInRange(cidr)),
148148
Err(err) => {
149-
log::error!("cannot parse cidr {range}: {err}");
149+
tracing::error!("cannot parse cidr {range}: {err}");
150150
}
151151
},
152152
IpConstraint::NotOneOf(list) => {
153153
let mut ips = Vec::new();
154154
for ip_str in list {
155155
match ip_str.parse::<IpAddr>() {
156156
Ok(ip) => ips.push(ip),
157-
Err(err) => log::error!("cannot parse ip {ip_str}: {err}"),
157+
Err(err) => tracing::error!("cannot parse ip {ip_str}: {err}"),
158158
}
159159
}
160160

@@ -283,7 +283,7 @@ impl Rule {
283283
},
284284
},
285285
unknown => {
286-
log::error!("unsupported header constraint type {unknown}");
286+
tracing::error!("unsupported header constraint type {unknown}");
287287

288288
continue;
289289
}

src/callback_log.rs

Lines changed: 9 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,16 @@
1-
use std::{
2-
os::raw::{c_char, c_short, c_void},
3-
sync::Once,
4-
};
5-
6-
use log::{Metadata, Record};
7-
8-
use crate::ffi_helpers::string_to_c_char;
9-
1+
use std::os::raw::{c_char, c_short, c_void};
102
#[allow(non_camel_case_types)]
113
pub type redirectionio_log_callback = extern "C" fn(*const c_char, *const c_void, c_short);
124

13-
pub struct CallbackLogger {
14-
pub callback: Option<redirectionio_log_callback>,
15-
pub data: Option<&'static c_void>,
16-
}
17-
18-
impl log::Log for CallbackLogger {
19-
fn enabled(&self, _metadata: &Metadata) -> bool {
20-
true
21-
}
22-
23-
fn log(&self, record: &Record) {
24-
if self.callback.is_none() {
25-
return;
26-
}
27-
28-
if self.data.is_none() {
29-
return;
30-
}
31-
32-
if self.enabled(record.metadata()) {
33-
let log_str = format!("{} - {}", record.level(), record.args());
34-
let cstr = string_to_c_char(log_str);
35-
36-
(self.callback.unwrap())(cstr, self.data.unwrap(), record.level() as i16);
37-
}
38-
}
39-
40-
fn flush(&self) {}
5+
#[unsafe(no_mangle)]
6+
pub unsafe extern "C" fn redirectionio_log_init_with_callback(_callback: redirectionio_log_callback, _data: &'static c_void) {
7+
// do nothing
8+
// bc layer
419
}
4210

43-
static INIT: Once = Once::new();
44-
4511
#[unsafe(no_mangle)]
46-
pub unsafe extern "C" fn redirectionio_log_init_with_callback(callback: redirectionio_log_callback, data: &'static c_void) {
47-
let logger = CallbackLogger {
48-
callback: Some(callback),
49-
data: Some(data),
50-
};
51-
52-
INIT.call_once(|| {
53-
log::set_boxed_logger(Box::new(logger))
54-
.map(|()| log::set_max_level(log::LevelFilter::Trace))
55-
.expect("cannot set logger");
56-
});
12+
pub unsafe extern "C" fn redirectionio_trace_init() {
13+
if let Err(e) = tracing_subscriber::fmt::try_init() {
14+
eprintln!("[redirectionio] unable to set global default subscriber: {e}");
15+
}
5716
}

src/ffi_helpers.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{
44
ptr::null,
55
};
66

7-
pub fn c_char_to_str(ptr: *const c_char) -> Option<&'static str> {
7+
pub fn c_char_to_str<'a>(ptr: *const c_char) -> Option<&'a str> {
88
if ptr.is_null() {
99
return None;
1010
}
@@ -14,7 +14,7 @@ pub fn c_char_to_str(ptr: *const c_char) -> Option<&'static str> {
1414

1515
match cstr.to_str() {
1616
Err(error) => {
17-
log::error!(
17+
tracing::error!(
1818
"unable to create string for '{}': {}",
1919
String::from_utf8_lossy(cstr.to_bytes()),
2020
error,
@@ -29,7 +29,7 @@ pub fn c_char_to_str(ptr: *const c_char) -> Option<&'static str> {
2929
pub fn string_to_c_char(str: String) -> *const c_char {
3030
let string = match CString::new(str.as_str()) {
3131
Err(error) => {
32-
log::error!("cannot create c string {str}: {error}");
32+
tracing::error!("cannot create c string {str}: {error}");
3333

3434
return null();
3535
}

src/filter/filter_body.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ impl FilterBodyAction {
103103
Self { chain, in_error: false }
104104
}
105105
None => {
106-
log::error!("redirectionio does not support content-encoding {encoding}, filtering will be disable for this request");
106+
tracing::error!(
107+
"redirectionio does not support content-encoding {encoding}, filtering will be disable for this request"
108+
);
107109

108110
Self {
109111
chain: Vec::new(),
@@ -127,7 +129,7 @@ impl FilterBodyAction {
127129
match self.do_filter(data.clone(), unit_trace) {
128130
Ok(filtered) => filtered,
129131
Err(err) => {
130-
log::error!("error while filtering: {err:?}");
132+
tracing::error!("error while filtering: {err:?}");
131133
self.in_error = true;
132134

133135
data
@@ -155,7 +157,7 @@ impl FilterBodyAction {
155157
match self.do_end(unit_trace) {
156158
Ok(end) => end,
157159
Err(err) => {
158-
log::error!("error while ending filtering: {err}");
160+
tracing::error!("error while ending filtering: {err}");
159161

160162
Vec::new()
161163
}
@@ -203,7 +205,7 @@ impl FilterBodyActionItem {
203205
.map(|visitor| Self::Html(Box::new(HtmlFilterBodyAction::new(visitor))))
204206
}
205207
_ => {
206-
log::warn!(
208+
tracing::warn!(
207209
"html filtering is only supported for text/html content type, {} received",
208210
content_type.unwrap_or_default()
209211
);
@@ -230,7 +232,7 @@ impl FilterBodyActionItem {
230232
Some(Self::HtmlToMarkdown(Box::new(HtmlToMarkdownFilter::new(html_to_md_filter.options))))
231233
}
232234
_ => {
233-
log::warn!(
235+
tracing::warn!(
234236
"html to markdown is only supported for text/html content type, {} received",
235237
content_type.unwrap_or_default()
236238
);
@@ -239,7 +241,7 @@ impl FilterBodyActionItem {
239241
}
240242
},
241243
BodyFilter::Other(_) => {
242-
log::warn!("unsupported body filter: {filter:?}, you may need to update your agent or module");
244+
tracing::warn!("unsupported body filter: {filter:?}, you may need to update your agent or module");
243245
None
244246
}
245247
}

src/filter/html_body_action/body_append.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,14 @@ impl BodyAppend {
5959
(base, Some(checker)) => match (checker.parse(), base.parse()) {
6060
(Ok(checker_selector), Ok(base_selector)) => (base_selector, Some(checker_selector)),
6161
_ => {
62-
log::error!("failed to parse CSS selector: {}", self.css_selector);
62+
tracing::error!("failed to parse CSS selector: {}", self.css_selector);
6363
return;
6464
}
6565
},
6666
(base, None) => match base.parse() {
6767
Ok(selector) => (selector, None),
6868
Err(_) => {
69-
log::error!("failed to parse CSS selector: {}", self.css_selector);
69+
tracing::error!("failed to parse CSS selector: {}", self.css_selector);
7070
return;
7171
}
7272
},

src/filter/html_body_action/body_capture.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ impl BodyCapture {
8888
let css_selector = match selector.parse() {
8989
Ok(selector) => selector,
9090
Err(_) => {
91-
log::error!("Failed to parse CSS selector: {}", selector);
91+
tracing::error!("Failed to parse CSS selector: {}", selector);
9292
continue;
9393
}
9494
};

src/filter/html_to_markdown.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ impl HtmlToMarkdownFilter {
2222
pub fn end(self) -> Vec<u8> {
2323
let html = match String::from_utf8(self.buffer) {
2424
Err(e) => {
25-
log::error!("error while converting to utf8: {}", e);
25+
tracing::error!("error while converting to utf8: {}", e);
2626

2727
return e.into_bytes();
2828
}
@@ -32,7 +32,7 @@ impl HtmlToMarkdownFilter {
3232
match convert(html.as_str(), self.options) {
3333
Ok(md) => md.content.unwrap_or_default().into_bytes(),
3434
Err(e) => {
35-
log::error!("error while converting html to markdown: {}", e);
35+
tracing::error!("error while converting html to markdown: {}", e);
3636

3737
html.into_bytes()
3838
}

0 commit comments

Comments
 (0)