summaryrefslogtreecommitdiffstats
path: root/src/corelib/global/qlogging.cpp
diff options
context:
space:
mode:
authorKai Koehne <kai.koehne@nokia.com>2012-04-13 13:37:56 +0200
committerQt by Nokia <qt-info@nokia.com>2012-04-24 17:12:46 +0200
commitc0d249019b098890fb8e5e9e144c2dd8029a670c (patch)
treec65c071920b8e8ebb3b1ad65ae1f5df7851ddcf3 /src/corelib/global/qlogging.cpp
parente92e5fda44602d7595aca329a3133ecb9991a6dd (diff)
Allow qDebug output to be configured by qSetMessagePattern()
Add qSetMessagePattern() to configure the default message pattern. This one can still be overwritten by setting the QT_MESSAGE_PATTERN environment variable. Without this method, there's actually no way to change the default output programatically. Since QT_MESSAGE_PATTERN is evaluated when the first message arrives, setting it via e.g. qputenv might have no effect/be too late. Change-Id: I115e0c30606f128fdbf5c169a951ffa2a6a48517 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/global/qlogging.cpp')
-rw-r--r--src/corelib/global/qlogging.cpp64
1 files changed, 51 insertions, 13 deletions
diff --git a/src/corelib/global/qlogging.cpp b/src/corelib/global/qlogging.cpp
index 1f5b121143..8726c18689 100644
--- a/src/corelib/global/qlogging.cpp
+++ b/src/corelib/global/qlogging.cpp
@@ -45,6 +45,7 @@
#include "qstring.h"
#include "qvarlengtharray.h"
#include "qdebug.h"
+#include "qmutex.h"
#ifndef QT_BOOTSTRAPPED
#include "qcoreapplication.h"
#include "qthread.h"
@@ -400,21 +401,53 @@ static const char appnameTokenC[] = "%{appname}";
static const char threadidTokenC[] = "%{threadid}";
static const char emptyTokenC[] = "";
+static const char defaultPattern[] = "%{message}";
+
+
struct QMessagePattern {
QMessagePattern();
~QMessagePattern();
+ void setPattern(const QString &pattern);
+
// 0 terminated arrays of literal tokens / literal or placeholder tokens
const char **literals;
const char **tokens;
+
+ bool fromEnvironment;
+ static QBasicMutex mutex;
};
+QBasicMutex QMessagePattern::mutex;
+
QMessagePattern::QMessagePattern()
+ : literals(0)
+ , tokens(0)
+ , fromEnvironment(false)
{
- QString pattern = QString::fromLocal8Bit(qgetenv("QT_MESSAGE_PATTERN"));
- if (pattern.isEmpty()) {
- pattern = QLatin1String("%{message}");
+ const QString envPattern = QString::fromLocal8Bit(qgetenv("QT_MESSAGE_PATTERN"));
+ if (envPattern.isEmpty()) {
+ setPattern(QLatin1String(defaultPattern));
+ } else {
+ setPattern(envPattern);
+ fromEnvironment = true;
}
+}
+
+QMessagePattern::~QMessagePattern()
+{
+ for (int i = 0; literals[i] != 0; ++i)
+ delete [] literals[i];
+ delete [] literals;
+ literals = 0;
+ delete [] tokens;
+ tokens = 0;
+}
+
+void QMessagePattern::setPattern(const QString &pattern)
+{
+ delete [] tokens;
+ delete [] literals;
// scanner
QList<QString> lexemes;
@@ -495,16 +528,6 @@ QMessagePattern::QMessagePattern()
memcpy(literals, literalsVar.constData(), literalsVar.size() * sizeof(const char*));
}
-QMessagePattern::~QMessagePattern()
-{
- for (int i = 0; literals[i] != 0; ++i)
- delete [] literals[i];
- delete [] literals;
- literals = 0;
- delete [] tokens;
- tokens = 0;
-}
-
Q_GLOBAL_STATIC(QMessagePattern, qMessagePattern)
/*!
@@ -515,6 +538,8 @@ Q_CORE_EXPORT QString qMessageFormatString(QtMsgType type, const QMessageLogCont
{
QString message;
+ QMutexLocker lock(&QMessagePattern::mutex);
+
QMessagePattern *pattern = qMessagePattern();
if (!pattern) {
// after destruction of static QMessagePattern instance
@@ -523,6 +548,10 @@ Q_CORE_EXPORT QString qMessageFormatString(QtMsgType type, const QMessageLogCont
return message;
}
+ // don't print anything if pattern was empty
+ if (pattern->tokens[0] == 0)
+ return message;
+
// we do not convert file, function, line literals to local encoding due to overhead
for (int i = 0; pattern->tokens[i] != 0; ++i) {
const char *token = pattern->tokens[i];
@@ -741,6 +770,14 @@ QtMsgHandler qInstallMsgHandler(QtMsgHandler h)
return old;
}
+void qSetMessagePattern(const QString &pattern)
+{
+ QMutexLocker lock(&QMessagePattern::mutex);
+
+ if (!qMessagePattern()->fromEnvironment)
+ qMessagePattern()->setPattern(pattern);
+}
+
void QMessageLogContext::copy(const QMessageLogContext &logContext)
{
this->category = logContext.category;
@@ -748,4 +785,5 @@ void QMessageLogContext::copy(const QMessageLogContext &logContext)
this->line = logContext.line;
this->function = logContext.function;
}
+
QT_END_NAMESPACE