3dphotoshoot

view src/android/logger.c @ 27:3d082c566b53

fixed all the bugs, pc version works
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 18 Jun 2015 04:32:25 +0300
parents
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 static void *thread_func(void *arg);
10 static int pfd[2];
11 static pthread_t thr;
12 static int initialized;
14 int start_logger(void)
15 {
16 if(initialized) {
17 return 1;
18 }
20 /* set stdout to line-buffered, and stderr to unbuffered */
21 setvbuf(stdout, 0, _IOLBF, 0);
22 setvbuf(stderr, 0, _IONBF, 0);
24 if(pipe(pfd) == -1) {
25 perror("failed to create logging pipe");
26 return -1;
27 }
28 assert(pfd[0] > 2 && pfd[1] > 2);
30 /* redirect stdout & stderr to the write-end of the pipe */
31 dup2(pfd[1], 1);
32 dup2(pfd[1], 2);
34 /* start the logging thread */
35 if(pthread_create(&thr, 0, thread_func, 0) == -1) {
36 perror("failed to spawn logging thread");
37 return -1;
38 }
39 pthread_detach(thr);
40 return 0;
41 }
43 static void *thread_func(void *arg)
44 {
45 ssize_t rdsz;
46 char buf[257];
48 __android_log_print(ANDROID_LOG_DEBUG, APP_NAME, "logger starting up...");
50 while((rdsz = read(pfd[0], buf, sizeof buf - 1)) > 0) {
51 if(buf[rdsz - 1] == '\n') {
52 --rdsz;
53 }
54 buf[rdsz] = 0;
55 __android_log_write(ANDROID_LOG_DEBUG, APP_NAME, buf);
56 }
58 __android_log_print(ANDROID_LOG_DEBUG, APP_NAME, "logger shutting down...");
59 return 0;
60 }