Skip to main content

async_file/
async_file.rs

1// =============================================================================
2//        #######
3//     ###       ###     F: async_file.rs
4//    ##   ## ##   ##    P: AppCore-Runtime
5//         ## ##
6//                       C: 2026/09/07 00:00:00 by dnettoRaw
7//    ##   ## ##   ##    U: 2026/09/07 00:00:00 by dnettoRaw
8//      ###########      S: 1.0.2-rc
9// =============================================================================
10
11//! Explicit bounded asynchronous file delivery with a visible output path.
12
13use appcore_log::{
14    AsyncSink, AsyncSinkConfig, FileSink, FileSinkConfig, LogDispatcher, LogError, LogPolicy,
15    LOG_SIZE_8_MIB,
16};
17use std::sync::Arc;
18
19fn main() -> Result<(), LogError> {
20    let directory = std::path::PathBuf::from("target/appcore-log-example");
21    std::fs::create_dir_all(&directory).map_err(|_| LogError::Io)?;
22
23    let path = directory.join("async.jsonl");
24    let file = Arc::new(FileSink::new(FileSinkConfig {
25        path: path.clone(),
26        max_bytes: LOG_SIZE_8_MIB,
27        sync_each_write: true,
28        retention: 2,
29        archive: None,
30    })?);
31
32    // This queue retains at most 256 events and 1 MiB, including active I/O.
33    let asynchronous = Arc::new(AsyncSink::new(
34        AsyncSinkConfig {
35            max_events: 256,
36            max_bytes: 1024 * 1024,
37        },
38        file,
39    )?);
40    let dispatcher = LogDispatcher::new(LogPolicy::default(), vec![asynchronous.clone()]);
41    let log = dispatcher.event(0, "application");
42
43    log.info("application started");
44    log.warn("storage response is slow");
45
46    // The lifecycle owner drains durable writes before exiting.
47    asynchronous.shutdown()?;
48    let path = std::fs::canonicalize(path).map_err(|_| LogError::Io)?;
49    println!("log written to {}", path.display());
50    Ok(())
51}