]> sigrok.org Git - pulseview.git/blame - pv/logging.cpp
Fix #1198 by removing the 1:1 zoom button
[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
37const int Logging::MAX_BUFFER_SIZE = 50000;
38
39Logging::~Logging()
40{
37134086 41 qInstallMessageHandler(nullptr);
bcb4c327
SA
42 sr_log_callback_set_default();
43#ifdef ENABLE_DECODE
44 srd_log_callback_set_default();
45#endif
46
47 GlobalSettings::remove_change_handler(this);
48}
49
50void Logging::init()
51{
52 GlobalSettings settings;
53
54 buffer_size_ =
55 settings.value(GlobalSettings::Key_Log_BufferSize).toInt();
56
57 buffer_.reserve(buffer_size_);
58
59 qInstallMessageHandler(log_pv);
348bae08 60 sr_log_callback_set(log_sr, nullptr);
bcb4c327 61#ifdef ENABLE_DECODE
348bae08 62 srd_log_callback_set(log_srd, nullptr);
bcb4c327
SA
63#endif
64
65 GlobalSettings::add_change_handler(this);
66}
67
68int Logging::get_log_level() const
69{
70 // We assume that libsigrok and libsrd always have the same log level
71 return sr_log_loglevel_get();
72}
73
74void Logging::set_log_level(int level)
75{
76 sr_log_loglevel_set(level);
b5131072 77#ifdef ENABLE_DECODE
bcb4c327 78 srd_log_loglevel_set(level);
b5131072 79#endif
bcb4c327
SA
80}
81
82QString Logging::get_log() const
83{
84 return buffer_.join("<br />\n");
85}
86
87void Logging::log(const QString &text, int source)
88{
23a923e3
SA
89 lock_guard<mutex> log_lock(log_mutex_);
90
bcb4c327
SA
91 if (buffer_.size() >= buffer_size_)
92 buffer_.removeFirst();
93
94 QString s;
95
96 if (text.contains("warning", Qt::CaseInsensitive)) {
97 s = QString("<font color=\"darkorange\">%1</font>").arg(text);
98 goto out;
99 }
100
101 if (text.contains("error", Qt::CaseInsensitive)) {
102 s = QString("<font color=\"darkred\">%1</font>").arg(text);
103 goto out;
104 }
105
106 switch (source) {
107 case LogSource_pv:
108 s = QString("pv: ") + text; // black is default color
109 break;
110 case LogSource_sr:
111 s = QString("<font color=\"blue\">sr: %1</font>").arg(text);
112 break;
113 case LogSource_srd:
d1c343c8 114 s = QString("<font color=\"olive\">srd: %1</font>").arg(text);
bcb4c327
SA
115 break;
116 default:
117 s = text;
118 break;
119 }
120
121out:
122 buffer_.append(s);
123
124 // If we're tearing down the program, sending out notifications to UI
125 // elements that can no longer function properly is a bad idea
126 if (!QApplication::closingDown())
127 logged_text(s);
128}
129
130void Logging::log_pv(QtMsgType type, const QMessageLogContext &context, const QString &msg)
131{
132 (void)type;
133 (void)context;
134
135 logging.log(msg, LogSource_pv);
136}
137
348bae08 138int Logging::log_sr(void *cb_data, int loglevel, const char *format, va_list args)
bcb4c327
SA
139{
140 (void)cb_data;
141 (void)loglevel;
142
143 char *text = g_strdup_vprintf(format, args);
144 logging.log(QString::fromUtf8(text), LogSource_sr);
145 g_free(text);
146
147 return SR_OK;
148}
149
150#ifdef ENABLE_DECODE
348bae08 151int Logging::log_srd(void *cb_data, int loglevel, const char *format, va_list args)
bcb4c327
SA
152{
153 (void)cb_data;
154 (void)loglevel;
155
156 char *text = g_strdup_vprintf(format, args);
157 logging.log(QString::fromUtf8(text), LogSource_srd);
158 g_free(text);
159
160 return SR_OK;
161}
162#endif
163
164void Logging::on_setting_changed(const QString &key, const QVariant &value)
165{
166 if (key == GlobalSettings::Key_Log_BufferSize) {
167 // Truncate buffer if needed
168 const int delta = buffer_.size() - value.toInt();
169 if (delta > 0)
170 buffer_.erase(buffer_.begin(), buffer_.begin() + delta);
171
172 buffer_size_ = value.toInt();
173 buffer_.reserve(buffer_size_);
174 }
175}
176
177} // namespace pv