summaryrefslogtreecommitdiff
path: root/ext/Log4Qt/src/helpers/patternformatter.cpp
blob: cf23198fba7c2ed7d85acc5eacf735da8c8f06f2 (plain)
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
/******************************************************************************
 *
 * package:     Log4Qt
 * file:        patternformatter.cpp
 * created:     September 2007
 * author:      Martin Heinrich
 *
 *
 * changes      Feb 2009, Martin Heinrich
 *              - Fixed VS 2008 unreferenced formal parameter warning by using
 *                Q_UNUSED in LiteralPatternConverter::convert.
 *
 *
 * Copyright 2007 - 2009 Martin Heinrich
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************************/



/******************************************************************************
 * Dependencies
 ******************************************************************************/

#include "helpers/patternformatter.h"

#include <QtCore/QString>
#include <QtCore/QDebug>
#include <limits.h>
#include "helpers/datetime.h"
#include "helpers/logerror.h"
#include "layout.h"
#include "logger.h"
#include "loggingevent.h"



namespace Log4Qt
{


	/**************************************************************************
	 *Declarations
	 **************************************************************************/


	/*!
	 * \brief The class FormattingInfo stores the formatting modifier for a
	 * pattern converter.
	 *
	 * \sa PatternConverter
	 */
	class FormattingInfo
	{
	public:
		FormattingInfo()
		{ clear(); }
		// FormattingInfo(const FormattingInfo &rOther); // Use compiler default
		// virtual ~FormattingInfo(); // Use compiler default
		// FormattingInfo &operator=(const FormattingInfo &rOther); // Use compiler default

		void clear();
		static QString intToString(int i);

	public:
		int mMinLength;
		int mMaxLength;
		bool mLeftAligned;
	};


	/*!
	 * \brief The class PatternConverter is the abstract base class for all
	 * pattern converters.
	 *
	 * PatternConverter handles the minimum and maximum modifier for a
	 * conversion character. The actual conversion is by calling the
	 * convert() member function of the derived class.
	 *
	 * \sa PatternLayout::format()
	 */
	class PatternConverter
	{
	public:
		PatternConverter(const FormattingInfo &rFormattingInfo = FormattingInfo()) :
			mFormattingInfo(rFormattingInfo)
		{};
		virtual ~PatternConverter()
		{};
	private:
		PatternConverter(const PatternConverter &rOther); // Not implemented
		PatternConverter &operator=(const PatternConverter &rOther); // Not implemented

	public:
		void format(QString &rFormat, const LoggingEvent &rLoggingEvent) const;

	protected:
		virtual QString convert(const LoggingEvent &rLoggingEvent) const = 0;
	#ifndef QT_NO_DEBUG_STREAM
		virtual QDebug debug(QDebug &rDebug) const = 0;
		friend QDebug operator<<(QDebug, const PatternConverter &rPatternConverter);
	#endif

	protected:
		FormattingInfo mFormattingInfo;
	};


	/*!
	 * \brief The class BasicPatternConverter converts several members of a
	 *        LoggingEvent to a string.
	 *
	 * BasicPatternConverter is used by PatternLayout to convert members that
	 * do not reuquire additional formatting to a string as part of formatting
	 * the LoggingEvent. It handles the following conversion characters:
	 * 'm', 'p', 't', 'x'
	 *
	 * \sa PatternLayout::format()
	 * \sa PatternConverter::format()
	 */
	class BasicPatternConverter : public PatternConverter
	{
	public:
		enum Type {
			MESSAGE_CONVERTER,
			NDC_CONVERTER,
			LEVEL_CONVERTER,
			THREAD_CONVERTER
		};

	public:
		BasicPatternConverter(const FormattingInfo &rFormattingInfo,
														Type type) :
					PatternConverter(rFormattingInfo),
					mType(type)
			{};
		// virtual ~BasicPatternConverter(); // Use compiler default
	private:
		BasicPatternConverter(const BasicPatternConverter &rOther); // Not implemented
		BasicPatternConverter &operator=(const BasicPatternConverter &rOther); // Not implemented

	protected:
		virtual QString convert(const LoggingEvent &rLoggingEvent) const;
	#ifndef QT_NO_DEBUG_STREAM
		virtual QDebug debug(QDebug &rDebug) const;
	#endif

