Skip to content

rust: print: add "continuable" as option

Gary Guo lkml rust-for-linux 1 version New

Get series with b4
b4 am [email protected] change-id 20260728-printk-3c0fc5831444

Versions newest first

  1. v1 Latest5 patches 0 repliesNew

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/5rust: print: handle `pr_cont` separatelyrust/kernel/print.rs+35 -400 replies
2/5rust: print: use `CStr` for __LOG_PREFIXrust/kernel/lib.rs rust/kernel/print.rs rust/macros/module.rs +1 more+7 -100 replies
3/5rust: print: accept level in `print_macro` instead of expressionrust/kernel/print.rs+48 -570 replies
4/5rust: macros: use `syn` for `fmt!` string parsingrust/macros/fmt.rs rust/macros/lib.rs+52 -440 replies
5/5rust: print: add `continuable` as optionlib/find_bit_benchmark_rust.rs rust/kernel/print.rs samples/rust/rust_print_main.rs+80 -80 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [PATCH 0/5] rust: print: add "continuable" as option

This series include some cleanups to print macro, followed by the last patch which adds "continuable" as an option.

This is some ability that is unique to Rust macros; C macros cannot do complex matching, so the options to printk are mostly specified via string control flags (like log levels using \x01, continuable or not using trailing \n) or additional macros (e.g. once and ratelimited).

Rust macros support pattern matching (macros by example) so it is possible to do more. In theory you can do this:

    // dev_err!(...)
    log!(target: dev, level: err, continuable, ratelimited, "my messsage {foo}");
    // pr_err!(...)
    log!(target: module, level: err, continuable, ratelimited, "my messsage {foo}");

Userspace Rust logging usually have levels being part of the macro name, like this:

    error!([options], "my messsage {foo}");

Danilo points out that we still want split between pr_ and dev_ [1], but I think adding options would a good to have. This series add an option "continuable"; absence of which indicates that the user do not want the line to be continued. The name is preliminary; we might want to call it "cont" or something else to save some typing.

With that, we have the option to auto add newlines to the print message and not having trailing "\n" in the macro invocations. I personally think not having "\n" is better given that printk is treating it rather like a control flag of whether message can be continued instead of actual new line; it is stripped away inside printk code eventually. Printk has all the control flags being strings due to limitation of C macros, but Rust macros are more versatile so we might be able to take advantage of that and just pass control flags directly in the future. The "lib/find_bit_benchmark_rust.rs" is a good example of how this can be beneficial; it has a pr_err! without newline and pr_cont! following it immediately adds one to it; the newline has to be part of the second message as otherwise printk would treat it as a control flag to commit immediately. If the control flags are passed on its own, we can then have continuable message that still ends with a newline.

However Danilo pointed out in [1] that always adding newline automatically can create issue for developers that need to switch back and worth between C and Rust:

Quoted 3 lines

I don't think it is a good idea to always add newlines. It might be fine if you only do C code or only do Rust code, but if you are switching back and forth, it is a horrible inconsistency for muscle memory.

So the newline is made optional if the message is not continuable. It behaves the same with or without the trailing newline.

---
Gary Guo (5):
rust: print: handle pr_cont separately
rust: print: use CStr for __LOG_PREFIX
rust: print: accept level in print_macro instead of expression
rust: macros: use syn for fmt! string parsing
rust: print: add continuable as option

 lib/find_bit_benchmark_rust.rs  |   2 +-
 rust/kernel/lib.rs              |   2 +-
 rust/kernel/print.rs            | 217 +++++++++++++++++++++++++---------------
 rust/macros/fmt.rs              |  92 +++++++++--------
 rust/macros/lib.rs              |   4 +-
 rust/macros/module.rs           |   2 +-
 samples/rust/rust_print_main.rs |   4 +-
 scripts/rustdoc_test_gen.rs     |   2 +-
 8 files changed, 194 insertions(+), 131 deletions(-)

---
base-commit: 0d33d21e47d9dc66f91e44da3fc9220c74d93df7
change-id: 20260728-printk-3c0fc5831444

Best regards,

Signature · 1 line
Gary Guo <[email protected]>