From dde13a19c81eb66c70867ea962461603300031e1 Mon Sep 17 00:00:00 2001 From: ArsenArsen Date: Mon, 1 Jan 2018 16:37:31 +0100 Subject: [PATCH] rewrite logging --- src/colorpicker/colorpickerscene.cpp | 6 ++-- src/cropeditor/cropscene.cpp | 1 - src/cropeditor/selectionrectangle.cpp | 1 - src/logger.cpp | 48 +++++++++++++++++++++++++++ src/logger.hpp | 14 ++++++++ src/logs/requestlogging.cpp | 2 -- src/main.cpp | 15 ++------- src/mainwindow.cpp | 4 +-- src/recording/recordingformats.cpp | 8 ++--- src/recording/recordingpreview.cpp | 1 - src/settings.cpp | 4 +-- src/src.pro | 6 ++-- src/uploaders/uploadersingleton.cpp | 6 ++-- src/utils.cpp | 8 ++--- 14 files changed, 85 insertions(+), 39 deletions(-) create mode 100644 src/logger.cpp create mode 100644 src/logger.hpp diff --git a/src/colorpicker/colorpickerscene.cpp b/src/colorpicker/colorpickerscene.cpp index 367d46b..a8eb148 100644 --- a/src/colorpicker/colorpickerscene.cpp +++ b/src/colorpicker/colorpickerscene.cpp @@ -1,7 +1,7 @@ #include "colorpickerscene.hpp" #include #include -#include +#include #include #include #include @@ -24,7 +24,7 @@ void ColorPickerScene::keyPressEvent(QKeyEvent *event) { color = image.pixelColor(cursorPos().toPoint()); if (event->key() == Qt::Key_Return) { QApplication::clipboard()->setText(color.name()); - qInfo().noquote() << tr("Copied hex code to clipboard."); + logger::info(tr("Copied hex code to clipboard.")); } if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Escape) close(); } @@ -33,7 +33,7 @@ void ColorPickerScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *) { color = image.pixelColor(cursorPos().toPoint()); QApplication::clipboard()->setText(color.name()); close(); - qInfo().noquote() << tr("Copied hex code to clipboard."); + logger::info(tr("Copied hex code to clipboard.")); } QString ColorPickerScene::generateHint() { diff --git a/src/cropeditor/cropscene.cpp b/src/cropeditor/cropscene.cpp index 4374d9b..b6540d3 100644 --- a/src/cropeditor/cropscene.cpp +++ b/src/cropeditor/cropscene.cpp @@ -3,7 +3,6 @@ #include "selectionrectangle.hpp" #include #include -#include #include #include #include diff --git a/src/cropeditor/selectionrectangle.cpp b/src/cropeditor/selectionrectangle.cpp index 342cc70..ae9df3c 100644 --- a/src/cropeditor/selectionrectangle.cpp +++ b/src/cropeditor/selectionrectangle.cpp @@ -1,6 +1,5 @@ #include "selectionrectangle.hpp" -#include #include #include #include diff --git a/src/logger.cpp b/src/logger.cpp new file mode 100644 index 0000000..8a79337 --- /dev/null +++ b/src/logger.cpp @@ -0,0 +1,48 @@ +#include "logger.hpp" +#include "mainwindow.hpp" +#include "notifications.hpp" +#include +#include +#include +#include + +void logger::info(QString info) { + qInfo() << info; + if (MainWindow::inst() && MainWindow::inst()->valid()) { + MainWindow::inst()->ui->logBox->addItem(info); + notifications::notifyNolog(QObject::tr("KShare Message"), info, QSystemTrayIcon::Information); + } +} + +void logger::warn(QString info) { + qWarning() << info; + if (MainWindow::inst() && MainWindow::inst()->valid()) { + MainWindow::inst()->ui->logBox->addItem(info); + notifications::notifyNolog(QObject::tr("KShare Warning"), info, QSystemTrayIcon::Warning); + } +} + +void logger::error(QString info) { + std::cerr << "ERROR: " << info.toStdString(); + if (MainWindow::inst() && MainWindow::inst()->valid()) { + MainWindow::inst()->ui->logBox->addItem(info); + notifications::notifyNolog(QObject::tr("KShare Error"), info, QSystemTrayIcon::Critical); + } +} + +void logger::fatal(QString info) { + std::cerr << "FATAL: " << info.toStdString(); + if (MainWindow::inst() && MainWindow::inst()->valid()) { + MainWindow::inst()->ui->logBox->addItem(info); + notifications::notifyNolog(QObject::tr("KShare Fatal Error"), info, QSystemTrayIcon::Critical); + } +} + +void logger::abort(QString info) { + std::cerr << "ABORT:" << info.toStdString(); + if (MainWindow::inst() && MainWindow::inst()->valid()) { + MainWindow::inst()->ui->logBox->addItem(info); + notifications::notifyNolog(QObject::tr("KShare Even More Fatal Error"), info, QSystemTrayIcon::Critical); + } + ::abort(); +} diff --git a/src/logger.hpp b/src/logger.hpp new file mode 100644 index 0000000..a8141d4 --- /dev/null +++ b/src/logger.hpp @@ -0,0 +1,14 @@ +#ifndef LOGGER_HPP +#define LOGGER_HPP + +#include + +namespace logger { + void info(QString info); // Something went okay, notify user about it + void warn(QString info); // Something that does not interrupt execution, but could be improved + void error(QString info); // Oh no - it errored, but is recoverable, and still important enough to notify + void fatal(QString info); // Unrecoverable error, procedure aborts and notifies the user + void abort(QString info); // tell the user that it gonna ded +} + +#endif /* LOGGER_HPP */ diff --git a/src/logs/requestlogging.cpp b/src/logs/requestlogging.cpp index 3da7d51..e01174f 100644 --- a/src/logs/requestlogging.cpp +++ b/src/logs/requestlogging.cpp @@ -1,7 +1,5 @@ #include "requestlogging.hpp" #include -#include -#include #include // $type $url $status $time diff --git a/src/main.cpp b/src/main.cpp index 9b839e8..42d7340 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,8 +1,7 @@ #include "mainwindow.hpp" -#include "ui_mainwindow.h" +#include #include #include -#include #include #include #include @@ -26,12 +25,7 @@ bool verbose = false; // still alive bool stillAlive = true; -#define LOGACT(lvl) \ - std::cout << lvl << stdMsg << "\n" << std::flush; \ - if (stillAlive && MainWindow::inst() && MainWindow::inst()->valid()) { \ - MainWindow::inst()->ui->logBox->addItem(lvl + msg); \ - } - +#define LOGACT(lvl) std::cout << lvl << stdMsg << "\n" << std::flush; void handler(QtMsgType type, const QMessageLogContext &, const QString &msg) { if (!verbose && msg.startsWith("QPixmap::fromWinHBITMAP")) return; std::string stdMsg = msg.toStdString(); @@ -43,19 +37,15 @@ void handler(QtMsgType type, const QMessageLogContext &, const QString &msg) { break; case QtInfoMsg: LOGACT("INFO: ") - if (stillAlive) notifications::notifyNolog("KShare", msg); break; case QtWarningMsg: LOGACT("WARN: ") - if (stillAlive) notifications::notifyNolog("KShare Warning", msg, QSystemTrayIcon::Warning); break; case QtCriticalMsg: LOGACT("CRIT: ") - if (stillAlive) notifications::notifyNolog("KShare Critical Error", msg, QSystemTrayIcon::Critical); break; case QtFatalMsg: LOGACT("FATAL: ") - if (stillAlive) notifications::notifyNolog("KShare Fatal Error", msg, QSystemTrayIcon::Critical); break; } } @@ -108,6 +98,5 @@ int main(int argc, char *argv[]) { a.connect(&a, &QApplication::aboutToQuit, [] { stillAlive = false; }); if (!parser.isSet(h)) w.show(); - qDebug() << "lol"; return a.exec(); } diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 90e014f..4e32ca7 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -4,7 +4,6 @@ #include "settingsdialog.hpp" #include "ui_mainwindow.h" #include "utils.hpp" -#include #include #include #include @@ -15,6 +14,7 @@ #include #include #include +#include MainWindow *MainWindow::instance; @@ -23,7 +23,7 @@ void MainWindow::rec() { auto f = static_cast( settings::settings().value("recording/format", static_cast(formats::Recording::None)).toInt()); if (f >= formats::Recording::None) { - qWarning() << tr("Recording format not set in settings. Aborting."); + logger::warn(tr("Recording format not set in settings. Aborting.")); return; } RecordingContext *ctx = new RecordingContext; diff --git a/src/recording/recordingformats.cpp b/src/recording/recordingformats.cpp index b6840f7..0fb64c1 100644 --- a/src/recording/recordingformats.cpp +++ b/src/recording/recordingformats.cpp @@ -2,7 +2,7 @@ #include #include -#include +#include #include #include #include @@ -19,7 +19,7 @@ RecordingFormats::RecordingFormats(formats::Recording f) { if (!tmpDir.isValid()) { validator = [](QSize) { return false; }; - qCritical().noquote() << tr("Could not create temporary directory. Error: ") + tmpDir.errorString(); + logger::fatal(tr("Could not create temporary directory. Error: ") + tmpDir.errorString()); return; } iFormat = QImage::Format_RGB888; @@ -39,7 +39,7 @@ RecordingFormats::RecordingFormats(formats::Recording f) { return false; } } catch (std::runtime_error &e) { - qCritical() << tr("Encoder error: ") << e.what(); + logger::fatal(tr("Encoder error: ") + e.what()); interrupt = true; delete enc; return false; @@ -52,7 +52,7 @@ RecordingFormats::RecordingFormats(formats::Recording f) { frameAdded = true; enc->addFrame(img); } catch (std::runtime_error &e) { - qCritical() << tr("Encoder error: ") << e.what(); + logger::fatal(tr("Encoder error: ") + e.what()); interrupt = true; } }; diff --git a/src/recording/recordingpreview.cpp b/src/recording/recordingpreview.cpp index 1b079e6..a8ecd47 100644 --- a/src/recording/recordingpreview.cpp +++ b/src/recording/recordingpreview.cpp @@ -1,6 +1,5 @@ #include "recordingpreview.hpp" #include -#include #include #include #include diff --git a/src/settings.cpp b/src/settings.cpp index bb5ac9e..5c55461 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -1,6 +1,6 @@ #include "settings.hpp" -#include +#include #include QSettings &settings::settings() { @@ -13,7 +13,7 @@ QDir settings::dir() { if (configDir.dirName() != "KShare") { if (!configDir.cd("KShare")) { if (!configDir.mkdir("KShare")) { - qFatal("%s", QObject::tr("Could not make config directory").toLocal8Bit().constData()); + logger::abort(QObject::tr("Could not make config directory")); } else { configDir.cd("KShare"); } diff --git a/src/src.pro b/src/src.pro index d77b947..b5d22d1 100644 --- a/src/src.pro +++ b/src/src.pro @@ -72,7 +72,8 @@ SOURCES += main.cpp\ cropeditor/selectionrectangle.cpp \ screenoverlay/screenoverlayview.cpp \ screenoverlay/screenoverlay.cpp \ - screenoverlay/screenoverlaysettings.cpp + screenoverlay/screenoverlaysettings.cpp \ + logger.cpp HEADERS += mainwindow.hpp \ cropeditor/cropeditor.hpp \ @@ -123,7 +124,8 @@ HEADERS += mainwindow.hpp \ cropeditor/selectionrectangle.hpp \ screenoverlay/screenoverlayview.hpp \ screenoverlay/screenoverlay.hpp \ - screenoverlay/screenoverlaysettings.hpp + screenoverlay/screenoverlaysettings.hpp \ + logger.hpp nopkg { # win32 { diff --git a/src/uploaders/uploadersingleton.cpp b/src/uploaders/uploadersingleton.cpp index 4bac1fa..431ac9c 100644 --- a/src/uploaders/uploadersingleton.cpp +++ b/src/uploaders/uploadersingleton.cpp @@ -3,7 +3,7 @@ #include "default/clipboarduploader.hpp" #include "default/imguruploader.hpp" #include -#include +#include #include #include #include @@ -44,7 +44,7 @@ UploaderSingleton::UploaderSingleton() : QObject() { try { registerUploader(new CustomUploader(configDir.absoluteFilePath(file))); } catch (std::runtime_error &e) { - qWarning() << e.what(); + logger::warn(QString::fromStdString(e.what())); errs << e; } } @@ -77,7 +77,7 @@ void UploaderSingleton::upload(QPixmap pixmap) { if (!u->validate()) { u = uploaders.value("imgur"); set("imgur"); - qWarning() << tr("Currently selected uploader is not set up properly! Falling back to imgur"); + logger::warn(tr("Currently selected uploader is not set up properly! Falling back to imgur")); } QString format = settings::settings().value("captureformat", "PNG").toString(); QFile file(saveDir.absoluteFilePath( diff --git a/src/utils.cpp b/src/utils.cpp index f44a112..7ee95ed 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -2,11 +2,11 @@ #include #include -#include #include #include #include #include +#include #include #include @@ -136,8 +136,7 @@ void utils::externalScreenshot(std::function callback) { QObject::connect(process, QOverload::of(&QProcess::finished), [callback, process, tempPath](int code, QProcess::ExitStatus) { if (code != 0) { - qCritical().noquote() << "Failed to take external screenshot: \n" - << process->readAllStandardError(); + logger::fatal(QObject::tr("Failed to take external screenshot: \n") + process->readAllStandardError()); } else { QPixmap pixmap; if (!tempPath.isEmpty()) @@ -168,8 +167,7 @@ void utils::externalScreenshotActive(std::function callback) { QObject::connect(process, QOverload::of(&QProcess::finished), [callback, process, tempPath](int code, QProcess::ExitStatus) { if (code != 0) { - qCritical().noquote() << "Failed to take external screenshot: \n" - << process->readAllStandardError(); + logger::fatal(QObject::tr("Failed to take external screenshot: \n") + process->readAllStandardError()); } else { QPixmap pixmap; if (!tempPath.isEmpty())