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