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