Skip to content

Commit dbf147f

Browse files
feat: Add tracing feature (#24)
* feat: Add --features tracing Also includes new examples/tracing.rs * chore: Remove outdated TODO * example: Update tracing example to fallback to trace if RUST_LOG isn't set Also refactor to assume tracing feature is enabled as specified in Cargo.toml * feat: Add `default` and `full` meta-features * chore: Enable --features=full in .vscode/settings.json * chore: Define private macros module to DRY out conditional compilation of tracing events * docs: Document crate feature flags * fix: Fix clippy lints and light refactor pass for readability * fix: Fix doc test
1 parent b66e83a commit dbf147f

File tree

8 files changed

+460
-38
lines changed

8 files changed

+460
-38
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"rust-analyzer.cargo.features": ["full"]
3+
}

Cargo.lock

Lines changed: 171 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,30 @@ readme = "README.md"
99
repository = "https://github.com/brannondorsey/mem-isolate"
1010
keywords = ["memory", "isolation", "sandbox", "unix"]
1111
categories = ["memory-management", "os::unix-apis", "rust-patterns"]
12-
exclude = [
13-
".github/*",
14-
]
12+
exclude = [".github/*"]
13+
14+
[features]
15+
# Include no features by default
16+
default = []
17+
18+
# Include all features
19+
full = ["tracing"]
20+
21+
# Actual features
22+
tracing = ["dep:tracing"]
1523

1624
[dependencies]
1725
bincode = "1"
1826
libc = "0.2"
1927
serde = { version = "1", features = ["derive"] }
2028
thiserror = "2"
29+
tracing = { version = "0.1.41", optional = true }
2130

2231
[dev-dependencies]
2332
criterion = "0.5.1"
2433
rand = "0.9.0"
2534
tempfile = "3.18.0"
35+
tracing-subscriber = { version = "0.3.19", features = ["env-filter"] }
2636

2737
[[bench]]
2838
name = "benchmarks"
@@ -35,3 +45,7 @@ test = true
3545
[[example]]
3646
name = "error-handling-complete"
3747
test = true
48+
49+
[[example]]
50+
name = "tracing"
51+
required-features = ["tracing"]

examples/tracing.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Compile error if tracing is not enabled
2+
#[cfg(not(feature = "tracing"))]
3+
compile_error!(
4+
"This example requires the 'tracing' feature to be enabled. Run with: cargo run --example tracing --features tracing"
5+
);
6+
7+
use mem_isolate::MemIsolateError;
8+
use tracing::{Level, info, instrument};
9+
use tracing_subscriber::EnvFilter;
10+
11+
#[instrument]
12+
fn main() -> Result<(), MemIsolateError> {
13+
init_tracing_subscriber();
14+
15+
mem_isolate::execute_in_isolated_process(|| {
16+
info!("look ma, I'm in the callable!");
17+
42
18+
})?;
19+
20+
Ok(())
21+
}
22+
23+
/// Defaults to TRACE but can be overridden by the RUST_LOG environment variable
24+
fn init_tracing_subscriber() {
25+
let env_filter = EnvFilter::builder()
26+
.with_default_directive(Level::TRACE.into())
27+
.from_env_lossy();
28+
tracing_subscriber::fmt().with_env_filter(env_filter).init();
29+
}

src/c.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub struct PipeFds {
1818
}
1919

2020
// Define the core trait for system functions
21-
pub trait SystemFunctions {
21+
pub trait SystemFunctions: std::fmt::Debug {
2222
fn fork(&self) -> Result<ForkReturn, io::Error>;
2323
fn pipe(&self) -> Result<PipeFds, io::Error>;
2424
fn close(&self, fd: c_int) -> Result<(), io::Error>;

src/c/mock.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::io;
66
use std::thread_local;
77

88
// Type for representing a mock result
9-
#[derive(Clone)]
9+
#[derive(Clone, Debug)]
1010
enum MockResult<T> {
1111
Ok(T),
1212
Err(i32), // OS error code
@@ -37,7 +37,7 @@ impl<T> MockResult<T> {
3737
}
3838

3939
/// Defines how a call should be implemented - real or mocked
40-
#[derive(Clone)]
40+
#[derive(Clone, Debug)]
4141
enum CallImplementation<T> {
4242
Real, // Use real implementation
4343
Mock(MockResult<T>), // Use mocked result
@@ -50,7 +50,7 @@ pub enum CallBehavior<T> {
5050
}
5151

5252
/// A generic queue of call implementations
53-
#[derive(Clone)]
53+
#[derive(Clone, Debug)]
5454
struct CallQueue<T> {
5555
queue: RefCell<VecDeque<CallImplementation<T>>>,
5656
name: &'static str, // For better error messages
@@ -93,7 +93,7 @@ impl<T: Clone> CallQueue<T> {
9393
}
9494

9595
/// Mock implementation that returns predefined values and can fall back to real implementation
96-
#[derive(Clone)]
96+
#[derive(Clone, Debug)]
9797
pub struct MockableSystemFunctions {
9898
real_impl: RealSystemFunctions,
9999
fallback_enabled: Cell<bool>,

0 commit comments

Comments
 (0)