istereo2
annotate src/android/logger.c @ 28:74b50b538858
added nexus7 device id for ad testing
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 05 Oct 2015 17:16:31 +0300 |
parents | 7d795dade0bc |
children |
rev | line source |
---|---|
nuclear@23 | 1 #include <stdio.h> |
nuclear@23 | 2 #include <assert.h> |
nuclear@23 | 3 #include <unistd.h> |
nuclear@23 | 4 #include <pthread.h> |
nuclear@23 | 5 #include <android/log.h> |
nuclear@23 | 6 #include "logger.h" |
nuclear@23 | 7 |
nuclear@24 | 8 #ifndef APP_NAME |
nuclear@24 | 9 #define APP_NAME "stereotunnel" |
nuclear@24 | 10 #endif |
nuclear@24 | 11 |
nuclear@23 | 12 static void *thread_func(void *arg); |
nuclear@23 | 13 |
nuclear@23 | 14 static int pfd[2]; |
nuclear@23 | 15 static pthread_t thr; |
nuclear@23 | 16 static int initialized; |
nuclear@23 | 17 |
nuclear@23 | 18 int start_logger(void) |
nuclear@23 | 19 { |
nuclear@23 | 20 if(initialized) { |
nuclear@23 | 21 return 1; |
nuclear@23 | 22 } |
nuclear@23 | 23 |
nuclear@23 | 24 /* set stdout to line-buffered, and stderr to unbuffered */ |
nuclear@23 | 25 setvbuf(stdout, 0, _IOLBF, 0); |
nuclear@23 | 26 setvbuf(stderr, 0, _IONBF, 0); |
nuclear@23 | 27 |
nuclear@23 | 28 if(pipe(pfd) == -1) { |
nuclear@23 | 29 perror("failed to create logging pipe"); |
nuclear@23 | 30 return -1; |
nuclear@23 | 31 } |
nuclear@23 | 32 assert(pfd[0] > 2 && pfd[1] > 2); |
nuclear@23 | 33 |
nuclear@23 | 34 /* redirect stdout & stderr to the write-end of the pipe */ |
nuclear@23 | 35 dup2(pfd[1], 1); |
nuclear@23 | 36 dup2(pfd[1], 2); |
nuclear@23 | 37 |
nuclear@23 | 38 /* start the logging thread */ |
nuclear@23 | 39 if(pthread_create(&thr, 0, thread_func, 0) == -1) { |
nuclear@23 | 40 perror("failed to spawn logging thread"); |
nuclear@23 | 41 return -1; |
nuclear@23 | 42 } |
nuclear@23 | 43 pthread_detach(thr); |
nuclear@23 | 44 return 0; |
nuclear@23 | 45 } |
nuclear@23 | 46 |
nuclear@23 | 47 static void *thread_func(void *arg) |
nuclear@23 | 48 { |
nuclear@23 | 49 ssize_t rdsz; |
nuclear@23 | 50 char buf[257]; |
nuclear@23 | 51 |
nuclear@23 | 52 __android_log_print(ANDROID_LOG_DEBUG, APP_NAME, "logger starting up..."); |
nuclear@23 | 53 |
nuclear@23 | 54 while((rdsz = read(pfd[0], buf, sizeof buf - 1)) > 0) { |
nuclear@23 | 55 if(buf[rdsz - 1] == '\n') { |
nuclear@23 | 56 --rdsz; |
nuclear@23 | 57 } |
nuclear@23 | 58 buf[rdsz] = 0; |
nuclear@23 | 59 __android_log_write(ANDROID_LOG_DEBUG, APP_NAME, buf); |
nuclear@23 | 60 } |
nuclear@23 | 61 |
nuclear@23 | 62 __android_log_print(ANDROID_LOG_DEBUG, APP_NAME, "logger shutting down..."); |
nuclear@23 | 63 return 0; |
nuclear@23 | 64 } |