	private:
		Type mType;
	};


	/*!
	 * \brief The class DatePatternConverter converts the time stamp of a
	 *        LoggingEvent to a string.
	 *
	 * DatePatternConverter is used by PatternLayout to convert the time stamp
	 * of a LoggingEvent to a string as part of formatting the LoggingEvent.
	 * It handles the 'd' and 'r' conversion character.
	 *
	 * \sa PatternLayout::format()
	 * \sa PatternConverter::format()
	 */
	class DatePatternConverter : public PatternConverter
	{
	public:
		DatePatternConverter(const FormattingInfo &rFormattingInfo,
										 const QString &rFormat) :
					PatternConverter(rFormattingInfo),
			mFormat(rFormat)
		{};
			// virtual ~DatePatternConverter(); // Use compiler default
	private:
		DatePatternConverter(const DatePatternConverter &rOther); // Not implemented
		DatePatternConverter &operator=(const DatePatternConverter &rOther); // Not implemented

	protected:
		virtual QString convert(const LoggingEvent &rLoggingEvent) const;
	#ifndef QT_NO_DEBUG_STREAM
		virtual QDebug debug(QDebug &rDebug) const;
	#endif

	private:
		QString mFormat;
	};


	/*!
	 * \brief The class LiteralPatternConverter provides string literals.
	 *
	 * LiteralPatternConverter is used by PatternLayout to embed string
	 * literals as part of formatting the LoggingEvent. It handles string
	 * literals and the 'n' conversion character.
	 *
	 * \sa PatternLayout::format()
	 * \sa PatternConverter::format()
	 */
	class LiteralPatternConverter : public PatternConverter
	{
	public:
		LiteralPatternConverter(const QString &rLiteral) :
					PatternConverter(),
					mLiteral(rLiteral)
			{};
			// virtual ~LiteralPatternConverter(); // Use compiler default
	private:
		LiteralPatternConverter(const LiteralPatternConverter &rOther); // Not implemented
		LiteralPatternConverter &operator=(const LiteralPatternConverter &rOther); // Not implemented

	protected:
		virtual QString convert(const LoggingEvent &rLoggingEvent) const;
	#ifndef QT_NO_DEBUG_STREAM
		virtual QDebug debug(QDebug &rDebug) const;
	#endif

	private:
		QString mLiteral;
	};


	/*!
	 * \brief The class LoggerPatternConverter converts the Logger name of a
	 *        LoggingEvent to a string.
	 *
	 * LoggerPatternConverter is used by PatternLayout to convert the Logger
	 * name of a LoggingEvent to a string as part of formatting the
	 * LoggingEvent. It handles the 'c' conversion character.
	 *
	 * \sa PatternLayout::format()
	 * \sa PatternConverter::format()
	 */
	class LoggerPatternConverter : public PatternConverter
	{
	public:
		LoggerPatternConverter(const FormattingInfo &rFormattingInfo,
														 int precision) :
				 PatternConverter(rFormattingInfo),
				 mPrecision(precision)
			{};
		// virtual ~LoggerPatternConverter(); // Use compiler default
	private:
		LoggerPatternConverter(const LoggerPatternConverter &rOther); // Not implemented
		LoggerPatternConverter &operator=(const LoggerPatternConverter &rOther); // Not implemented

	protected:
		virtual QString convert(const LoggingEvent &rLoggingEvent) const;
	#ifndef QT_NO_DEBUG_STREAM
		virtual QDebug debug(QDebug &rDebug) const;
	#endif

	private:
		int mPrecision;
	};



	/*!
	 * \brief The class MDCPatternConverter converts the MDC data of a
	 *        LoggingEvent to a string.
	 *
	 * MDCPatternConverter is used by PatternLayout to convert the MDC data of
	 * a LoggingEvent to a string as part of formatting the LoggingEvent. It
	 * handles the 'X' conversion character.
	 *
	 * \sa PatternLayout::format()
	 * \sa PatternConverter::format()
	 */
	class MDCPatternConverter : public PatternConverter
	{
	public:
		MDCPatternConverter(const FormattingInfo &rFormattingInfo,
										const QString &rKey) :
					PatternConverter(rFormattingInfo),
					mKey(rKey)
			{};
		// virtual ~MDCPatternConverter(); // Use compiler default
	private:
		MDCPatternConverter(const MDCPatternConverter &rOther); // Not implemented
		MDCPatternConverter &operator=(const MDCPatternConverter &rOther); // Not implemented

