rewrite logging

This commit is contained in:
ArsenArsen 2018-01-01 16:37:31 +01:00
parent 6a0f46f67b
commit dde13a19c8
No known key found for this signature in database
GPG Key ID: 683D2F43B0CA4BD2
14 changed files with 85 additions and 39 deletions

View File

@ -1,7 +1,7 @@
#include "colorpickerscene.hpp" #include "colorpickerscene.hpp"
#include <QApplication> #include <QApplication>
#include <QClipboard> #include <QClipboard>
#include <QDebug> #include <logger.hpp>
#include <QGraphicsEllipseItem> #include <QGraphicsEllipseItem>
#include <QGraphicsPixmapItem> #include <QGraphicsPixmapItem>
#include <QGraphicsTextItem> #include <QGraphicsTextItem>
@ -24,7 +24,7 @@ void ColorPickerScene::keyPressEvent(QKeyEvent *event) {
color = image.pixelColor(cursorPos().toPoint()); color = image.pixelColor(cursorPos().toPoint());
if (event->key() == Qt::Key_Return) { if (event->key() == Qt::Key_Return) {
QApplication::clipboard()->setText(color.name()); 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(); 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()); color = image.pixelColor(cursorPos().toPoint());
QApplication::clipboard()->setText(color.name()); QApplication::clipboard()->setText(color.name());
close(); close();
qInfo().noquote() << tr("Copied hex code to clipboard."); logger::info(tr("Copied hex code to clipboard."));
} }
QString ColorPickerScene::generateHint() { QString ColorPickerScene::generateHint() {

View File

@ -3,7 +3,6 @@
#include "selectionrectangle.hpp" #include "selectionrectangle.hpp"
#include <QApplication> #include <QApplication>
#include <QColorDialog> #include <QColorDialog>
#include <QDebug>
#include <QFontDialog> #include <QFontDialog>
#include <QGraphicsPolygonItem> #include <QGraphicsPolygonItem>
#include <QGraphicsProxyWidget> #include <QGraphicsProxyWidget>

View File

@ -1,6 +1,5 @@
#include "selectionrectangle.hpp" #include "selectionrectangle.hpp"
#include <QDebug>
#include <QGraphicsScene> #include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent> #include <QGraphicsSceneMouseEvent>
#include <QGraphicsView> #include <QGraphicsView>

48
src/logger.cpp Normal file
View File

@ -0,0 +1,48 @@
#include "logger.hpp"
#include "mainwindow.hpp"
#include "notifications.hpp"
#include <QDebug>
#include <iostream>
#include <stdlib.h>
#include <ui_mainwindow.h>
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();
}

14
src/logger.hpp Normal file
View File

@ -0,0 +1,14 @@
#ifndef LOGGER_HPP
#define LOGGER_HPP
#include <QString>
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 */

View File

@ -1,7 +1,5 @@
#include "requestlogging.hpp" #include "requestlogging.hpp"
#include <QDateTime> #include <QDateTime>
#include <QDebug>
#include <Qt>
#include <io/ioutils.hpp> #include <io/ioutils.hpp>
// $type $url $status $time // $type $url $status $time

View File

@ -1,8 +1,7 @@
#include "mainwindow.hpp" #include "mainwindow.hpp"
#include "ui_mainwindow.h" #include <ui_mainwindow.h>
#include <QApplication> #include <QApplication>
#include <QCommandLineParser> #include <QCommandLineParser>
#include <QDebug>
#include <QDir> #include <QDir>
#include <QScreen> #include <QScreen>
#include <QtGlobal> #include <QtGlobal>
@ -26,12 +25,7 @@ bool verbose = false;
// still alive // still alive
bool stillAlive = true; bool stillAlive = true;
#define LOGACT(lvl) \ #define LOGACT(lvl) std::cout << lvl << stdMsg << "\n" << std::flush;
std::cout << lvl << stdMsg << "\n" << std::flush; \
if (stillAlive && MainWindow::inst() && MainWindow::inst()->valid()) { \
MainWindow::inst()->ui->logBox->addItem(lvl + msg); \
}
void handler(QtMsgType type, const QMessageLogContext &, const QString &msg) { void handler(QtMsgType type, const QMessageLogContext &, const QString &msg) {
if (!verbose && msg.startsWith("QPixmap::fromWinHBITMAP")) return; if (!verbose && msg.startsWith("QPixmap::fromWinHBITMAP")) return;
std::string stdMsg = msg.toStdString(); std::string stdMsg = msg.toStdString();
@ -43,19 +37,15 @@ void handler(QtMsgType type, const QMessageLogContext &, const QString &msg) {
break; break;
case QtInfoMsg: case QtInfoMsg:
LOGACT("INFO: ") LOGACT("INFO: ")
if (stillAlive) notifications::notifyNolog("KShare", msg);
break; break;
case QtWarningMsg: case QtWarningMsg:
LOGACT("WARN: ") LOGACT("WARN: ")
if (stillAlive) notifications::notifyNolog("KShare Warning", msg, QSystemTrayIcon::Warning);
break; break;
case QtCriticalMsg: case QtCriticalMsg:
LOGACT("CRIT: ") LOGACT("CRIT: ")
if (stillAlive) notifications::notifyNolog("KShare Critical Error", msg, QSystemTrayIcon::Critical);
break; break;
case QtFatalMsg: case QtFatalMsg:
LOGACT("FATAL: ") LOGACT("FATAL: ")
if (stillAlive) notifications::notifyNolog("KShare Fatal Error", msg, QSystemTrayIcon::Critical);
break; break;
} }
} }
@ -108,6 +98,5 @@ int main(int argc, char *argv[]) {
a.connect(&a, &QApplication::aboutToQuit, [] { stillAlive = false; }); a.connect(&a, &QApplication::aboutToQuit, [] { stillAlive = false; });
if (!parser.isSet(h)) w.show(); if (!parser.isSet(h)) w.show();
qDebug() << "lol";
return a.exec(); return a.exec();
} }

