Skip to content

Commit

Permalink
Added support for logging variables
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuri6037 committed Aug 28, 2024
1 parent ee5cd85 commit 3725ec0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bp3d-debug"
version = "1.0.0-rc.4.0.0"
version = "1.0.0-rc.5.0.0"
authors = ["Yuri Edward <yuri6037@outlook.com>"]
edition = "2021"
description = "Tracing subscriber implementations for use with BP3D software. Supports traditional logging through bp3d-logger and supports remote profiling through TCP."
Expand Down
19 changes: 12 additions & 7 deletions src/engine/void.rs → src/engine/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ use std::fmt::Arguments;
use std::num::NonZeroU32;
use std::sync::atomic::Ordering;

pub struct VoidDebugger {}
pub struct DefaultDebugger {}

impl crate::profiler::Profiler for VoidDebugger {
impl crate::profiler::Profiler for DefaultDebugger {
fn section_register(&self, _: &'static crate::profiler::section::Section) -> NonZeroU32 {
ENGINE_INIT_FLAG.store(true, Ordering::Relaxed);
unsafe { NonZeroU32::new_unchecked(1) }
Expand All @@ -46,7 +46,7 @@ impl crate::profiler::Profiler for VoidDebugger {
}
}

impl crate::trace::Tracer for VoidDebugger {
impl crate::trace::Tracer for DefaultDebugger {
fn register_callsite(&self, _: &'static Callsite) -> NonZeroU32 {
ENGINE_INIT_FLAG.store(true, Ordering::Relaxed);
unsafe { NonZeroU32::new_unchecked(1) }
Expand All @@ -70,13 +70,18 @@ impl crate::trace::Tracer for VoidDebugger {
}
}

impl crate::logger::Logger for VoidDebugger {
fn log(&self, callsite: &'static crate::logger::Callsite, args: Arguments, _: &[Field]) {
impl crate::logger::Logger for DefaultDebugger {
fn log(&self, callsite: &'static crate::logger::Callsite, args: Arguments, fields: &[Field]) {
let mut s = String::new();
for field in fields {
s += &format!(", {}={}", field.name(), field.value());
}
println!(
"[{}] {}: {}",
"[{}] {}: {}{}",
callsite.level(),
callsite.location().module_path(),
args
args,
s
);
ENGINE_INIT_FLAG.store(true, Ordering::Relaxed);
}
Expand Down
10 changes: 5 additions & 5 deletions src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

use std::sync::atomic::{AtomicBool, Ordering};

mod void;
mod default;

pub trait Engine:
crate::logger::Logger + crate::profiler::Profiler + crate::trace::Tracer + Sync
Expand All @@ -41,7 +41,7 @@ impl<T: crate::logger::Logger + crate::profiler::Profiler + crate::trace::Tracer

static ENGINE_INIT_FLAG: AtomicBool = AtomicBool::new(false);

static mut ENGINE: &dyn Engine = &void::VoidDebugger {};
static mut ENGINE: &dyn Engine = &default::DefaultDebugger {};

pub fn get() -> &'static dyn Engine {
unsafe { ENGINE }
Expand All @@ -64,15 +64,15 @@ mod tests {

#[test]
fn basic() {
crate::engine::set(&crate::engine::void::VoidDebugger {});
assert!(!crate::engine::set(&crate::engine::void::VoidDebugger {}));
crate::engine::set(&crate::engine::default::DefaultDebugger {});
assert!(!crate::engine::set(&crate::engine::default::DefaultDebugger {}));
}

#[test]
fn after_use() {
crate::engine::get().span_exit(Id::new(unsafe { NonZeroU32::new_unchecked(1) }, unsafe {
NonZeroU32::new_unchecked(1)
}));
assert!(!crate::engine::set(&crate::engine::void::VoidDebugger {}));
assert!(!crate::engine::set(&crate::engine::default::DefaultDebugger {}));
}
}

0 comments on commit 3725ec0

Please sign in to comment.