1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
//!C stdio writer for [ufmt](https://github.com/japaric/ufmt)

#![warn(missing_docs)]
#![no_std]

pub extern crate ufmt;

pub use ufmt::{uDebug, uDisplay, uWrite, uwrite, uwriteln};

#[cfg(windows)]
extern "system" {
    fn SetConsoleOutputCP(wCodePageID: u32) -> i32;
}

///Initializes runtime, when necessary.
///
///On windows it calls `SetConsoleOutputCP(65001)` to enable unicode output
pub fn init() {
    #[cfg(windows)]
    {
        unsafe {
            SetConsoleOutputCP(65001);
        }
    }
}

mod imp;
pub use imp::{Stdout, Stderr};

///Prints to the stdout with newline
#[macro_export]
macro_rules! println {
    () => {
        let _ = $crate::ufmt::uwrite!($crate::Stdout::new(), "\n");
    };
    ($($arg:tt)*) => {
        let _ = $crate::ufmt::uwriteln!($crate::Stdout::new(), $($arg)*);
    };
}

#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
///Prints to the stdout
#[macro_export]
macro_rules! print {
    ($($arg:tt)*) => {
        let _ = $crate::ufmt::uwrite!($crate::Stdout::new(), $($arg)*);
    };
}

///Prints to the stderr with newline
#[macro_export]
macro_rules! eprintln {
    () => {
        let _ = $crate::ufmt::uwrite!($crate::Stderr::new(), "\n");
    };
    ($($arg:tt)*) => {
        let _ = $crate::ufmt::uwriteln!($crate::Stderr::new(), $($arg)*);
    };
}

#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
///Prints to the stderr
#[macro_export]
macro_rules! eprint {
    ($($arg:tt)*) => {
        let _ = $crate::ufmt::uwrite!($crate::Stderr::new(), $($arg)*);
    };
}

///Prints to the stdout with newline in debug mode only
#[cfg(debug_assertions)]
#[macro_export]
macro_rules! d_println {
    () => {
        let _ = $crate::ufmt::uwrite!($crate::Stdout::new(), "\n");
    };
    ($($arg:tt)*) => {
        let _ = $crate::ufmt::uwriteln!($crate::Stdout::new(), $($arg)*);
    };
}

#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
///Prints to the stdout in debug mode only
#[cfg(debug_assertions)]
#[macro_export]
macro_rules! d_print {
    ($($arg:tt)*) => {
        let _ = $crate::ufmt::uwrite!($crate::Stdout::new(), $($arg)*);
    };
}

///Prints to the stdout with newline in debug mode only
#[cfg(not(debug_assertions))]
#[macro_export]
macro_rules! d_println {
    ($($arg:tt)*) => {
    };
}

#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
///Prints to the stdout in debug mode only
#[cfg(not(debug_assertions))]
#[macro_export]
macro_rules! d_print {
    ($($arg:tt)*) => {
    };
}