How to set the handler for println?

Hi!
Usually, the println prints to the stdout but microcontrollers don't have it.
How can i set a handler for the println/rprintln/hprintln to print, for example, to some USART?

You can print to other things using the write! and writeln! macros.

writeln!(io_resource, "format string {}", my_var);

You can't redirect the existing print macros, but you might be able to define your own using write!?

2 Likes

Currently i use it but i would like to use something like println

Note that in case of a microcontroller you generally use #![no_std] (as there is no OS for libstd to use) in which case println!() is entirely unavailable as it is part of libstd.

1 Like

i know, but there are rprintln/hprintln

Just DIY via macro_rules! and write macro, as @alice said :wink:

Also you can implement your own _print and then print macro will look like this:

macro_rules! print {
    ($($args:tt)*) => {
        $crate::io::_print(format_args!($($args)*));
    }
}
1 Like

If you can already use write, you can just define your own print macro.

You might find some inspiration in A println Macro from the excellent Writing an OS in Rust.