From 4b7921a221f0c1c2f3c707cfbddf56c85f3c8c70 Mon Sep 17 00:00:00 2001 From: fincs Date: Wed, 15 Apr 2020 18:34:45 +0200 Subject: [PATCH] Make nxlink stdio more flexible, see details: - Added nxlinkConnectToHost with separate flags for redirecting stdout/err - Added nxlinkStdioForDebug, which only redirects stderr - (nxlinkStdio now just calls nxlinkConnectToHost) --- nx/include/switch/runtime/nxlink.h | 17 +++++++++++++++-- nx/source/runtime/nxlink_stdio.c | 19 ++++++++++++------- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/nx/include/switch/runtime/nxlink.h b/nx/include/switch/runtime/nxlink.h index febda99e..5072a58c 100644 --- a/nx/include/switch/runtime/nxlink.h +++ b/nx/include/switch/runtime/nxlink.h @@ -5,6 +5,7 @@ * @copyright libnx Authors */ #pragma once +#include "../types.h" struct in_addr; @@ -15,8 +16,20 @@ extern struct in_addr __nxlink_host; #define NXLINK_CLIENT_PORT 28771 ///< nxlink TCP client port /** - * @brief Sets up stdout/stderr redirection to the nxlink host. + * @brief Connects to the nxlink host, setting up an output stream. + * @param[in] redirStdout Whether to redirect stdout to nxlink output. + * @param[in] redirStderr Whether to redirect stderr to nxlink output. * @return Socket fd on success, negative number on failure. * @note The socket should be closed with close() during application cleanup. */ -int nxlinkStdio(void); +int nxlinkConnectToHost(bool redirStdout, bool redirStderr); + +/// Same as \ref nxlinkConnectToHost but redirecting both stdout/stderr. +NX_INLINE int nxlinkStdio(void) { + return nxlinkConnectToHost(true, true); +} + +/// Same as \ref nxlinkConnectToHost but redirecting only stderr. +NX_INLINE int nxlinkStdioForDebug(void) { + return nxlinkConnectToHost(false, true); +} diff --git a/nx/source/runtime/nxlink_stdio.c b/nx/source/runtime/nxlink_stdio.c index e2b0c3a4..aedd4c5c 100644 --- a/nx/source/runtime/nxlink_stdio.c +++ b/nx/source/runtime/nxlink_stdio.c @@ -9,7 +9,7 @@ static int sock = -1; -int nxlinkStdio(void) +int nxlinkConnectToHost(bool redirStdout, bool redirStderr) { int ret = -1; struct sockaddr_in srv_addr; @@ -30,12 +30,17 @@ int nxlinkStdio(void) return -1; } - // redirect stdout - fflush(stdout); - dup2(sock, STDOUT_FILENO); - // redirect stderr - fflush(stderr); - dup2(sock, STDERR_FILENO); + if (redirStdout) { + // redirect stdout + fflush(stdout); + dup2(sock, STDOUT_FILENO); + } + + if (redirStderr) { + // redirect stderr + fflush(stderr); + dup2(sock, STDERR_FILENO); + } return sock; }