-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6cb78d6
commit 27e11e7
Showing
5 changed files
with
70 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
// You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
// A simple logger that sets the max log level to Trace in debug builds and to Info in release ones. | ||
|
||
use log::{self, LogRecord, LogLevelFilter, LogMetadata, SetLoggerError}; | ||
|
||
#[macro_export] | ||
macro_rules! print { | ||
($($args:tt)*) => { | ||
// Ignore logging errors. It's not worth killing the program because of | ||
// failed debug output. It would be nicer to save the error and report | ||
// it later, however. | ||
use core::fmt::Write; | ||
let mut console = $crate::cc3200::Console {}; | ||
let _ = write!(console, $($args)*); | ||
} | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! println { | ||
($fmt:expr) => ( print!(concat!($fmt, '\n')) ); | ||
($fmt:expr, $($args:tt)*) => ( print!(concat!($fmt, '\n'), $($args)*) ); | ||
} | ||
|
||
pub struct SimpleLogger; | ||
|
||
#[cfg(debug_assertions)] | ||
static MAX_LOG_LEVEL: LogLevelFilter = LogLevelFilter::Trace; | ||
|
||
#[cfg(not(debug_assertions))] | ||
static MAX_LOG_LEVEL: LogLevelFilter = LogLevelFilter::Info; | ||
|
||
impl log::Log for SimpleLogger { | ||
fn enabled(&self, metadata: &LogMetadata) -> bool { | ||
return metadata.level() <= MAX_LOG_LEVEL; | ||
} | ||
|
||
fn log(&self, record: &LogRecord) { | ||
if self.enabled(record.metadata()) { | ||
println!("{:5} [{}@{}] {}", | ||
record.level(), | ||
record.target(), | ||
record.location().line(), | ||
record.args()); | ||
} | ||
} | ||
} | ||
|
||
impl SimpleLogger { | ||
pub fn init() -> Result<(), SetLoggerError> { | ||
println!("Logger level is {}", MAX_LOG_LEVEL); | ||
unsafe { | ||
log::set_logger_raw(|max_log_level| { | ||
max_log_level.set(MAX_LOG_LEVEL); | ||
&SimpleLogger | ||
}) | ||
} | ||
} | ||
} |