Merge pull request #6556 from jbroadus/category-for-glog-messages

Category for glog messages
This commit is contained in:
John Maguire 2020-01-30 20:10:36 +00:00 committed by GitHub
commit c34f9339ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 71 additions and 42 deletions

View File

@ -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_FLAG_FATAL:
case G_LOG_LEVEL_ERROR: case G_LOG_LEVEL_ERROR:
case G_LOG_LEVEL_CRITICAL: case G_LOG_LEVEL_CRITICAL:
qLog(Error) << message; qLogCat(Error, domain) << message;
break; break;
case G_LOG_LEVEL_WARNING: case G_LOG_LEVEL_WARNING:
qLog(Warning) << message; qLogCat(Warning, domain) << message;
break; break;
case G_LOG_LEVEL_MESSAGE: case G_LOG_LEVEL_MESSAGE:
case G_LOG_LEVEL_INFO: case G_LOG_LEVEL_INFO:
qLog(Info) << message; qLogCat(Info, domain) << message;
break; break;
case G_LOG_LEVEL_DEBUG: case G_LOG_LEVEL_DEBUG:
default: default:
qLog(Debug) << message; qLogCat(Debug, domain) << message;
break; break;
} }
} }
@ -92,7 +92,8 @@ static void MessageHandler(QtMsgType type, const QMessageLogContext &context, co
} }
for (const QString& line : message.split('\n')) { 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) { if (type == QtFatalMsg) {
@ -164,7 +165,8 @@ QString ParsePrettyFunction(const char* pretty_function) {
return class_name; 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 // Map the level to a string
const char* level_name = nullptr; const char* level_name = nullptr;
switch (level) { switch (level) {
@ -185,10 +187,11 @@ QDebug CreateLogger(Level level, const QString& class_name, int line) {
break; break;
} }
QString filter_category = (category != nullptr) ? category : class_name;
// Check the settings to see if we're meant to show or hide this message. // Check the settings to see if we're meant to show or hide this message.
Level threshold_level = sDefaultLevel; Level threshold_level = sDefaultLevel;
if (sClassLevels && sClassLevels->contains(class_name)) { if (sClassLevels && sClassLevels->contains(filter_category)) {
threshold_level = sClassLevels->value(class_name); threshold_level = sClassLevels->value(filter_category);
} }
if (level > threshold_level) { if (level > threshold_level) {
@ -199,6 +202,9 @@ QDebug CreateLogger(Level level, const QString& class_name, int line) {
if (line != -1) { if (line != -1) {
function_line += ":" + QString::number(line); function_line += ":" + QString::number(line);
} }
if (category) {
function_line += "(" + QString(category) + ")";
}
QtMsgType type = QtDebugMsg; QtMsgType type = QtDebugMsg;
if (level == Level_Fatal) { if (level == Level_Fatal) {
@ -271,32 +277,39 @@ void DumpStackTrace() {
#endif #endif
} }
QDebug CreateLoggerFatal(int line, const char* pretty_function) { QDebug CreateLoggerFatal(int line, const char* pretty_function,
return qCreateLogger(line, pretty_function, Fatal); const char* category) {
return qCreateLogger(line, pretty_function, category, Fatal);
} }
QDebug CreateLoggerError(int line, const char* pretty_function) { QDebug CreateLoggerError(int line, const char* pretty_function,
return qCreateLogger(line, pretty_function, Error); const char* category) {
return qCreateLogger(line, pretty_function, category, Error);
} }
#ifdef QT_NO_WARNING_OUTPUT #ifdef QT_NO_WARNING_OUTPUT
QNoDebug CreateLoggerWarning(int, const char*) { return QNoDebug(); } QNoDebug CreateLoggerWarning(int, const char*, const char*) {
#else return QNoDebug();
QDebug CreateLoggerWarning(int line, const char* pretty_function) {
return qCreateLogger(line, pretty_function, Warning);
} }
#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 #ifdef QT_NO_DEBUG_OUTPUT
QNoDebug CreateLoggerInfo(int, const char*) { return QNoDebug(); } QNoDebug CreateLoggerInfo(int, const char*, const char*) { return QNoDebug(); }
QNoDebug CreateLoggerDebug(int, const char*) { return QNoDebug(); } QNoDebug CreateLoggerDebug(int, const char*, const char*) { return QNoDebug(); }
#else #else
QDebug CreateLoggerInfo(int line, const char* pretty_function) { QDebug CreateLoggerInfo(int line, const char* pretty_function,
return qCreateLogger(line, pretty_function, Info); const char* category) {
return qCreateLogger(line, pretty_function, category, Info);
} }
QDebug CreateLoggerDebug(int line, const char* pretty_function) { QDebug CreateLoggerDebug(int line, const char* pretty_function,
return qCreateLogger(line, pretty_function, Debug); const char* category) {
return qCreateLogger(line, pretty_function, category, Debug);
} }
#endif // QT_NO_DEBUG_OUTPUT #endif // QT_NO_DEBUG_OUTPUT
} // namespace logging } // namespace logging

View File

@ -29,16 +29,27 @@
#ifdef QT_NO_DEBUG_STREAM #ifdef QT_NO_DEBUG_STREAM
#define qLog(level) \ #define qLog(level) \
while (false) QNoDebug() while (false) QNoDebug()
#define qLogCat(level, category) \
while (false) QNoDebug()
#else #else
#define qLog(level) \ #define qLog(level) \
logging::CreateLogger##level(__LINE__, __PRETTY_FUNCTION__) logging::CreateLogger##level(__LINE__, __PRETTY_FUNCTION__, nullptr)
#define qCreateLogger(line, pretty_function, level) \ // This macro specifies a separate category for message filtering. The default
logging::CreateLogger(logging::Level_##level, \ // qLog will use the class name extracted from the function name for this
logging::ParsePrettyFunction(pretty_function), line) // 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 { namespace logging {
class NullDevice : public QIODevice { class NullDevice : public QIODevice {
@ -61,30 +72,35 @@ void SetLevels(const QString& levels);
void DumpStackTrace(); void DumpStackTrace();
QString ParsePrettyFunction(const char* pretty_function); 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 CreateLoggerFatal(int line, const char* pretty_function,
QDebug CreateLoggerError(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 #ifdef QT_NO_WARNING_OUTPUT
QNoDebug CreateLoggerWarning(int, const char*); QNoDebug CreateLoggerWarning(int, const char*, const char*);
#else #else
QDebug CreateLoggerWarning(int line, const char* pretty_function); QDebug CreateLoggerWarning(int line, const char* pretty_function,
#endif // QT_NO_WARNING_OUTPUT const char* category);
#endif // QT_NO_WARNING_OUTPUT
#ifdef QT_NO_DEBUG_OUTPUT #ifdef QT_NO_DEBUG_OUTPUT
QNoDebug CreateLoggerInfo(int, const char*); QNoDebug CreateLoggerInfo(int, const char*, const char*);
QNoDebug CreateLoggerDebug(int, const char*); QNoDebug CreateLoggerDebug(int, const char*, const char*);
#else #else
QDebug CreateLoggerInfo(int line, const char* pretty_function); QDebug CreateLoggerInfo(int line, const char* pretty_function,
QDebug CreateLoggerDebug(int line, const char* pretty_function); const char* category);
#endif // QT_NO_DEBUG_OUTPUT 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); void GLog(const char* domain, int level, const char* message, void* user_data);
extern const char* kDefaultLogLevels; extern const char* kDefaultLogLevels;
} } // namespace logging
QDebug operator<<(QDebug debug, std::chrono::seconds secs); QDebug operator<<(QDebug debug, std::chrono::seconds secs);