istereo2

view src/android/logger.c @ 35:643f4ab609a4

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