From 308390defd2ba73286b34877f54c72ddcf736216 Mon Sep 17 00:00:00 2001 From: Fenrir Date: Sun, 25 Feb 2024 21:02:57 -0700 Subject: [PATCH] Support console stderr redirection Allows you read stderr output from `GDB` or other debuggers --- ctru-rs/src/console.rs | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/ctru-rs/src/console.rs b/ctru-rs/src/console.rs index 02702ccb..c3152d95 100644 --- a/ctru-rs/src/console.rs +++ b/ctru-rs/src/console.rs @@ -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}; @@ -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 ConsoleScreen for S {} @@ -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. ///