	protected:
		virtual QString convert(const LoggingEvent &rLoggingEvent) const;
	#ifndef QT_NO_DEBUG_STREAM
		virtual QDebug debug(QDebug &rDebug) const;
	#endif

	private:
		QString mKey;
	};


	#ifndef QT_NO_DEBUG_STREAM
	QDebug operator<<(QDebug, const FormattingInfo &rFormattingInfo);
	#endif


	#ifndef QT_NO_DEBUG_STREAM
	QDebug operator<<(QDebug, const PatternConverter &rPatternConverter);
	#endif



	/**************************************************************************
	 * C helper functions
	 **************************************************************************/


		LOG4QT_DECLARE_STATIC_LOGGER(logger, Log4Qt::PatternFormatter)



	/**************************************************************************
	 * Class implementation: PatternFormatter
	 **************************************************************************/


	PatternFormatter::PatternFormatter(const QString &rPattern) :
		mIgnoreCharacters(QLatin1String("CFlLM")),
		mConversionCharacters(QLatin1String("cdmprtxX")),
		mOptionCharacters(QLatin1String("cd")),
		mPattern(rPattern),
		mPatternConverters()
	{
		parse();
	}


	PatternFormatter::~PatternFormatter()
	{
		PatternConverter *p_converter;
		Q_FOREACH(p_converter, mPatternConverters)
			delete p_converter;
	}


	QString PatternFormatter::format(const LoggingEvent &rLoggingEvent) const
	{
		QString result;
		PatternConverter *p_converter;
		Q_FOREACH(p_converter, mPatternConverters)
			p_converter->format(result, rLoggingEvent);
		return result;
	}


	bool PatternFormatter::addDigit(const QChar &rDigit,
																	int &rValue)
	{
		if (!rDigit.isDigit())
			return false;

		int digit_value = rDigit.digitValue();
		if (rValue > (INT_MAX - digit_value) / 10)
			rValue = INT_MAX;
		else
			rValue = rValue * 10 + digit_value;
		return true;
	}


	void PatternFormatter::createConverter(const QChar &rChar,
																				 const FormattingInfo &rFormattingInfo,
																				 const QString &rOption)
	{
		Q_ASSERT_X(mConversionCharacters.indexOf(rChar) >= 0, "PatternFormatter::createConverter", "Unknown conversion character" );

			LogError e("Creating Converter for character '%1' min %2, max %3, left %4 and option '%5'");
			e << QString(rChar)
				<< FormattingInfo::intToString(rFormattingInfo.mMinLength)
				<< FormattingInfo::intToString(rFormattingInfo.mMaxLength)
				<< rFormattingInfo.mLeftAligned
				<< rOption;
		logger()->trace(e);

		switch (rChar.toLatin1())
		{
			case 'c':
				mPatternConverters << new LoggerPatternConverter(rFormattingInfo,
																															 parseIntegerOption(rOption));
				break;
			case 'd':
			{
				QString option = rOption;
				if (rOption.isEmpty()) {
					option = QLatin1String("ISO8601");
				} else if (rOption == "locale:long") {
				  option = QLocale().dateTimeFormat(QLocale::LongFormat);
				} else if (rOption == "locale:short") {
				  option = QLocale().dateTimeFormat(QLocale::ShortFormat);
				} else if (rOption == "locale:narrow") {
				  option = QLocale().dateTimeFormat(QLocale::NarrowFormat);
				} else if (rOption == "locale") {
				  option = QLocale().dateTimeFormat(QLocale::ShortFormat);
				}
				mPatternConverters << new DatePatternConverter(rFormattingInfo,
																														 option);
				break;
			}
			case 'm':
				mPatternConverters << new BasicPatternConverter(rFormattingInfo,
																															BasicPatternConverter::MESSAGE_CONVERTER);
				break;
			case 'p':
				mPatternConverters << new BasicPatternConverter(rFormattingInfo,
																															BasicPatternConverter::LEVEL_CONVERTER);
				break;
			case 'r':
				mPatternConverters << new DatePatternConverter(rFormattingInfo,
																														 QLatin1String("RELATIVE"));
				break;
			case 't':
				mPatternConverters << new BasicPatternConverter(rFormattingInfo,
																															BasicPatternConverter::THREAD_CONVERTER);
				break;
			case 'x':
				mPatternConverters << new BasicPatternConverter(rFormattingInfo,
																															BasicPatternConverter::NDC_CONVERTER);
				break;
			case 'X':
				mPatternConverters << new MDCPatternConverter(rFormattingInfo,
																														rOption);
				break;
			default:
				Q_ASSERT_X(false, "PatternFormatter::createConverter", "Unknown pattern character");
		}
	}


