Inconsistency in formated output with respect to functions in standard lib. #407

Closed
opened 2020-08-06 10:00:02 -05:00 by FabienTregan · 4 comments
FabienTregan commented 2020-08-06 10:00:02 -05:00 (Migrated from github.com)

Using log 0.4.11, the following code:

#[macro_use]
extern crate log;
extern crate stderrlog;

fn main() {
	stderrlog::new().module(module_path!()).init().unwrap();

	println!("{{{{this}}}} prints a way");
	println!("{{{{this}}}} prints {}", "in the same way");

	error!("{{{{this}}}} logs a way");
	error!("{{{{this}}}} logs {}", "in another way");
}

produces this output:

{{this}} prints a way
{{this}} prints in the same way
ERROR - {{{{this}}}} logs a way
ERROR - {{this}} logs in another way

This is due to github.com/rust-lang/log@f06a18d010/src/macros.rs (L36) checking that the message is correct when there are no arguments but ignoring the result and logging the original message.
Standard rust macros (format!, write!, println!, ...) call std::format_args! which already handles both cases:
github.com/rust-lang/rust@ac48e62db8/library/alloc/src/macros.rs (L105-L110)
github.com/rust-lang/rust@4b0882cfaa/library/core/src/macros/mod.rs (L792-L795)

If you think it is desirable to have a behavior that is analog to rust macros, I can try doing the change.

Using log 0.4.11, the following code: ```rust #[macro_use] extern crate log; extern crate stderrlog; fn main() { stderrlog::new().module(module_path!()).init().unwrap(); println!("{{{{this}}}} prints a way"); println!("{{{{this}}}} prints {}", "in the same way"); error!("{{{{this}}}} logs a way"); error!("{{{{this}}}} logs {}", "in another way"); } ``` produces this output: ``` {{this}} prints a way {{this}} prints in the same way ERROR - {{{{this}}}} logs a way ERROR - {{this}} logs in another way ``` This is due to https://github.com/rust-lang/log/blob/f06a18d0108e279c9c983b1efdbe8f61065da4ab/src/macros.rs#L36 checking that the message is correct when there are no arguments but ignoring the result and logging the original message. Standard rust macros (`format!`, `write!`, `println!`, ...) call `std::format_args!` which already handles both cases: https://github.com/rust-lang/rust/blob/ac48e62db85e6db4bbe026490381ab205f4a614d/library/alloc/src/macros.rs#L105-L110 https://github.com/rust-lang/rust/blob/4b0882cfaa33de2796eac01afeac69097fbccbd2/library/core/src/macros/mod.rs#L792-L795 If you think it is desirable to have a behavior that is analog to rust macros, I can try doing the change.
FabienTregan commented 2020-08-07 11:26:53 -05:00 (Migrated from github.com)

The same code with 4.0.8 gives the expected result.

The same code with `4.0.8` gives the expected result.
sfackler commented 2020-08-07 11:31:09 -05:00 (Migrated from github.com)

We started special-casing the no-args case to minimize codegen, but it seems like that's not quite the correct behavior: https://github.com/rust-lang/log/blob/master/src/lib.rs#L1388-L1403.

That special case will need to go away anyway to support https://github.com/rust-lang/rfcs/blob/master/text/2795-format-args-implicit-identifiers.md.

We started special-casing the no-args case to minimize codegen, but it seems like that's not quite the correct behavior: https://github.com/rust-lang/log/blob/master/src/lib.rs#L1388-L1403. That special case will need to go away anyway to support https://github.com/rust-lang/rfcs/blob/master/text/2795-format-args-implicit-identifiers.md.
sfackler commented 2020-11-12 18:02:01 -06:00 (Migrated from github.com)

There's a new API on Arguments that we should be able to use to recover this optimization: https://github.com/rust-lang/rust/pull/74056

There's a new API on Arguments that we should be able to use to recover this optimization: https://github.com/rust-lang/rust/pull/74056
sfackler commented 2021-01-24 08:31:50 -06:00 (Migrated from github.com)

Fixed by #446. If Arguments.as_str stabilizes, we can use that to add the optimization back.

Fixed by #446. If Arguments.as_str stabilizes, we can use that to add the optimization back.
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
gh-rust-lang/log#407
No description provided.