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