summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorUwe Klotz <uklotz@mixxx.org>2020-05-03 10:48:31 +0200
committerUwe Klotz <uklotz@mixxx.org>2020-05-03 10:50:41 +0200
commit6416aa790310fb7d9adb0a40dd412d29a575c8f9 (patch)
tree1b7bd018c7fbfe6fc438ebf895d12de2061de438 /src
parent0f266a9247f6795c280c2e5936c6de122598b9b1 (diff)
Reformat logging code
Diffstat (limited to 'src')
-rw-r--r--src/util/logging.cpp113
-rw-r--r--src/util/logging.h24
2 files changed, 69 insertions, 68 deletions
diff --git a/src/util/logging.cpp b/src/util/logging.cpp
index 9e29712cf7..c5b20739bc 100644
--- a/src/util/logging.cpp
+++ b/src/util/logging.cpp
@@ -1,20 +1,20 @@
#include "util/logging.h"
-#include <stdio.h>
#include <signal.h>
+#include <stdio.h>
#include <QByteArray>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QIODevice>
+#include <QLoggingCategory>
#include <QMutex>
#include <QMutexLocker>
#include <QString>
#include <QThread>
#include <QtDebug>
#include <QtGlobal>
-#include <QLoggingCategory>
#include "controllers/controllerdebug.h"
#include "util/assert.h"
@@ -36,8 +36,10 @@ QFile g_logfile;
bool g_debugAssertBreak = false;
// Handles actually writing to stderr and the log.
-inline void writeToLog(const QByteArray& message, bool shouldPrint,
- bool shouldFlush) {
+inline void writeToLog(
+ const QByteArray& message,
+ bool shouldPrint,
+ bool shouldFlush) {
if (shouldPrint) {
fwrite(message.constData(), sizeof(char), message.size(), stderr);
}
@@ -55,8 +57,10 @@ inline void writeToLog(const QByteArray& message, bool shouldPrint,
// Debug message handler which outputs to stderr and a logfile, prepending the
// thread name and log level.
-void MessageHandler(QtMsgType type,
- const QMessageLogContext&, const QString& input) {
+void MessageHandler(
+ QtMsgType type,
+ const QMessageLogContext&,
+ const QString& input) {
// For "]: " and '\n'.
size_t baSize = 4;
const char* tag = nullptr;
@@ -65,47 +69,48 @@ void MessageHandler(QtMsgType type,
bool isDebugAssert = false;
bool isControllerDebug = false;
switch (type) {
- case QtDebugMsg:
- tag = "Debug [";
- baSize += strlen(tag);
- isControllerDebug = input.startsWith(QLatin1String(
+ case QtDebugMsg:
+ tag = "Debug [";
+ baSize += strlen(tag);
+ isControllerDebug = input.startsWith(QLatin1String(
ControllerDebug::kLogMessagePrefix));
- shouldPrint = Logging::enabled(LogLevel::Debug) ||
- isControllerDebug;
- shouldFlush = Logging::flushing(LogLevel::Debug);
- break;
- case QtInfoMsg:
- tag = "Info [";
- baSize += strlen(tag);
- shouldPrint = Logging::enabled(LogLevel::Info);
- shouldFlush = Logging::flushing(LogLevel::Info);
- break;
- case QtWarningMsg:
- tag = "Warning [";
- baSize += strlen(tag);
- shouldPrint = Logging::enabled(LogLevel::Warning);
- shouldFlush = Logging::flushing(LogLevel::Warning);
- break;
- case QtCriticalMsg:
- tag = "Critical [";
- baSize += strlen(tag);
- shouldFlush = true;
- isDebugAssert = input.startsWith(QLatin1String(kDebugAssertPrefix));
- break;
- case QtFatalMsg:
- tag = "Fatal [";
- baSize += strlen(tag);
- shouldFlush = true;
- break;
- default:
- tag = "Unknown [";
- baSize += strlen(tag);
+ shouldPrint = Logging::enabled(LogLevel::Debug) ||
+ isControllerDebug;
+ shouldFlush = Logging::flushing(LogLevel::Debug);
+ break;
+ case QtInfoMsg:
+ tag = "Info [";
+ baSize += strlen(tag);
+ shouldPrint = Logging::enabled(LogLevel::Info);
+ shouldFlush = Logging::flushing(LogLevel::Info);
+ break;
+ case QtWarningMsg:
+ tag = "Warning [";
+ baSize += strlen(tag);
+ shouldPrint = Logging::enabled(LogLevel::Warning);
+ shouldFlush = Logging::flushing(LogLevel::Warning);
+ break;
+ case QtCriticalMsg:
+ tag = "Critical [";
+ baSize += strlen(tag);
+ shouldFlush = true;
+ isDebugAssert = input.startsWith(QLatin1String(kDebugAssertPrefix));
+ break;
+ case QtFatalMsg:
+ tag = "Fatal [";
+ baSize += strlen(tag);
+ shouldFlush = true;
+ break;
+ default:
+ tag = "Unknown [";
+ baSize += strlen(tag);
}
// qthread.cpp contains a Q_ASSERT that currentThread does not return
// nullptr.
QByteArray threadName = QThread::currentThread()
- ->objectName().toLocal8Bit();
+ ->objectName()
+ .toLocal8Bit();
baSize += threadName.length();
QByteArray input8Bit;
@@ -145,13 +150,14 @@ void MessageHandler(QtMsgType type,
writeToLog(ba, shouldPrint, shouldFlush);
}
-} // namespace
+} // namespace
// static
-void Logging::initialize(const QDir& settingsDir,
- LogLevel logLevel,
- LogLevel logFlushLevel,
- bool debugAssertBreak) {
+void Logging::initialize(
+ const QDir& settingsDir,
+ LogLevel logLevel,
+ LogLevel logFlushLevel,
+ bool debugAssertBreak) {
VERIFY_OR_DEBUG_ASSERT(!g_logfile.isOpen()) {
// Somebody already called Logging::initialize.
return;
@@ -178,8 +184,7 @@ void Logging::initialize(const QDir& settingsDir,
QFile::remove(olderlogname);
}
if (!QFile::rename(logFileName, olderlogname)) {
- fprintf(stderr, "Error rolling over logfile %s",
- logFileName.toLocal8Bit().constData());
+ fprintf(stderr, "Error rolling over logfile %s", logFileName.toLocal8Bit().constData());
}
}
}
@@ -201,17 +206,13 @@ void Logging::initialize(const QDir& settingsDir,
// Ubuntu: https://bugs.launchpad.net/ubuntu/+source/qtbase-opensource-src/+bug/1731646
// Somehow this causes a segfault on macOS though?? https://bugs.launchpad.net/mixxx/+bug/1871238
#ifdef __LINUX__
- QLoggingCategory::setFilterRules("*.debug=true\n"
- "qt.*.debug=false");
+ QLoggingCategory::setFilterRules(
+ "*.debug=true\n"
+ "qt.*.debug=false");
#endif
}
// static
-void Logging::setLogLevel(LogLevel logLevel) {
- g_logLevel = logLevel;
-}
-
-// static
void Logging::shutdown() {
// Reset the Qt message handler to default.
qInstallMessageHandler(nullptr);
@@ -232,4 +233,4 @@ void Logging::flushLogFile() {
}
}
-} // namespace mixxx
+} // namespace mixxx
diff --git a/src/util/logging.h b/src/util/logging.h
index 79f71eb927..5d85ba133d 100644
--- a/src/util/logging.h
+++ b/src/util/logging.h
@@ -1,9 +1,7 @@
-#ifndef MIXXX_UTIL_LOGGING_H
-#define MIXXX_UTIL_LOGGING_H
+#pragma once
#include <QDir>
-
namespace mixxx {
enum class LogLevel {
@@ -27,13 +25,17 @@ extern LogLevel g_logFlushLevel;
class Logging {
public:
// These are not thread safe. Only call them on Mixxx startup and shutdown.
- static void initialize(const QDir& settingsDir,
- LogLevel logLevel,
- LogLevel logFlushLevel,
- bool debugAssertBreak);
+ static void initialize(
+ const QDir& settingsDir,
+ LogLevel logLevel,
+ LogLevel logFlushLevel,
+ bool debugAssertBreak);
- // Sets only the loglevel without the on-disk settings. Used by mixxx-test.
- static void setLogLevel(LogLevel logLevel);
+ // Sets only the loglevel without the on-disk settings. Used by mixxx-test.
+ static void setLogLevel(
+ LogLevel logLevel) {
+ g_logLevel = logLevel;
+ }
static void shutdown();
@@ -59,6 +61,4 @@ class Logging {
Logging() = delete;
};
-} // namespace mixxx
-
-#endif /* MIXXX_UTIL_LOGGING_H */
+} // namespace mixxx