View File

@ -4,7 +4,6 @@
#include "settingsdialog.hpp" #include "settingsdialog.hpp"
#include "ui_mainwindow.h" #include "ui_mainwindow.h"
#include "utils.hpp" #include "utils.hpp"
#include <QDebug>
#include <QMessageBox> #include <QMessageBox>
#include <QShortcut> #include <QShortcut>
#include <colorpicker/colorpickerscene.hpp> #include <colorpicker/colorpickerscene.hpp>
@ -15,6 +14,7 @@
#include <recording/recordingformats.hpp> #include <recording/recordingformats.hpp>
#include <settings.hpp> #include <settings.hpp>
#include <uploaders/uploadersingleton.hpp> #include <uploaders/uploadersingleton.hpp>
#include <logger.hpp>
MainWindow *MainWindow::instance; MainWindow *MainWindow::instance;
@ -23,7 +23,7 @@ void MainWindow::rec() {
auto f = static_cast<formats::Recording>( auto f = static_cast<formats::Recording>(
settings::settings().value("recording/format", static_cast<int>(formats::Recording::None)).toInt()); settings::settings().value("recording/format", static_cast<int>(formats::Recording::None)).toInt());
if (f >= formats::Recording::None) { 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; return;
} }
RecordingContext *ctx = new RecordingContext; RecordingContext *ctx = new RecordingContext;

View File

@ -2,7 +2,7 @@
#include <QBuffer> #include <QBuffer>
#include <QDateTime> #include <QDateTime>
#include <QDebug> #include <logger.hpp>
#include <QDir> #include <QDir>
#include <QFile> #include <QFile>
#include <QStandardPaths> #include <QStandardPaths>
@ -19,7 +19,7 @@
RecordingFormats::RecordingFormats(formats::Recording f) { RecordingFormats::RecordingFormats(formats::Recording f) {
if (!tmpDir.isValid()) { if (!tmpDir.isValid()) {
validator = [](QSize) { return false; }; 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; return;
} }
iFormat = QImage::Format_RGB888; iFormat = QImage::Format_RGB888;
@ -39,7 +39,7 @@ RecordingFormats::RecordingFormats(formats::Recording f) {
return false; return false;
} }
} catch (std::runtime_error &e) { } catch (std::runtime_error &e) {
qCritical() << tr("Encoder error: ") << e.what(); logger::fatal(tr("Encoder error: ") + e.what());
interrupt = true; interrupt = true;
delete enc; delete enc;
return false; return false;
@ -52,7 +52,7 @@ RecordingFormats::RecordingFormats(formats::Recording f) {
frameAdded = true; frameAdded = true;
enc->addFrame(img); enc->addFrame(img);
} catch (std::runtime_error &e) { } catch (std::runtime_error &e) {
qCritical() << tr("Encoder error: ") << e.what(); logger::fatal(tr("Encoder error: ") + e.what());
interrupt = true; interrupt = true;
} }
}; };

View File

@ -1,6 +1,5 @@
#include "recordingpreview.hpp" #include "recordingpreview.hpp"
#include <QApplication> #include <QApplication>
#include <QDebug>
#include <QGridLayout> #include <QGridLayout>
#include <QLayout> #include <QLayout>
#include <QScreen> #include <QScreen>

View File

