3dphotoshoot

diff src/android/logger.c @ 0:a4bf2687e406

3d photoshoot initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 12 May 2015 05:31:21 +0300
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/android/logger.c	Tue May 12 05:31:21 2015 +0300
     1.3 @@ -0,0 +1,60 @@
     1.4 +#include <stdio.h>
     1.5 +#include <assert.h>
     1.6 +#include <unistd.h>
     1.7 +#include <pthread.h>
     1.8 +#include <android/log.h>
     1.9 +#include "logger.h"
    1.10 +
    1.11 +static void *thread_func(void *arg);
    1.12 +
    1.13 +static int pfd[2];
    1.14 +static pthread_t thr;
    1.15 +static int initialized;
    1.16 +
    1.17 +int start_logger(void)
    1.18 +{
    1.19 +	if(initialized) {
    1.20 +		return 1;
    1.21 +	}
    1.22 +
    1.23 +	/* set stdout to line-buffered, and stderr to unbuffered */
    1.24 +	setvbuf(stdout, 0, _IOLBF, 0);
    1.25 +	setvbuf(stderr, 0, _IONBF, 0);
    1.26 +
    1.27 +	if(pipe(pfd) == -1) {
    1.28 +		perror("failed to create logging pipe");
    1.29 +		return -1;
    1.30 +	}
    1.31 +	assert(pfd[0] > 2 && pfd[1] > 2);
    1.32 +
    1.33 +	/* redirect stdout & stderr to the write-end of the pipe */
    1.34 +	dup2(pfd[1], 1);
    1.35 +	dup2(pfd[1], 2);
    1.36 +
    1.37 +	/* start the logging thread */
    1.38 +	if(pthread_create(&thr, 0, thread_func, 0) == -1) {
    1.39 +		perror("failed to spawn logging thread");
    1.40 +		return -1;
    1.41 +	}
    1.42 +	pthread_detach(thr);
    1.43 +	return 0;
    1.44 +}
    1.45 +
    1.46 +static void *thread_func(void *arg)
    1.47 +{
    1.48 +	ssize_t rdsz;
    1.49 +	char buf[257];
    1.50 +
    1.51 +	__android_log_print(ANDROID_LOG_DEBUG, APP_NAME, "logger starting up...");
    1.52 +
    1.53 +	while((rdsz = read(pfd[0], buf, sizeof buf - 1)) > 0) {
    1.54 +		if(buf[rdsz - 1] == '\n') {
    1.55 +			--rdsz;
    1.56 +		}
    1.57 +		buf[rdsz] = 0;
    1.58 +		__android_log_write(ANDROID_LOG_DEBUG, APP_NAME, buf);
    1.59 +	}
    1.60 +
    1.61 +	__android_log_print(ANDROID_LOG_DEBUG, APP_NAME, "logger shutting down...");
    1.62 +	return 0;
    1.63 +}