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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//! Lightweight logging framework for resource constrained devices
//!
//! ![stlog running on a Cortex-M microcontroller][img]
//!
//! [img]: https://i.imgur.com/9K3SrI2.png
//!
//! # Features
//!
//! - `O(1)` runtime. Logging strings of arbitrary size takes a constant number
//!   of instructions.
//!
//! - `O(0)` memory usage. The logged strings are NOT stored in the device
//!   memory.
//!
//! - Supports different logging levels: trace, debug, info, warn and error.
//!
//! # Non-features
//!
//! - `printf` style or any other kind of formatting
//!
//! # Limitations
//!
//! - The current implementation only supports 256 different log strings. This
//!   restriction will be lifted in the future.
//!
//! # Requirements
//!
//! The device program must be stored in ELF format.
//!
//! The device linker script must allocate a `.stlog` section in the output ELF
//! file as described below:
//!
//! ``` text
//! SECTIONS
//! {
//!     /* .. */
//!
//!     .stlog 0 (INFO) : {
//!         _sstlog_trace = .;
//!         *(.stlog.trace*);
//!         _estlog_trace = .;
//!
//!         _sstlog_debug = .;
//!         *(.stlog.debug*);
//!         _estlog_debug = .;
//!
//!         _sstlog_info = .;
//!         *(.stlog.info*);
//!         _estlog_info = .;
//!
//!         _sstlog_warn = .;
//!         *(.stlog.warn*);
//!         _estlog_warn = .;
//!
//!         _sstlog_error = .;
//!         *(.stlog.error*);
//!         _estlog_error = .;
//!     }
//!
//!     /* .. */
//! }
//! ```
//!
//! # Example
//!
//! - Device side
//!
//! ``` ignore
//! #[macro_use]
//! extern crate stlog;
//!
//! struct Logger { .. }
//!
//! impl<'a> stlog::Logger for &'a Logger { .. }
//!
//! fn main() {
//!     let logger = Logger { .. };
//!
//!     info!(&logger, "Hello, world!");
//!     warn!(&logger, "The quick brown fox jumps over the lazy dog.");
//! }
//! ```
//!
//! - Host side
//!
//! ``` text
//! $ # flash and execute the device program
//! $ arm-none-eabi-gdb /path/to/device/binary &
//!
//! $ # stcat is required to decode strings logged via `stlog`
//! $ cargo install stcat --vers 0.1.0
//!
//! $ cat /dev/ttyUSB0 | stcat -e /path/to/device/binary
//! INFO Hello, world!
//! WARN The quick brown fox jumps over the lazy dog.
//! ```
//!
//! Assuming that the device is logging the strings through the `/dev/ttyUSB0`
//! interface. The device binary must *not* be stripped.

#![deny(missing_docs)]
#![deny(warnings)]
#![no_std]

/// A logger compatible with the `stlog` logging framework
///
/// # Contract
///
/// The `log` implementation must simply send its argument as a byte through
/// some interface.
pub trait Logger {
    /// Error type of the log operation
    type Error;

    /// Logs a string stored in debuginfo with address `addr`
    fn log(self, addr: u8) -> Result<(), Self::Error>;
}

#[macro_export]
macro_rules! trace {
    ($logger:expr, $string:expr) => {
        {
            #[link_section = ".stlog.trace"]
            #[export_name = $string]
            static SYMBOL: bool = false;

            $crate::Logger::log($logger, &SYMBOL as *const _ as usize as u8)
        }
    };
}

#[macro_export]
macro_rules! debug {
    ($logger:expr, $string:expr) => {
        {
            #[link_section = ".stlog.debug"]
            #[export_name = $string]
            static SYMBOL: bool = false;

            $crate::Logger::log($logger, &SYMBOL as *const _ as usize as u8)
        }
    };
}

#[macro_export]
macro_rules! info {
    ($logger:expr, $string:expr) => {
        {
            #[link_section = ".stlog.info"]
            #[export_name = $string]
            static SYMBOL: bool = false;

            $crate::Logger::log($logger, &SYMBOL as *const _ as usize as u8)
        }
    };
}

#[macro_export]
macro_rules! warn {
    ($logger:expr, $string:expr) => {
        {
            #[link_section = ".stlog.warn"]
            #[export_name = $string]
            static SYMBOL: bool = false;

            $crate::Logger::log($logger, &SYMBOL as *const _ as usize as u8)
        }
    };
}

#[macro_export]
macro_rules! error {
    ($logger:expr, $string:expr) => {
        {
            #[link_section = ".stlog.error"]
            #[export_name = $string]
            static SYMBOL: bool = false;

            $crate::Logger::log($logger, &SYMBOL as *const _ as usize as u8)
        }
    };
}