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