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 <QApplication>
#include <QClipboard>
#include <QDebug>
#include <logger.hpp>
#include <QGraphicsEllipseItem>
#include <QGraphicsPixmapItem>
#include <QGraphicsTextItem>
@ -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() {

View File

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

View File

@ -1,6 +1,5 @@
#include "selectionrectangle.hpp"
#include <QDebug>
#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
#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 <QDateTime>
#include <QDebug>
#include <Qt>
#include <io/ioutils.hpp>
// $type $url $status $time

View File

@ -1,8 +1,7 @@
#include "mainwindow.hpp"
#include "ui_mainwindow.h"
#include <ui_mainwindow.h>
#include <QApplication>
#include <QCommandLineParser>
#include <QDebug>
#include <QDir>
#include <QScreen>
#include <QtGlobal>
@ -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();
}

View File

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

View File

@ -2,7 +2,7 @@
#include <QBuffer>
#include <QDateTime>
#include <QDebug>
#include <logger.hpp>
#include <QDir>
#include <QFile>
#include <QStandardPaths>
@ -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;
}
};

View File

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

View File

@ -1,6 +1,6 @@
#include "settings.hpp"
#include <QDebug>
#include <logger.hpp>
#include <QStandardPaths>
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");
}

View File

@ -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 {

View File

@ -3,7 +3,7 @@
#include "default/clipboarduploader.hpp"
#include "default/imguruploader.hpp"
#include <QBuffer>
#include <QDebug>
#include <logger.hpp>
#include <QDir>
#include <QFile>
#include <QStandardPaths>
@ -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(

View File

@ -2,11 +2,11 @@
#include <QApplication>
#include <QClipboard>
#include <QDebug>
#include <QPainter>
#include <QPixmap>
#include <QProcess>
#include <QScreen>
#include <logger.hpp>
#include <platformbackend.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),
[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<void(QPixmap)> callback) {
QObject::connect(process, QOverload<int, QProcess::ExitStatus>::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())