Session: Use a monotonic clock to measure acquisition time.
[pulseview.git] / android / loghandler.cpp
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2014 Marcus Comstedt <marcus@mc.pp.se>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #ifdef ENABLE_DECODE
21 #include <libsigrokdecode/libsigrokdecode.h> /* First, so we avoid a _POSIX_C_SOURCE warning. */
22 #endif
23
24 #include <android/log.h>
25
26 #include <cstdint>
27 #include <libsigrok/libsigrok.h>
28
29 #include "android/loghandler.hpp"
30
31 namespace pv {
32
33 static sr_log_callback prev_sr_log_cb;
34 static void *prev_sr_log_cb_data;
35
36 #ifdef ENABLE_DECODE
37 static srd_log_callback prev_srd_log_cb;
38 static void *prev_srd_log_cb_data;
39 #endif
40
41 int AndroidLogHandler::sr_callback(void *cb_data, int loglevel, const char *format, va_list args)
42 {
43         static const int prio[] = {
44                 [SR_LOG_NONE] = ANDROID_LOG_SILENT,
45                 [SR_LOG_ERR] = ANDROID_LOG_ERROR,
46                 [SR_LOG_WARN] = ANDROID_LOG_WARN,
47                 [SR_LOG_INFO] = ANDROID_LOG_INFO,
48                 [SR_LOG_DBG] = ANDROID_LOG_DEBUG,
49                 [SR_LOG_SPEW] = ANDROID_LOG_VERBOSE,
50         };
51         va_list args2;
52         int ret;
53
54         /* This specific log callback doesn't need the void pointer data. */
55         (void)cb_data;
56
57         /* Call the previously registered log callback (library's default). */
58         va_copy(args2, args);
59         if (prev_sr_log_cb)
60                 prev_sr_log_cb(prev_sr_log_cb_data, loglevel, format, args2);
61         va_end(args2);
62
63         /* Only output messages of at least the selected loglevel(s). */
64         if (loglevel > sr_log_loglevel_get())
65                 return SR_OK;
66
67         if (loglevel < SR_LOG_NONE)
68                 loglevel = SR_LOG_NONE;
69         else if (loglevel > SR_LOG_SPEW)
70                 loglevel = SR_LOG_SPEW;
71
72         ret = __android_log_vprint(prio[loglevel], "sr", format, args);
73
74         return ret;
75 }
76
77 int AndroidLogHandler::srd_callback(void *cb_data, int loglevel, const char *format, va_list args)
78 {
79 #ifdef ENABLE_DECODE
80         static const int prio[] = {
81                 [SRD_LOG_NONE] = ANDROID_LOG_SILENT,
82                 [SRD_LOG_ERR] = ANDROID_LOG_ERROR,
83                 [SRD_LOG_WARN] = ANDROID_LOG_WARN,
84                 [SRD_LOG_INFO] = ANDROID_LOG_INFO,
85                 [SRD_LOG_DBG] = ANDROID_LOG_DEBUG,
86                 [SRD_LOG_SPEW] = ANDROID_LOG_VERBOSE,
87         };
88         va_list args2;
89         int ret;
90
91         /* This specific log callback doesn't need the void pointer data. */
92         (void)cb_data;
93
94         /* Call the previously registered log callback (library's default). */
95         va_copy(args2, args);
96         if (prev_srd_log_cb)
97                 prev_srd_log_cb(prev_srd_log_cb_data, loglevel, format, args2);
98         va_end(args2);
99
100         /* Only output messages of at least the selected loglevel(s). */
101         if (loglevel > srd_log_loglevel_get())
102                 return SRD_OK;
103
104         if (loglevel < SRD_LOG_NONE)
105                 loglevel = SRD_LOG_NONE;
106         else if (loglevel > SRD_LOG_SPEW)
107                 loglevel = SRD_LOG_SPEW;
108
109         ret = __android_log_vprint(prio[loglevel], "srd", format, args);
110
111         return ret;
112 #else
113         return 0;
114 #endif
115 }
116
117 void AndroidLogHandler::install_callbacks()
118 {
119         sr_log_callback_get(&prev_sr_log_cb, &prev_sr_log_cb_data);
120         sr_log_callback_set(sr_callback, nullptr);
121 #ifdef ENABLE_DECODE
122         srd_log_callback_get(&prev_srd_log_cb, &prev_srd_log_cb_data);
123         srd_log_callback_set(srd_callback, nullptr);
124 #endif
125 }
126
127 } // namespace pv