-
Notifications
You must be signed in to change notification settings - Fork 267
Add an optional logger param #664
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
d5061b6
2851f2b
a3f72c8
dd835fe
c300df0
cde2585
2bb47d6
11af9f1
da3dd1f
0bf505d
66a7ab7
088f9f3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is just a good target to test our new Previously, the global logger state is shared with all the |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,31 +53,31 @@ fn main() { | |
last_log_location: Mutex::new(None), | ||
}); | ||
let a = me.clone(); | ||
set_boxed_logger(Box::new(Logger(me))).unwrap(); | ||
let logger = Logger(me); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens if we call a log macro with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IIUC it would be One of the rationales is @EFanZh's comment about "binary size", see: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see. If explicitly passing ref from the user side is intentional, then I'm fine with that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I made a comment above, but don't think we should take There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @KodrAus: How about keeping There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @EFanZh That sounds good to me 👍 We're currently halfway there; the |
||
|
||
test_filter(&a, LevelFilter::Off); | ||
test_filter(&a, LevelFilter::Error); | ||
test_filter(&a, LevelFilter::Warn); | ||
test_filter(&a, LevelFilter::Info); | ||
test_filter(&a, LevelFilter::Debug); | ||
test_filter(&a, LevelFilter::Trace); | ||
test_filter(&logger, &a, LevelFilter::Off); | ||
test_filter(&logger, &a, LevelFilter::Error); | ||
test_filter(&logger, &a, LevelFilter::Warn); | ||
test_filter(&logger, &a, LevelFilter::Info); | ||
test_filter(&logger, &a, LevelFilter::Debug); | ||
test_filter(&logger, &a, LevelFilter::Trace); | ||
|
||
test_line_numbers(&a); | ||
test_line_numbers(&logger, &a); | ||
} | ||
} | ||
|
||
fn test_filter(a: &State, filter: LevelFilter) { | ||
fn test_filter(logger: &dyn Log, a: &State, filter: LevelFilter) { | ||
// tests to ensure logs with a level beneath 'max_level' are filtered out | ||
log::set_max_level(filter); | ||
error!(""); | ||
error!(logger: logger, ""); | ||
tisonkun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
last(a, t(Level::Error, filter)); | ||
warn!(""); | ||
warn!(logger: logger, ""); | ||
last(a, t(Level::Warn, filter)); | ||
info!(""); | ||
info!(logger: logger, ""); | ||
last(a, t(Level::Info, filter)); | ||
debug!(""); | ||
debug!(logger: logger, ""); | ||
last(a, t(Level::Debug, filter)); | ||
trace!(""); | ||
trace!(logger: logger, ""); | ||
last(a, t(Level::Trace, filter)); | ||
|
||
fn t(lvl: Level, filter: LevelFilter) -> Option<Level> { | ||
|
@@ -93,10 +93,10 @@ fn test_filter(a: &State, filter: LevelFilter) { | |
} | ||
} | ||
|
||
fn test_line_numbers(state: &State) { | ||
fn test_line_numbers(logger: &dyn Log, state: &State) { | ||
log::set_max_level(LevelFilter::Trace); | ||
|
||
info!(""); // ensure check_line function follows log macro | ||
info!(logger: logger, ""); // ensure check_line function follows log macro | ||
check_log_location(state); | ||
|
||
#[track_caller] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
use log::{log, log_enabled}; | ||
use log::{log, log_enabled, Log, Metadata, Record}; | ||
|
||
macro_rules! all_log_macros { | ||
($($arg:tt)*) => ({ | ||
|
@@ -10,6 +10,16 @@ macro_rules! all_log_macros { | |
}); | ||
} | ||
|
||
struct NopLogger; | ||
|
||
impl Log for NopLogger { | ||
fn enabled(&self, _: &Metadata) -> bool { | ||
false | ||
} | ||
fn log(&self, _: &Record) {} | ||
fn flush(&self) {} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This inspires me to consider whether we should export the NopLogger .. |
||
|
||
#[test] | ||
fn no_args() { | ||
for lvl in log::Level::iter() { | ||
|
@@ -28,6 +38,11 @@ fn no_args() { | |
|
||
all_log_macros!(target: "my_target", "hello"); | ||
all_log_macros!(target: "my_target", "hello",); | ||
|
||
all_log_macros!(logger: NopLogger, "hello"); | ||
all_log_macros!(logger: NopLogger, "hello",); | ||
all_log_macros!(logger: NopLogger, target: "my_target", "hello"); | ||
all_log_macros!(logger: NopLogger, target: "my_target", "hello",); | ||
} | ||
|
||
#[test] | ||
|
@@ -115,6 +130,8 @@ fn kv_no_args() { | |
log!(lvl, cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello"); | ||
} | ||
|
||
all_log_macros!(logger: NopLogger, cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello"); | ||
all_log_macros!(logger: NopLogger, target: "my_target", cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello"); | ||
all_log_macros!(target: "my_target", cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello"); | ||
all_log_macros!(target = "my_target", cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello"); | ||
all_log_macros!(cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello"); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we should take
logger
by value here since we don't take any other inputs by value, and thewrite!
macro doesn't take its input writer by value.In order to get the most benefit from a zero-sized logger we'd need to do some refactoring of the
__private_api::log
function, to invoke thelog
function at the callsite, or figure something else out, but we can do that in a follow-up PR.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your advice. Let me give it another consideration ..