	void PatternFormatter::createLiteralConverter(const QString &rLiteral)
	{
		logger()->trace("Creating literal LiteralConverter with Literal '%1'",
								rLiteral);
		mPatternConverters << new LiteralPatternConverter(rLiteral);
	}


	void PatternFormatter::parse()
	{
		enum State {
			LITERAL_STATE,
			ESCAPE_STATE,
			MIN_STATE,
			DOT_STATE,
			MAX_STATE,
			CHARACTER_STATE,
			POSSIBLEOPTION_STATE,
			OPTION_STATE
		};

		int i = 0;
		QChar c;
		char ch;
		State state = LITERAL_STATE;
		FormattingInfo formatting_info;
		QString literal;
		int converter_start = 0;
		int option_start = 0;
		while (i < mPattern.length())
		{
					// i points to the current character
			// c contains the current character
			// ch contains the Latin1 equivalent of the current character
			// i is incremented at the end of the loop to consume the character
			// continue is used to change state without consuming the character

			c = mPattern.at(i);
			ch = c.toLatin1();
					switch (state)
					{
							case LITERAL_STATE:
								if (ch == '%')
								{
									formatting_info.clear();
									converter_start = i;
									state = ESCAPE_STATE;
								} else
									literal += c;
								break;
							case ESCAPE_STATE:
								if (ch == '%')
								{
									literal += c;
									state = LITERAL_STATE;
								}
								else if (ch == 'n')
								{
									literal += Layout::endOfLine();
									state = LITERAL_STATE;
								}
								else
								{
									if (!literal.isEmpty())
									{
										createLiteralConverter(literal);
										literal.clear();
									}
									if (ch == '-')
										formatting_info.mLeftAligned = true;
									else if (c.isDigit())
									{
									formatting_info.mMinLength = c.digitValue();
										state = MIN_STATE;
									}
									else if (ch == '.')
										state = DOT_STATE;
									else
									{
										state = CHARACTER_STATE;
										continue;
									}
								}
								break;
							case MIN_STATE:
								if (!addDigit(c, formatting_info.mMinLength))
								{
									if (ch == '.')
										state = DOT_STATE;
									else
									{
										state = CHARACTER_STATE;
										continue;
									}
								}
								break;
							case DOT_STATE:
								if (c.isDigit())
								{
								formatting_info.mMaxLength = c.digitValue();
									state = MAX_STATE;
								}
								else
								{
											LogError e = LOG4QT_ERROR(QT_TR_NOOP("Found character '%1' where digit was expected."),
																									LAYOUT_EXPECTED_DIGIT_ERROR,
																									"Log4Qt::PatternFormatter");
											e << QString(c);
											logger()->error(e);
								}
								break;
							case MAX_STATE:
								if (!addDigit(c, formatting_info.mMaxLength))
								{
									state = CHARACTER_STATE;
									continue;
								}
								break;
							case CHARACTER_STATE:
								if (mIgnoreCharacters.indexOf(c) >= 0)
									state = LITERAL_STATE;
								else if (mOptionCharacters.indexOf(c) >= 0)
									state = POSSIBLEOPTION_STATE;
								else if (mConversionCharacters.indexOf(c) >= 0)
								{
									createConverter(c, formatting_info);
									state = LITERAL_STATE;
								}
								else
								{
									logger()->warn("Invalid conversion character '%1' at %2 in pattern '%3'",
														 c, i, mPattern);
									createLiteralConverter(mPattern.mid(converter_start, i - converter_start + 1));
									state = LITERAL_STATE;
								}
								break;
							case POSSIBLEOPTION_STATE:
								if (ch == '{')
								{
										option_start = i;
									state = OPTION_STATE;
								}
								else
								{
									createConverter(mPattern.at(i - 1),
																			formatting_info);
									state = LITERAL_STATE;
									continue;
								}
								break;
							case OPTION_STATE:
								if (ch == '}')
								{
									createConverter(mPattern.at(option_start - 1),
																			formatting_info,
																			mPattern.mid(option_start + 1, i - option_start - 1));
									state = LITERAL_STATE;
								}
									break;
							default:
								Q_ASSERT_X(false, "PatternFormatter::parse()", "Unknown parsing state constant");
							state = LITERAL_STATE;
					}
			i++;
		}

		if (state != LITERAL_STATE)
		{
			logger()->warn("Unexptected end of pattern '%1'", mPattern);
			if (state == ESCAPE_STATE)
				literal += c;
			else
				literal += mPattern.mid(converter_start);
		}

		if (!literal.isEmpty())
			createLiteralConverter(literal);
	}


