diff options
author | Andreas Baumann <mail@andreasbaumann.cc> | 2014-12-03 20:22:56 +0100 |
---|---|---|
committer | Andreas Baumann <mail@andreasbaumann.cc> | 2014-12-03 20:22:56 +0100 |
commit | 5c214383ebb7efb088e184854ddd3a7530086c83 (patch) | |
tree | dfd630233a37a94f9cbb9fdcf3b815fe167a9ab8 | |
parent | f00af59e805a80cf74efffb800e83dc250698ba8 (diff) | |
download | biruda-5c214383ebb7efb088e184854ddd3a7530086c83.tar.gz biruda-5c214383ebb7efb088e184854ddd3a7530086c83.tar.bz2 |
printing output of worker
-rw-r--r-- | src/worker.c | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/worker.c b/src/worker.c index b290855..77ae401 100644 --- a/src/worker.c +++ b/src/worker.c @@ -59,9 +59,12 @@ worker_state_t worker_state_from_str( const char *s ) typedef struct { GPid pid; gint out, err; + GIOChannel *out_channel; + GIOChannel *err_channel; worker_t *worker; pthread_t thread; GMainLoop *main_loop; + } direct_glib_execution_worker_data_t; static void watch_child( GPid pid, gint status, gpointer *data ) @@ -75,6 +78,31 @@ static void watch_child( GPid pid, gint status, gpointer *data ) g_main_loop_quit( wed->main_loop ); } +static gboolean watch_output( GIOChannel *channel, GIOCondition condition, gpointer *data ) +{ + direct_glib_execution_worker_data_t *wed = (direct_glib_execution_worker_data_t *)data; + worker_t *worker = wed->worker; + + bool isStdout = ( wed->out_channel == channel ); + + gchar *buf; + gsize len; + + if( condition == G_IO_HUP ) { + g_io_channel_unref( channel ); + return FALSE; + } + + g_io_channel_read_line( channel, &buf, &len, NULL, NULL ); + + fprintf( stderr, "worker %s %s: %*s\n", worker->name, + isStdout ? "stdout" : "stderr", len, buf ); + + g_free( buf ); + + return TRUE; +} + static void *worker_func( void *thread_data ) { direct_glib_execution_worker_data_t *wed = (direct_glib_execution_worker_data_t *)thread_data; @@ -99,6 +127,18 @@ static void *worker_func( void *thread_data ) g_child_watch_add( wed->pid, (GChildWatchFunc)watch_child, (gpointer *)wed ); + +#ifdef G_OS_WIN32 + wed->out_channel = g_io_channel_win32_new_fd( wed->out ); + wed->err_channel = g_io_channel_win32_new_fd( wed->err ); +#else + wed->out_channel = g_io_channel_unix_new( wed->out ); + wed->err_channel = g_io_channel_unix_new( wed->err ); +#endif + + g_io_add_watch( wed->out_channel, G_IO_IN | G_IO_HUP, (GIOFunc)watch_output, (gpointer *)wed ); + g_io_add_watch( wed->err_channel, G_IO_IN | G_IO_HUP, (GIOFunc)watch_output, (gpointer *)wed ); + worker->state = WORKER_STATE_RUNNING; printf( "worker %s: entering glib worker main loop..\n", worker->name ); |