1
0
Fork 0

[Logging] Improved logging using spdlog. Also increased build time by 5x!

This commit is contained in:
NamedKitten 2018-12-09 02:35:08 +00:00
parent 1f14f656be
commit 2d06de8548
10 changed files with 157 additions and 28 deletions

View file

@ -4,8 +4,13 @@ project(KittehPlayer)
include_directories(${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR}) include_directories(${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR})
set(CMAKE_AUTOMOC ON) set(CMAKE_AUTOMOC ON)
include(ExternalProject)
find_package(Qt5Core REQUIRED) find_package(Qt5Core REQUIRED)
find_package(Qt5Gui REQUIRED) find_package(Qt5Gui REQUIRED)
find_package(Qt5Concurrent REQUIRED)
find_package(Qt5 CONFIG REQUIRED COMPONENTS Qml Quick Gui Widgets Core X11Extras) find_package(Qt5 CONFIG REQUIRED COMPONENTS Qml Quick Gui Widgets Core X11Extras)
find_package(Qt5QuickCompiler) find_package(Qt5QuickCompiler)
@ -16,6 +21,16 @@ pkg_check_modules(MPV REQUIRED mpv)
pkg_check_modules(X11 x11) pkg_check_modules(X11 x11)
pkg_check_modules(Xext xext) pkg_check_modules(Xext xext)
execute_process(
COMMAND git clone --depth 1 https://github.com/gabime/spdlog.git
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)
include_directories(${CMAKE_BINARY_DIR}/spdlog/include)
if(X11_FOUND AND Xext_FOUND) if(X11_FOUND AND Xext_FOUND)
add_definitions(-DENABLE_X11) add_definitions(-DENABLE_X11)
endif(X11_FOUND AND Xext_FOUND) endif(X11_FOUND AND Xext_FOUND)
@ -27,6 +42,7 @@ set(SOURCES
src/enums.hpp src/enums.hpp
src/Process.cpp src/Process.cpp
src/ThumbnailCache.cpp src/ThumbnailCache.cpp
src/logger.cpp
) )
if(MPV_VERSION VERSION_GREATER_EQUAL "1.28.0") if(MPV_VERSION VERSION_GREATER_EQUAL "1.28.0")
@ -58,7 +74,7 @@ add_executable(KittehPlayer ${SOURCES} ${qml_QRC})
# Use the Qml/Quick modules from Qt 5. # Use the Qml/Quick modules from Qt 5.
target_link_libraries(KittehPlayer ${MPV_LIBRARIES} ${X11_LIBRARIES} ${Xext_LIBRARIES} Qt5::X11Extras) target_link_libraries(KittehPlayer ${MPV_LIBRARIES} ${X11_LIBRARIES} ${Xext_LIBRARIES} Qt5::X11Extras)
include_directories(${Qt5Gui_PRIVATE_INCLUDE_DIRS}) include_directories(${Qt5Gui_PRIVATE_INCLUDE_DIRS} ${Qt5Concurrent_INCLUDE_DIRS})
qt5_use_modules(KittehPlayer Qml Quick Core Gui Widgets X11Extras) qt5_use_modules(KittehPlayer Qml Quick Core Gui Widgets X11Extras)
install (TARGETS ${PROJECT_NAME} DESTINATION bin) install (TARGETS ${PROJECT_NAME} DESTINATION bin)

View file

