aboutsummaryrefslogtreecommitdiff
path: root/src/protocol/console_output.rs
blob: 54774bec8fc38d7fb14b089d4368dc196c184f74 (plain)
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
#[cfg(feature = "alloc")]
use alloc::vec::Vec;
use core::fmt;

/// Helper struct to send console output to GDB.
///
/// The recommended way to interact with `ConsoleOutput` is through the provided
/// [`output!`] and [`outputln!`] macros.
///
/// On resource constrained systems which might want to avoid using Rust's
/// [fairly "heavy" formatting machinery](https://jamesmunns.com/blog/fmt-unreasonably-expensive/),
/// the `write_raw()` method can be used to write raw data directly to the GDB
/// console.
///
/// When the `alloc` feature is disabled, all output buffering is disabled, and
/// each call to `output!` will automatically flush data over the Connection.
///
/// [`output!`]: crate::output
/// [`outputln!`]: crate::outputln
// TODO: support user-provided output buffers for no-`alloc` environments.
pub struct ConsoleOutput<'a> {
    #[cfg(feature = "alloc")]
    buf: Vec<u8>,
    callback: &'a mut dyn FnMut(&[u8]),
}

impl<'a> fmt::Write for ConsoleOutput<'a> {
    fn write_str(&mut self, s: &str) -> fmt::Result {
        self.write_raw(s.as_bytes());
        Ok(())
    }
}

impl<'a> ConsoleOutput<'a> {
    pub(crate) fn new(callback: &'a mut dyn FnMut(&[u8])) -> ConsoleOutput<'a> {
        ConsoleOutput {
            #[cfg(feature = "alloc")]
            buf: Vec::new(),
            callback,
        }
    }

    /// Write raw (non UTF-8) data to the GDB console.
    pub fn write_raw(&mut self, bytes: &[u8]) {
        cfg_if::cfg_if! {
            if #[cfg(feature = "alloc")] {
                self.buf.extend_from_slice(bytes);
            } else {
                (self.callback)(bytes);
            }
        }
    }

    /// Flush the internal output buffer.
    ///
    /// Only available when `alloc` is enabled.
    #[cfg(feature = "alloc")]
    pub fn flush(&mut self) {
        if !self.buf.is_empty() {
            (self.callback)(&self.buf);
            self.buf.clear()
        }
    }
}

impl Drop for ConsoleOutput<'_> {
    fn drop(&mut self) {
        #[cfg(feature = "alloc")]
        self.flush()
    }
}

/// Send formatted data to the GDB client console.
///
/// The first argument must be a [`ConsoleOutput`].
#[macro_export]
macro_rules! output {
    ($console_output:expr, $($args:tt)*) => {{
        use core::fmt::Write;
        let _ = write!($console_output, $($args)*);
    }};
}

/// Send formatted data to the GDB client console, with a newline appended.
///
/// The first argument must be a [`ConsoleOutput`].
#[macro_export]
macro_rules! outputln {
    ($console_output:expr) => {{
        use core::fmt::Write;
        let _ = writeln!($console_output);
    }};
    ($console_output:expr,) => {
        outputln!($console_output)
    };
    ($console_output:expr, $($args:tt)*) => {{
        use core::fmt::Write;
        let _ = writeln!($console_output, $($args)*);
    }};
}