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_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;
}
}
@ -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

View File

@ -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);