@ -1,8 +1,5 @@
#include "MpvPlayerBackend.h" #include "MpvPlayerBackend.h"
#include <clocale> #include "logger.h"
#include <stdbool.h>
#include <stdexcept>
#include "utils.hpp" #include "utils.hpp"
#include <QApplication> #include <QApplication>
#include <QElapsedTimer> #include <QElapsedTimer>
@ -10,7 +7,10 @@
#include <QOpenGLFramebufferObject> #include <QOpenGLFramebufferObject>
#include <QQuickWindow> #include <QQuickWindow>
#include <QSequentialIterable> #include <QSequentialIterable>
#include <clocale>
#include <math.h> #include <math.h>
#include <stdbool.h>
#include <stdexcept>
#ifdef __linux__ #ifdef __linux__
#include <QX11Info> #include <QX11Info>
@ -20,6 +20,8 @@
#include <qpa/qplatformnativeinterface.h> #include <qpa/qplatformnativeinterface.h>
#endif #endif
auto mpvLogger = initLogger("mpv");
namespace { namespace {
void void
@ -80,13 +82,11 @@ public:
if (QGuiApplication::platformName().contains("xcb")) { if (QGuiApplication::platformName().contains("xcb")) {
params[2].type = MPV_RENDER_PARAM_X11_DISPLAY; params[2].type = MPV_RENDER_PARAM_X11_DISPLAY;
params[2].data = QX11Info::display(); params[2].data = QX11Info::display();
qDebug() << "On Xorg.";
} else if (QGuiApplication::platformName().contains("wayland")) { } else if (QGuiApplication::platformName().contains("wayland")) {
QPlatformNativeInterface* native = QPlatformNativeInterface* native =
QGuiApplication::platformNativeInterface(); QGuiApplication::platformNativeInterface();
params[2].type = MPV_RENDER_PARAM_WL_DISPLAY; params[2].type = MPV_RENDER_PARAM_WL_DISPLAY;
params[2].data = native->nativeResourceForWindow("display", nullptr); params[2].data = native->nativeResourceForWindow("display", nullptr);
qDebug() << "On wayland.";
} }
#endif #endif
@ -129,10 +129,11 @@ MpvPlayerBackend::MpvPlayerBackend(QQuickItem* parent)
, mpv{ mpv_create() } , mpv{ mpv_create() }
, mpv_gl(nullptr) , mpv_gl(nullptr)
{ {
mpvLogger->set_pattern("%^[%d-%m-%Y %T.%e][%l][%n]%v%$");
if (!mpv) if (!mpv)
throw std::runtime_error("could not create mpv context"); throw std::runtime_error("could not create mpv context");
mpv_set_option_string(mpv, "terminal", "off"); mpv_set_option_string(mpv, "terminal", "on");
mpv_set_option_string(mpv, "msg-level", "all=v"); mpv_set_option_string(mpv, "msg-level", "all=v");
// Fix? // Fix?
@ -161,6 +162,9 @@ MpvPlayerBackend::MpvPlayerBackend(QQuickItem* parent)
mpv_observe_property(mpv, 0, "pause", MPV_FORMAT_NODE); mpv_observe_property(mpv, 0, "pause", MPV_FORMAT_NODE);
mpv_observe_property(mpv, 0, "playlist", MPV_FORMAT_NODE); mpv_observe_property(mpv, 0, "playlist", MPV_FORMAT_NODE);
mpv_observe_property(mpv, 0, "speed", MPV_FORMAT_DOUBLE); mpv_observe_property(mpv, 0, "speed", MPV_FORMAT_DOUBLE);
mpv_request_log_messages(mpv, "debug");
mpv_set_wakeup_callback(mpv, wakeup, this); mpv_set_wakeup_callback(mpv, wakeup, this);
if (mpv_initialize(mpv) < 0) if (mpv_initialize(mpv) < 0)
@ -550,6 +554,20 @@ MpvPlayerBackend::handle_mpv_event(mpv_event* event)
} }
break; break;
} }
case MPV_EVENT_LOG_MESSAGE: {
struct mpv_event_log_message* msg =
(struct mpv_event_log_message*)event->data;
QString logMsg = "[" + QString(msg->prefix) + "] " + QString(msg->text);
QString msgLevel = QString(msg->level);
if (msgLevel.startsWith("d")) {
mpvLogger->debug("{}", logMsg.toUtf8().constData());
} else if (msgLevel.startsWith("v") || msgLevel.startsWith("i")) {
mpvLogger->info("{}", logMsg.toUtf8().constData());
}
break;
}
case MPV_EVENT_SHUTDOWN: { case MPV_EVENT_SHUTDOWN: {
qApp->exit(); qApp->exit();
break; break;

21
src/logger.cpp Normal file
View file

@ -0,0 +1,21 @@
#include "logger.h"
#include <QObject>
#include <QString>
#include <iostream>
std::shared_ptr<spdlog::logger>
initLogger(std::string name)
{
std::vector<spdlog::sink_ptr> sinks;
sinks.push_back(std::make_shared<spdlog::sinks::stdout_color_sink_mt>());
sinks.push_back(std::make_shared<spdlog::sinks::basic_file_sink_mt>(
"/tmp/KittehPlayer.log"));
auto console =
std::make_shared<spdlog::logger>(name, begin(sinks), end(sinks));
console->set_pattern("%^[%d-%m-%Y %T.%e][%l][%n] %v%$");
spdlog::register_logger(console);
return spdlog::get(name);
}

20
src/logger.h Normal file
View file

@ -0,0 +1,20 @@
#ifndef LOGGER_HPP
#define LOGGER_HPP
#include <spdlog/spdlog.h>
#include <spdlog/sinks/basic_file_sink.h>
#include <spdlog/sinks/daily_file_sink.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <QProcessEnvironment>
#include <QQmlApplicationEngine>
#include <QSequentialIterable>
#include <QString>
#include <QVariant>
#include <QtCore>
#include <QtNetwork>
std::shared_ptr<spdlog::logger>
initLogger(std::string name);
#endif

View file

@ -4,6 +4,7 @@
#endif #endif
#include "enums.hpp" #include "enums.hpp"
#include "logger.h"
#include "utils.hpp" #include "utils.hpp"
#include <cstdlib> #include <cstdlib>
@ -12,6 +13,7 @@
#include <QApplication> #include <QApplication>
#include <QProcessEnvironment> #include <QProcessEnvironment>
#include <QQmlApplicationEngine> #include <QQmlApplicationEngine>
#include <QtConcurrent>
#include <QtCore> #include <QtCore>
#include <QtQml> #include <QtQml>
#include <stdbool.h> #include <stdbool.h>
@ -45,10 +47,50 @@ catchUnixSignals(std::initializer_list<int> quitSignals)
} }
#endif #endif
auto qmlLogger = initLogger("qml");
auto miscLogger = initLogger("misc");
void
spdLogger(QtMsgType type, const QMessageLogContext& context, const QString& msg)
{
std::string localMsg = msg.toUtf8().constData();
std::shared_ptr<spdlog::logger> logger;
if (QString(context.category).startsWith(QString("qml"))) {
logger = qmlLogger;
} else {
logger = miscLogger;
}
switch (type) {
case QtDebugMsg:
logger->debug("{}", localMsg);
break;
case QtInfoMsg:
logger->info("{}", localMsg);
break;
case QtWarningMsg:
logger->warn("{}", localMsg);
break;
case QtCriticalMsg:
logger->critical("{}", localMsg);
break;
case QtFatalMsg:
logger->critical("{}", localMsg);
abort();
}
}
int int
main(int argc, char* argv[]) main(int argc, char* argv[])
{ {
qInstallMessageHandler(spdLogger);
auto launcherLogger = initLogger("launcher");
launcherLogger->info("Starting up!");
QString backendString;
#ifdef DISABLE_MpvPlayerBackend #ifdef DISABLE_MpvPlayerBackend
Enums::Backends backend = Enums::Backends::DirectMpvBackend; Enums::Backends backend = Enums::Backends::DirectMpvBackend;
#else #else
@ -66,7 +108,6 @@ main(int argc, char* argv[])
QSettings settings; QSettings settings;
#ifdef GIT_COMMIT_HASH
bool checkForUpdates = bool checkForUpdates =
settings.value("Backend/checkForUpdatesOnLaunch", false).toBool(); settings.value("Backend/checkForUpdatesOnLaunch", false).toBool();
for (int i = 1; i < argc; ++i) { for (int i = 1; i < argc; ++i) {
@ -76,9 +117,8 @@ main(int argc, char* argv[])
} }
if (checkForUpdates) { if (checkForUpdates) {
Utils::checkForUpdates(); QtConcurrent::run(Utils::checkForUpdates);
} }
#endif
QString backendSetting = settings.value("Backend/backend", "").toString(); QString backendSetting = settings.value("Backend/backend", "").toString();
if (backendSetting.length() == 0) { if (backendSetting.length() == 0) {
@ -88,20 +128,23 @@ main(int argc, char* argv[])
settings.setValue("Backend/backend", "direct-mpv"); settings.setValue("Backend/backend", "direct-mpv");
#endif #endif
} }
backendString = backendSetting;
qDebug() << backendSetting;
for (int i = 1; i < argc; ++i) { for (int i = 1; i < argc; ++i) {
if (!qstrcmp(argv[i], "--update")) { if (!qstrcmp(argv[i], "--update")) {
Utils::updateAppImage(); Utils::updateAppImage();
} else if (!qstrcmp(argv[i], "--backend=mpv") || backendSetting == "mpv") { } else if (!qstrcmp(argv[i], "--backend=mpv") || backendSetting == "mpv") {
backend = Enums::Backends::MpvBackend; backend = Enums::Backends::MpvBackend;
backendString = QString("mpv");
} else if (!qstrcmp(argv[i], "--backend=direct-mpv") || } else if (!qstrcmp(argv[i], "--backend=direct-mpv") ||
backendSetting == "direct-mpv") { backendSetting == "direct-mpv") {
backendString = QString("direct-mpv");
backend = Enums::Backends::DirectMpvBackend; backend = Enums::Backends::DirectMpvBackend;
} }
} }
launcherLogger->info("Using backend={}", backendString.toUtf8().constData());
Utils::SetDPMS(false); Utils::SetDPMS(false);
QString newpath = QString newpath =
@ -116,6 +159,7 @@ main(int argc, char* argv[])
qRegisterMetaType<Enums::Backends>("Enums.Backends"); qRegisterMetaType<Enums::Backends>("Enums.Backends");
qRegisterMetaType<Enums::Commands>("Enums.Commands"); qRegisterMetaType<Enums::Commands>("Enums.Commands");
qmlRegisterType<Process>("player", 1, 0, "Process"); qmlRegisterType<Process>("player", 1, 0, "Process");
qmlRegisterType<ThumbnailCache>("player", 1, 0, "ThumbnailCache"); qmlRegisterType<ThumbnailCache>("player", 1, 0, "ThumbnailCache");
qmlRegisterType<UtilsClass>("player", 1, 0, "Utils"); qmlRegisterType<UtilsClass>("player", 1, 0, "Utils");
@ -139,6 +183,7 @@ main(int argc, char* argv[])
} }
std::setlocale(LC_NUMERIC, "C"); std::setlocale(LC_NUMERIC, "C");
launcherLogger->info("Loading player...");
QQmlApplicationEngine engine; QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:///main.qml"))); engine.load(QUrl(QStringLiteral("qrc:///main.qml")));

