fn flush_messages()

in reverie-examples/chunky_print.rs [97:155]


    fn flush_messages(&mut self) -> io::Result<()> {
        let non_empty = self
            .printbuf
            .iter()
            .fold(0, |acc, (_, v)| if v.is_empty() { acc } else { acc + 1 });
        if non_empty > 1 {
            let mut strbuf = String::new();
            for (tid, v) in self.printbuf.iter() {
                let _ = write!(&mut strbuf, "tid {}:{{", tid);
                let mut iter = v.iter();
                if let Some((_, b)) = iter.next() {
                    let _ = write!(&mut strbuf, "{}", b.len());
                    for (_, b) in iter {
                        let _ = write!(&mut strbuf, ", {}", b.len());
                    }
                }
                let _ = write!(&mut strbuf, "}} ");
            }
            info!(
                " [chunky_print] {} threads concurrent output in epoch {}, sizes: {}",
                non_empty, self.epoch_num, strbuf
            );
        } else {
            debug!(
                " [chunky_print] output from {} thread(s) in epoch {}: {} bytes",
                non_empty,
                self.epoch_num,
                self.printbuf
                    .iter()
                    .fold(0, |acc, (_, v)| v.iter().fold(acc, |a, (_, b)| a + b.len()))
            );
        }
        for (tid, v) in self.printbuf.iter_mut() {
            for (w, b) in v.iter() {
                match w {
                    Which::Stdout => {
                        trace!(
                            " [chunky_print] writing {} bytes to stdout from tid {}",
                            b.len(),
                            tid
                        );
                        io::Write::write_all(&mut io::stdout(), b)?;
                    }
                    Which::Stderr => {
                        trace!(
                            " [chunky_print] writing {} bytes to stderr from tid {}",
                            b.len(),
                            tid
                        );
                        io::Write::write_all(&mut io::stderr(), b)?;
                    }
                }
            }
            v.clear();
        }
        io::Write::flush(&mut io::stdout())?;
        io::Write::flush(&mut io::stderr())?;
        Ok(())
    }