]> sigrok.org Git - pulseview.git/blame - pv/logging.cpp
Include suggested changes by clang-tidy
[pulseview.git] / pv / logging.cpp
CommitLineData
bcb4c327
SA
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2018 Soeren Apel <soeren@apelpie.net>
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 2 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#include "logging.hpp"
21#include "globalsettings.hpp"
22
cd0b243a
SA
23#include <iostream>
24
bcb4c327
SA
25#ifdef ENABLE_DECODE
26#include <libsigrokdecode/libsigrokdecode.h> /* First, so we avoid a _POSIX_C_SOURCE warning. */
27#endif
28
29#include <libsigrokcxx/libsigrokcxx.hpp>
30
31#include <QApplication>
32
cd0b243a
SA
33using std::cout;
34using std::endl;
23a923e3
SA
35using std::lock_guard;
36
bcb4c327
SA
37namespace pv {
38
39Logging logging;
40
b9a3a67e 41const int Logging::MIN_BUFFER_SIZE = 10;
bcb4c327
SA
42const int Logging::MAX_BUFFER_SIZE = 50000;
43
4b923408
GS
44static sr_log_callback prev_sr_log_cb;
45static void *prev_sr_log_cb_data;
46
47#ifdef ENABLE_DECODE
48static srd_log_callback prev_srd_log_cb;
49static void *prev_srd_log_cb_data;
50#endif
51
bcb4c327
SA
52Logging::~Logging()
53{
37134086 54 qInstallMessageHandler(nullptr);
4b923408 55 sr_log_callback_set(prev_sr_log_cb, prev_sr_log_cb_data);
1f2082e2
SA
56 prev_sr_log_cb = nullptr;
57 prev_sr_log_cb_data = nullptr;
bcb4c327 58#ifdef ENABLE_DECODE
4b923408 59 srd_log_callback_set(prev_srd_log_cb, prev_srd_log_cb_data);
1f2082e2
SA
60 prev_srd_log_cb = nullptr;
61 prev_srd_log_cb_data = nullptr;
bcb4c327
SA
62#endif
63
64 GlobalSettings::remove_change_handler(this);
65}
66
67void Logging::init()
68{
69 GlobalSettings settings;
70
71 buffer_size_ =
72 settings.value(GlobalSettings::Key_Log_BufferSize).toInt();
73
74 buffer_.reserve(buffer_size_);
75
76 qInstallMessageHandler(log_pv);
4b923408 77 sr_log_callback_get(&prev_sr_log_cb, &prev_sr_log_cb_data);
348bae08 78 sr_log_callback_set(log_sr, nullptr);
bcb4c327 79#ifdef ENABLE_DECODE
4b923408 80 srd_log_callback_get(&prev_srd_log_cb, &prev_srd_log_cb_data);
348bae08 81 srd_log_callback_set(log_srd, nullptr);
bcb4c327
SA
82#endif
83
84 GlobalSettings::add_change_handler(this);
85}
86
87int Logging::get_log_level() const
88{
89 // We assume that libsigrok and libsrd always have the same log level
90 return sr_log_loglevel_get();
91}
92
93void Logging::set_log_level(int level)
94{
95 sr_log_loglevel_set(level);
b5131072 96#ifdef ENABLE_DECODE
bcb4c327 97 srd_log_loglevel_set(level);
b5131072 98#endif
bcb4c327
SA
99}
100
101QString Logging::get_log() const
102{
103 return buffer_.join("<br />\n");
104}
105
106void Logging::log(const QString &text, int source)
107{
23a923e3
SA
108 lock_guard<mutex> log_lock(log_mutex_);
109
bcb4c327
SA
110 if (buffer_.size() >= buffer_size_)
111 buffer_.removeFirst();
112
113 QString s;
114
115 if (text.contains("warning", Qt::CaseInsensitive)) {
116 s = QString("<font color=\"darkorange\">%1</font>").arg(text);
117 goto out;
118 }
119
120 if (text.contains("error", Qt::CaseInsensitive)) {
121 s = QString("<font color=\"darkred\">%1</font>").arg(text);
122 goto out;
123 }
124
125 switch (source) {
126 case LogSource_pv:
127 s = QString("pv: ") + text; // black is default color
128 break;
129 case LogSource_sr:
130 s = QString("<font color=\"blue\">sr: %1</font>").arg(text);
131 break;
132 case LogSource_srd:
d1c343c8 133 s = QString("<font color=\"olive\">srd: %1</font>").arg(text);
bcb4c327
SA
134 break;
135 default:
136 s = text;
137 break;
138 }
139
140out:
141 buffer_.append(s);
142
143 // If we're tearing down the program, sending out notifications to UI
144 // elements that can no longer function properly is a bad idea
145 if (!QApplication::closingDown())
146 logged_text(s);
147}
148
149void Logging::log_pv(QtMsgType type, const QMessageLogContext &context, const QString &msg)
150{
151 (void)type;
152 (void)context;
153
154 logging.log(msg, LogSource_pv);
cd0b243a
SA
155
156 cout << msg.toUtf8().data() << endl;
bcb4c327
SA
157}
158
348bae08 159int Logging::log_sr(void *cb_data, int loglevel, const char *format, va_list args)
bcb4c327 160{
4b923408
GS
161 va_list args2;
162
bcb4c327 163 (void)cb_data;
4b923408
GS
164
165 va_copy(args2, args);
166 if (prev_sr_log_cb)
167 prev_sr_log_cb(prev_sr_log_cb_data, loglevel, format, args2);
168 va_end(args2);
bcb4c327
SA
169
170 char *text = g_strdup_vprintf(format, args);
171 logging.log(QString::fromUtf8(text), LogSource_sr);
172 g_free(text);
173
174 return SR_OK;
175}
176
177#ifdef ENABLE_DECODE
348bae08 178int Logging::log_srd(void *cb_data, int loglevel, const char *format, va_list args)
bcb4c327 179{
4b923408
GS
180 va_list args2;
181
bcb4c327 182 (void)cb_data;
4b923408
GS
183
184 va_copy(args2, args);
185 if (prev_srd_log_cb)
186 prev_srd_log_cb(prev_srd_log_cb_data, loglevel, format, args2);
187 va_end(args2);
bcb4c327
SA
188
189 char *text = g_strdup_vprintf(format, args);
190 logging.log(QString::fromUtf8(text), LogSource_srd);
191 g_free(text);
192
193 return SR_OK;
194}
195#endif
196
197void Logging::on_setting_changed(const QString &key, const QVariant &value)
198{
199 if (key == GlobalSettings::Key_Log_BufferSize) {
200 // Truncate buffer if needed
201 const int delta = buffer_.size() - value.toInt();
202 if (delta > 0)
203 buffer_.erase(buffer_.begin(), buffer_.begin() + delta);
204
205 buffer_size_ = value.toInt();
206 buffer_.reserve(buffer_size_);
207 }
208}
209
210} // namespace pv