|
3 | 3 | //! 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, |
4 | 4 | //! such as in simple "Hello World" applications or more complex software that does not need much user interaction. |
5 | 5 | //! |
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. |
8 | 7 | use std::cell::{RefMut, UnsafeCell}; |
9 | 8 |
|
10 | | -use ctru_sys::{PrintConsole, consoleClear, consoleInit, consoleSelect, consoleSetWindow}; |
| 9 | +use ctru_sys::{ |
| 10 | + PrintConsole, consoleClear, consoleDebugInit, consoleInit, consoleSelect, consoleSetWindow, |
| 11 | +}; |
11 | 12 |
|
12 | 13 | use crate::services::gfx::{Flush, Screen, Swap}; |
13 | 14 |
|
@@ -38,6 +39,18 @@ pub enum Dimension { |
38 | 39 | Height, |
39 | 40 | } |
40 | 41 |
|
| 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 | + |
41 | 54 | /// A [`Screen`] that can be used as a target for [`Console`]. |
42 | 55 | pub trait ConsoleScreen: Screen + Swap + Flush {} |
43 | 56 | impl<S: Screen + Swap + Flush> ConsoleScreen for S {} |
@@ -66,6 +79,18 @@ pub struct Console<'screen> { |
66 | 79 | screen: RefMut<'screen, dyn ConsoleScreen>, |
67 | 80 | } |
68 | 81 |
|
| 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 | + |
69 | 94 | impl<'screen> Console<'screen> { |
70 | 95 | /// Initialize a console on the chosen screen. |
71 | 96 | /// |
|
0 commit comments