	int PatternFormatter::parseIntegerOption(const QString &rOption)
	{
		if (rOption.isEmpty())
			return 0;

		bool ok;
		int result = rOption.toInt(&ok);
		if (!ok)
		{
					LogError e = LOG4QT_ERROR(QT_TR_NOOP("Option '%1' cannot be converted into an integer"),
																			LAYOUT_OPTION_IS_NOT_INTEGER_ERROR,
																			"Log4Qt::PatterFormatter");
					e << rOption;
					logger()->error(e);
		}
		if (result < 0)
		{
					LogError e = LOG4QT_ERROR(QT_TR_NOOP("Option %1 isn't a positive integer"),
																			LAYOUT_INTEGER_IS_NOT_POSITIVE_ERROR,
																			"Log4Qt::PatterFormatter");
					e << result;
					logger()->error(e);
			result = 0;
		}
		return result;
	}


	/**************************************************************************
	 * Class implementation: FormattingInfo
	 **************************************************************************/


	void FormattingInfo::clear()
	{
		mMinLength = 0;
		mMaxLength = INT_MAX;
		mLeftAligned = false;
	}


	QString FormattingInfo::intToString(int i)
	{
			if (i == INT_MAX)
				return QLatin1String("INT_MAX");
			else
				return QString::number(i);
	}



	/**************************************************************************
	 * Class implementation: PatternConverter
	 **************************************************************************/


	void PatternConverter::format(QString &rFormat, const LoggingEvent &rLoggingEvent) const
	{
		const QLatin1Char space(' ');
		QString s = convert(rLoggingEvent);

		if (s.length() > mFormattingInfo.mMaxLength)
			rFormat += s.left(mFormattingInfo.mMaxLength);
		else if (mFormattingInfo.mLeftAligned)
			rFormat += s.leftJustified(mFormattingInfo.mMinLength, space, false);
		else
			rFormat += s.rightJustified(mFormattingInfo.mMinLength, space, false);
	}



	/**************************************************************************
	 * Class implementation: BasicPatternConverter
	 **************************************************************************/


	QString BasicPatternConverter::convert(const LoggingEvent &rLoggingEvent) const
	{
		switch (mType)
		{
					case MESSAGE_CONVERTER:
						return rLoggingEvent.message();
						break;
					case NDC_CONVERTER:
						return rLoggingEvent.ndc();
						break;
					case LEVEL_CONVERTER:
						return rLoggingEvent.level().toString();
						break;
					case THREAD_CONVERTER:
						return rLoggingEvent.threadName();
						break;
					default:
						Q_ASSERT_X(false, "BasicPatternConverter::convert()", "Unkown type constant");
						return QString();
		}
	}


	QDebug BasicPatternConverter::debug(QDebug &rDebug) const
	{
		QString type;
		switch (mType)
		{
					case MESSAGE_CONVERTER:
						type = QLatin1String("MESSAGE_CONVERTER");
						break;
					case NDC_CONVERTER:
						type = QLatin1String("NDC_CONVERTER");
						break;
					case LEVEL_CONVERTER:
						type = QLatin1String("LEVEL_CONVERTER");
						break;
					case THREAD_CONVERTER:
						type = QLatin1String("THREAD_CONVERTER");
						break;
					default:
						Q_ASSERT_X(false, "BasicPatternConverter::debug()", "Unkown type constant");
		}
			rDebug.nospace() << "BasicPatternConverter("
					<< mFormattingInfo
					<< "type:" << type
					<< ")";
			return rDebug.space();
	}