@ -1,6 +1,6 @@
#include "settings.hpp" #include "settings.hpp"
#include <QDebug> #include <logger.hpp>
#include <QStandardPaths> #include <QStandardPaths>
QSettings &settings::settings() { QSettings &settings::settings() {
@ -13,7 +13,7 @@ QDir settings::dir() {
if (configDir.dirName() != "KShare") { if (configDir.dirName() != "KShare") {
if (!configDir.cd("KShare")) { if (!configDir.cd("KShare")) {
if (!configDir.mkdir("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 { } else {
configDir.cd("KShare"); configDir.cd("KShare");
} }

View File

@ -72,7 +72,8 @@ SOURCES += main.cpp\
cropeditor/selectionrectangle.cpp \ cropeditor/selectionrectangle.cpp \
screenoverlay/screenoverlayview.cpp \ screenoverlay/screenoverlayview.cpp \
screenoverlay/screenoverlay.cpp \ screenoverlay/screenoverlay.cpp \
screenoverlay/screenoverlaysettings.cpp screenoverlay/screenoverlaysettings.cpp \
logger.cpp
HEADERS += mainwindow.hpp \ HEADERS += mainwindow.hpp \
cropeditor/cropeditor.hpp \ cropeditor/cropeditor.hpp \
@ -123,7 +124,8 @@ HEADERS += mainwindow.hpp \
cropeditor/selectionrectangle.hpp \ cropeditor/selectionrectangle.hpp \
screenoverlay/screenoverlayview.hpp \ screenoverlay/screenoverlayview.hpp \
screenoverlay/screenoverlay.hpp \ screenoverlay/screenoverlay.hpp \
screenoverlay/screenoverlaysettings.hpp screenoverlay/screenoverlaysettings.hpp \
logger.hpp
nopkg { nopkg {
# win32 { # win32 {

View File

@ -3,7 +3,7 @@
#include "default/clipboarduploader.hpp" #include "default/clipboarduploader.hpp"
#include "default/imguruploader.hpp" #include "default/imguruploader.hpp"
#include <QBuffer> #include <QBuffer>
#include <QDebug> #include <logger.hpp>
#include <QDir> #include <QDir>
#include <QFile> #include <QFile>
#include <QStandardPaths> #include <QStandardPaths>
@ -44,7 +44,7 @@ UploaderSingleton::UploaderSingleton() : QObject() {
try { try {
registerUploader(new CustomUploader(configDir.absoluteFilePath(file))); registerUploader(new CustomUploader(configDir.absoluteFilePath(file)));
} catch (std::runtime_error &e) { } catch (std::runtime_error &e) {
qWarning() << e.what(); logger::warn(QString::fromStdString(e.what()));
errs << e; errs << e;
} }
} }
@ -77,7 +77,7 @@ void UploaderSingleton::upload(QPixmap pixmap) {
if (!u->validate()) { if (!u->validate()) {
u = uploaders.value("imgur"); u = uploaders.value("imgur");
set("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(); QString format = settings::settings().value("captureformat", "PNG").toString();
QFile file(saveDir.absoluteFilePath( QFile file(saveDir.absoluteFilePath(

View File

@ -2,11 +2,11 @@
#include <QApplication> #include <QApplication>
#include <QClipboard> #include <QClipboard>
#include <QDebug>
#include <QPainter> #include <QPainter>
#include <QPixmap> #include <QPixmap>
#include <QProcess> #include <QProcess>
#include <QScreen> #include <QScreen>
#include <logger.hpp>
#include <platformbackend.hpp> #include <platformbackend.hpp>
#include <settings.hpp> #include <settings.hpp>
@ -136,8 +136,7 @@ void utils::externalScreenshot(std::function<void(QPixmap)> callback) {
QObject::connect(process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), QObject::connect(process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
[callback, process, tempPath](int code, QProcess::ExitStatus) { [callback, process, tempPath](int code, QProcess::ExitStatus) {
if (code != 0) { if (code != 0) {
qCritical().noquote() << "Failed to take external screenshot: \n" logger::fatal(QObject::tr("Failed to take external screenshot: \n") + process->readAllStandardError());
<< process->readAllStandardError();
} else { } else {
QPixmap pixmap; QPixmap pixmap;
if (!tempPath.isEmpty()) if (!tempPath.isEmpty())
@ -168,8 +167,7 @@ void utils::externalScreenshotActive(std::function<void(QPixmap)> callback) {
QObject::connect(process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), QObject::connect(process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
[callback, process, tempPath](int code, QProcess::ExitStatus) { [callback, process, tempPath](int code, QProcess::ExitStatus) {
if (code != 0) { if (code != 0) {
qCritical().noquote() << "Failed to take external screenshot: \n" logger::fatal(QObject::tr("Failed to take external screenshot: \n") + process->readAllStandardError());
<< process->readAllStandardError();
} else { } else {
QPixmap pixmap; QPixmap pixmap;
if (!tempPath.isEmpty()) if (!tempPath.isEmpty())