Skip to content

Commit

Permalink
Logging support (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
fabricedesre authored Nov 7, 2016
1 parent 6cb78d6 commit 27e11e7
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 19 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ name = "cc3200"
cc3200-sys = { path = "cc3200-sys" }
freertos_alloc = { path = "freertos_alloc" }
freertos_rs = "0.1"
log = { version = "0.3", default-features = false }

[[example]]
name = "blinky"
Expand Down
4 changes: 3 additions & 1 deletion example/blinky.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ extern crate cc3200;
extern crate alloc;
extern crate freertos_rs;
extern crate freertos_alloc;
#[macro_use]
extern crate log;

use cc3200::cc3200::{Board, Console, Utils, LedEnum, LedName};

Expand All @@ -37,7 +39,7 @@ pub fn start() -> ! {

Console::init_term();
Console::clear_term();
println!("Welcome to CC3200 blinking leds version {}", VERSION);
info!("Welcome to CC3200 blinking leds version {}", VERSION);

Board::test();

Expand Down
20 changes: 2 additions & 18 deletions src/cc3200.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ extern crate cc3200_sys;
use core;
use self::cc3200_sys::{board_init, GPIO_IF_LedConfigure, GPIO_IF_LedOn, GPIO_IF_LedOff,
MAP_UtilsDelay};
use logger::SimpleLogger;

#[allow(non_camel_case_types, dead_code)]
pub enum LedName {
Expand Down Expand Up @@ -38,6 +39,7 @@ pub struct Board { }

impl Board {
pub fn init() {
SimpleLogger::init().unwrap();
unsafe {
board_init();
}
Expand Down Expand Up @@ -125,24 +127,6 @@ impl core::fmt::Write for Console {
}
}

#[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 Utils { }

impl Utils {
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
#![feature(asm, lang_items)]

extern crate cc3200_sys;
#[macro_use]
extern crate log;

#[macro_use]
pub mod logger;
pub mod cc3200;
pub mod isr_vectors;

Expand Down
61 changes: 61 additions & 0 deletions src/logger.rs
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
})
}
}
}

0 comments on commit 27e11e7

Please sign in to comment.