Skip to content

Instantly share code, notes, and snippets.

@trevnorris
Last active February 27, 2017 21:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trevnorris/135cd4b0e3c8191acbeaba819f87d17e to your computer and use it in GitHub Desktop.
Save trevnorris/135cd4b0e3c8191acbeaba819f87d17e to your computer and use it in GitHub Desktop.
Example of how to intercept messages to stdout/stderr
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#if defined(__unix__) || defined(__unix) || \
(defined(__APPLE__) && defined(__MACH__))
#include <unistd.h>
#endif
#ifdef WIN32
#include <io.h>
#define pipe(X) _pipe(X, 4096, O_BINARY)
#define fileno _fileno
#define dup _dup
#define dup2 _dup2
#define read _read
#endif
#include "uv.h"
uv_thread_t stdout_thread;
uv_thread_t stderr_thread;
static bool process_alive_ = true;
static void stdio_dup(void* arg) {
FILE* std_io = static_cast<FILE*>(arg);
char buf[256];
int fds[2];
int res;
int saved_stdio = dup(fileno(std_io));
res = pipe(fds);
assert(res == 0);
res = dup2(fds[1], fileno(std_io));
assert(res != -1);
// Windows does not buffer calls.
#if defined(__unix__) || defined(__unix) || \
(defined(__APPLE__) && defined(__MACH__))
setvbuf(std_io, nullptr, _IONBF, 0);
#endif
while (process_alive_) {
res = read(fds[0], buf, sizeof(buf) - 1);
assert(res >= 0 && res < sizeof(buf));
buf[res] = '\0';
dprintf(saved_stdio, "buf => %s\n", buf);
if (!process_alive_) break;
}
dup2(saved_stdio, fileno(std_io));
close(saved_stdio);
close(fds[1]);
close(fds[0]);
// TODO(trevnorris): Restore vbuf settings for std_io.
}
int main() {
uv_thread_create(&stdout_thread, stdio_dup, stdout);
uv_thread_create(&stderr_thread, stdio_dup, stderr);
sleep(1);
printf("sleep 1");
sleep(1);
printf("sleep 2");
sleep(1);
printf("sleep 3");
process_alive_ = false;
printf("\nbye stdout");
fprintf(stderr, "\nbye stderr");
uv_thread_join(&stdout_thread);
uv_thread_join(&stderr_thread);
printf("all done\n");
return 0;
}
@pmuellr
Copy link

pmuellr commented Feb 24, 2017

Good job @trevnorris!

But two threads? It's not like you're having to deal with Java or something ... ;-)

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