From a2d2850884c6ff55267e0d8b66f4a28aacf308f7 Mon Sep 17 00:00:00 2001 From: Jim Broadus Date: Wed, 29 Jan 2020 21:32:09 -0800 Subject: [PATCH 1/2] Add a new qLogCat macro to specify category. In some cases, such as message handling callback functions, the line and function macros don't provide a lot of useful information. In other cases, we may want more granularity of control withing a class. For these cases, add a qLogCat that takes a category string. Print this string in the message and use it as the filter category. --- ext/libclementine-common/core/logging.cpp | 53 ++++++++++++++--------- ext/libclementine-common/core/logging.h | 52 ++++++++++++++-------- 2 files changed, 67 insertions(+), 38 deletions(-) diff --git a/ext/libclementine-common/core/logging.cpp b/ext/libclementine-common/core/logging.cpp index 0c4809f35..42105151e 100644 --- a/ext/libclementine-common/core/logging.cpp +++ b/ext/libclementine-common/core/logging.cpp @@ -92,7 +92,8 @@ static void MessageHandler(QtMsgType type, const QMessageLogContext &context, co } for (const QString& line : message.split('\n')) { - CreateLogger(level, "unknown", -1) << line.toLocal8Bit().constData(); + CreateLogger(level, "unknown", -1, nullptr) + << line.toLocal8Bit().constData(); } if (type == QtFatalMsg) { @@ -164,7 +165,8 @@ QString ParsePrettyFunction(const char* pretty_function) { return class_name; } -QDebug CreateLogger(Level level, const QString& class_name, int line) { +QDebug CreateLogger(Level level, const QString& class_name, int line, + const char* category) { // Map the level to a string const char* level_name = nullptr; switch (level) { @@ -185,10 +187,11 @@ QDebug CreateLogger(Level level, const QString& class_name, int line) { break; } + QString filter_category = (category != nullptr) ? category : class_name; // Check the settings to see if we're meant to show or hide this message. Level threshold_level = sDefaultLevel; - if (sClassLevels && sClassLevels->contains(class_name)) { - threshold_level = sClassLevels->value(class_name); + if (sClassLevels && sClassLevels->contains(filter_category)) { + threshold_level = sClassLevels->value(filter_category); } if (level > threshold_level) { @@ -199,6 +202,9 @@ QDebug CreateLogger(Level level, const QString& class_name, int line) { if (line != -1) { function_line += ":" + QString::number(line); } + if (category) { + function_line += "(" + QString(category) + ")"; + } QtMsgType type = QtDebugMsg; if (level == Level_Fatal) { @@ -271,32 +277,39 @@ void DumpStackTrace() { #endif } -QDebug CreateLoggerFatal(int line, const char* pretty_function) { - return qCreateLogger(line, pretty_function, Fatal); +QDebug CreateLoggerFatal(int line, const char* pretty_function, + const char* category) { + return qCreateLogger(line, pretty_function, category, Fatal); } -QDebug CreateLoggerError(int line, const char* pretty_function) { - return qCreateLogger(line, pretty_function, Error); +QDebug CreateLoggerError(int line, const char* pretty_function, + const char* category) { + return qCreateLogger(line, pretty_function, category, Error); } #ifdef QT_NO_WARNING_OUTPUT -QNoDebug CreateLoggerWarning(int, const char*) { return QNoDebug(); } -#else -QDebug CreateLoggerWarning(int line, const char* pretty_function) { - return qCreateLogger(line, pretty_function, Warning); +QNoDebug CreateLoggerWarning(int, const char*, const char*) { + return QNoDebug(); } -#endif // QT_NO_WARNING_OUTPUT +#else +QDebug CreateLoggerWarning(int line, const char* pretty_function, + const char* category) { + return qCreateLogger(line, pretty_function, category, Warning); +} +#endif // QT_NO_WARNING_OUTPUT #ifdef QT_NO_DEBUG_OUTPUT -QNoDebug CreateLoggerInfo(int, const char*) { return QNoDebug(); } -QNoDebug CreateLoggerDebug(int, const char*) { return QNoDebug(); } +QNoDebug CreateLoggerInfo(int, const char*, const char*) { return QNoDebug(); } +QNoDebug CreateLoggerDebug(int, const char*, const char*) { return QNoDebug(); } #else -QDebug CreateLoggerInfo(int line, const char* pretty_function) { - return qCreateLogger(line, pretty_function, Info); +QDebug CreateLoggerInfo(int line, const char* pretty_function, + const char* category) { + return qCreateLogger(line, pretty_function, category, Info); } -QDebug CreateLoggerDebug(int line, const char* pretty_function) { - return qCreateLogger(line, pretty_function, Debug); +QDebug CreateLoggerDebug(int line, const char* pretty_function, + const char* category) { + return qCreateLogger(line, pretty_function, category, Debug); } -#endif // QT_NO_DEBUG_OUTPUT +#endif // QT_NO_DEBUG_OUTPUT } // namespace logging diff --git a/ext/libclementine-common/core/logging.h b/ext/libclementine-common/core/logging.h index a68f141de..ec075f2cf 100644 --- a/ext/libclementine-common/core/logging.h +++ b/ext/libclementine-common/core/logging.h @@ -29,16 +29,27 @@ #ifdef QT_NO_DEBUG_STREAM #define qLog(level) \ while (false) QNoDebug() + +#define qLogCat(level, category) \ + while (false) QNoDebug() #else #define qLog(level) \ - logging::CreateLogger##level(__LINE__, __PRETTY_FUNCTION__) + logging::CreateLogger##level(__LINE__, __PRETTY_FUNCTION__, nullptr) -#define qCreateLogger(line, pretty_function, level) \ - logging::CreateLogger(logging::Level_##level, \ - logging::ParsePrettyFunction(pretty_function), line) +// This macro specifies a separate category for message filtering. The default +// qLog will use the class name extracted from the function name for this +// purpose. The category is also printed in the message along with the class +// name. +#define qLogCat(level, category) \ + logging::CreateLogger##level(__LINE__, __PRETTY_FUNCTION__, category) -#endif // QT_NO_DEBUG_STREAM +#define qCreateLogger(line, pretty_function, category, level) \ + logging::CreateLogger(logging::Level_##level, \ + logging::ParsePrettyFunction(pretty_function), line, \ + category) + +#endif // QT_NO_DEBUG_STREAM namespace logging { class NullDevice : public QIODevice { @@ -61,30 +72,35 @@ void SetLevels(const QString& levels); void DumpStackTrace(); QString ParsePrettyFunction(const char* pretty_function); -QDebug CreateLogger(Level level, const QString& class_name, int line); +QDebug CreateLogger(Level level, const QString& class_name, int line, + const char* category); -QDebug CreateLoggerFatal(int line, const char* pretty_function); -QDebug CreateLoggerError(int line, const char* pretty_function); +QDebug CreateLoggerFatal(int line, const char* pretty_function, + const char* category); +QDebug CreateLoggerError(int line, const char* pretty_function, + const char* category); #ifdef QT_NO_WARNING_OUTPUT -QNoDebug CreateLoggerWarning(int, const char*); +QNoDebug CreateLoggerWarning(int, const char*, const char*); #else -QDebug CreateLoggerWarning(int line, const char* pretty_function); -#endif // QT_NO_WARNING_OUTPUT +QDebug CreateLoggerWarning(int line, const char* pretty_function, + const char* category); +#endif // QT_NO_WARNING_OUTPUT #ifdef QT_NO_DEBUG_OUTPUT -QNoDebug CreateLoggerInfo(int, const char*); -QNoDebug CreateLoggerDebug(int, const char*); +QNoDebug CreateLoggerInfo(int, const char*, const char*); +QNoDebug CreateLoggerDebug(int, const char*, const char*); #else -QDebug CreateLoggerInfo(int line, const char* pretty_function); -QDebug CreateLoggerDebug(int line, const char* pretty_function); -#endif // QT_NO_DEBUG_OUTPUT - +QDebug CreateLoggerInfo(int line, const char* pretty_function, + const char* category); +QDebug CreateLoggerDebug(int line, const char* pretty_function, + const char* category); +#endif // QT_NO_DEBUG_OUTPUT void GLog(const char* domain, int level, const char* message, void* user_data); extern const char* kDefaultLogLevels; -} +} // namespace logging QDebug operator<<(QDebug debug, std::chrono::seconds secs); From 4b37e0c5779c595bfc98b6d5b13cf12a2245376e Mon Sep 17 00:00:00 2001 From: Jim Broadus Date: Wed, 29 Jan 2020 21:39:11 -0800 Subject: [PATCH 2/2] Use qLogCat for GLog handler. Provide the domain string from the GLog callback as the category. --- ext/libclementine-common/core/logging.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ext/libclementine-common/core/logging.cpp b/ext/libclementine-common/core/logging.cpp index 42105151e..2dc894bdb 100644 --- a/ext/libclementine-common/core/logging.cpp +++ b/ext/libclementine-common/core/logging.cpp @@ -54,18 +54,18 @@ void GLog(const char* domain, int level, const char* message, void* user_data) { case G_LOG_FLAG_FATAL: case G_LOG_LEVEL_ERROR: case G_LOG_LEVEL_CRITICAL: - qLog(Error) << message; + qLogCat(Error, domain) << message; break; case G_LOG_LEVEL_WARNING: - qLog(Warning) << message; + qLogCat(Warning, domain) << message; break; case G_LOG_LEVEL_MESSAGE: case G_LOG_LEVEL_INFO: - qLog(Info) << message; + qLogCat(Info, domain) << message; break; case G_LOG_LEVEL_DEBUG: default: - qLog(Debug) << message; + qLogCat(Debug, domain) << message; break; } }