Skip to content

Commit e994358

Browse files
Friedemann KleintFriedemann Kleint
Friedemann Kleint
authored and
Friedemann Kleint
committed
Qt Service: Fix compilation with Qt 5 with define QTSERVICE_DEBUG.
Use Qt 5's new message handler within #ifdef and rewrite code use QByteArray instead of using toAscii(), which no longer exists in Qt 5. Change-Id: Iad6aa794c6578fd796e3b2bb402d3e290bdc711f Task-number: QTSOLBUG-184 Reviewed-by: Joerg Bornemann <joerg.bornemann@theqtcompany.com>
1 parent 17b5654 commit e994358

File tree

1 file changed

+32
-14
lines changed

1 file changed

+32
-14
lines changed

qtservice/src/qtservice.cpp

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848

4949
#if defined(QTSERVICE_DEBUG)
5050
#include <QDebug>
51+
#include <QString>
5152
#include <QFile>
5253
#include <QTime>
5354
#include <QMutex>
@@ -64,25 +65,31 @@ static void qtServiceCloseDebugLog()
6465
{
6566
if (!f)
6667
return;
67-
QString ps(QTime::currentTime().toString("HH:mm:ss.zzz ") + QLatin1String("--- DEBUG LOG CLOSED ---\n\n"));
68-
f->write(ps.toAscii());
68+
f->write(QTime::currentTime().toString("HH:mm:ss.zzz").toLatin1());
69+
f->write(" --- DEBUG LOG CLOSED ---\n\n");
6970
f->flush();
7071
f->close();
7172
delete f;
7273
f = 0;
7374
}
7475

76+
#if QT_VERSION >= 0x050000
77+
void qtServiceLogDebug(QtMsgType type, const QMessageLogContext &context, const QString &msg)
78+
#else
7579
void qtServiceLogDebug(QtMsgType type, const char* msg)
80+
#endif
7681
{
7782
static QMutex mutex;
7883
QMutexLocker locker(&mutex);
79-
QString s(QTime::currentTime().toString("HH:mm:ss.zzz "));
80-
s += QString("[%1] ").arg(
8184
#if defined(Q_OS_WIN32)
82-
GetCurrentProcessId());
85+
const qulonglong processId = GetCurrentProcessId();
8386
#else
84-
getpid());
87+
const qulonglong processId = getpid();
8588
#endif
89+
QByteArray s(QTime::currentTime().toString("HH:mm:ss.zzz").toLatin1());
90+
s += " [";
91+
s += QByteArray::number(processId);
92+
s += "] ";
8693

8794
if (!f) {
8895
#if defined(Q_OS_WIN32)
@@ -95,32 +102,39 @@ void qtServiceLogDebug(QtMsgType type, const char* msg)
95102
f = 0;
96103
return;
97104
}
98-
QString ps(QLatin1String("\n") + s + QLatin1String("--- DEBUG LOG OPENED ---\n"));
99-
f->write(ps.toAscii());
105+
QByteArray ps('\n' + s + "--- DEBUG LOG OPENED ---\n");
106+
f->write(ps);
100107
}
101108

102109
switch (type) {
103110
case QtWarningMsg:
104-
s += QLatin1String("WARNING: ");
111+
s += "WARNING: ";
105112
break;
106113
case QtCriticalMsg:
107-
s += QLatin1String("CRITICAL: ");
114+
s += "CRITICAL: ";
108115
break;
109116
case QtFatalMsg:
110-
s+= QLatin1String("FATAL: ");
117+
s+= "FATAL: ";
111118
break;
112119
case QtDebugMsg:
113-
s += QLatin1String("DEBUG: ");
120+
s += "DEBUG: ";
114121
break;
115122
default:
116123
// Nothing
117124
break;
118125
}
119126

127+
#if QT_VERSION >= 0x050400
128+
s += qFormatLogMessage(type, context, msg).toLocal8Bit();
129+
#elif QT_VERSION >= 0x050000
130+
s += msg.toLocal8Bit();
131+
Q_UNUSED(context)
132+
#else
120133
s += msg;
121-
s += QLatin1String("\n");
134+
#endif
135+
s += '\n';
122136

123-
f->write(s.toAscii());
137+
f->write(s);
124138
f->flush();
125139

126140
if (type == QtFatalMsg) {
@@ -625,7 +639,11 @@ int QtServiceBasePrivate::run(bool asService, const QStringList &argList)
625639
QtServiceBase::QtServiceBase(int argc, char **argv, const QString &name)
626640
{
627641
#if defined(QTSERVICE_DEBUG)
642+
# if QT_VERSION >= 0x050000
643+
qInstallMessageHandler(qtServiceLogDebug);
644+
# else
628645
qInstallMsgHandler(qtServiceLogDebug);
646+
# endif
629647
qAddPostRoutine(qtServiceCloseDebugLog);
630648
#endif
631649

0 commit comments

Comments
 (0)