Skip to content

Commit bc2ac0d

Browse files
authored
Expose async logging for backends (#70)
Expose async logging for backends and clean up logging code in general. Thanks @kinetiknz for the help on this one -- really appreciate your taking the time to help familiarize me with the codebase and the suggestions / discussion!
1 parent 8944c8d commit bc2ac0d

File tree

11 files changed

+92
-60
lines changed

11 files changed

+92
-60
lines changed

.gitmodules

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[submodule "cubeb-sys/libcubeb"]
22
path = cubeb-sys/libcubeb
3-
url = https://github.com/kinetiknz/cubeb
3+
url = https://github.com/mozilla/cubeb

cubeb-api/Cargo.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
[package]
22
name = "cubeb"
3-
version = "0.10.0"
3+
version = "0.10.1"
44
authors = ["Dan Glastonbury <dglastonbury@mozilla.com>"]
55
license = "ISC"
66
readme = "README.md"
77
keywords = ["cubeb"]
8-
repository = "https://github.com/djg/cubeb-rs"
9-
homepage = "https://github.com/djg/cubeb-rs"
8+
repository = "https://github.com/mozilla/cubeb-rs"
9+
homepage = "https://github.com/mozilla/cubeb-rs"
1010
description = """
1111
Bindings to libcubeb for interacting with system audio from rust.
1212
"""
1313
categories = ["api-bindings"]
1414

1515
[badges]
16-
circle-ci = { repository = "djg/cubeb-rs" }
16+
circle-ci = { repository = "mozilla/cubeb-rs" }
1717

1818
[features]
1919
gecko-in-tree = ["cubeb-core/gecko-in-tree"]
2020

2121
[dependencies]
22-
cubeb-core = { path = "../cubeb-core", version = "0.10.0" }
22+
cubeb-core = { path = "../cubeb-core", version = "0.10.1" }

cubeb-backend/Cargo.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
[package]
22
name = "cubeb-backend"
3-
version = "0.10.0"
3+
version = "0.10.1"
44
authors = ["Dan Glastonbury <dglastonbury@mozilla.com>"]
55
license = "ISC"
66
keywords = ["cubeb"]
7-
repository = "https://github.com/djg/cubeb-rs"
8-
homepage = "https://github.com/djg/cubeb-rs"
7+
repository = "https://github.com/mozilla/cubeb-rs"
8+
homepage = "https://github.com/mozilla/cubeb-rs"
99
description = """
1010
Bindings to libcubeb internals to facilitate implementing cubeb backends in rust.
1111
"""
1212
categories = ["api-bindings"]
1313

1414
[badges]
15-
circle-ci = { repository = "djg/cubeb-rs" }
15+
circle-ci = { repository = "mozilla/cubeb-rs" }
1616

1717
[features]
1818
gecko-in-tree = ["cubeb-core/gecko-in-tree"]
1919

2020
[dependencies]
21-
cubeb-core = { path = "../cubeb-core", version = "0.10.0" }
21+
cubeb-core = { path = "../cubeb-core", version = "0.10.1" }

cubeb-backend/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ extern crate cubeb_core;
77

88
pub mod capi;
99
#[macro_use]
10-
mod log;
10+
pub mod log;
1111
mod ops;
1212
mod traits;
1313

cubeb-backend/src/log.rs

Lines changed: 64 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,71 +3,98 @@
33
// This program is made available under an ISC-style license. See the
44
// accompanying file LICENSE for details.
55

6+
/// Annotates input buffer string with logging information.
7+
/// Returns result as a ffi::CStr for use with native cubeb logging functions.
8+
pub fn cubeb_log_internal_buf_fmt<'a>(
9+
buf: &'a mut [u8; 1024],
10+
file: &str,
11+
line: u32,
12+
msg: &str,
13+
) -> &'a std::ffi::CStr {
14+
use std::io::Write;
15+
let filename = std::path::Path::new(file)
16+
.file_name()
17+
.unwrap()
18+
.to_str()
19+
.unwrap();
20+
// 2 for ':', 1 for ' ', 1 for '\n', and 1 for converting `line!()` to number of digits
21+
let len = filename.len() + ((line as f32).log10().trunc() as usize) + msg.len() + 5;
22+
debug_assert!(len < buf.len(), "log will be truncated");
23+
let _ = writeln!(&mut buf[..], "{}:{}: {}", filename, line, msg);
24+
let last = std::cmp::min(len, buf.len() - 1);
25+
buf[last] = 0;
26+
let cstr = unsafe { std::ffi::CStr::from_bytes_with_nul_unchecked(&buf[..=last]) };
27+
cstr
28+
}
29+
630
#[macro_export]
731
macro_rules! cubeb_log_internal {
8-
($level: expr, $msg: expr) => {
9-
#[allow(unused_unsafe)]
10-
unsafe {
11-
if $level <= $crate::ffi::g_cubeb_log_level.into() {
12-
cubeb_log_internal!(__INTERNAL__ $msg);
13-
}
14-
}
32+
($log_callback: expr, $level: expr, $fmt: expr, $($arg: expr),+) => {
33+
cubeb_log_internal!($log_callback, $level, format!($fmt, $($arg),*));
1534
};
16-
($level: expr, $fmt: expr, $($arg: expr),+) => {
35+
($log_callback: expr, $level: expr, $msg: expr) => {
1736
#[allow(unused_unsafe)]
1837
unsafe {
1938
if $level <= $crate::ffi::g_cubeb_log_level.into() {
20-
cubeb_log_internal!(__INTERNAL__ format!($fmt, $($arg),*));
39+
if let Some(log_callback) = $log_callback {
40+
let mut buf = [0u8; 1024];
41+
log_callback(
42+
$crate::log::cubeb_log_internal_buf_fmt(&mut buf, file!(), line!(), &$msg)
43+
.as_ptr(),
44+
);
45+
}
2146
}
2247
}
2348
};
24-
(__INTERNAL__ $msg: expr) => {
25-
if let Some(log_callback) = $crate::ffi::g_cubeb_log_callback {
26-
use std::io::Write;
49+
}
2750

28-
let mut buf = [0 as u8; 1024];
29-
let filename = std::path::Path::new(file!())
30-
.file_name()
31-
.unwrap()
32-
.to_str()
33-
.unwrap();
34-
// 2 for ':', 1 for ' ', 1 for '\n', and 1 for converting `line!()` to number of digits
35-
let len = filename.len() + ((line!() as f32).log10().trunc() as usize) + $msg.len() + 5;
36-
debug_assert!(len < buf.len(), "log will be truncated");
37-
let _ = write!(&mut buf[..], "{}:{}: {}\n", filename, line!(), $msg);
38-
let last = std::cmp::min(len, buf.len() - 1);
39-
buf[last] = 0;
40-
let cstr = unsafe { std::ffi::CStr::from_bytes_with_nul_unchecked(&buf[..=last]) };
41-
log_callback(cstr.as_ptr());
42-
}
43-
}
51+
#[macro_export]
52+
macro_rules! cubeb_log {
53+
($($arg: expr),+) => (cubeb_log_internal!($crate::ffi::g_cubeb_log_callback, $crate::LogLevel::Normal, $($arg),+));
4454
}
4555

4656
#[macro_export]
4757
macro_rules! cubeb_logv {
48-
($msg: expr) => (cubeb_log_internal!($crate::LogLevel::Verbose, $msg));
49-
($fmt: expr, $($arg: expr),+) => (cubeb_log_internal!($crate::LogLevel::Verbose, $fmt, $($arg),*));
58+
($($arg: expr),+) => (cubeb_log_internal!($crate::ffi::g_cubeb_log_callback, $crate::LogLevel::Verbose, $($arg),+));
5059
}
5160

5261
#[macro_export]
53-
macro_rules! cubeb_log {
54-
($msg: expr) => (cubeb_log_internal!($crate::LogLevel::Normal, $msg));
55-
($fmt: expr, $($arg: expr),+) => (cubeb_log_internal!($crate::LogLevel::Normal, $fmt, $($arg),*));
62+
macro_rules! cubeb_alog {
63+
($($arg: expr),+) => (cubeb_log_internal!($crate::ffi::cubeb_async_log.into(), $crate::LogLevel::Normal, $($arg),+));
64+
}
65+
66+
#[macro_export]
67+
macro_rules! cubeb_alogv {
68+
($($arg: expr),+) => (cubeb_log_internal!($crate::ffi::cubeb_async_log.into(), $crate::LogLevel::Verbose, $($arg),+));
5669
}
5770

5871
#[cfg(test)]
5972
mod tests {
6073
#[test]
61-
fn test_normal_logging() {
62-
cubeb_log!("This is log at normal level");
74+
fn test_normal_logging_sync() {
75+
cubeb_log!("This is synchronous log output at normal level");
6376
cubeb_log!("{} Formatted log", 1);
6477
cubeb_log!("{} Formatted {} log {}", 1, 2, 3);
6578
}
6679

6780
#[test]
68-
fn test_verbose_logging() {
69-
cubeb_logv!("This is a log at verbose level");
81+
fn test_verbose_logging_sync() {
82+
cubeb_logv!("This is synchronous log output at verbose level");
7083
cubeb_logv!("{} Formatted log", 1);
7184
cubeb_logv!("{} Formatted {} log {}", 1, 2, 3);
7285
}
86+
87+
#[test]
88+
fn test_normal_logging_async() {
89+
cubeb_alog!("This is asynchronous log output at normal level");
90+
cubeb_alog!("{} Formatted log", 1);
91+
cubeb_alog!("{} Formatted {} log {}", 1, 2, 3);
92+
}
93+
94+
#[test]
95+
fn test_verbose_logging_async() {
96+
cubeb_alogv!("This is asynchronous log output at verbose level");
97+
cubeb_alogv!("{} Formatted log", 1);
98+
cubeb_alogv!("{} Formatted {} log {}", 1, 2, 3);
99+
}
73100
}

cubeb-core/Cargo.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
[package]
22
name = "cubeb-core"
3-
version = "0.10.0"
3+
version = "0.10.1"
44
authors = ["Dan Glastonbury <dglastonbury@mozilla.com>"]
55
license = "ISC"
66
keywords = ["cubeb"]
7-
repository = "https://github.com/djg/cubeb-rs"
8-
homepage = "https://github.com/djg/cubeb-rs"
7+
repository = "https://github.com/mozilla/cubeb-rs"
8+
homepage = "https://github.com/mozilla/cubeb-rs"
99
description = """
1010
Common types and definitions for cubeb rust and C bindings. Not intended for direct use.
1111
"""
1212
categories = ["api-bindings"]
1313

1414
[badges]
15-
circle-ci = { repository = "djg/cubeb-rs" }
15+
circle-ci = { repository = "mozilla/cubeb-rs" }
1616

1717
[features]
1818
gecko-in-tree = ["cubeb-sys/gecko-in-tree"]
1919

2020
[dependencies]
2121
bitflags = "1.2.0"
22-
cubeb-sys = { path = "../cubeb-sys", version = "0.10.0" }
22+
cubeb-sys = { path = "../cubeb-sys", version = "0.10.1" }

cubeb-sys/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
[package]
22
name = "cubeb-sys"
3-
version = "0.10.0"
3+
version = "0.10.1"
44
authors = ["Dan Glastonbury <dglastonbury@mozilla.com>"]
5-
repository = "https://github.com/djg/cubeb-rs"
5+
repository = "https://github.com/mozilla/cubeb-rs"
66
license = "ISC"
77
description = "Native bindings to the cubeb library"
88

99
links = "cubeb"
1010
build = "build.rs"
1111

1212
[badges]
13-
circle-ci = { repository = "djg/cubeb-rs" }
13+
circle-ci = { repository = "mozilla/cubeb-rs" }
1414

1515
[features]
1616
gecko-in-tree = []

cubeb-sys/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ fn main() {
9595
let _ = pkg_config::find_library("alsa");
9696
let _ = pkg_config::find_library("libpulse");
9797
let _ = pkg_config::find_library("jack");
98+
let _ = pkg_config::find_library("speexdsp");
9899
if android {
99100
println!("cargo:rustc-link-lib=dylib=OpenSLES");
100101
}

cubeb-sys/src/log.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// This program is made available under an ISC-style license. See the
44
// accompanying file LICENSE for details.
55

6-
use std::os::raw::{c_char, c_int};
6+
use std::os::raw::{c_char, c_int, c_void};
77

88
cubeb_enum! {
99
pub enum cubeb_log_level {
@@ -23,4 +23,7 @@ extern "C" {
2323

2424
pub static g_cubeb_log_level: cubeb_log_level;
2525
pub static g_cubeb_log_callback: cubeb_log_callback;
26+
27+
pub fn cubeb_async_log_reset_threads(_: c_void) -> c_void;
28+
pub fn cubeb_async_log(msg: *const c_char, ...) -> c_void;
2629
}

0 commit comments

Comments
 (0)