Skip to content

Instantly share code, notes, and snippets.

@jun66j5
Last active July 25, 2021 08:58
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jun66j5/fd8b9c53d3670c8bcf6f to your computer and use it in GitHub Desktop.
Save jun66j5/fd8b9c53d3670c8bcf6f to your computer and use it in GitHub Desktop.
Redirect stdout/stderr to NSLog on iOS Simulator
#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;
/* ... */
}
@GiordanoVicoli
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment