[PATCH 0/5] rust: print: add "continuable" as option
From: Gary Guo
Date: Tue Jul 28 2026 - 11:15:17 EST
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:
> 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.
Link: https://lore.kernel.org/rust-for-linux/DFJ8XRR2T22T.3R4FQHF50B4P4@xxxxxxxxxx/ [1]
---
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,
--
Gary Guo <gary@xxxxxxxxxxx>