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)
This commit is contained in:
fincs 2020-04-15 18:34:45 +02:00
parent 4c9b2ac048
commit 4b7921a221
No known key found for this signature in database
GPG Key ID: 62C7609ADA219C60
2 changed files with 27 additions and 9 deletions

View File

@ -5,6 +5,7 @@
* @copyright libnx Authors * @copyright libnx Authors
*/ */
#pragma once #pragma once
#include "../types.h"
struct in_addr; struct in_addr;
@ -15,8 +16,20 @@ extern struct in_addr __nxlink_host;
#define NXLINK_CLIENT_PORT 28771 ///< nxlink TCP client port #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. * @return Socket fd on success, negative number on failure.
* @note The socket should be closed with close() during application cleanup. * @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);
}

View File

@ -9,7 +9,7 @@
static int sock = -1; static int sock = -1;
int nxlinkStdio(void) int nxlinkConnectToHost(bool redirStdout, bool redirStderr)
{ {
int ret = -1; int ret = -1;
struct sockaddr_in srv_addr; struct sockaddr_in srv_addr;
@ -30,12 +30,17 @@ int nxlinkStdio(void)
return -1; return -1;
} }
if (redirStdout) {
// redirect stdout // redirect stdout
fflush(stdout); fflush(stdout);
dup2(sock, STDOUT_FILENO); dup2(sock, STDOUT_FILENO);
}
if (redirStderr) {
// redirect stderr // redirect stderr
fflush(stderr); fflush(stderr);
dup2(sock, STDERR_FILENO); dup2(sock, STDERR_FILENO);
}
return sock; return sock;
} }