Logging: Write messages from qDebug() and friends also to stdout
[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 #include <iostream>
24
25 #ifdef ENABLE_DECODE
26 #include <libsigrokdecode/libsigrokdecode.h> /* First, so we avoid a _POSIX_C_SOURCE warning. */
27 #endif
28
29 #include <libsigrokcxx/libsigrokcxx.hpp>
30
31 #include <QApplication>
32
33 using std::cout;
34 using std::endl;
35 using std::lock_guard;
36
37 namespace pv {
38
39 Logging logging;
40
41 const int Logging::MIN_BUFFER_SIZE = 10;
42 const int Logging::MAX_BUFFER_SIZE = 50000;
43
44 static sr_log_callback prev_sr_log_cb;
45 static void *prev_sr_log_cb_data;
46
47 #ifdef ENABLE_DECODE
48 static srd_log_callback prev_srd_log_cb;
49 static void *prev_srd_log_cb_data;
50 #endif
51
52 Logging::~Logging()
53 {
54         qInstallMessageHandler(nullptr);
55         sr_log_callback_set(prev_sr_log_cb, prev_sr_log_cb_data);
56         prev_sr_log_cb = NULL;
57         prev_sr_log_cb_data = NULL;
58 #ifdef ENABLE_DECODE
59         srd_log_callback_set(prev_srd_log_cb, prev_srd_log_cb_data);
60         prev_srd_log_cb = NULL;
61         prev_srd_log_cb_data = NULL;
62 #endif
63
64         GlobalSettings::remove_change_handler(this);
65 }
66
67 void Logging::init()
68 {
69         GlobalSettings settings;
70
71         buffer_size_ =
72                 settings.value(GlobalSettings::Key_Log_BufferSize).toInt();
73
74         buffer_.reserve(buffer_size_);
75
76         qInstallMessageHandler(log_pv);
77         sr_log_callback_get(&prev_sr_log_cb, &prev_sr_log_cb_data);
78         sr_log_callback_set(log_sr, nullptr);
79 #ifdef ENABLE_DECODE
80         srd_log_callback_get(&prev_srd_log_cb, &prev_srd_log_cb_data);
81         srd_log_callback_set(log_srd, nullptr);
82 #endif
83
84         GlobalSettings::add_change_handler(this);
85 }
86
87 int Logging::get_log_level() const
88 {
89         // We assume that libsigrok and libsrd always have the same log level
90         return sr_log_loglevel_get();
91 }
92
93 void Logging::set_log_level(int level)
94 {
95         sr_log_loglevel_set(level);
96 #ifdef ENABLE_DECODE
97         srd_log_loglevel_set(level);
98 #endif
99 }
100
101 QString Logging::get_log() const
102 {
103         return buffer_.join("<br />\n");
104 }
105
106 void Logging::log(const QString &text, int source)
107 {
108         lock_guard<mutex> log_lock(log_mutex_);
109
110         if (buffer_.size() >= buffer_size_)
111                 buffer_.removeFirst();
112
113         QString s;
114
115         if (text.contains("warning", Qt::CaseInsensitive)) {
116                 s = QString("<font color=\"darkorange\">%1</font>").arg(text);
117                 goto out;
118         }
119
120         if (text.contains("error", Qt::CaseInsensitive)) {
121                 s = QString("<font color=\"darkred\">%1</font>").arg(text);
122                 goto out;
123         }
124
125         switch (source) {
126         case LogSource_pv:
127                 s = QString("pv: ") + text;  // black is default color
128                 break;
129         case LogSource_sr:
130                 s = QString("<font color=\"blue\">sr: %1</font>").arg(text);
131                 break;
132         case LogSource_srd:
133                 s = QString("<font color=\"olive\">srd: %1</font>").arg(text);
134                 break;
135         default:
136                 s = text;
137                 break;
138         }
139
140 out:
141         buffer_.append(s);
142
143         // If we're tearing down the program, sending out notifications to UI
144         // elements that can no longer function properly is a bad idea
145         if (!QApplication::closingDown())
146                 logged_text(s);
147 }
148
149 void Logging::log_pv(QtMsgType type, const QMessageLogContext &context, const QString &msg)
150 {
151         (void)type;
152         (void)context;
153
154         logging.log(msg, LogSource_pv);
155
156         cout << msg.toUtf8().data() << endl;
157 }
158
159 int Logging::log_sr(void *cb_data, int loglevel, const char *format, va_list args)
160 {
161         va_list args2;
162
163         (void)cb_data;
164
165         va_copy(args2, args);
166         if (prev_sr_log_cb)
167                 prev_sr_log_cb(prev_sr_log_cb_data, loglevel, format, args2);
168         va_end(args2);
169
170         char *text = g_strdup_vprintf(format, args);
171         logging.log(QString::fromUtf8(text), LogSource_sr);
172         g_free(text);
173
174         return SR_OK;
175 }
176
177 #ifdef ENABLE_DECODE
178 int Logging::log_srd(void *cb_data, int loglevel, const char *format, va_list args)
179 {
180         va_list args2;
181
182         (void)cb_data;
183
184         va_copy(args2, args);
185         if (prev_srd_log_cb)
186                 prev_srd_log_cb(prev_srd_log_cb_data, loglevel, format, args2);
187         va_end(args2);
188
189         char *text = g_strdup_vprintf(format, args);
190         logging.log(QString::fromUtf8(text), LogSource_srd);
191         g_free(text);
192
193         return SR_OK;
194 }
195 #endif
196
197 void Logging::on_setting_changed(const QString &key, const QVariant &value)
198 {
199         if (key == GlobalSettings::Key_Log_BufferSize) {
200                 // Truncate buffer if needed
201                 const int delta = buffer_.size() - value.toInt();
202                 if (delta > 0)
203                         buffer_.erase(buffer_.begin(), buffer_.begin() + delta);
204
205                 buffer_size_ = value.toInt();
206                 buffer_.reserve(buffer_size_);
207         }
208 }
209
210 } // namespace pv