X-Git-Url: http://git.code-monkey.de/?p=pulseview.git;a=blobdiff_plain;f=pv%2Flogging.cpp;fp=pv%2Flogging.cpp;h=ea0798286e6135dc8a57ab0b2b46669d06f28b93;hp=0000000000000000000000000000000000000000;hb=bcb4c327ee9b8d2172d126429ba017884d079d4c;hpb=c1a688dec0b117146a47cc5200e5bc81e445fd2c diff --git a/pv/logging.cpp b/pv/logging.cpp new file mode 100644 index 0000000..ea07982 --- /dev/null +++ b/pv/logging.cpp @@ -0,0 +1,171 @@ +/* + * This file is part of the PulseView project. + * + * Copyright (C) 2018 Soeren Apel + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ + +#include "logging.hpp" +#include "globalsettings.hpp" + +#ifdef ENABLE_DECODE +#include /* First, so we avoid a _POSIX_C_SOURCE warning. */ +#endif + +#include + +#include + +namespace pv { + +Logging logging; + +const int Logging::MAX_BUFFER_SIZE = 50000; + +Logging::~Logging() +{ + qInstallMessageHandler(0); + sr_log_callback_set_default(); +#ifdef ENABLE_DECODE + srd_log_callback_set_default(); +#endif + + GlobalSettings::remove_change_handler(this); +} + +void Logging::init() +{ + GlobalSettings settings; + + buffer_size_ = + settings.value(GlobalSettings::Key_Log_BufferSize).toInt(); + + buffer_.reserve(buffer_size_); + + qInstallMessageHandler(log_pv); + sr_log_callback_set(log_libsigrok, nullptr); +#ifdef ENABLE_DECODE + srd_log_callback_set(log_libsrd, nullptr); +#endif + + GlobalSettings::add_change_handler(this); +} + +int Logging::get_log_level() const +{ + // We assume that libsigrok and libsrd always have the same log level + return sr_log_loglevel_get(); +} + +void Logging::set_log_level(int level) +{ + sr_log_loglevel_set(level); + srd_log_loglevel_set(level); +} + +QString Logging::get_log() const +{ + return buffer_.join("
\n"); +} + +void Logging::log(const QString &text, int source) +{ + if (buffer_.size() >= buffer_size_) + buffer_.removeFirst(); + + QString s; + + if (text.contains("warning", Qt::CaseInsensitive)) { + s = QString("%1").arg(text); + goto out; + } + + if (text.contains("error", Qt::CaseInsensitive)) { + s = QString("%1").arg(text); + goto out; + } + + switch (source) { + case LogSource_pv: + s = QString("pv: ") + text; // black is default color + break; + case LogSource_sr: + s = QString("sr: %1").arg(text); + break; + case LogSource_srd: + s = QString("srd: %1").arg(text); + break; + default: + s = text; + break; + } + +out: + buffer_.append(s); + + // If we're tearing down the program, sending out notifications to UI + // elements that can no longer function properly is a bad idea + if (!QApplication::closingDown()) + logged_text(s); +} + +void Logging::log_pv(QtMsgType type, const QMessageLogContext &context, const QString &msg) +{ + (void)type; + (void)context; + + logging.log(msg, LogSource_pv); +} + +int Logging::log_libsigrok(void *cb_data, int loglevel, const char *format, va_list args) +{ + (void)cb_data; + (void)loglevel; + + char *text = g_strdup_vprintf(format, args); + logging.log(QString::fromUtf8(text), LogSource_sr); + g_free(text); + + return SR_OK; +} + +#ifdef ENABLE_DECODE +int Logging::log_libsrd(void *cb_data, int loglevel, const char *format, va_list args) +{ + (void)cb_data; + (void)loglevel; + + char *text = g_strdup_vprintf(format, args); + logging.log(QString::fromUtf8(text), LogSource_srd); + g_free(text); + + return SR_OK; +} +#endif + +void Logging::on_setting_changed(const QString &key, const QVariant &value) +{ + if (key == GlobalSettings::Key_Log_BufferSize) { + // Truncate buffer if needed + const int delta = buffer_.size() - value.toInt(); + if (delta > 0) + buffer_.erase(buffer_.begin(), buffer_.begin() + delta); + + buffer_size_ = value.toInt(); + buffer_.reserve(buffer_size_); + } +} + +} // namespace pv