8000 log: format a message that has no arguments after it by benface · Pull Request #687 · not-fl3/miniquad · GitHub
[go: up one dir, main page]

Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,6 @@ impl Ord for Level {

#[macro_export(local_inner_macros)]
macro_rules! log {
(target: $target:expr, $lvl:expr, $message:expr) => ({
let lvl = $lvl;
//if lvl <= $crate::STATIC_MAX_LEVEL && lvl <= $crate::max_level() {
// ensure that $message is a valid format string literal
let _ = __log_format_args!($message);
$crate::log::__private_api_log_lit(
$message,
lvl,
&($target, __log_module_path!(), __log_file!(), __log_line!()),
);
//}
});
(target: $target:expr, $lvl:expr, $($arg:tt)+) => ({
let lvl = $lvl;
//if lvl <= $crate::STATIC_MAX_LEVEL && lvl <= $crate::max_level() {
Expand Down Expand Up @@ -184,6 +172,8 @@ pub fn __private_api_log_lit(
_level: Level,
&(_target, _module_path, _file, _line): &(&str, &'static str, &'static str, u32),
) {
#[cfg(test)]
LOGGED.with(|logged| logged.borrow_mut().push(message.to_owned()));
eprintln!("{}", message);
}

Expand Down Expand Up @@ -237,20 +227,39 @@ pub fn __private_api_log_lit(
crate::native::ios::log(message);
}

#[cfg(test)]
thread_local! {
/// Every message the desktop logger was given, so `test_logs` can check them.
static LOGGED: std::cell::RefCell<Vec<String>> = std::cell::RefCell::new(Vec::new());
}

#[test]
fn test_logs() {
let value = 1;

trace!("info");
trace!("info: {}", 1);
trace!("info: {value}");

debug!("info");
debug!("info: {}", 1);
debug!("info: {value}");

info!("info");
info!("info: {}", 1);
info!("info: {value}");

warn!("info");
warn!("info: {}", 1);
warn!("info: {value}");

error!("info");
error!("info: {}", 1);
error!("info: {value}");

info!("{{value}}");

let mut expected = ["info", "info: 1", "info: 1"].repeat(5);
expected.push("{value}");
LOGGED.with(|logged| assert_eq!(*logged.borrow(), expected));
}
Loading
0