diff options
Diffstat (limited to 'src/corelib/global/qlogging.cpp')
-rw-r--r-- | src/corelib/global/qlogging.cpp | 111 |
1 files changed, 33 insertions, 78 deletions
diff --git a/src/corelib/global/qlogging.cpp b/src/corelib/global/qlogging.cpp index ca0fb1bb23..5b4726f67d 100644 --- a/src/corelib/global/qlogging.cpp +++ b/src/corelib/global/qlogging.cpp @@ -48,6 +48,8 @@ #include "qmutex.h" #include "qloggingcategory.h" #ifndef QT_BOOTSTRAPPED +#include "qelapsedtimer.h" +#include "qdatetime.h" #include "qcoreapplication.h" #include "qthread.h" #include "private/qloggingregistry_p.h" @@ -171,89 +173,15 @@ static inline void convert_to_wchar_t_elided(wchar_t *d, size_t space, const cha } #endif -#if !defined(QT_NO_EXCEPTIONS) -/*! - \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) Q_DECL_NOEXCEPT -{ - char emergency_buf[256] = { '\0' }; - emergency_buf[sizeof emergency_buf - 1] = '\0'; -#if defined(Q_OS_WIN) && defined(QT_BUILD_CORE_LIB) && (defined(Q_OS_WINCE) || defined(Q_OS_WINRT)) \ - || defined(Q_CC_MSVC) && defined(QT_DEBUG) && defined(_DEBUG) && defined(_CRT_ERROR) - wchar_t emergency_bufL[sizeof emergency_buf]; -#endif - - if (msg) - qvsnprintf(emergency_buf, sizeof emergency_buf - 1, msg, ap); - -#if defined(Q_OS_WIN) && defined(QT_BUILD_CORE_LIB) -# if defined(Q_OS_WINCE) || defined(Q_OS_WINRT) - convert_to_wchar_t_elided(emergency_bufL, sizeof emergency_buf, emergency_buf); - OutputDebugStringW(emergency_bufL); -# else - if (qWinLogToStderr()) { - fprintf(stderr, "%s\n", emergency_buf); - fflush(stderr); - } else { - OutputDebugStringA(emergency_buf); - } -# endif -#else - fprintf(stderr, "%s\n", emergency_buf); - fflush(stderr); -#endif - - if (isFatal(msgType)) { -#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); -# ifndef Q_OS_WINCE // otherwise already converted to wchar_t above - convert_to_wchar_t_elided(emergency_bufL, sizeof emergency_buf, emergency_buf); -# endif - int ret = _CrtDbgReportW(_CRT_ERROR, _CRT_WIDE(__FILE__), __LINE__, - _CRT_WIDE(QT_VERSION_STR), - emergency_bufL); - 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 - /*! \internal */ static void qt_message(QtMsgType msgType, const QMessageLogContext &context, const char *msg, va_list ap, QString &buf) { -#if !defined(QT_NO_EXCEPTIONS) - if (std::uncaught_exception()) { - qEmergencyOut(msgType, msg, ap); - return; - } -#endif - if (msg) { - QT_TRY { - buf = QString().vsprintf(msg, ap); - } QT_CATCH(const std::bad_alloc &) { -#if !defined(QT_NO_EXCEPTIONS) - qEmergencyOut(msgType, msg, ap); - // don't rethrow - we use qWarning and friends in destructors. - return; -#endif - } - } + + if (msg) + buf = QString().vsprintf(msg, ap); qt_message_print(msgType, context, buf); } @@ -834,6 +762,7 @@ static const char functionTokenC[] = "%{function}"; static const char pidTokenC[] = "%{pid}"; static const char appnameTokenC[] = "%{appname}"; static const char threadidTokenC[] = "%{threadid}"; +static const char timeTokenC[] = "%{time"; static const char ifCategoryTokenC[] = "%{if-category}"; static const char ifDebugTokenC[] = "%{if-debug}"; static const char ifWarningTokenC[] = "%{if-warning}"; @@ -854,9 +783,14 @@ struct QMessagePattern { // 0 terminated arrays of literal tokens / literal or placeholder tokens const char **literals; const char **tokens; + QString timeFormat; bool fromEnvironment; static QBasicMutex mutex; +#ifndef QT_BOOTSTRAPPED + QElapsedTimer timer; + QDateTime startTime; +#endif }; QBasicMutex QMessagePattern::mutex; @@ -865,7 +799,13 @@ QMessagePattern::QMessagePattern() : literals(0) , tokens(0) , fromEnvironment(false) +#ifndef QT_BOOTSTRAPPED + , startTime(QDateTime::currentDateTime()) +#endif { +#ifndef QT_BOOTSTRAPPED + timer.start(); +#endif const QString envPattern = QString::fromLocal8Bit(qgetenv("QT_MESSAGE_PATTERN")); if (envPattern.isEmpty()) { setPattern(QLatin1String(defaultPattern)); @@ -953,6 +893,12 @@ void QMessagePattern::setPattern(const QString &pattern) tokens[i] = appnameTokenC; else if (lexeme == QLatin1String(threadidTokenC)) tokens[i] = threadidTokenC; + else if (lexeme.startsWith(QLatin1String(timeTokenC))) { + tokens[i] = timeTokenC; + int spaceIdx = lexeme.indexOf(QChar::fromLatin1(' ')); + if (spaceIdx > 0) + timeFormat = lexeme.mid(spaceIdx + 1, lexeme.length() - spaceIdx - 2); + } #define IF_TOKEN(LEVEL) \ else if (lexeme == QLatin1String(LEVEL)) { \ @@ -1122,6 +1068,12 @@ Q_CORE_EXPORT QString qMessageFormatString(QtMsgType type, const QMessageLogCont } else if (token == threadidTokenC) { message.append(QLatin1String("0x")); message.append(QString::number(qlonglong(QThread::currentThread()->currentThread()), 16)); + } else if (token == timeTokenC) { + quint64 ms = pattern->timer.elapsed(); + if (pattern->timeFormat.isEmpty()) + message.append(QString().sprintf("%6d.%03d", uint(ms / 1000), uint(ms % 1000))); + else + message.append(pattern->startTime.addMSecs(ms).toString(pattern->timeFormat)); #endif } else if (token == ifCategoryTokenC) { if (!context.category || (strcmp(context.category, "default") == 0)) @@ -1503,6 +1455,9 @@ void qErrnoWarning(int code, const char *msg, ...) \row \li \c %{pid} \li QCoreApplication::applicationPid() \row \li \c %{threadid} \li ID of current thread \row \li \c %{type} \li "debug", "warning", "critical" or "fatal" + \row \li \c %{time} \li time of the message, in seconds since the process started + \row \li \c %{time format} \li system time when the message occurred, formatted by + passing the \c format to \l QDateTime::toString() \endtable You can also use conditionals on the type of the message using \c %{if-debug}, @@ -1514,7 +1469,7 @@ void qErrnoWarning(int code, const char *msg, ...) Example: \code - QT_MESSAGE_PATTERN="[%{if-debug}D%{endif}%{if-warning}W%{endif}%{if-critical}C%{endif}%{if-fatal}F%{endif}] %{file}:%{line} - %{message}" + QT_MESSAGE_PATTERN="[%{time yyyyMMdd h:mm:ss.zzz t} %{if-debug}D%{endif}%{if-warning}W%{endif}%{if-critical}C%{endif}%{if-fatal}F%{endif}] %{file}:%{line} - %{message}" \endcode The default \a pattern is "%{if-category}%{category}: %{endif}%{message}". |