Use a template for CreateLogger to allow different QDebug derivatives.
This commit is contained in:
parent
4fe845c2c5
commit
09723d516c
@ -51,7 +51,8 @@ static const char* kMessageHandlerMagic = "__logging_message__";
|
|||||||
static const int kMessageHandlerMagicLength = strlen(kMessageHandlerMagic);
|
static const int kMessageHandlerMagicLength = strlen(kMessageHandlerMagic);
|
||||||
static QtMessageHandler sOriginalMessageHandler = nullptr;
|
static QtMessageHandler sOriginalMessageHandler = nullptr;
|
||||||
|
|
||||||
static QDebug CreateLogger(Level level, const QString& class_name, int line,
|
template <class T>
|
||||||
|
static T CreateLogger(Level level, const QString& class_name, int line,
|
||||||
const char* category);
|
const char* category);
|
||||||
|
|
||||||
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) {
|
||||||
@ -98,7 +99,7 @@ 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, nullptr)
|
CreateLogger<QDebug>(level, "unknown", -1, nullptr)
|
||||||
<< line.toLocal8Bit().constData();
|
<< line.toLocal8Bit().constData();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -171,7 +172,8 @@ static QString ParsePrettyFunction(const char* pretty_function) {
|
|||||||
return class_name;
|
return class_name;
|
||||||
}
|
}
|
||||||
|
|
||||||
static QDebug CreateLogger(Level level, const QString& class_name, int line,
|
template <class T>
|
||||||
|
static T CreateLogger(Level level, const QString& class_name, int line,
|
||||||
const char* category) {
|
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;
|
||||||
@ -201,7 +203,7 @@ static QDebug CreateLogger(Level level, const QString& class_name, int line,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (level > threshold_level) {
|
if (level > threshold_level) {
|
||||||
return QDebug(sNullDevice);
|
return T(sNullDevice);
|
||||||
}
|
}
|
||||||
|
|
||||||
QString function_line = class_name;
|
QString function_line = class_name;
|
||||||
@ -217,7 +219,7 @@ static QDebug CreateLogger(Level level, const QString& class_name, int line,
|
|||||||
type = QtFatalMsg;
|
type = QtFatalMsg;
|
||||||
}
|
}
|
||||||
|
|
||||||
QDebug ret(type);
|
T ret(type);
|
||||||
ret.nospace() << kMessageHandlerMagic
|
ret.nospace() << kMessageHandlerMagic
|
||||||
<< QDateTime::currentDateTime()
|
<< QDateTime::currentDateTime()
|
||||||
.toString("hh:mm:ss.zzz")
|
.toString("hh:mm:ss.zzz")
|
||||||
@ -284,9 +286,9 @@ void DumpStackTrace() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#define qCreateLogger(line, pretty_function, category, level) \
|
#define qCreateLogger(line, pretty_function, category, level) \
|
||||||
logging::CreateLogger(logging::Level_##level, \
|
logging::CreateLogger<QDebug>(logging::Level_##level, \
|
||||||
logging::ParsePrettyFunction(pretty_function), line, \
|
logging::ParsePrettyFunction(pretty_function), \
|
||||||
category)
|
line, category)
|
||||||
|
|
||||||
QDebug CreateLoggerFatal(int line, const char* pretty_function,
|
QDebug CreateLoggerFatal(int line, const char* pretty_function,
|
||||||
const char* category) {
|
const char* category) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user