48
48
49
49
#if defined(QTSERVICE_DEBUG)
50
50
#include < QDebug>
51
+ #include < QString>
51
52
#include < QFile>
52
53
#include < QTime>
53
54
#include < QMutex>
@@ -64,25 +65,31 @@ static void qtServiceCloseDebugLog()
64
65
{
65
66
if (!f)
66
67
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 " );
69
70
f->flush ();
70
71
f->close ();
71
72
delete f;
72
73
f = 0 ;
73
74
}
74
75
76
+ #if QT_VERSION >= 0x050000
77
+ void qtServiceLogDebug (QtMsgType type, const QMessageLogContext &context, const QString &msg)
78
+ #else
75
79
void qtServiceLogDebug (QtMsgType type, const char * msg)
80
+ #endif
76
81
{
77
82
static QMutex mutex;
78
83
QMutexLocker locker (&mutex);
79
- QString s (QTime::currentTime ().toString (" HH:mm:ss.zzz " ));
80
- s += QString (" [%1] " ).arg (
81
84
#if defined(Q_OS_WIN32)
82
- GetCurrentProcessId () );
85
+ const qulonglong processId = GetCurrentProcessId ();
83
86
#else
84
- getpid () );
87
+ const qulonglong processId = getpid ();
85
88
#endif
89
+ QByteArray s (QTime::currentTime ().toString (" HH:mm:ss.zzz" ).toLatin1 ());
90
+ s += " [" ;
91
+ s += QByteArray::number (processId);
92
+ s += " ] " ;
86
93
87
94
if (!f) {
88
95
#if defined(Q_OS_WIN32)
@@ -95,32 +102,39 @@ void qtServiceLogDebug(QtMsgType type, const char* msg)
95
102
f = 0 ;
96
103
return ;
97
104
}
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);
100
107
}
101
108
102
109
switch (type) {
103
110
case QtWarningMsg:
104
- s += QLatin1String ( " WARNING: " ) ;
111
+ s += " WARNING: " ;
105
112
break ;
106
113
case QtCriticalMsg:
107
- s += QLatin1String ( " CRITICAL: " ) ;
114
+ s += " CRITICAL: " ;
108
115
break ;
109
116
case QtFatalMsg:
110
- s+= QLatin1String ( " FATAL: " ) ;
117
+ s+= " FATAL: " ;
111
118
break ;
112
119
case QtDebugMsg:
113
- s += QLatin1String ( " DEBUG: " ) ;
120
+ s += " DEBUG: " ;
114
121
break ;
115
122
default :
116
123
// Nothing
117
124
break ;
118
125
}
119
126
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
120
133
s += msg;
121
- s += QLatin1String (" \n " );
134
+ #endif
135
+ s += ' \n ' ;
122
136
123
- f->write (s. toAscii () );
137
+ f->write (s);
124
138
f->flush ();
125
139
126
140
if (type == QtFatalMsg) {
@@ -625,7 +639,11 @@ int QtServiceBasePrivate::run(bool asService, const QStringList &argList)
625
639
QtServiceBase::QtServiceBase (int argc, char **argv, const QString &name)
626
640
{
627
641
#if defined(QTSERVICE_DEBUG)
642
+ # if QT_VERSION >= 0x050000
643
+ qInstallMessageHandler (qtServiceLogDebug);
644
+ # else
628
645
qInstallMsgHandler (qtServiceLogDebug);
646
+ # endif
629
647
qAddPostRoutine (qtServiceCloseDebugLog);
630
648
#endif
631
649
0 commit comments