Last active
July 25, 2021 08:58
-
-
Save jun66j5/fd8b9c53d3670c8bcf6f to your computer and use it in GitHub Desktop.
Redirect stdout/stderr to NSLog on iOS Simulator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <unistd.h> | |
static int redirect_nslog(const char *prefix, const char *buffer, int size) | |
{ | |
NSLog(@"%s (%d bytes): %.*s", prefix, size, size, buffer); | |
return size; | |
} | |
static int stderr_redirect_nslog(void *inFD, const char *buffer, int size) | |
{ | |
return redirect_nslog("stderr", buffer, size); | |
} | |
static int stdout_redirect_nslog(void *inFD, const char *buffer, int size) | |
{ | |
return redirect_nslog("stdout", buffer, size); | |
} | |
int main(int argc, char *argv[]) | |
{ | |
setlinebuf(stdout); | |
setlinebuf(stderr); | |
stdout->_write = stdout_redirect_nslog; | |
stderr->_write = stderr_redirect_nslog; | |
/* ... */ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, your code works fine but I have a question:
stdout->_write = stdout_redirect_nslog; redirect the stdout to the function but how can reset the stdout to the default value?
Many thanks and regards,
Giordano