diff options
Diffstat (limited to 'src/corelib')
-rw-r--r-- | src/corelib/global/qglobal.cpp | 16 | ||||
-rw-r--r-- | src/corelib/global/qlogging.cpp | 163 | ||||
-rw-r--r-- | src/corelib/global/qlogging.h | 7 | ||||
-rw-r--r-- | src/corelib/io/qdebug.h | 2 | ||||
-rw-r--r-- | src/corelib/kernel/qcoreapplication_win.cpp | 17 |
5 files changed, 135 insertions, 70 deletions
diff --git a/src/corelib/global/qglobal.cpp b/src/corelib/global/qglobal.cpp index 74c65d2504..7036b72f08 100644 --- a/src/corelib/global/qglobal.cpp +++ b/src/corelib/global/qglobal.cpp @@ -440,7 +440,7 @@ QT_BEGIN_NAMESPACE Finally, the QtMsgType definition identifies the various messages that can be generated and sent to a Qt message handler; - QMessageHandler is a type definition for a pointer to a function with + QtMessageHandler is a type definition for a pointer to a function with the signature \c {void myMessageHandler(QtMsgType, const QMessageLogContext &, const char *)}. QMessageLogContext class contains the line, file, and function the @@ -476,7 +476,7 @@ QT_BEGIN_NAMESPACE accept a \l qreal value as their argument returning the value rounded up to the nearest integer and 64-bit integer respectively, the qInstallMessageHandler() function which installs the given - QMessageHandler, and the qVersion() function which returns the + QtMessageHandler, and the qVersion() function which returns the version number of Qt at run-time as a string. \section1 Macros @@ -694,7 +694,7 @@ QT_BEGIN_NAMESPACE \value QtSystemMsg - \sa QMessageHandler, qInstallMessageHandler() + \sa QtMessageHandler, qInstallMessageHandler() */ /*! \typedef QFunctionPointer @@ -3026,12 +3026,12 @@ bool QInternal::activateCallbacks(Callback cb, void **parameters) \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 7 - This typedef is deprecated, you should use QMessageHandler instead. - \sa QtMsgType, QMessageHandler, qInstallMsgHandler(), qInstallMessageHandler() + This typedef is deprecated, you should use QtMessageHandler instead. + \sa QtMsgType, QtMessageHandler, qInstallMsgHandler(), qInstallMessageHandler() */ /*! - \typedef QMessageHandler + \typedef QtMessageHandler \relates <QtGlobal> \since 5.0 @@ -3044,7 +3044,7 @@ bool QInternal::activateCallbacks(Callback cb, void **parameters) */ /*! - \fn QMessageHandler qInstallMessageHandler(QMessageHandler handler) + \fn QtMessageHandler qInstallMessageHandler(QtMessageHandler handler) \relates <QtGlobal> \since 5.0 @@ -3074,7 +3074,7 @@ bool QInternal::activateCallbacks(Callback cb, void **parameters) \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 23 - \sa QMessageHandler, QtMsgType, qDebug(), qWarning(), qCritical(), qFatal(), + \sa QtMessageHandler, QtMsgType, qDebug(), qWarning(), qCritical(), qFatal(), {Debugging Techniques} */ diff --git a/src/corelib/global/qlogging.cpp b/src/corelib/global/qlogging.cpp index afefb11aa1..1f5b121143 100644 --- a/src/corelib/global/qlogging.cpp +++ b/src/corelib/global/qlogging.cpp @@ -49,7 +49,7 @@ #include "qcoreapplication.h" #include "qthread.h" #endif -#ifdef Q_OS_WINCE +#ifdef Q_OS_WIN #include <qt_windows.h> #endif @@ -66,7 +66,7 @@ QT_BEGIN_NAMESPACE The class provides information about the source code location a qDebug(), qWarning(), qCritical() or qFatal() message was generated. - \sa QMessageLogger, QMessageHandler, qInstallMessageHandler() + \sa QMessageLogger, QtMessageHandler, qInstallMessageHandler() */ /*! @@ -92,6 +92,7 @@ QT_BEGIN_NAMESPACE \internal Uses a local buffer to output the message. Not locale safe + cuts off everything after character 255, but will work in out of memory situations. + Stop the execution afterwards. */ static void qEmergencyOut(QtMsgType msgType, const char *msg, va_list ap) { @@ -99,8 +100,33 @@ static void qEmergencyOut(QtMsgType msgType, const char *msg, va_list ap) emergency_buf[255] = '\0'; if (msg) qvsnprintf(emergency_buf, 255, msg, ap); - QMessageLogContext context; - qt_message_output(msgType, context, emergency_buf); + +#if defined(Q_OS_WIN) && defined(QT_BUILD_CORE_LIB) + OutputDebugStringA(emergency_buf); +#else + fprintf(stderr, "%s", emergency_buf); + fflush(stderr); +#endif + + if (msgType == QtFatalMsg + || (msgType == QtWarningMsg + && (!qgetenv("QT_FATAL_WARNINGS").isNull())) ) { +#if defined(Q_CC_MSVC) && defined(QT_DEBUG) && defined(_DEBUG) && defined(_CRT_ERROR) + // get the current report mode + int reportMode = _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_WNDW); + _CrtSetReportMode(_CRT_ERROR, reportMode); + int ret = _CrtDbgReport(_CRT_ERROR, __FILE__, __LINE__, QT_VERSION_STR, + msg); + if (ret == 1) + _CrtDbgBreak(); +#endif + +#if (defined(Q_OS_UNIX) || defined(Q_CC_MINGW)) + abort(); // trap; generates core dump +#else + exit(1); // goodbye cruel world +#endif + } } #endif @@ -116,10 +142,10 @@ static void qt_message(QtMsgType msgType, const QMessageLogContext &context, con return; } #endif - QByteArray buf; + QString buf; if (msg) { QT_TRY { - buf = QString().vsprintf(msg, ap).toLocal8Bit(); + buf = QString().vsprintf(msg, ap); } QT_CATCH(const std::bad_alloc &) { #if !defined(QT_NO_EXCEPTIONS) qEmergencyOut(msgType, msg, ap); @@ -128,7 +154,7 @@ static void qt_message(QtMsgType msgType, const QMessageLogContext &context, con #endif } } - qt_message_output(msgType, context, buf.constData()); + qt_message_output(msgType, context, buf); } #undef qDebug @@ -452,13 +478,13 @@ QMessagePattern::QMessagePattern() else { fprintf(stderr, "%s\n", QString::fromLatin1("QT_MESSAGE_PATTERN: Unknown placeholder %1\n" - ).arg(lexeme).toLocal8Bit().constData()); + ).arg(lexeme).toLatin1().constData()); fflush(stderr); tokens[i] = emptyTokenC; } } else { char *literal = new char[lexeme.size() + 1]; - strncpy(literal, lexeme.toLocal8Bit().constData(), lexeme.size()); + strncpy(literal, lexeme.toLatin1().constData(), lexeme.size()); literal[lexeme.size()] = '\0'; literalsVar.append(literal); tokens[i] = literal; @@ -484,16 +510,16 @@ Q_GLOBAL_STATIC(QMessagePattern, qMessagePattern) /*! \internal */ -Q_CORE_EXPORT QByteArray qMessageFormatString(QtMsgType type, const QMessageLogContext &context, - const char *str) +Q_CORE_EXPORT QString qMessageFormatString(QtMsgType type, const QMessageLogContext &context, + const QString &str) { - QByteArray message; + QString message; QMessagePattern *pattern = qMessagePattern(); if (!pattern) { // after destruction of static QMessagePattern instance message.append(str); - message.append('\n'); + message.append(QLatin1Char('\n')); return message; } @@ -503,57 +529,58 @@ Q_CORE_EXPORT QByteArray qMessageFormatString(QtMsgType type, const QMessageLogC if (token == messageTokenC) { message.append(str); } else if (token == categoryTokenC) { - message.append(context.category); + message.append(QLatin1String(context.category)); } else if (token == typeTokenC) { switch (type) { - case QtDebugMsg: message.append("debug"); break; - case QtWarningMsg: message.append("warning"); break; - case QtCriticalMsg:message.append("critical"); break; - case QtFatalMsg: message.append("fatal"); break; + case QtDebugMsg: message.append(QLatin1String("debug")); break; + case QtWarningMsg: message.append(QLatin1String("warning")); break; + case QtCriticalMsg:message.append(QLatin1String("critical")); break; + case QtFatalMsg: message.append(QLatin1String("fatal")); break; } } else if (token == fileTokenC) { if (context.file) - message.append(context.file); + message.append(QLatin1String(context.file)); else - message.append("unknown"); + message.append(QLatin1String("unknown")); } else if (token == lineTokenC) { - message.append(QByteArray::number(context.line)); + message.append(QString::number(context.line)); } else if (token == functionTokenC) { if (context.function) - message.append(qCleanupFuncinfo(context.function)); + message.append(QString::fromLatin1(qCleanupFuncinfo(context.function))); else - message.append("unknown"); + message.append(QLatin1String("unknown")); #ifndef QT_BOOTSTRAPPED } else if (token == pidTokenC) { - message.append(QByteArray::number(QCoreApplication::applicationPid())); + message.append(QString::number(QCoreApplication::applicationPid())); } else if (token == appnameTokenC) { - message.append(QCoreApplication::applicationName().toUtf8().constData()); + message.append(QCoreApplication::applicationName()); } else if (token == threadidTokenC) { - message.append("0x" + QByteArray::number(qlonglong(QThread::currentThread()->currentThread()), 16)); + message.append(QLatin1String("0x")); + message.append(QString::number(qlonglong(QThread::currentThread()->currentThread()), 16)); #endif } else { - message.append(token); + message.append(QLatin1String(token)); } } - message.append('\n'); + message.append(QLatin1Char('\n')); return message; } static QtMsgHandler msgHandler = 0; // pointer to debug handler (without context) -static QMessageHandler messageHandler = 0; // pointer to debug handler (with context) +static QtMessageHandler messageHandler = 0; // pointer to debug handler (with context) +static QMessageHandler messageHandler2 = 0; // TODO: Remove before Qt5.0 beta /*! \internal */ static void qDefaultMessageHandler(QtMsgType type, const QMessageLogContext &context, - const char *buf) + const QString &buf) { - QByteArray logMessage = qMessageFormatString(type, context, buf); + QString logMessage = qMessageFormatString(type, context, buf); #if defined(Q_OS_WINCE) - QString fstr = QString::fromLocal8Bit(logMessage); - OutputDebugString(reinterpret_cast<const wchar_t *> (fstr.utf16())); + OutputDebugString(reinterpret_cast<const wchar_t *> (logMessage.utf16())); #else - fprintf(stderr, "%s", logMessage.constData()); + fprintf(stderr, "%s", logMessage.toLocal8Bit().constData()); fflush(stderr); #endif } @@ -561,28 +588,43 @@ static void qDefaultMessageHandler(QtMsgType type, const QMessageLogContext &con /*! \internal */ +static void qDefaultMessageHandler2(QtMsgType type, const QMessageLogContext &context, + const char *buf) +{ + qDefaultMessageHandler(type, context, QString::fromLocal8Bit(buf)); +} + +/*! + \internal +*/ static void qDefaultMsgHandler(QtMsgType type, const char *buf) { QMessageLogContext emptyContext; - qDefaultMessageHandler(type, emptyContext, buf); + qDefaultMessageHandler(type, emptyContext, QLatin1String(buf)); } /*! \internal */ -void qt_message_output(QtMsgType msgType, const QMessageLogContext &context, const char *buf) +void qt_message_output(QtMsgType msgType, const QMessageLogContext &context, const QString &message) { if (!msgHandler) msgHandler = qDefaultMsgHandler; if (!messageHandler) messageHandler = qDefaultMessageHandler; + if (!messageHandler2) + messageHandler2 = qDefaultMessageHandler2; + + if (messageHandler == qDefaultMessageHandler + && messageHandler2 != qDefaultMessageHandler2) + (*messageHandler2)(msgType, context, message.toLocal8Bit().constData()); // prefer new message handler over the old one if (msgHandler == qDefaultMsgHandler || messageHandler != qDefaultMessageHandler) { - (*messageHandler)(msgType, context, buf); + (*messageHandler)(msgType, context, message); } else { - (*msgHandler)(msgType, buf); + (*msgHandler)(msgType, message.toLocal8Bit().constData()); } if (msgType == QtFatalMsg @@ -593,14 +635,12 @@ void qt_message_output(QtMsgType msgType, const QMessageLogContext &context, con // get the current report mode int reportMode = _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_WNDW); _CrtSetReportMode(_CRT_ERROR, reportMode); -#if !defined(Q_OS_WINCE) - int ret = _CrtDbgReport(_CRT_ERROR, context.file, context.line, QT_VERSION_STR, buf); -#else - int ret = _CrtDbgReportW(_CRT_ERROR, _CRT_WIDE(context.file), + + int ret = _CrtDbgReportW(_CRT_ERROR, reinterpret_cast<const wchar_t *> ( + QString::fromLatin1(context.file).utf16()), context.line, _CRT_WIDE(QT_VERSION_STR), reinterpret_cast<const wchar_t *> ( - QString::fromLatin1(buf).utf16())); -#endif + message.utf16())); if (ret == 0 && reportMode & _CRTDBG_MODE_WNDW) return; // ignore else if (ret == 1) @@ -626,8 +666,9 @@ void qErrnoWarning(const char *msg, ...) buf.vsprintf(msg, ap); va_end(ap); - QMessageLogger().critical("%s (%s)", buf.toLocal8Bit().constData(), - qt_error_string(-1).toLocal8Bit().constData()); + buf += QLatin1String(" (") + qt_error_string(-1) + QLatin1Char(')'); + QMessageLogContext context; + qt_message_output(QtCriticalMsg, context, buf); } void qErrnoWarning(int code, const char *msg, ...) @@ -641,22 +682,29 @@ void qErrnoWarning(int code, const char *msg, ...) buf.vsprintf(msg, ap); va_end(ap); - QMessageLogger().critical("%s (%s)", buf.toLocal8Bit().constData(), - qt_error_string(code).toLocal8Bit().constData()); + buf += QLatin1String(" (") + qt_error_string(code) + QLatin1Char(')'); + QMessageLogContext context; + qt_message_output(QtCriticalMsg, context, buf); } #if defined(Q_OS_WIN) && defined(QT_BUILD_CORE_LIB) extern bool usingWinMain; extern Q_CORE_EXPORT void qWinMsgHandler(QtMsgType t, const char *str); extern Q_CORE_EXPORT void qWinMessageHandler(QtMsgType t, const QMessageLogContext &context, - const char *str); + const QString &str); + +void qWinMessageHandler2(QtMsgType t, const QMessageLogContext &context, + const char *str) +{ + qWinMessageHandler(t, context, QString::fromLocal8Bit(str)); +} #endif -QMessageHandler qInstallMessageHandler(QMessageHandler h) +QtMessageHandler qInstallMessageHandler(QtMessageHandler h) { if (!messageHandler) messageHandler = qDefaultMessageHandler; - QMessageHandler old = messageHandler; + QtMessageHandler old = messageHandler; messageHandler = h; #if defined(Q_OS_WIN) && defined(QT_BUILD_CORE_LIB) if (!messageHandler && usingWinMain) @@ -665,6 +713,19 @@ QMessageHandler qInstallMessageHandler(QMessageHandler h) return old; } +QMessageHandler qInstallMessageHandler(QMessageHandler h) +{ + if (!messageHandler2) + messageHandler2 = qDefaultMessageHandler2; + QMessageHandler old = messageHandler2; + messageHandler2 = h; +#if defined(Q_OS_WIN) && defined(QT_BUILD_CORE_LIB) + if (!messageHandler2 && usingWinMain) + messageHandler2 = qWinMessageHandler2; +#endif + return old; +} + QtMsgHandler qInstallMsgHandler(QtMsgHandler h) { //if handler is 0, set it to the diff --git a/src/corelib/global/qlogging.h b/src/corelib/global/qlogging.h index 29313bd582..216b847952 100644 --- a/src/corelib/global/qlogging.h +++ b/src/corelib/global/qlogging.h @@ -151,7 +151,8 @@ private: # define qWarning QT_NO_QWARNING_MACRO #endif -Q_CORE_EXPORT void qt_message_output(QtMsgType, const QMessageLogContext &context, const char *buf); +Q_CORE_EXPORT void qt_message_output(QtMsgType, const QMessageLogContext &context, + const QString &message); Q_CORE_EXPORT void qErrnoWarning(int code, const char *msg, ...); Q_CORE_EXPORT void qErrnoWarning(const char *msg, ...); @@ -160,6 +161,10 @@ Q_CORE_EXPORT void qErrnoWarning(const char *msg, ...); typedef void (*QtMsgHandler)(QtMsgType, const char *); Q_CORE_EXPORT QtMsgHandler qInstallMsgHandler(QtMsgHandler); +typedef void (*QtMessageHandler)(QtMsgType, const QMessageLogContext &, const QString &); +Q_CORE_EXPORT QtMessageHandler qInstallMessageHandler(QtMessageHandler); + +// TODO: Remove before Qt5.0 Beta typedef void (*QMessageHandler)(QtMsgType, const QMessageLogContext &, const char *); Q_CORE_EXPORT QMessageHandler qInstallMessageHandler(QMessageHandler); diff --git a/src/corelib/io/qdebug.h b/src/corelib/io/qdebug.h index 48a382eea6..34abb5b75b 100644 --- a/src/corelib/io/qdebug.h +++ b/src/corelib/io/qdebug.h @@ -85,7 +85,7 @@ public: QT_TRY { qt_message_output(stream->type, stream->context, - stream->buffer.toLocal8Bit().data()); + stream->buffer); } QT_CATCH(std::bad_alloc&) { /* We're out of memory - give up. */ } } delete stream; diff --git a/src/corelib/kernel/qcoreapplication_win.cpp b/src/corelib/kernel/qcoreapplication_win.cpp index 7cc1f0e286..5649a8dd76 100644 --- a/src/corelib/kernel/qcoreapplication_win.cpp +++ b/src/corelib/kernel/qcoreapplication_win.cpp @@ -141,29 +141,28 @@ public: }; // defined in qlogging.cpp -extern Q_CORE_EXPORT QByteArray qMessageFormatString(QtMsgType type, - const QMessageLogContext &context, - const char *str); +extern Q_CORE_EXPORT QString qMessageFormatString(QtMsgType type, + const QMessageLogContext &context, + const QString &str); -Q_CORE_EXPORT void qWinMessageHandler(QtMsgType t, const QMessageLogContext &context, const char *str) +Q_CORE_EXPORT void qWinMessageHandler(QtMsgType t, const QMessageLogContext &context, const QString &str) { // cannot use QMutex here, because qWarning()s in the QMutex // implementation may cause this function to recurse static QWinMsgHandlerCriticalSection staticCriticalSection; - QByteArray message = qMessageFormatString(t, context, str); - QString s(QString::fromLocal8Bit(message)); + QString message = qMessageFormatString(t, context, str); // OutputDebugString is not threadsafe. staticCriticalSection.lock(); - OutputDebugString((wchar_t*)s.utf16()); + OutputDebugString((wchar_t*)message.utf16()); staticCriticalSection.unlock(); } Q_CORE_EXPORT void qWinMsgHandler(QtMsgType t, const char *str) { QMessageLogContext emptyContext; - qWinMessageHandler(t, emptyContext, str); + qWinMessageHandler(t, emptyContext, QString::fromLocal8Bit(str)); } /***************************************************************************** @@ -189,7 +188,7 @@ void qWinMain(HINSTANCE instance, HINSTANCE prevInstance, LPSTR cmdParam, usingWinMain = true; // Install default debug handler - qInstallMsgHandler(qWinMsgHandler); + qInstallMessageHandler(qWinMessageHandler); // Create command line argv = qWinCmdLine<char>(cmdParam, int(strlen(cmdParam)), argc); |