Skip to content

Commit

Permalink
Debug feature and improved logging
Browse files Browse the repository at this point in the history
This dramatically increases logging verbosity and adds a feature that
allows logging to be enabled in production use cases without the test or
debug flag.
  • Loading branch information
jkilpatr committed Jun 29, 2021
1 parent cbb3401 commit 0fe344f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "compressed_log"
version = "0.3.4"
version = "0.3.7"
authors = ["Michał Papierski <[email protected]>, Justin Kilpatrick <[email protected]>"]
edition = "2018"
include = [
Expand All @@ -24,4 +24,8 @@ futures = "0.3"
backoff = "0.3"
serde_json = "1.0"
serde_derive = "1.0"
serde = "1.0"
serde = "1.0"

[features]
# prints debug messages as a feature
debug = []
8 changes: 6 additions & 2 deletions src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ impl Logger {

impl Drop for Logger {
fn drop(&mut self) {
debug_eprintln!("Drop handler called!");
// Unconditional rotation of log
let encoder = self.encoder.lock().expect("Unable to acquire buffer lock");
let data = self.rotate(&encoder).expect("Unable to rotate the buffer");
Expand Down Expand Up @@ -112,15 +113,16 @@ impl Log for Logger {
let log_string = (self.format)(&record);

// First, write whole formatted string
encoder.borrow_mut().add_line(log_string);
encoder.borrow_mut().add_line(log_string.clone());

// Rotate the buffer based on a threshold
let current_size = {
let encoder = encoder.borrow();
encoder.len()
};
if current_size < self.threshold {
//debug_eprintln!("Buffer {} of {}", current_size, self.threshold);
debug_eprintln!("Buffer {} of {}", current_size, self.threshold);
debug_eprintln!("Line {}", log_string);
// Compressed log didn't hit the size threshold
return;
}
Expand Down Expand Up @@ -149,9 +151,11 @@ impl Log for Logger {
}

fn upload_logs(url: String, data: FinishValue) {
debug_eprintln!("Uploading logs");
// create a new thread for the actix executor
// to adopt in order to run the future to completion
thread::spawn(|| {
debug_eprintln!("thread spawned");
let runner = System::new();
runner.block_on(async move {
match data {
Expand Down
5 changes: 2 additions & 3 deletions src/macros.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#[allow(unused_macros)]
macro_rules! debug_eprintln {
() => ({
#[cfg(any(debug_assertions,test))]
#[cfg(any(debug_assertions,test,feature = "debug"))]
eprint!("\n")
});
($($arg:tt)*) => ({
#[cfg(any(debug_assertions,test))]
#[cfg(any(debug_assertions,test,feature = "debug"))]
eprintln!($($arg)*);
})
}
Expand Down

0 comments on commit 0fe344f

Please sign in to comment.