Skip to content

Instantly share code, notes, and snippets.

@Longhanks
Forked from jun66j5/main.m
Created January 31, 2018 14:29
Show Gist options
  • Save Longhanks/8315cfdb8ec5f4150936b919b2e47fd5 to your computer and use it in GitHub Desktop.
Save Longhanks/8315cfdb8ec5f4150936b919b2e47fd5 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;
/* ... */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment