Skip to content

Commit bb8c79c

Browse files
authored
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.
1 parent c9e4285 commit bb8c79c

File tree

7 files changed

+121
-14
lines changed

7 files changed

+121
-14
lines changed

.github/workflows/rust.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ jobs:
6868
uses: actions-rs/cargo@v1
6969
with:
7070
command: test
71-
args: --verbose -p xcoder-logan -- --test-threads=1
71+
args: --verbose -p xcoder-logan -p xcoder-logan-310-sys -- --test-threads=1
7272
test_xcoder_logan_v2_compat:
7373
name: Test xcoder-logan v2-compat
7474
runs-on:
@@ -91,7 +91,7 @@ jobs:
9191
uses: actions-rs/cargo@v1
9292
with:
9393
command: test
94-
args: --verbose -p xcoder-logan --features v2-compat -- --test-threads=1
94+
args: --verbose -p xcoder-logan -p xcoder-logan-259-sys --features v2-compat -- --test-threads=1
9595
test_xcoder_quadra:
9696
name: Test xcoder-quadra
9797
runs-on:
@@ -114,4 +114,4 @@ jobs:
114114
uses: actions-rs/cargo@v1
115115
with:
116116
command: test
117-
args: --verbose -p xcoder-quadra -- --test-threads=1
117+
args: --verbose -p xcoder-quadra -p xcoder-quadra-sys -- --test-threads=1

Cargo.lock

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

xcoder/logging_shim.c

+6-11
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,19 @@
1111
#endif
1212

1313
#ifdef LOGAN
14-
void rust_netint_logan_callback(int level, char* message);
15-
#else
16-
void rust_netint_callback(int level, char* message);
14+
#define rust_netint_callback rust_netint_logan_callback
15+
#define netint_log_callback netint_logan_log_callback
16+
#define ni_log_set_callback ni_logan_log_set_callback
1717
#endif
1818

19-
void netint_log_callback(int level, const char* format, ...) {
20-
va_list args;
21-
va_start(args, format);
19+
void rust_netint_callback(int level, char* message);
20+
21+
static void netint_log_callback(int level, const char* format, va_list args) {
2222
char buf[2048] = {0};
2323
size_t buf_len = sizeof(buf) / sizeof(buf[0]);
2424
int chars_written = vsnprintf(buf, buf_len, format, args);
25-
va_end(args);
2625
if (chars_written > -1 && chars_written + 1 < ((int) buf_len)) {
27-
#ifdef LOGAN
28-
rust_netint_logan_callback(level, buf);
29-
#else
3026
rust_netint_callback(level, buf);
31-
#endif
3227
}
3328
}
3429

xcoder/xcoder-logan/xcoder-logan-310-sys/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ log = "0.4"
1111
# We're very permissive here with bindgen due to https://github.com/rust-lang/cargo/issues/5237
1212
bindgen = "0.*"
1313
cc = "1.0"
14+
15+
[dev-dependencies]
16+
cstr = "0.2.12"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#[cfg(target_os = "linux")]
2+
mod linux {
3+
use cstr::cstr;
4+
use std::sync::Mutex;
5+
6+
use xcoder_logan_310_sys as sys;
7+
8+
#[derive(Debug, Eq, PartialEq)]
9+
struct LogEntry {
10+
level: log::Level,
11+
message: String,
12+
}
13+
14+
#[derive(Default)]
15+
struct MyLog(Mutex<Vec<LogEntry>>);
16+
17+
impl log::Log for MyLog {
18+
fn enabled(&self, _metadata: &log::Metadata) -> bool {
19+
true
20+
}
21+
22+
fn log(&self, record: &log::Record) {
23+
let mut l = self.0.lock().expect("not poisoned");
24+
l.push(LogEntry {
25+
level: record.level(),
26+
message: record.args().to_string(),
27+
})
28+
}
29+
30+
fn flush(&self) {}
31+
}
32+
33+
#[test]
34+
fn blah() {
35+
let log = Box::leak(Box::<MyLog>::default());
36+
log::set_logger(log).expect("installing logger should succeed");
37+
log::set_max_level(log::LevelFilter::Info);
38+
unsafe {
39+
sys::setup_rust_netint_logging();
40+
sys::ni_logan_log(sys::ni_log_level_t_NI_LOG_ERROR, cstr!("foo %s %d").as_ptr(), cstr!("bar").as_ptr(), 1234u32);
41+
}
42+
let l = log.0.lock().expect("not poisoned");
43+
assert_eq!(l.len(), 1);
44+
assert_eq!(
45+
&LogEntry {
46+
level: log::Level::Error,
47+
message: "foo bar 1234".to_owned(),
48+
},
49+
&l[0]
50+
);
51+
}
52+
}

xcoder/xcoder-quadra/xcoder-quadra-sys/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ log = "0.4"
1111
# We're very permissive here with bindgen due to https://github.com/rust-lang/cargo/issues/5237
1212
bindgen = "0.*"
1313
cc = "1.0"
14+
15+
[dev-dependencies]
16+
cstr = "0.2.12"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#[cfg(target_os = "linux")]
2+
mod linux {
3+
use cstr::cstr;
4+
use std::sync::Mutex;
5+
6+
use xcoder_quadra_sys as sys;
7+
8+
#[derive(Debug, Eq, PartialEq)]
9+
struct LogEntry {
10+
level: log::Level,
11+
message: String,
12+
}
13+
14+
#[derive(Default)]
15+
struct MyLog(Mutex<Vec<LogEntry>>);
16+
17+
impl log::Log for MyLog {
18+
fn enabled(&self, _metadata: &log::Metadata) -> bool {
19+
true
20+
}
21+
22+
fn log(&self, record: &log::Record) {
23+
let mut l = self.0.lock().expect("not poisoned");
24+
l.push(LogEntry {
25+
level: record.level(),
26+
message: record.args().to_string(),
27+
})
28+
}
29+
30+
fn flush(&self) {}
31+
}
32+
33+
#[test]
34+
fn blah() {
35+
let log = Box::leak(Box::<MyLog>::default());
36+
log::set_logger(log).expect("installing logger should succeed");
37+
log::set_max_level(log::LevelFilter::Info);
38+
unsafe {
39+
sys::setup_rust_netint_logging();
40+
sys::ni_log(sys::ni_log_level_t_NI_LOG_ERROR, cstr!("foo %s %d").as_ptr(), cstr!("bar").as_ptr(), 1234u32);
41+
}
42+
let l = log.0.lock().expect("not poisoned");
43+
assert_eq!(l.len(), 1);
44+
assert_eq!(
45+
&LogEntry {
46+
level: log::Level::Error,
47+
message: "foo bar 1234".to_owned(),
48+
},
49+
&l[0]
50+
);
51+
}
52+
}

0 commit comments

Comments
 (0)