diff --git a/libctru/include/3ds/console.h b/libctru/include/3ds/console.h index 95b39a1..48c7224 100644 --- a/libctru/include/3ds/console.h +++ b/libctru/include/3ds/console.h @@ -102,6 +102,13 @@ typedef struct PrintConsole #define CONSOLE_CONCEAL (1<<7) #define CONSOLE_CROSSED_OUT (1<<8) +//! Console debug devices supported by libnds. +typedef enum { + debugDevice_NULL, //!< swallows prints to stderr + debugDevice_3DMOO, //!< Directs stderr debug statements to 3dmoo + debugDevice_CONSOLE, //!< Directs stderr debug statements to 3DS console window +} debugDevice; + /*! \brief Loads the font into the console \param console pointer to the console to update, if NULL it will update the current console \param font the font to load @@ -136,6 +143,12 @@ PrintConsole *consoleSelect(PrintConsole* console); */ PrintConsole* consoleInit(gfxScreen_t screen, PrintConsole* console); +/*! \brief Initializes debug console output on stderr to the specified device + \param device The debug device (or devices) to output debug print statements to +*/ +void consoleDebugInit(debugDevice device); + + //! Clears the screan by using iprintf("\x1b[2J"); void consoleClear(void); diff --git a/libctru/source/console.c b/libctru/source/console.c index ffd5d49..c6469f2 100644 --- a/libctru/source/console.c +++ b/libctru/source/console.c @@ -3,6 +3,7 @@ #include #include <3ds/gfx.h> #include <3ds/console.h> +#include <3ds/svc.h> #include "default_font_bin.h" @@ -467,6 +468,25 @@ static const devoptab_t dotab_stdout = { NULL }; +//--------------------------------------------------------------------------------- +ssize_t debug_write(struct _reent *r, int fd, const char *ptr, size_t len) { +//--------------------------------------------------------------------------------- + svcOutputDebugString(ptr,len); + return len; +} + +static const devoptab_t dotab_3dmoo = { + "3dmoo", + 0, + NULL, + NULL, + debug_write, + NULL, + NULL, + NULL +}; + + static const devoptab_t dotab_null = { "null", 0, @@ -519,6 +539,30 @@ PrintConsole* consoleInit(gfxScreen_t screen, PrintConsole* console) { return currentConsole; } + +//--------------------------------------------------------------------------------- +void consoleDebugInit(debugDevice device){ +//--------------------------------------------------------------------------------- + + int buffertype = _IONBF; + + switch(device) { + + case debugDevice_3DMOO: + devoptab_list[STD_ERR] = &dotab_3dmoo; + buffertype = _IOLBF; + break; + case debugDevice_CONSOLE: + devoptab_list[STD_ERR] = &dotab_stdout; + break; + case debugDevice_NULL: + devoptab_list[STD_ERR] = &dotab_null; + break; + } + setvbuf(stderr, NULL , buffertype, 0); + +} + //--------------------------------------------------------------------------------- PrintConsole *consoleSelect(PrintConsole* console){ //---------------------------------------------------------------------------------