diff options
-rw-r--r-- | src/corelib/global/qlogging.cpp | 95 | ||||
-rw-r--r-- | src/corelib/global/qlogging.h | 26 | ||||
-rw-r--r-- | src/corelib/io/qloggingcategory.h | 1 | ||||
-rw-r--r-- | tests/auto/corelib/io/qloggingcategory/tst_qloggingcategory.cpp | 17 |
4 files changed, 133 insertions, 6 deletions
diff --git a/src/corelib/global/qlogging.cpp b/src/corelib/global/qlogging.cpp index 1ce55c3f14..213273ed67 100644 --- a/src/corelib/global/qlogging.cpp +++ b/src/corelib/global/qlogging.cpp @@ -862,6 +862,56 @@ QDebug QMessageLogger::critical(QMessageLogger::CategoryFunction catFunc) const #endif #undef qFatal + +/*! + Logs a fatal message specified with format \a msg for the context \a cat. + Additional parameters, specified by \a msg, may be used. + + \since 6.5 + \sa qCFatal() +*/ +void QMessageLogger::fatal(const QLoggingCategory &cat, const char *msg, ...) const noexcept +{ + QMessageLogContext ctxt; + ctxt.copyContextFrom(context); + ctxt.category = cat.categoryName(); + + QString message; + + va_list ap; + va_start(ap, msg); // use variable arg list + QT_TERMINATE_ON_EXCEPTION(message = qt_message(QtFatalMsg, ctxt, msg, ap)); + va_end(ap); + + qt_message_fatal(QtCriticalMsg, ctxt, message); +} + +/*! + Logs a fatal message specified with format \a msg for the context returned + by \a catFunc. Additional parameters, specified by \a msg, may be used. + + \since 6.5 + \sa qCFatal() +*/ +void QMessageLogger::fatal(QMessageLogger::CategoryFunction catFunc, + const char *msg, ...) const noexcept +{ + const QLoggingCategory &cat = (*catFunc)(); + + QMessageLogContext ctxt; + ctxt.copyContextFrom(context); + ctxt.category = cat.categoryName(); + + QString message; + + va_list ap; + va_start(ap, msg); // use variable arg list + QT_TERMINATE_ON_EXCEPTION(message = qt_message(QtFatalMsg, ctxt, msg, ap)); + va_end(ap); + + qt_message_fatal(QtFatalMsg, ctxt, message); +} + /*! Logs a fatal message specified with format \a msg. Additional parameters, specified by \a msg, may be used. @@ -880,6 +930,51 @@ void QMessageLogger::fatal(const char *msg, ...) const noexcept qt_message_fatal(QtFatalMsg, context, message); } +#ifndef QT_NO_DEBUG_STREAM +/*! + Logs a fatal message using a QDebug stream. + + \since 6.5 + + \sa qFatal(), QDebug +*/ +QDebug QMessageLogger::fatal() const +{ + QDebug dbg = QDebug(QtFatalMsg); + QMessageLogContext &ctxt = dbg.stream->context; + ctxt.copyContextFrom(context); + return dbg; +} + +/*! + Logs a fatal message into category \a cat using a QDebug stream. + + \since 6.5 + \sa qCFatal(), QDebug +*/ +QDebug QMessageLogger::fatal(const QLoggingCategory &cat) const +{ + QDebug dbg = QDebug(QtFatalMsg); + + QMessageLogContext &ctxt = dbg.stream->context; + ctxt.copyContextFrom(context); + ctxt.category = cat.categoryName(); + + return dbg; +} + +/*! + Logs a fatal message into category returned by \a catFunc using a QDebug stream. + + \since 6.5 + \sa qCFatal(), QDebug +*/ +QDebug QMessageLogger::fatal(QMessageLogger::CategoryFunction catFunc) const +{ + return fatal((*catFunc)()); +} +#endif // QT_NO_DEBUG_STREAM + /*! \internal */ diff --git a/src/corelib/global/qlogging.h b/src/corelib/global/qlogging.h index 5111daefd3..ea1c68b168 100644 --- a/src/corelib/global/qlogging.h +++ b/src/corelib/global/qlogging.h @@ -54,6 +54,12 @@ private: class QLoggingCategory; +#ifdef Q_CC_MSVC +# define QT_MESSAGE_LOGGER_NORETURN +#else +# define QT_MESSAGE_LOGGER_NORETURN Q_NORETURN +#endif + class Q_CORE_EXPORT QMessageLogger { Q_DISABLE_COPY(QMessageLogger) @@ -72,6 +78,8 @@ public: void warning(const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(2, 3); Q_DECL_COLD_FUNCTION void critical(const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(2, 3); + QT_MESSAGE_LOGGER_NORETURN Q_DECL_COLD_FUNCTION + void fatal(const char *msg, ...) const noexcept Q_ATTRIBUTE_FORMAT_PRINTF(2, 3); typedef const QLoggingCategory &(*CategoryFunction)(); @@ -87,12 +95,10 @@ public: void critical(const QLoggingCategory &cat, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4); Q_DECL_COLD_FUNCTION void critical(CategoryFunction catFunc, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4); - -#ifndef Q_CC_MSVC - Q_NORETURN -#endif - Q_DECL_COLD_FUNCTION - void fatal(const char *msg, ...) const noexcept Q_ATTRIBUTE_FORMAT_PRINTF(2, 3); + QT_MESSAGE_LOGGER_NORETURN Q_DECL_COLD_FUNCTION + void fatal(const QLoggingCategory &cat, const char *msg, ...) const noexcept Q_ATTRIBUTE_FORMAT_PRINTF(3, 4); + QT_MESSAGE_LOGGER_NORETURN Q_DECL_COLD_FUNCTION + void fatal(CategoryFunction catFunc, const char *msg, ...) const noexcept Q_ATTRIBUTE_FORMAT_PRINTF(3, 4); #ifndef QT_NO_DEBUG_STREAM QDebug debug() const; @@ -113,6 +119,12 @@ public: QDebug critical(const QLoggingCategory &cat) const; Q_DECL_COLD_FUNCTION QDebug critical(CategoryFunction catFunc) const; + Q_DECL_COLD_FUNCTION + QDebug fatal() const; + Q_DECL_COLD_FUNCTION + QDebug fatal(const QLoggingCategory &cat) const; + Q_DECL_COLD_FUNCTION + QDebug fatal(CategoryFunction catFunc) const; QNoDebug noDebug() const noexcept; #endif // QT_NO_DEBUG_STREAM @@ -121,6 +133,8 @@ private: QMessageLogContext context; }; +#undef QT_MESSAGE_LOGGER_NORETURN + #if !defined(QT_MESSAGELOGCONTEXT) && !defined(QT_NO_MESSAGELOGCONTEXT) # if defined(QT_NO_DEBUG) # define QT_NO_MESSAGELOGCONTEXT diff --git a/src/corelib/io/qloggingcategory.h b/src/corelib/io/qloggingcategory.h index f990d8d0c4..c9b32a7227 100644 --- a/src/corelib/io/qloggingcategory.h +++ b/src/corelib/io/qloggingcategory.h @@ -125,6 +125,7 @@ template <> const bool QLoggingCategoryMacroHolder<QtWarningMsg>::IsOutputEnable #define qCInfo(category, ...) QT_MESSAGE_LOGGER_COMMON(category, QtInfoMsg).info(__VA_ARGS__) #define qCWarning(category, ...) QT_MESSAGE_LOGGER_COMMON(category, QtWarningMsg).warning(__VA_ARGS__) #define qCCritical(category, ...) QT_MESSAGE_LOGGER_COMMON(category, QtCriticalMsg).critical(__VA_ARGS__) +#define qCFatal(category, ...) QT_MESSAGE_LOGGER_COMMON(category, QtFatalMsg).fatal(__VA_ARGS__) QT_END_NAMESPACE diff --git a/tests/auto/corelib/io/qloggingcategory/tst_qloggingcategory.cpp b/tests/auto/corelib/io/qloggingcategory/tst_qloggingcategory.cpp index d2bfdb54ca..af4fc00cbe 100644 --- a/tests/auto/corelib/io/qloggingcategory/tst_qloggingcategory.cpp +++ b/tests/auto/corelib/io/qloggingcategory/tst_qloggingcategory.cpp @@ -916,6 +916,23 @@ private slots: delete _config; qInstallMessageHandler(oldMessageHandler); } + + void qFatalMacros() + { + QLoggingCategory customCategory("custom"); + + // compile-only test for fatal macros + return; + + qFatal("Message"); + qFatal("Message %d", 42); + qFatal(customCategory, "Message %d", 42); + qFatal(TST_LOG, "Message %d", 42); + + qFatal() << "Message" << 42; + qCFatal(customCategory) << "Message" << 42; + qCFatal(TST_LOG) << "Message" << 42; + } }; QTEST_MAIN(tst_QLogging) |