⚠️ VeridianOS Kernel Documentation - This is low-level kernel code. All functions are unsafe unless explicitly marked otherwise. no_std

veridian_kernel/
print_capture.rs

1//! Output capture buffer for redirecting `println!` output.
2//!
3//! When capture mode is active, `println!` output is appended to a global
4//! String buffer in addition to serial/framebuffer.  The GUI terminal uses
5//! this to display command output.
6
7use alloc::string::String;
8use core::{
9    fmt,
10    sync::atomic::{AtomicBool, Ordering},
11};
12
13use spin::Mutex;
14
15/// Whether capture mode is active.
16static CAPTURING: AtomicBool = AtomicBool::new(false);
17
18/// The capture buffer (protected by a spinlock).
19static CAPTURE_BUF: Mutex<Option<String>> = Mutex::new(None);
20
21/// Start capturing `println!` output.
22pub fn start_capture() {
23    let mut buf = CAPTURE_BUF.lock();
24    *buf = Some(String::new());
25    CAPTURING.store(true, Ordering::Release);
26}
27
28/// Stop capturing and return the captured output.
29pub fn stop_capture() -> String {
30    CAPTURING.store(false, Ordering::Release);
31    let mut buf = CAPTURE_BUF.lock();
32    buf.take().unwrap_or_default()
33}
34
35/// Called by the `print!` macro.  Appends to the capture buffer if active.
36pub fn _capture_print(args: fmt::Arguments) {
37    if CAPTURING.load(Ordering::Acquire) {
38        use core::fmt::Write;
39        let mut buf = CAPTURE_BUF.lock();
40        if let Some(ref mut s) = *buf {
41            let _ = s.write_fmt(args);
42        }
43    }
44}