-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test and fix logging callback (#194)
* test and fix logging callback * `cstr` import * test `xcoder-*-sys` crates on CI * satisfy clippy * gate xcoder tests behind target_os linux I'm looking forward to <rust-lang/cargo#6179>; making an entire crate empty or not based on platform is an awkward workaround.
- Loading branch information
Showing
7 changed files
with
121 additions
and
14 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,52 @@ | ||
#[cfg(target_os = "linux")] | ||
mod linux { | ||
use cstr::cstr; | ||
use std::sync::Mutex; | ||
|
||
use xcoder_logan_310_sys as sys; | ||
|
||
#[derive(Debug, Eq, PartialEq)] | ||
struct LogEntry { | ||
level: log::Level, | ||
message: String, | ||
} | ||
|
||
#[derive(Default)] | ||
struct MyLog(Mutex<Vec<LogEntry>>); | ||
|
||
impl log::Log for MyLog { | ||
fn enabled(&self, _metadata: &log::Metadata) -> bool { | ||
true | ||
} | ||
|
||
fn log(&self, record: &log::Record) { | ||
let mut l = self.0.lock().expect("not poisoned"); | ||
l.push(LogEntry { | ||
level: record.level(), | ||
message: record.args().to_string(), | ||
}) | ||
} | ||
|
||
fn flush(&self) {} | ||
} | ||
|
||
#[test] | ||
fn blah() { | ||
let log = Box::leak(Box::<MyLog>::default()); | ||
log::set_logger(log).expect("installing logger should succeed"); | ||
log::set_max_level(log::LevelFilter::Info); | ||
unsafe { | ||
sys::setup_rust_netint_logging(); | ||
sys::ni_logan_log(sys::ni_log_level_t_NI_LOG_ERROR, cstr!("foo %s %d").as_ptr(), cstr!("bar").as_ptr(), 1234u32); | ||
} | ||
let l = log.0.lock().expect("not poisoned"); | ||
assert_eq!(l.len(), 1); | ||
assert_eq!( | ||
&LogEntry { | ||
level: log::Level::Error, | ||
message: "foo bar 1234".to_owned(), | ||
}, | ||
&l[0] | ||
); | ||
} | ||
} |
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,52 @@ | ||
#[cfg(target_os = "linux")] | ||
mod linux { | ||
use cstr::cstr; | ||
use std::sync::Mutex; | ||
|
||
use xcoder_quadra_sys as sys; | ||
|
||
#[derive(Debug, Eq, PartialEq)] | ||
struct LogEntry { | ||
level: log::Level, | ||
message: String, | ||
} | ||
|
||
#[derive(Default)] | ||
struct MyLog(Mutex<Vec<LogEntry>>); | ||
|
||
impl log::Log for MyLog { | ||
fn enabled(&self, _metadata: &log::Metadata) -> bool { | ||
true | ||
} | ||
|
||
fn log(&self, record: &log::Record) { | ||
let mut l = self.0.lock().expect("not poisoned"); | ||
l.push(LogEntry { | ||
level: record.level(), | ||
message: record.args().to_string(), | ||
}) | ||
} | ||
|
||
fn flush(&self) {} | ||
} | ||
|
||
#[test] | ||
fn blah() { | ||
let log = Box::leak(Box::<MyLog>::default()); | ||
log::set_logger(log).expect("installing logger should succeed"); | ||
log::set_max_level(log::LevelFilter::Info); | ||
unsafe { | ||
sys::setup_rust_netint_logging(); | ||
sys::ni_log(sys::ni_log_level_t_NI_LOG_ERROR, cstr!("foo %s %d").as_ptr(), cstr!("bar").as_ptr(), 1234u32); | ||
} | ||
let l = log.0.lock().expect("not poisoned"); | ||
assert_eq!(l.len(), 1); | ||
assert_eq!( | ||
&LogEntry { | ||
level: log::Level::Error, | ||
message: "foo bar 1234".to_owned(), | ||
}, | ||
&l[0] | ||
); | ||
} | ||
} |