View file

@ -36,7 +36,7 @@ Item {
var component = Qt.createComponent(themeName + "ButtonLayout.qml") var component = Qt.createComponent(themeName + "ButtonLayout.qml")
if (component.status == Component.Error) { if (component.status == Component.Error) {
console.log("Error loading component:", component.errorString()) console.error("Error loading component: " + component.errorString())
} }
component.createObject(controlsBar, { component.createObject(controlsBar, {

View file

@ -25,7 +25,6 @@ Button {
target: player target: player
enabled: true enabled: true
onPlayStatusChanged: function (status) { onPlayStatusChanged: function (status) {
console.log(icon.height)
if (status == Enums.PlayStatus.Playing) { if (status == Enums.PlayStatus.Playing) {
icon.source = "qrc:/icons/" + appearance.themeName + "/pause.svg" icon.source = "qrc:/icons/" + appearance.themeName + "/pause.svg"
} else if (status == Enums.PlayStatus.Paused) { } else if (status == Enums.PlayStatus.Paused) {

View file

@ -24,9 +24,6 @@ Button {
} }
function updateStatus(status) { function updateStatus(status) {
if (volumeButton == null)
console.log("OwO")
if (status == Enums.VolumeStatus.Muted) { if (status == Enums.VolumeStatus.Muted) {
volumeButton.icon.source = "qrc:/icons/" + appearance.themeName + "/volume-mute.svg" volumeButton.icon.source = "qrc:/icons/" + appearance.themeName + "/volume-mute.svg"
} else if (status == Enums.VolumeStatus.Low) { } else if (status == Enums.VolumeStatus.Low) {

View file

@ -202,6 +202,8 @@ Window {
} }
function startPlayer() { function startPlayer() {
console.info("OwO!")
var args = Qt.application.arguments var args = Qt.application.arguments
var len = Qt.application.arguments.length var len = Qt.application.arguments.length
var argNo = 0 var argNo = 0

View file

@ -1,4 +1,6 @@
#include "utils.hpp" #include "utils.hpp"
#include "logger.h"
#include <stdbool.h> #include <stdbool.h>
#include <QApplication> #include <QApplication>
@ -20,6 +22,9 @@
#endif #endif
#endif #endif
auto utilsLogger = initLogger("utils");
auto updaterLogger = initLogger("updater");
namespace Utils { namespace Utils {
QString QString
getPlatformName() getPlatformName()
@ -45,7 +50,8 @@ checkForUpdates()
#else #else
QString current_version = QString("Unknown"); QString current_version = QString("Unknown");
#endif #endif
qDebug() << "Current Version: " << current_version; updaterLogger->info("Current Version: {}",
current_version.toUtf8().constData());
QNetworkRequest request(QUrl("https://api.github.com/repos/NamedKitten/" QNetworkRequest request(QUrl("https://api.github.com/repos/NamedKitten/"
"KittehPlayer/releases/tags/continuous")); "KittehPlayer/releases/tags/continuous"));
@ -61,8 +67,10 @@ checkForUpdates()
if (json["target_commitish"].toString().length() != 0) { if (json["target_commitish"].toString().length() != 0) {
if (json["target_commitish"].toString().endsWith(current_version) == 0) { if (json["target_commitish"].toString().endsWith(current_version) == 0) {
qDebug() << "Latest Version: " << json["target_commitish"].toString(); updaterLogger->info(
qDebug() << "Update Available. Please update ASAP."; "Latest Version: {}",
json["target_commitish"].toString().toUtf8().constData());
updaterLogger->info("Update Available. Please update ASAP.");
QProcess notifier; QProcess notifier;
notifier.setProcessChannelMode(QProcess::ForwardedChannels); notifier.setProcessChannelMode(QProcess::ForwardedChannels);
notifier.start("notify-send", notifier.start("notify-send",
@ -72,13 +80,14 @@ checkForUpdates()
notifier.waitForFinished(); notifier.waitForFinished();
} }
} else { } else {
qDebug() << "Couldn't check for new version."; updaterLogger->error("Couldn't check for new version.");
} }
} }
void void
updateAppImage() updateAppImage()
{ {
updaterLogger->info("Launching updater");
QString program = QString program =
QProcessEnvironment::systemEnvironment().value("APPDIR", "") + QProcessEnvironment::systemEnvironment().value("APPDIR", "") +
"/usr/bin/appimageupdatetool"; "/usr/bin/appimageupdatetool";
@ -119,11 +128,11 @@ SetScreensaver(WId wid, bool on)
QProcess xdgScreensaver; QProcess xdgScreensaver;
xdgScreensaver.setProcessChannelMode(QProcess::ForwardedChannels); xdgScreensaver.setProcessChannelMode(QProcess::ForwardedChannels);
if (on) { if (on) {
qDebug() << "Enabling screensaver."; utilsLogger->info("Enabling screensaver.");
xdgScreensaver.start("xdg-screensaver", xdgScreensaver.start("xdg-screensaver",
QStringList() << "resume" << QString::number(wid)); QStringList() << "resume" << QString::number(wid));
} else { } else {
qDebug() << "Disabling screensaver."; utilsLogger->info("Disabling screensaver.");
xdgScreensaver.start("xdg-screensaver", xdgScreensaver.start("xdg-screensaver",
QStringList() << "suspend" << QString::number(wid)); QStringList() << "suspend" << QString::number(wid));
} }
@ -140,13 +149,13 @@ SetDPMS(bool on)
QProcess xsetProcess; QProcess xsetProcess;
xsetProcess.setProcessChannelMode(QProcess::ForwardedChannels); xsetProcess.setProcessChannelMode(QProcess::ForwardedChannels);
if (on) { if (on) {
qDebug() << "Enabled DPMS."; utilsLogger->info("Enabled DPMS.");
xsetProcess.start("xset", xsetProcess.start("xset",
QStringList() << "s" QStringList() << "s"
<< "on" << "on"
<< "+dpms"); << "+dpms");
} else { } else {
qDebug() << "Disabled DPMS."; utilsLogger->info("Disabled DPMS.");
xsetProcess.start("xset", xsetProcess.start("xset",
QStringList() << "s" QStringList() << "s"
<< "off" << "off"
@ -154,7 +163,8 @@ SetDPMS(bool on)
} }
xsetProcess.waitForFinished(); xsetProcess.waitForFinished();
#else #else
qDebug() << "Can't set DPMS for platform: " << getPlatformName(); utilsLogger->error("Can't set DPMS for platform: {}",
getPlatformName().toUtf8().constData());
#endif #endif
} }
@ -186,7 +196,8 @@ AlwaysOnTop(WId wid, bool on)
&event); &event);
#endif #endif
#else #else
qDebug() << "Can't set on top for platform: " << getPlatformName(); utilsLogger->error("Can't set on top for platform: {}",
getPlatformName().toUtf8().constData());
#endif #endif
} }
} }