fn spawn_logger<T>(name: &'static str, reader: BufReader<T>, dest: PathBuf, command: &str)
where
    BufReader<T>: AsyncBufReadExt + Unpin,
    T: 'static + Send,
{
    debug!("Storing {} logs in {:?}", name, dest);
    let command = format!("\ncommand: {}\n", command.to_string());
    tokio::task::spawn(async move {
        let mut file = OpenOptions::new()
            .create(true)
            .append(true)
            .open(dest)
            .await
            .with_context(|| format!("Could not create log file {}", name))?;
        {
            // Create a buffered writer, we don't want to hit the disk with
            // every single byte.
            let mut writer = BufWriter::new(&mut file);
            writer
                .write_all(command.as_bytes())
                .await
                .with_context(|| format!("Could not write log file {}", name))?;
            writer
                .flush()
                .await
                .with_context(|| format!("Could not write log file {}", name))?;

            let mut lines = reader.lines();
            while let Ok(Some(line)) = lines.next_line().await {
                // Display logs.
                info!("{}: {}", name, line);
                // Write logs to `dest`.
                writer
                    .write_all(line.as_bytes())
                    .await
                    .with_context(|| format!("Could not write log file {}", name))?;
                writer
                    .write_all(b"\n")
                    .await
                    .with_context(|| format!("Could not write log file {}", name))?;
                // Flush after each write, in case of crash.
                writer
                    .flush()
                    .await
                    .with_context(|| format!("Could not write log file {}", name))?;
            }
        }
        let _ = file.sync_data().await;
        Ok(()) as Result<(), anyhow::Error>
    });
}
/// Utility function: spawn an async task to asynchronously write the contents
/// of a reader to both a file and a log.
Utility function: spawn an async task to asynchronously write the contents
of a reader to both a file and a log.
Utility function : spawn an async task to asynchronously write the contents of a reader to both a file and a log .
fn spawn_logger<T>(name: &'static str, reader: BufReader<T>, dest: PathBuf, command: &str)
where
    BufReader<T>: AsyncBufReadExt + Unpin,
    T: 'static + Send,
{
    debug!("Storing {} logs in {:?}", name, dest);
    let command = format!("\ncommand: {}\n", command.to_string());
    tokio::task::spawn(async move {
        let mut file = OpenOptions::new()
            .create(true)
            .append(true)
            .open(dest)
            .await
            .with_context(|| format!("Could not create log file {}", name))?;
        {
            // Create a buffered writer, we don't want to hit the disk with
            // every single byte.
            let mut writer = BufWriter::new(&mut file);
            writer
                .write_all(command.as_bytes())
                .await
                .with_context(|| format!("Could not write log file {}", name))?;
            writer
                .flush()
                .await
                .with_context(|| format!("Could not write log file {}", name))?;
            let mut lines = reader.lines();
            while let Ok(Some(line)) = lines.next_line().await {
                // Display logs.
                info!("{}: {}", name, line);
                // Write logs to `dest`.
                writer
                    .write_all(line.as_bytes())
                    .await
                    .with_context(|| format!("Could not write log file {}", name))?;
                writer
                    .write_all(b"\n")
                    .await
                    .with_context(|| format!("Could not write log file {}", name))?;
                // Flush after each write, in case of crash.
                writer
                    .flush()
                    .await
                    .with_context(|| format!("Could not write log file {}", name))?;
            }
        }
        let _ = file.sync_data().await;
        Ok(()) as Result<(), anyhow::Error>
    });
}
fn spawn_logger < T > ( name : & ' static str , reader : BufReader < T > , dest : PathBuf , command : & str ) where BufReader < T > : AsyncBufReadExt + Unpin , T : ' static + Send , { debug ! ( " " name dest ) ; let command = format ! ( " \n \n " command to_string ( ) ) ; tokio :: task :: spawn ( async move { let mut file = OpenOptions :: new ( ) . create ( true ) . append ( true ) . open ( dest ) . await . with_context ( | | format ! ( " " name ) ) ? ; { // Create a buffered writer, we don't want to hit the disk with // every single byte. let mut writer = BufWriter :: new ( & mut file ) ; writer . write_all ( command . as_bytes ( ) ) . await . with_context ( | | format ! ( " " name ) ) ? ; writer . flush ( ) . await . with_context ( | | format ! ( " " name ) ) ? ; let mut lines = reader . lines ( ) ; while let Ok ( Some ( line ) ) = lines . next_line ( ) . await { // Display logs. info ! ( " " name line ) ; // Write logs to `dest`. writer . write_all ( line . as_bytes ( ) ) . await . with_context ( | | format ! ( " " name ) ) ? ; writer . write_all ( b" \n " ) . await . with_context ( | | format ! ( " " name ) ) ? ; // Flush after each write, in case of crash. writer . flush ( ) . await . with_context ( | | format ! ( " " name ) ) ? ; } } let _ = file . sync_data ( ) . await ; Ok ( ( ) ) as Result < ( ) , anyhow :: Error > } ) ; }
Utility function: spawn an async task to asynchronously write the contents
of a reader to both a file and a log.
Utility function : spawn an async task to asynchronously write the contents of a reader to both a file and a log .
name
&'static str
reader
BufReader<T>
dest
PathBuf
command
&str
name
&'static str
reader
BufReader<T>
dest
PathBuf
command
&str