ufmt_stdio/
lib.rs

1//!Minimal printing facilities for [ufmt](https://github.com/japaric/ufmt)
2//!
3//!## Supported platforms:
4//!
5//!- `wasm` via `wasm-bindings`;
6//!- `mos` microprocessors via linking `putchar`;
7//!- `riscv32` via `esp-*` features (see details in `Features` section) or being no-op otherwise;
8//!- All other platforms are built upon standard C library `write` function.
9//!
10//!## Features
11//!
12//!- `esp-uart` - Enables UART writer on `riscv32` targets. Mutually exclusive with `esp-jtag`. Requires user to provide symbols:
13//!  - `ESP_UART_ADDR` (e.g. on ESP32-C3 it is `#[no_mangle] static ESP_UART_ADDR: usize = 0x40000068`).
14//!- `esp-jtag` - Enables JTAG writer on `riscv32` targets. Mutually exclusive with `esp-jtag`. Requires user to provide symbols:
15//!  - `SERIAL_JTAG_FIFO_REG` (e.g. on ESP32-C3 it is `#[no_mangle] static SERIAL_JTAG_FIFO_REG: usize = 0x60043000`)
16//!  - `SERIAL_JTAG_CONF_REG` (e.g. on ESP32-C3 it is `#[no_mangle] static SERIAL_JTAG_CONF_REG: usize = 0x60043004`).
17
18#![warn(missing_docs)]
19#![no_std]
20
21pub extern crate ufmt;
22
23pub use ufmt::{uDebug, uDisplay, uWrite, uwrite, uwriteln};
24
25#[cfg(windows)]
26extern "system" {
27    fn SetConsoleOutputCP(wCodePageID: u32) -> i32;
28}
29
30///Initializes runtime, when necessary.
31///
32///On windows it calls `SetConsoleOutputCP(65001)` to enable unicode output
33pub fn init() {
34    #[cfg(windows)]
35    {
36        unsafe {
37            SetConsoleOutputCP(65001);
38        }
39    }
40}
41
42mod imp;
43pub use imp::{Stdout, Stderr};
44
45///Prints to the stdout with newline
46#[macro_export]
47macro_rules! println {
48    () => {
49        let _ = $crate::ufmt::uwrite!($crate::Stdout::new(), "\n");
50    };
51    ($($arg:tt)*) => {
52        let _ = $crate::ufmt::uwriteln!($crate::Stdout::new(), $($arg)*);
53    };
54}
55
56#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
57///Prints to the stdout
58#[macro_export]
59macro_rules! print {
60    ($($arg:tt)*) => {
61        let _ = $crate::ufmt::uwrite!($crate::Stdout::new(), $($arg)*);
62    };
63}
64
65///Prints to the stderr with newline
66#[macro_export]
67macro_rules! eprintln {
68    () => {
69        let _ = $crate::ufmt::uwrite!($crate::Stderr::new(), "\n");
70    };
71    ($($arg:tt)*) => {
72        let _ = $crate::ufmt::uwriteln!($crate::Stderr::new(), $($arg)*);
73    };
74}
75
76#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
77///Prints to the stderr
78#[macro_export]
79macro_rules! eprint {
80    ($($arg:tt)*) => {
81        let _ = $crate::ufmt::uwrite!($crate::Stderr::new(), $($arg)*);
82    };
83}
84
85///Prints to the stdout with newline in debug mode only
86#[cfg(debug_assertions)]
87#[macro_export]
88macro_rules! d_println {
89    () => {
90        let _ = $crate::ufmt::uwrite!($crate::Stdout::new(), "\n");
91    };
92    ($($arg:tt)*) => {
93        let _ = $crate::ufmt::uwriteln!($crate::Stdout::new(), $($arg)*);
94    };
95}
96
97#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
98///Prints to the stdout in debug mode only
99#[cfg(debug_assertions)]
100#[macro_export]
101macro_rules! d_print {
102    ($($arg:tt)*) => {
103        let _ = $crate::ufmt::uwrite!($crate::Stdout::new(), $($arg)*);
104    };
105}
106
107///Prints to the stdout with newline in debug mode only
108#[cfg(not(debug_assertions))]
109#[macro_export]
110macro_rules! d_println {
111    ($($arg:tt)*) => {
112    };
113}
114
115#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
116///Prints to the stdout in debug mode only
117#[cfg(not(debug_assertions))]
118#[macro_export]
119macro_rules! d_print {
120    ($($arg:tt)*) => {
121    };
122}