]> sigrok.org Git - pulseview.git/blame - android/loghandler.cpp
Add themes and implement theme support
[pulseview.git] / android / loghandler.cpp
CommitLineData
9137928c
MC
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
f5295834 26#include <cstdint>
9137928c
MC
27#include <libsigrok/libsigrok.h>
28
2acdb232 29#include "android/loghandler.hpp"
9137928c
MC
30
31namespace pv {
32
e2e4a66a
GS
33static sr_log_callback prev_sr_log_cb;
34static void *prev_sr_log_cb_data;
35
36#ifdef ENABLE_DECODE
37static srd_log_callback prev_srd_log_cb;
38static void *prev_srd_log_cb_data;
39#endif
40
9137928c
MC
41int 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 };
e2e4a66a 51 va_list args2;
9137928c
MC
52 int ret;
53
54 /* This specific log callback doesn't need the void pointer data. */
55 (void)cb_data;
56
e2e4a66a
GS
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
9137928c
MC
63 /* Only output messages of at least the selected loglevel(s). */
64 if (loglevel > sr_log_loglevel_get())
1c90d94f 65 return SR_OK;
9137928c
MC
66
67 if (loglevel < SR_LOG_NONE)
1c90d94f 68 loglevel = SR_LOG_NONE;
9137928c 69 else if (loglevel > SR_LOG_SPEW)
1c90d94f 70 loglevel = SR_LOG_SPEW;
9137928c
MC
71
72 ret = __android_log_vprint(prio[loglevel], "sr", format, args);
73
74 return ret;
75}
76
77int 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 };
e2e4a66a 88 va_list args2;
9137928c
MC
89 int ret;
90
91 /* This specific log callback doesn't need the void pointer data. */
92 (void)cb_data;
93
e2e4a66a
GS
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
9137928c
MC
100 /* Only output messages of at least the selected loglevel(s). */
101 if (loglevel > srd_log_loglevel_get())
1c90d94f 102 return SRD_OK;
9137928c
MC
103
104 if (loglevel < SRD_LOG_NONE)
1c90d94f 105 loglevel = SRD_LOG_NONE;
9137928c 106 else if (loglevel > SRD_LOG_SPEW)
1c90d94f 107 loglevel = SRD_LOG_SPEW;
9137928c
MC
108
109 ret = __android_log_vprint(prio[loglevel], "srd", format, args);
110
111 return ret;
112#else
113 return 0;
114#endif
115}
116
117void AndroidLogHandler::install_callbacks()
118{
e2e4a66a 119 sr_log_callback_get(&prev_sr_log_cb, &prev_sr_log_cb_data);
4c60462b 120 sr_log_callback_set(sr_callback, nullptr);
9137928c 121#ifdef ENABLE_DECODE
e2e4a66a 122 srd_log_callback_get(&prev_srd_log_cb, &prev_srd_log_cb_data);
4c60462b 123 srd_log_callback_set(srd_callback, nullptr);
9137928c
MC
124#endif
125}
126
127} // namespace pv