	/**************************************************************************
	 * Class implementation: DatePatternConverter
	 **************************************************************************/


	QString DatePatternConverter::convert(const LoggingEvent &rLoggingEvent) const
	{
		return DateTime::fromMilliSeconds(rLoggingEvent.timeStamp()).toString(mFormat);
	}


	QDebug DatePatternConverter::debug(QDebug &rDebug) const
	{
			rDebug.nospace() << "DatePatternConverter("
					<< mFormattingInfo
					<< "format:" << mFormat
					<< ")";
			return rDebug.space();
	}



	/**************************************************************************
	 * Class implementation: LiteralPatternConverter
	 **************************************************************************/


	QString LiteralPatternConverter::convert(const LoggingEvent &rLoggingEvent) const
	{
		Q_UNUSED(rLoggingEvent);
		return mLiteral;
	}


	QDebug LiteralPatternConverter::debug(QDebug &rDebug) const
	{
			rDebug.nospace() << "LiteralPatternConverter("
					<< mFormattingInfo
					<< "literal:" << mLiteral
					<< ")";
			return rDebug.space();
	}



	/**************************************************************************
	 * Class implementation: LoggerPatternConverter
	 **************************************************************************/


	QString LoggerPatternConverter::convert(const LoggingEvent &rLoggingEvent) const
	{
		if (!rLoggingEvent.logger())
			return QString();
		QString name = rLoggingEvent.logger()->name();
			if (mPrecision <= 0 || (name.isEmpty()))
				return name;

				const QString separator(QLatin1String("::"));

				int i = mPrecision;
			int begin = name.length();
			while ((i > 0) && (begin >= 0))
			{
				begin = name.lastIndexOf(separator, begin - name.length() - 1);
				i--;
			}
			if (begin < 0)
				begin = 0;
			else
				begin += 2;
			return name.mid(begin);
	}


	QDebug LoggerPatternConverter::debug(QDebug &rDebug) const
	{
			rDebug.nospace() << "LoggerPatternConverter("
					<< mFormattingInfo
					<< "precision:" << mPrecision
					<< ")";
			return rDebug.space();
	}



	/******************************************************************************
	 * Class implementation: MDCPatternConverter
	 ******************************************************************************/


	QString MDCPatternConverter::convert(const LoggingEvent &rLoggingEvent) const
	{
		return rLoggingEvent.mdc().value(mKey);
	}


	QDebug MDCPatternConverter::debug(QDebug &rDebug) const
	{
			rDebug.nospace() << "MDCPatternConverter("
					<< mFormattingInfo
					<< "key:" << mKey
					<< ")";
			return rDebug.space();
	}



	/**************************************************************************
	 * Implementation: Operators, Helper
	 **************************************************************************/


	#ifndef QT_NO_DEBUG_STREAM
	QDebug operator<<(QDebug debug, const PatternFormatter &rPatternFormatter)
	{
			debug.nospace() << "PatternFormatter("
					<< "pattern:" << rPatternFormatter.mPattern << " "
					<< "converters:(";
			int i;
			for (i = 0; i < rPatternFormatter.mPatternConverters.size(); i++)
			{
				if (i > 0)
					debug.nospace() << ", ";
				debug.nospace() << *rPatternFormatter.mPatternConverters.at(i);
			}
		debug.nospace() << ") )";
			return debug.space();
	}
	#endif


#ifndef QT_NO_DEBUG_STREAM
	QDebug operator<<(QDebug debug, const FormattingInfo &rFormattingInfo)
	{
		debug.nospace() << "FormattingInfo("
					<< "min:" << FormattingInfo::intToString(rFormattingInfo.mMinLength) << " "
					<< "max:" << FormattingInfo::intToString(rFormattingInfo.mMaxLength) << " "
					<< "left:" << rFormattingInfo.mLeftAligned
					<< ")";
		return debug.space();
	}
#endif // QT_NO_DEBUG_STREAM


#ifndef QT_NO_DEBUG_STREAM
	QDebug operator<<(QDebug debug, const PatternConverter &rPatternConverter)
	{
		return rPatternConverter.debug(debug);
	}
#endif // QT_NO_DEBUG_STREAM



} // namespace Log4Qt