/
qlogging.cpp
1948 lines (1669 loc) · 60.6 KB
/
qlogging.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Copyright (C) 2014 Olivier Goffart <ogoffart@woboq.com>
** Copyright (C) 2014 Intel Corporation.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the QtCore module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL21$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** As a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qlogging.h"
#include "qlist.h"
#include "qbytearray.h"
#include "qstring.h"
#include "qvarlengtharray.h"
#include "qdebug.h"
#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"
#include "private/qcoreapplication_p.h"
#endif
#ifdef Q_OS_WIN
#include <qt_windows.h>
#endif
#ifdef QT_USE_SLOG2
#include <slog2.h>
#endif
#ifdef Q_OS_ANDROID
#include <android/log.h>
#endif
#if defined(QT_USE_JOURNALD) && !defined(QT_BOOTSTRAPPED)
# define SD_JOURNAL_SUPPRESS_LOCATION
# include <systemd/sd-journal.h>
# include <syslog.h>
#endif
#if defined(QT_USE_SYSLOG) && !defined(QT_BOOTSTRAPPED)
# include <syslog.h>
#endif
#ifdef Q_OS_UNIX
# include <sys/types.h>
# include <sys/stat.h>
# include <unistd.h>
# include "private/qcore_unix_p.h"
#endif
#ifndef __has_include
# define __has_include(x) 0
#endif
#ifndef QT_BOOTSTRAPPED
#if !defined QT_NO_REGULAREXPRESSION
# ifdef __UCLIBC__
# if __UCLIBC_HAS_BACKTRACE__
# define QLOGGING_HAVE_BACKTRACE
# endif
# elif (defined(__GLIBC__) && defined(__GLIBCXX__)) || (__has_include(<cxxabi.h>) && __has_include(<execinfo.h>))
# define QLOGGING_HAVE_BACKTRACE
# endif
#endif
#if defined(QT_USE_SLOG2)
extern char *__progname;
#endif
#if defined(Q_OS_LINUX) && (defined(__GLIBC__) || __has_include(<sys/syscall.h>))
# include <sys/syscall.h>
# if defined(Q_OS_ANDROID) && !defined(SYS_gettid)
# define SYS_gettid __NR_gettid
# endif
static long qt_gettid()
{
// no error handling
// this syscall has existed since Linux 2.4.11 and cannot fail
return syscall(SYS_gettid);
}
#elif defined(Q_OS_DARWIN)
# include <pthread.h>
static int qt_gettid()
{
// no error handling: this call cannot fail
__uint64_t tid;
pthread_threadid_np(NULL, &tid);
return tid;
}
#elif defined(Q_OS_FREEBSD_KERNEL) && defined(__FreeBSD_version) && __FreeBSD_version >= 900031
# include <pthread_np.h>
static int qt_gettid()
{
return pthread_getthreadid_np();
}
#else
static QT_PREPEND_NAMESPACE(qint64) qt_gettid()
{
QT_USE_NAMESPACE
return qintptr(QThread::currentThreadId());
}
#endif
#ifdef QLOGGING_HAVE_BACKTRACE
# include <qregularexpression.h>
# include <cxxabi.h>
# include <execinfo.h>
#endif
#endif // !QT_BOOTSTRAPPED
#include <stdio.h>
QT_BEGIN_NAMESPACE
#if !defined(Q_CC_MSVC)
Q_NORETURN
#endif
static void qt_message_fatal(QtMsgType, const QMessageLogContext &context, const QString &message);
static void qt_message_print(QtMsgType, const QMessageLogContext &context, const QString &message);
static bool fatalCriticalsInitialized = false;
static bool fatalWarningsInitialized = false;
static bool isFatal(QtMsgType msgType)
{
if (msgType == QtFatalMsg)
return true;
if (msgType == QtCriticalMsg) {
static bool fatalCriticals = false;
if (!fatalCriticalsInitialized) {
fatalCriticals = !qEnvironmentVariableIsEmpty("QT_FATAL_CRITICALS");
fatalCriticalsInitialized = true;
}
return fatalCriticals;
}
if (msgType == QtWarningMsg || msgType == QtCriticalMsg) {
static bool fatalWarnings = false;
if (!fatalWarningsInitialized) {
fatalWarnings = !qEnvironmentVariableIsEmpty("QT_FATAL_WARNINGS");
fatalWarningsInitialized = true;
}
return fatalWarnings;
}
return false;
}
static bool willLogToConsole()
{
#if defined(Q_OS_WINCE) || defined(Q_OS_WINRT)
// these systems have no stderr, so always log to the system log
return false;
#elif defined(QT_BOOTSTRAPPED)
return true;
#else
// rules to determine if we'll log preferably to the console:
// 1) if QT_LOGGING_TO_CONSOLE is set, it determines behavior:
// - if it's set to 0, we will not log to console
// - if it's set to 1, we will log to console
// 2) otherwise, we will log to console if we have a console window (Windows)
// or a controlling TTY (Unix). This is done even if stderr was redirected
// to the blackhole device (NUL or /dev/null).
bool ok = true;
uint envcontrol = qgetenv("QT_LOGGING_TO_CONSOLE").toUInt(&ok);
if (ok)
return envcontrol;
# ifdef Q_OS_WIN
return GetConsoleWindow();
# elif defined(Q_OS_UNIX)
// if /dev/tty exists, we can only open it if we have a controlling TTY
int devtty = qt_safe_open("/dev/tty", O_RDONLY);
if (devtty == -1 && (errno == ENOENT || errno == EPERM)) {
// no /dev/tty, fall back to isatty on stderr
return isatty(STDERR_FILENO);
} else if (devtty != -1) {
// there is a /dev/tty and we could open it: we have a controlling TTY
qt_safe_close(devtty);
return true;
}
// no controlling TTY
return false;
# else
# error "Not Unix and not Windows?"
# endif
#endif
}
static bool logToConsoleInitialized = false;
Q_CORE_EXPORT bool qt_logging_to_console()
{
static bool logToConsole = false;
if (!logToConsoleInitialized) {
logToConsole = willLogToConsole();
logToConsoleInitialized = true;
}
return logToConsole;
}
/*!
\class QMessageLogContext
\inmodule QtCore
\brief The QMessageLogContext class provides additional information about a log message.
\since 5.0
The class provides information about the source code location a qDebug(), qInfo(), qWarning(),
qCritical() or qFatal() message was generated.
\note By default, this information is recorded only in debug builds. You can overwrite
this explicitly by defining \c QT_MESSAGELOGCONTEXT or \c{QT_NO_MESSAGELOGCONTEXT}.
\sa QMessageLogger, QtMessageHandler, qInstallMessageHandler()
*/
/*!
\class QMessageLogger
\inmodule QtCore
\brief The QMessageLogger class generates log messages.
\since 5.0
QMessageLogger is used to generate messages for the Qt logging framework. Usually one uses
it through qDebug(), qInfo(), qWarning(), qCritical, or qFatal() functions,
which are actually macros: For example qDebug() expands to
QMessageLogger(__FILE__, __LINE__, Q_FUNC_INFO).debug()
for debug builds, and QMessageLogger(0, 0, 0).debug() for release builds.
One example of direct use is to forward errors that stem from a scripting language, e.g. QML:
\snippet code/qlogging/qlogging.cpp 1
\sa QMessageLogContext, qDebug(), qInfo(), qWarning(), qCritical(), qFatal()
*/
#ifdef Q_OS_WIN
static inline void convert_to_wchar_t_elided(wchar_t *d, size_t space, const char *s) Q_DECL_NOEXCEPT
{
size_t len = qstrlen(s);
if (len + 1 > space) {
const size_t skip = len - space + 4; // 4 for "..." + '\0'
s += skip;
len -= skip;
for (int i = 0; i < 3; ++i)
*d++ = L'.';
}
while (len--)
*d++ = *s++;
*d++ = 0;
}
#endif
/*!
\internal
*/
Q_NEVER_INLINE
static QString qt_message(QtMsgType msgType, const QMessageLogContext &context, const char *msg, va_list ap)
{
QString buf = QString::vasprintf(msg, ap);
qt_message_print(msgType, context, buf);
return buf;
}
#undef qDebug
/*!
Logs a debug message specified with format \a msg. Additional
parameters, specified by \a msg, may be used.
\sa qDebug()
*/
void QMessageLogger::debug(const char *msg, ...) const
{
va_list ap;
va_start(ap, msg); // use variable arg list
const QString message = qt_message(QtDebugMsg, context, msg, ap);
va_end(ap);
if (isFatal(QtDebugMsg))
qt_message_fatal(QtDebugMsg, context, message);
}
#undef qInfo
/*!
Logs an informational message specified with format \a msg. Additional
parameters, specified by \a msg, may be used.
\sa qInfo()
\since 5.5
*/
void QMessageLogger::info(const char *msg, ...) const
{
va_list ap;
va_start(ap, msg); // use variable arg list
const QString message = qt_message(QtInfoMsg, context, msg, ap);
va_end(ap);
if (isFatal(QtInfoMsg))
qt_message_fatal(QtInfoMsg, context, message);
}
/*!
\typedef QMessageLogger::CategoryFunction
This is a typedef for a pointer to a function with the following
signature:
\snippet code/qlogging/qlogging.cpp 2
A function which this signature is generated by Q_DECLARE_LOGGING_CATEGORY,
Q_LOGGING_CATEGORY.
\since 5.3
*/
/*!
Logs a debug message specified with format \a msg for the context \a cat.
Additional parameters, specified by \a msg, may be used.
\since 5.3
\sa qCDebug()
*/
void QMessageLogger::debug(const QLoggingCategory &cat, const char *msg, ...) const
{
if (!cat.isDebugEnabled())
return;
QMessageLogContext ctxt;
ctxt.copy(context);
ctxt.category = cat.categoryName();
va_list ap;
va_start(ap, msg); // use variable arg list
const QString message = qt_message(QtDebugMsg, ctxt, msg, ap);
va_end(ap);
if (isFatal(QtDebugMsg))
qt_message_fatal(QtDebugMsg, ctxt, message);
}
/*!
Logs a debug message specified with format \a msg for the context returned
by \a catFunc. Additional parameters, specified by \a msg, may be used.
\since 5.3
\sa qCDebug()
*/
void QMessageLogger::debug(QMessageLogger::CategoryFunction catFunc,
const char *msg, ...) const
{
const QLoggingCategory &cat = (*catFunc)();
if (!cat.isDebugEnabled())
return;
QMessageLogContext ctxt;
ctxt.copy(context);
ctxt.category = cat.categoryName();
va_list ap;
va_start(ap, msg); // use variable arg list
const QString message = qt_message(QtDebugMsg, ctxt, msg, ap);
va_end(ap);
if (isFatal(QtDebugMsg))
qt_message_fatal(QtDebugMsg, ctxt, message);
}
#ifndef QT_NO_DEBUG_STREAM
/*!
Logs a debug message using a QDebug stream
\sa qDebug(), QDebug
*/
QDebug QMessageLogger::debug() const
{
QDebug dbg = QDebug(QtDebugMsg);
QMessageLogContext &ctxt = dbg.stream->context;
ctxt.copy(context);
return dbg;
}
/*!
Logs a debug message into category \a cat using a QDebug stream.
\since 5.3
\sa qCDebug(), QDebug
*/
QDebug QMessageLogger::debug(const QLoggingCategory &cat) const
{
QDebug dbg = QDebug(QtDebugMsg);
if (!cat.isDebugEnabled())
dbg.stream->message_output = false;
QMessageLogContext &ctxt = dbg.stream->context;
ctxt.copy(context);
ctxt.category = cat.categoryName();
return dbg;
}
/*!
Logs a debug message into category returned by \a catFunc using a QDebug stream.
\since 5.3
\sa qCDebug(), QDebug
*/
QDebug QMessageLogger::debug(QMessageLogger::CategoryFunction catFunc) const
{
return debug((*catFunc)());
}
/*!
\internal
Returns a QNoDebug object, which is used to ignore debugging output.
\sa QNoDebug, qDebug()
*/
QNoDebug QMessageLogger::noDebug() const Q_DECL_NOTHROW
{
return QNoDebug();
}
#endif
/*!
Logs an informational message specified with format \a msg for the context \a cat.
Additional parameters, specified by \a msg, may be used.
\since 5.5
\sa qCInfo()
*/
void QMessageLogger::info(const QLoggingCategory &cat, const char *msg, ...) const
{
if (!cat.isInfoEnabled())
return;
QMessageLogContext ctxt;
ctxt.copy(context);
ctxt.category = cat.categoryName();
va_list ap;
va_start(ap, msg); // use variable arg list
const QString message = qt_message(QtInfoMsg, ctxt, msg, ap);
va_end(ap);
if (isFatal(QtInfoMsg))
qt_message_fatal(QtInfoMsg, ctxt, message);
}
/*!
Logs an informational message specified with format \a msg for the context returned
by \a catFunc. Additional parameters, specified by \a msg, may be used.
\since 5.5
\sa qCInfo()
*/
void QMessageLogger::info(QMessageLogger::CategoryFunction catFunc,
const char *msg, ...) const
{
const QLoggingCategory &cat = (*catFunc)();
if (!cat.isInfoEnabled())
return;
QMessageLogContext ctxt;
ctxt.copy(context);
ctxt.category = cat.categoryName();
va_list ap;
va_start(ap, msg); // use variable arg list
const QString message = qt_message(QtInfoMsg, ctxt, msg, ap);
va_end(ap);
if (isFatal(QtInfoMsg))
qt_message_fatal(QtInfoMsg, ctxt, message);
}
#ifndef QT_NO_DEBUG_STREAM
/*!
Logs an informational message using a QDebug stream.
\since 5.5
\sa qInfo(), QDebug
*/
QDebug QMessageLogger::info() const
{
QDebug dbg = QDebug(QtInfoMsg);
QMessageLogContext &ctxt = dbg.stream->context;
ctxt.copy(context);
return dbg;
}
/*!
Logs an informational message into the category \a cat using a QDebug stream.
\since 5.5
\sa qCInfo(), QDebug
*/
QDebug QMessageLogger::info(const QLoggingCategory &cat) const
{
QDebug dbg = QDebug(QtInfoMsg);
if (!cat.isInfoEnabled())
dbg.stream->message_output = false;
QMessageLogContext &ctxt = dbg.stream->context;
ctxt.copy(context);
ctxt.category = cat.categoryName();
return dbg;
}
/*!
Logs an informational message into category returned by \a catFunc using a QDebug stream.
\since 5.5
\sa qCInfo(), QDebug
*/
QDebug QMessageLogger::info(QMessageLogger::CategoryFunction catFunc) const
{
return info((*catFunc)());
}
#endif
#undef qWarning
/*!
Logs a warning message specified with format \a msg. Additional
parameters, specified by \a msg, may be used.
\sa qWarning()
*/
void QMessageLogger::warning(const char *msg, ...) const
{
va_list ap;
va_start(ap, msg); // use variable arg list
const QString message = qt_message(QtWarningMsg, context, msg, ap);
va_end(ap);
if (isFatal(QtWarningMsg))
qt_message_fatal(QtWarningMsg, context, message);
}
/*!
Logs a warning message specified with format \a msg for the context \a cat.
Additional parameters, specified by \a msg, may be used.
\since 5.3
\sa qCWarning()
*/
void QMessageLogger::warning(const QLoggingCategory &cat, const char *msg, ...) const
{
if (!cat.isWarningEnabled())
return;
QMessageLogContext ctxt;
ctxt.copy(context);
ctxt.category = cat.categoryName();
va_list ap;
va_start(ap, msg); // use variable arg list
const QString message = qt_message(QtWarningMsg, ctxt, msg, ap);
va_end(ap);
if (isFatal(QtWarningMsg))
qt_message_fatal(QtWarningMsg, ctxt, message);
}
/*!
Logs a warning message specified with format \a msg for the context returned
by \a catFunc. Additional parameters, specified by \a msg, may be used.
\since 5.3
\sa qCWarning()
*/
void QMessageLogger::warning(QMessageLogger::CategoryFunction catFunc,
const char *msg, ...) const
{
const QLoggingCategory &cat = (*catFunc)();
if (!cat.isWarningEnabled())
return;
QMessageLogContext ctxt;
ctxt.copy(context);
ctxt.category = cat.categoryName();
va_list ap;
va_start(ap, msg); // use variable arg list
const QString message = qt_message(QtWarningMsg, ctxt, msg, ap);
va_end(ap);
if (isFatal(QtWarningMsg))
qt_message_fatal(QtWarningMsg, ctxt, message);
}
#ifndef QT_NO_DEBUG_STREAM
/*!
Logs a warning message using a QDebug stream
\sa qWarning(), QDebug
*/
QDebug QMessageLogger::warning() const
{
QDebug dbg = QDebug(QtWarningMsg);
QMessageLogContext &ctxt = dbg.stream->context;
ctxt.copy(context);
return dbg;
}
/*!
Logs a warning message into category \a cat using a QDebug stream.
\sa qCWarning(), QDebug
*/
QDebug QMessageLogger::warning(const QLoggingCategory &cat) const
{
QDebug dbg = QDebug(QtWarningMsg);
if (!cat.isWarningEnabled())
dbg.stream->message_output = false;
QMessageLogContext &ctxt = dbg.stream->context;
ctxt.copy(context);
ctxt.category = cat.categoryName();
return dbg;
}
/*!
Logs a warning message into category returned by \a catFunc using a QDebug stream.
\since 5.3
\sa qCWarning(), QDebug
*/
QDebug QMessageLogger::warning(QMessageLogger::CategoryFunction catFunc) const
{
return warning((*catFunc)());
}
#endif
#undef qCritical
/*!
Logs a critical message specified with format \a msg. Additional
parameters, specified by \a msg, may be used.
\sa qCritical()
*/
void QMessageLogger::critical(const char *msg, ...) const
{
va_list ap;
va_start(ap, msg); // use variable arg list
const QString message = qt_message(QtCriticalMsg, context, msg, ap);
va_end(ap);
if (isFatal(QtCriticalMsg))
qt_message_fatal(QtCriticalMsg, context, message);
}
/*!
Logs a critical message specified with format \a msg for the context \a cat.
Additional parameters, specified by \a msg, may be used.
\since 5.3
\sa qCCritical()
*/
void QMessageLogger::critical(const QLoggingCategory &cat, const char *msg, ...) const
{
if (!cat.isCriticalEnabled())
return;
QMessageLogContext ctxt;
ctxt.copy(context);
ctxt.category = cat.categoryName();
va_list ap;
va_start(ap, msg); // use variable arg list
const QString message = qt_message(QtCriticalMsg, ctxt, msg, ap);
va_end(ap);
if (isFatal(QtCriticalMsg))
qt_message_fatal(QtCriticalMsg, ctxt, message);
}
/*!
Logs a critical message specified with format \a msg for the context returned
by \a catFunc. Additional parameters, specified by \a msg, may be used.
\since 5.3
\sa qCCritical()
*/
void QMessageLogger::critical(QMessageLogger::CategoryFunction catFunc,
const char *msg, ...) const
{
const QLoggingCategory &cat = (*catFunc)();
if (!cat.isCriticalEnabled())
return;
QMessageLogContext ctxt;
ctxt.copy(context);
ctxt.category = cat.categoryName();
va_list ap;
va_start(ap, msg); // use variable arg list
const QString message = qt_message(QtCriticalMsg, ctxt, msg, ap);
va_end(ap);
if (isFatal(QtCriticalMsg))
qt_message_fatal(QtCriticalMsg, ctxt, message);
}
#ifndef QT_NO_DEBUG_STREAM
/*!
Logs a critical message using a QDebug stream
\sa qCritical(), QDebug
*/
QDebug QMessageLogger::critical() const
{
QDebug dbg = QDebug(QtCriticalMsg);
QMessageLogContext &ctxt = dbg.stream->context;
ctxt.copy(context);
return dbg;
}
/*!
Logs a critical message into category \a cat using a QDebug stream.
\since 5.3
\sa qCCritical(), QDebug
*/
QDebug QMessageLogger::critical(const QLoggingCategory &cat) const
{
QDebug dbg = QDebug(QtCriticalMsg);
if (!cat.isCriticalEnabled())
dbg.stream->message_output = false;
QMessageLogContext &ctxt = dbg.stream->context;
ctxt.copy(context);
ctxt.category = cat.categoryName();
return dbg;
}
/*!
Logs a critical message into category returned by \a catFunc using a QDebug stream.
\since 5.3
\sa qCCritical(), QDebug
*/
QDebug QMessageLogger::critical(QMessageLogger::CategoryFunction catFunc) const
{
return critical((*catFunc)());
}
#endif
#undef qFatal
/*!
Logs a fatal message specified with format \a msg. Additional
parameters, specified by \a msg, may be used.
\sa qFatal()
*/
void QMessageLogger::fatal(const char *msg, ...) const Q_DECL_NOTHROW
{
QString message;
va_list ap;
va_start(ap, msg); // use variable arg list
QT_TERMINATE_ON_EXCEPTION(message = qt_message(QtFatalMsg, context, msg, ap));
va_end(ap);
qt_message_fatal(QtFatalMsg, context, message);
}
/*!
\internal
*/
Q_AUTOTEST_EXPORT QByteArray qCleanupFuncinfo(QByteArray info)
{
// Strip the function info down to the base function name
// note that this throws away the template definitions,
// the parameter types (overloads) and any const/volatile qualifiers.
if (info.isEmpty())
return info;
int pos;
// Skip trailing [with XXX] for templates (gcc), but make
// sure to not affect Objective-C message names.
pos = info.size() - 1;
if (info.endsWith(']') && !(info.startsWith('+') || info.startsWith('-'))) {
while (--pos) {
if (info.at(pos) == '[')
info.truncate(pos);
}
}
// operator names with '(', ')', '<', '>' in it
static const char operator_call[] = "operator()";
static const char operator_lessThan[] = "operator<";
static const char operator_greaterThan[] = "operator>";
static const char operator_lessThanEqual[] = "operator<=";
static const char operator_greaterThanEqual[] = "operator>=";
// canonize operator names
info.replace("operator ", "operator");
// remove argument list
forever {
int parencount = 0;
pos = info.lastIndexOf(')');
if (pos == -1) {
// Don't know how to parse this function name
return info;
}
// find the beginning of the argument list
--pos;
++parencount;
while (pos && parencount) {
if (info.at(pos) == ')')
++parencount;
else if (info.at(pos) == '(')
--parencount;
--pos;
}
if (parencount != 0)
return info;
info.truncate(++pos);
if (info.at(pos - 1) == ')') {
if (info.indexOf(operator_call) == pos - (int)strlen(operator_call))
break;
// this function returns a pointer to a function
// and we matched the arguments of the return type's parameter list
// try again
info.remove(0, info.indexOf('('));
info.chop(1);
continue;
} else {
break;
}
}
// find the beginning of the function name
int parencount = 0;
int templatecount = 0;
--pos;
// make sure special characters in operator names are kept
if (pos > -1) {
switch (info.at(pos)) {
case ')':
if (info.indexOf(operator_call) == pos - (int)strlen(operator_call) + 1)
pos -= 2;
break;
case '<':
if (info.indexOf(operator_lessThan) == pos - (int)strlen(operator_lessThan) + 1)
--pos;
break;
case '>':
if (info.indexOf(operator_greaterThan) == pos - (int)strlen(operator_greaterThan) + 1)
--pos;
break;
case '=': {
int operatorLength = (int)strlen(operator_lessThanEqual);
if (info.indexOf(operator_lessThanEqual) == pos - operatorLength + 1)
pos -= 2;
else if (info.indexOf(operator_greaterThanEqual) == pos - operatorLength + 1)
pos -= 2;
break;
}
default:
break;
}
}
while (pos > -1) {
if (parencount < 0 || templatecount < 0)
return info;
char c = info.at(pos);
if (c == ')')
++parencount;
else if (c == '(')
--parencount;
else if (c == '>')
++templatecount;
else if (c == '<')
--templatecount;
else if (c == ' ' && templatecount == 0 && parencount == 0)
break;
--pos;
}
info = info.mid(pos + 1);
// remove trailing '*', '&' that are part of the return argument
while ((info.at(0) == '*')
|| (info.at(0) == '&'))
info = info.mid(1);
// we have the full function name now.
// clean up the templates
while ((pos = info.lastIndexOf('>')) != -1) {
if (!info.contains('<'))
break;
// find the matching close
int end = pos;
templatecount = 1;
--pos;
while (pos && templatecount) {
char c = info.at(pos);
if (c == '>')
++templatecount;
else if (c == '<')
--templatecount;
--pos;
}
++pos;
info.remove(pos, end - pos + 1);
}
return info;
}
// tokens as recognized in QT_MESSAGE_PATTERN
static const char categoryTokenC[] = "%{category}";
static const char typeTokenC[] = "%{type}";
static const char messageTokenC[] = "%{message}";
static const char fileTokenC[] = "%{file}";
static const char lineTokenC[] = "%{line}";
static const char functionTokenC[] = "%{function}";
static const char pidTokenC[] = "%{pid}";
static const char appnameTokenC[] = "%{appname}";
static const char threadidTokenC[] = "%{threadid}";
static const char qthreadptrTokenC[] = "%{qthreadptr}";
static const char timeTokenC[] = "%{time"; //not a typo: this command has arguments
static const char backtraceTokenC[] = "%{backtrace"; //ditto
static const char ifCategoryTokenC[] = "%{if-category}";
static const char ifDebugTokenC[] = "%{if-debug}";
static const char ifInfoTokenC[] = "%{if-info}";
static const char ifWarningTokenC[] = "%{if-warning}";
static const char ifCriticalTokenC[] = "%{if-critical}";
static const char ifFatalTokenC[] = "%{if-fatal}";
static const char endifTokenC[] = "%{endif}";
static const char emptyTokenC[] = "";
static const char defaultPattern[] = "%{if-category}%{category}: %{endif}%{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;
QList<QString> timeArgs; // timeFormats in sequence of %{time