-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
use std::{ | ||
env, | ||
fmt::Write, | ||
os::raw::c_int, | ||
process, | ||
sync::{ | ||
atomic::{AtomicBool, Ordering}, | ||
Arc, | ||
}, | ||
}; | ||
|
||
use spdlog::{ | ||
formatter::Formatter, | ||
prelude::*, | ||
sink::{AsyncPoolSink, Sink}, | ||
ErrorHandler, | ||
}; | ||
|
||
static IS_FLUSHED: AtomicBool = AtomicBool::new(false); | ||
|
||
struct SetFlagSink; | ||
|
||
impl Sink for SetFlagSink { | ||
fn log(&self, _record: &spdlog::Record) -> error::Result<()> { | ||
Ok(()) | ||
} | ||
|
||
fn flush(&self) -> error::Result<()> { | ||
IS_FLUSHED.store(true, Ordering::SeqCst); | ||
Ok(()) | ||
} | ||
|
||
fn level_filter(&self) -> LevelFilter { | ||
LevelFilter::All | ||
} | ||
|
||
fn set_level_filter(&self, _level_filter: LevelFilter) { | ||
unimplemented!() | ||
} | ||
|
||
fn set_formatter(&self, _formatter: Box<dyn Formatter>) { | ||
unimplemented!() | ||
} | ||
|
||
fn set_error_handler(&self, _handler: Option<ErrorHandler>) { | ||
unimplemented!() | ||
} | ||
} | ||
|
||
fn run_test() { | ||
{ | ||
extern "C" fn check() { | ||
assert!(IS_FLUSHED.load(Ordering::SeqCst)); | ||
} | ||
// Setup `atexit`` to check the flag at the end of the program | ||
extern "C" { | ||
fn atexit(cb: extern "C" fn()) -> c_int; | ||
} | ||
assert_eq!(unsafe { atexit(check) }, 0); | ||
|
||
let async_pool_sink = Arc::new( | ||
AsyncPoolSink::builder() | ||
.sink(Arc::new(SetFlagSink)) | ||
.build() | ||
.unwrap(), | ||
); | ||
|
||
let logger = Arc::new(Logger::builder().sink(async_pool_sink).build().unwrap()); | ||
logger.set_flush_level_filter(LevelFilter::All); | ||
spdlog::set_default_logger(logger); | ||
} | ||
|
||
info!("hello async_pool_sink"); | ||
|
||
// Workaround, adding these 2 lines at the end of `main` function | ||
// | ||
// std::thread::sleep(std::time::Duration::from_millis(100)); | ||
// spdlog::default_logger().flush(); | ||
} | ||
|
||
fn main() { | ||
// https://github.com/SpriteOvO/spdlog-rs/issues/64 | ||
|
||
// This is a flaky test, it only has a certain probability of failing, so we run | ||
// it multiple times to make sure it's really working properly. | ||
{ | ||
let mut captured_output = String::new(); | ||
let args = env::args().collect::<Vec<_>>(); | ||
// If this is the parent process (no additional arguments) | ||
if args.len() == 1 { | ||
for i in 0..1000 { | ||
let status = process::Command::new(&args[0]) | ||
.arg("child") | ||
.spawn() | ||
.unwrap() | ||
.wait() | ||
.unwrap(); | ||
let success = status.success(); | ||
|
||
writeln!( | ||
captured_output, | ||
"Attempt #{i} = {}", | ||
if success { "ok" } else { "failed!" } | ||
) | ||
.unwrap(); | ||
|
||
if !success { | ||
eprintln!("{captured_output}"); | ||
panic!("Test failed"); | ||
} | ||
} | ||
return; | ||
} | ||
|
||
// Run the test after leaving the scope, so the main function ends | ||
// without dropping additional variables, thus exiting faster. This | ||
// should increase the probability of reproducing the error. | ||
} | ||
run_test(); | ||
} |