-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAndroidLogger.hpp
More file actions
54 lines (44 loc) · 1 KB
/
AndroidLogger.hpp
File metadata and controls
54 lines (44 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#pragma once
#ifdef __ANDROID__
#include <pthread.h>
#include <android/log.h>
namespace android_log
{
static int pfd[2];
static pthread_t thr;
static const char* tag = "App";
static void* thread_func(void*)
{
ssize_t rdsz;
char buf[256];
while((rdsz = read(pfd[0], buf, sizeof buf - 1)) > 0)
{
if(buf[rdsz - 1] == '\n')
{
--rdsz;
}
buf[rdsz] = 0; /* add null-terminator */
__android_log_write(ANDROID_LOG_DEBUG, tag, buf);
}
return 0;
}
int start_logger(const char* app_name)
{
tag = app_name;
/* make stdout line-buffered and stderr unbuffered */
setvbuf(stdout, 0, _IOLBF, 0);
setvbuf(stderr, 0, _IONBF, 0);
/* create the pipe and redirect stdout and stderr */
pipe(pfd);
dup2(pfd[1], 1);
dup2(pfd[1], 2);
/* spawn the logging thread */
if(pthread_create(&thr, 0, thread_func, 0) == -1)
{
return -1;
}
pthread_detach(thr);
return 0;
}
} //android_log
#endif