Skip to content

Commit

Permalink
no parent id
Browse files Browse the repository at this point in the history
Signed-off-by: Yoshua Wuyts <yoshuawuyts@gmail.com>
  • Loading branch information
yoshuawuyts committed Jun 11, 2019
1 parent 0bcbc4c commit f5f37cf
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
4 changes: 2 additions & 2 deletions examples/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ fn setup_logger() {
.filter(None, log::LevelFilter::Trace)
.build();

async_log::Logger::wrap(logger, || (12, Some(13)))
async_log::Logger::wrap(logger, || 12)
.start(log::LevelFilter::Trace)
.unwrap();
}
Expand All @@ -20,6 +20,6 @@ fn main() {

span!("inner, x={}", x, {
info!("we must go deeper {}", x);
});
})
})
}
7 changes: 5 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,12 @@
//! use log::info;
//!
//! fn setup_logger() {
//! env_logger::Builder::new()
//! let logger = env_logger::Builder::new()
//! .filter(None, log::LevelFilter::Trace)
//! .try_init()
//! .build();
//!
//! async_log::Logger::wrap(logger, || (12, Some(13)))
//! .start(log::LevelFilter::Trace)
//! .unwrap();
//! }
//!
Expand Down
16 changes: 6 additions & 10 deletions src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::thread;
#[derive(Debug)]
pub struct Logger<L: Log + 'static, F>
where
F: Fn() -> (u64, Option<u64>) + Send + Sync + 'static,
F: Fn() -> u64 + Send + Sync + 'static,
{
backtrace: bool,
logger: L,
Expand All @@ -16,7 +16,7 @@ where

impl<L: Log + 'static, F> Logger<L, F>
where
F: Fn() -> (u64, Option<u64>) + Send + Sync + 'static,
F: Fn() -> u64 + Send + Sync + 'static,
{
/// Wrap an existing logger, extending it with async functionality.
pub fn wrap(logger: L, with: F) -> Self {
Expand All @@ -40,7 +40,7 @@ where
}

/// Call the `self.with` closure, and return its results.
fn with(&self) -> (u64, Option<u64>) {
fn with(&self) -> u64 {
(self.with)()
}

Expand All @@ -62,23 +62,20 @@ fn thread_id() -> String {

impl<L: Log, F> log::Log for Logger<L, F>
where
F: Fn() -> (u64, Option<u64>) + Send + Sync + 'static,
F: Fn() -> u64 + Send + Sync + 'static,
{
fn enabled(&self, metadata: &Metadata<'_>) -> bool {
self.logger.enabled(metadata)
}

fn log(&self, record: &Record<'_>) {
if self.enabled(record.metadata()) {
let (curr_id, parent_id) = self.with();
let curr_id = self.with();
let depth = self.compute_stack_depth(&record);
let symbol = async_log_capture_caller(depth);

let thread_id = format!(", thread_id={}", thread_id());
let task_id = format!(", task_id={}", curr_id);
let parent_id = parent_id
.map(|pid| format!(", task_parent_id={}", pid))
.unwrap_or_else(|| String::from(""));

let (line, filename) = if self.backtrace {
match symbol {
Expand Down Expand Up @@ -107,12 +104,11 @@ where
&log::Record::builder()
.args(record.args().clone())
.args(format_args!(
"{}{}{}{}{}{}",
"{}{}{}{}{}",
record.args(),
filename,
line,
task_id,
parent_id,
thread_id
))
.metadata(record.metadata().clone())
Expand Down

0 comments on commit f5f37cf

Please # to comment.