Skip to content

Commit 2a92f8f

Browse files
authored
Merge pull request #224 from rust3ds/feature/debug-console-init
Support console stderr redirection
2 parents cb77ad2 + 695f9b7 commit 2a92f8f

1 file changed

Lines changed: 28 additions & 3 deletions

File tree

ctru-rs/src/console.rs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
//! 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,
44
//! such as in simple "Hello World" applications or more complex software that does not need much user interaction.
55
//!
6-
//! Have a look at [`Soc::redirect_to_3dslink()`](crate::services::soc::Soc::redirect_to_3dslink) for a better alternative when debugging applications.
7-
6+
//! Have a look at [`redirect_stderr`] or [`Soc::redirect_to_3dslink`](crate::services::soc::Soc::redirect_to_3dslink) for better alternatives when debugging applications.
87
use std::cell::{RefMut, UnsafeCell};
98

10-
use ctru_sys::{PrintConsole, consoleClear, consoleInit, consoleSelect, consoleSetWindow};
9+
use ctru_sys::{
10+
PrintConsole, consoleClear, consoleDebugInit, consoleInit, consoleSelect, consoleSetWindow,
11+
};
1112

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

@@ -38,6 +39,18 @@ pub enum Dimension {
3839
Height,
3940
}
4041

42+
/// Destination for stderr redirection with [`redirect_stderr`].
43+
#[doc(alias = "debugDevice")]
44+
#[repr(u8)]
45+
pub enum Destination {
46+
/// Print stderr to the active [`Console`] window. This is the default behavior.
47+
Console = ctru_sys::debugDevice_CONSOLE,
48+
/// Print stderr via [`ctru_sys::svcOutputDebugString`]. This allows you to capture error and panic messages with `GDB` or other debuggers.
49+
Debugger = ctru_sys::debugDevice_SVC,
50+
/// Swallow outputs from stderr.
51+
Null = ctru_sys::debugDevice_NULL,
52+
}
53+
4154
/// A [`Screen`] that can be used as a target for [`Console`].
4255
pub trait ConsoleScreen: Screen + Swap + Flush {}
4356
impl<S: Screen + Swap + Flush> ConsoleScreen for S {}
@@ -66,6 +79,18 @@ pub struct Console<'screen> {
6679
screen: RefMut<'screen, dyn ConsoleScreen>,
6780
}
6881

82+
/// Send output from stderr to the specified [`Destination`]. This function can be used to capture error and panic messages with `GDB` or other debuggers.
83+
///
84+
/// # Notes:
85+
///
86+
/// This function is similar in purpose to [`Soc::redirect_to_3dslink`](crate::services::soc::Soc::redirect_to_3dslink), but they each offer slightly different functionality.
87+
/// `redirect_stderr` enables messages printed via `stderr` to show up in `GDB`, while `redirect_to_3dslink` completely replaces the file descriptor for `stderr` with a network socket instead.
88+
/// As such, `redirect_to_3dslink` will take priority over this function if both of them are called.
89+
#[doc(alias = "consoleDebugInit")]
90+
pub fn redirect_stderr(dest: Destination) {
91+
unsafe { consoleDebugInit(dest as _) }
92+
}
93+
6994
impl<'screen> Console<'screen> {
7095
/// Initialize a console on the chosen screen.
7196
///

0 commit comments

Comments
 (0)