2 * This file is part of the PulseView project.
4 * Copyright (C) 2018 Soeren Apel <soeren@apelpie.net>
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.
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.
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/>.
20 #include "logging.hpp"
21 #include "globalsettings.hpp"
24 #include <libsigrokdecode/libsigrokdecode.h> /* First, so we avoid a _POSIX_C_SOURCE warning. */
27 #include <libsigrokcxx/libsigrokcxx.hpp>
29 #include <QApplication>
31 using std::lock_guard;
37 const int Logging::MIN_BUFFER_SIZE = 10;
38 const int Logging::MAX_BUFFER_SIZE = 50000;
40 static sr_log_callback prev_sr_log_cb;
41 static void *prev_sr_log_cb_data;
44 static srd_log_callback prev_srd_log_cb;
45 static void *prev_srd_log_cb_data;
50 qInstallMessageHandler(nullptr);
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;
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;
60 GlobalSettings::remove_change_handler(this);
65 GlobalSettings settings;
68 settings.value(GlobalSettings::Key_Log_BufferSize).toInt();
70 buffer_.reserve(buffer_size_);
72 qInstallMessageHandler(log_pv);
73 sr_log_callback_get(&prev_sr_log_cb, &prev_sr_log_cb_data);
74 sr_log_callback_set(log_sr, nullptr);
76 srd_log_callback_get(&prev_srd_log_cb, &prev_srd_log_cb_data);
77 srd_log_callback_set(log_srd, nullptr);
80 GlobalSettings::add_change_handler(this);
83 int Logging::get_log_level() const
85 // We assume that libsigrok and libsrd always have the same log level
86 return sr_log_loglevel_get();
89 void Logging::set_log_level(int level)
91 sr_log_loglevel_set(level);
93 srd_log_loglevel_set(level);
97 QString Logging::get_log() const
99 return buffer_.join("<br />\n");
102 void Logging::log(const QString &text, int source)
104 lock_guard<mutex> log_lock(log_mutex_);
106 if (buffer_.size() >= buffer_size_)
107 buffer_.removeFirst();
111 if (text.contains("warning", Qt::CaseInsensitive)) {
112 s = QString("<font color=\"darkorange\">%1</font>").arg(text);
116 if (text.contains("error", Qt::CaseInsensitive)) {
117 s = QString("<font color=\"darkred\">%1</font>").arg(text);
123 s = QString("pv: ") + text; // black is default color
126 s = QString("<font color=\"blue\">sr: %1</font>").arg(text);
129 s = QString("<font color=\"olive\">srd: %1</font>").arg(text);
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())
145 void Logging::log_pv(QtMsgType type, const QMessageLogContext &context, const QString &msg)
150 logging.log(msg, LogSource_pv);
153 int Logging::log_sr(void *cb_data, int loglevel, const char *format, va_list args)
159 va_copy(args2, args);
161 prev_sr_log_cb(prev_sr_log_cb_data, loglevel, format, args2);
164 char *text = g_strdup_vprintf(format, args);
165 logging.log(QString::fromUtf8(text), LogSource_sr);
172 int Logging::log_srd(void *cb_data, int loglevel, const char *format, va_list args)
178 va_copy(args2, args);
180 prev_srd_log_cb(prev_srd_log_cb_data, loglevel, format, args2);
183 char *text = g_strdup_vprintf(format, args);
184 logging.log(QString::fromUtf8(text), LogSource_srd);
191 void Logging::on_setting_changed(const QString &key, const QVariant &value)
193 if (key == GlobalSettings::Key_Log_BufferSize) {
194 // Truncate buffer if needed
195 const int delta = buffer_.size() - value.toInt();
197 buffer_.erase(buffer_.begin(), buffer_.begin() + delta);
199 buffer_size_ = value.toInt();
200 buffer_.reserve(buffer_size_);