Skip to content

Commit

Permalink
Support console stderr redirection
Browse files Browse the repository at this point in the history
Allows you read stderr output from `GDB` or other debuggers
  • Loading branch information
FenrirWolf committed Feb 26, 2024
1 parent 10163fd commit 308390d
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions ctru-rs/src/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
//! The [`Console`] works as a virtual shell that renders on screen all output of `stdout`. As such, it is useful as a basic interface to show info to the user,
//! such as in simple "Hello World" applications or more complex software that does not need much user interaction.
//!
//! Have a look at [`Soc::redirect_to_3dslink()`](crate::services::soc::Soc::redirect_to_3dslink) for a better alternative when debugging applications.
//! Have a look at [`redirect_stderr`] or [`Soc::redirect_to_3dslink`](crate::services::soc::Soc::redirect_to_3dslink) for better alternatives when debugging applications.
use std::cell::{RefMut, UnsafeCell};

use ctru_sys::{consoleClear, consoleInit, consoleSelect, consoleSetWindow, PrintConsole};
use ctru_sys::{
consoleClear, consoleDebugInit, consoleInit, consoleSelect, consoleSetWindow, PrintConsole,
};

use crate::services::gfx::{Flush, Screen, Swap};

Expand Down Expand Up @@ -38,6 +39,18 @@ pub enum Dimension {
Height,
}

/// Destination for stderr redirection with [`redirect_stderr`].
#[doc(alias = "debugDevice")]
#[repr(u32)]
pub enum Destination {
/// Print stderr to the active [`Console`] window. This is the default behavior.
Console = ctru_sys::debugDevice_CONSOLE,
/// Print stderr via [`ctru_sys::svcOutputDebugString`]. This allows you to capture error and panic messages with `GDB` or other debuggers.
Debugger = ctru_sys::debugDevice_SVC,
/// Swallow outputs from stderr.
Null = ctru_sys::debugDevice_NULL,
}

/// A [`Screen`] that can be used as a target for [`Console`].
pub trait ConsoleScreen: Screen + Swap + Flush {}
impl<S: Screen + Swap + Flush> ConsoleScreen for S {}
Expand Down Expand Up @@ -66,6 +79,12 @@ pub struct Console<'screen> {
screen: RefMut<'screen, dyn ConsoleScreen>,
}

/// Send output from stderr to the specified [`Destination`]. This function can be used to capture error and panic messages with `GDB` or other debuggers.
#[doc(alias = "consoleDebugInit")]
pub fn redirect_stderr(dest: Destination) {
unsafe { consoleDebugInit(dest as _) }
}

impl<'screen> Console<'screen> {
/// Initialize a console on the chosen screen.
///
Expand Down

0 comments on commit 308390d

Please sign in to comment.