1//
2// PatternFormatter.h
3//
4// Library: Foundation
5// Package: Logging
6// Module: PatternFormatter
7//
8// Definition of the PatternFormatter class.
9//
10// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
11// and Contributors.
12//
13// SPDX-License-Identifier: BSL-1.0
14//
15
16
17#ifndef Foundation_PatternFormatter_INCLUDED
18#define Foundation_PatternFormatter_INCLUDED
19
20
21#include "Poco/Foundation.h"
22#include "Poco/Formatter.h"
23#include "Poco/Message.h"
24#include <vector>
25
26
27namespace Poco {
28
29
30class Foundation_API PatternFormatter: public Formatter
31 /// This Formatter allows for custom formatting of
32 /// log messages based on format patterns.
33 ///
34 /// The format pattern is used as a template to format the message and
35 /// is copied character by character except for the following special characters,
36 /// which are replaced by the corresponding value.
37 ///
38 /// * %s - message source
39 /// * %t - message text
40 /// * %l - message priority level (1 .. 7)
41 /// * %p - message priority (Fatal, Critical, Error, Warning, Notice, Information, Debug, Trace)
42 /// * %q - abbreviated message priority (F, C, E, W, N, I, D, T)
43 /// * %P - message process identifier
44 /// * %T - message thread name
45 /// * %I - message thread identifier (numeric)
46 /// * %O - message thread OS identifier (numeric)
47 /// * %N - node or host name
48 /// * %U - message source file path (empty string if not set)
49 /// * %u - message source line number (0 if not set)
50 /// * %w - message date/time abbreviated weekday (Mon, Tue, ...)
51 /// * %W - message date/time full weekday (Monday, Tuesday, ...)
52 /// * %b - message date/time abbreviated month (Jan, Feb, ...)
53 /// * %B - message date/time full month (January, February, ...)
54 /// * %d - message date/time zero-padded day of month (01 .. 31)
55 /// * %e - message date/time day of month (1 .. 31)
56 /// * %f - message date/time space-padded day of month ( 1 .. 31)
57 /// * %m - message date/time zero-padded month (01 .. 12)
58 /// * %n - message date/time month (1 .. 12)
59 /// * %o - message date/time space-padded month ( 1 .. 12)
60 /// * %y - message date/time year without century (70)
61 /// * %Y - message date/time year with century (1970)
62 /// * %H - message date/time hour (00 .. 23)
63 /// * %h - message date/time hour (00 .. 12)
64 /// * %a - message date/time am/pm
65 /// * %A - message date/time AM/PM
66 /// * %M - message date/time minute (00 .. 59)
67 /// * %S - message date/time second (00 .. 59)
68 /// * %i - message date/time millisecond (000 .. 999)
69 /// * %c - message date/time centisecond (0 .. 9)
70 /// * %F - message date/time fractional seconds/microseconds (000000 - 999999)
71 /// * %z - time zone differential in ISO 8601 format (Z or +NN.NN)
72 /// * %Z - time zone differential in RFC format (GMT or +NNNN)
73 /// * %L - convert time to local time (must be specified before any date/time specifier; does not itself output anything)
74 /// * %E - epoch time (UTC, seconds since midnight, January 1, 1970)
75 /// * %v[width] - the message source (%s) but text length is padded/cropped to 'width'
76 /// * %[name] - the value of the message parameter with the given name
77 /// * %% - percent sign
78
79{
80public:
81 typedef AutoPtr<PatternFormatter> Ptr;
82
83 PatternFormatter();
84 /// Creates a PatternFormatter.
85 /// The format pattern must be specified with
86 /// a call to setProperty.
87
88 PatternFormatter(const std::string& format);
89 /// Creates a PatternFormatter that uses the
90 /// given format pattern.
91
92 ~PatternFormatter();
93 /// Destroys the PatternFormatter.
94
95 void format(const Message& msg, std::string& text);
96 /// Formats the message according to the specified
97 /// format pattern and places the result in text.
98
99 void setProperty(const std::string& name, const std::string& value);
100 /// Sets the property with the given name to the given value.
101 ///
102 /// The following properties are supported:
103 ///
104 /// * pattern: The format pattern. See the PatternFormatter class
105 /// for details.
106 /// * times: Specifies whether times are adjusted for local time
107 /// or taken as they are in UTC. Supported values are "local" and "UTC".
108 /// * priorityNames: Provide a comma-separated list of custom priority names,
109 /// e.g. "Fatal, Critical, Error, Warning, Notice, Information, Debug, Trace"
110 ///
111 /// If any other property name is given, a PropertyNotSupported
112 /// exception is thrown.
113
114 std::string getProperty(const std::string& name) const;
115 /// Returns the value of the property with the given name or
116 /// throws a PropertyNotSupported exception if the given
117 /// name is not recognized.
118
119 static const std::string PROP_PATTERN;
120 static const std::string PROP_TIMES;
121 static const std::string PROP_PRIORITY_NAMES;
122
123protected:
124 const std::string& getPriorityName(int);
125 /// Returns a string for the given priority value.
126
127private:
128 struct PatternAction
129 {
130 PatternAction(): key(0), length(0)
131 {
132 }
133
134 char key;
135 int length;
136 std::string property;
137 std::string prepend;
138 };
139
140 void parsePattern();
141 /// Will parse the _pattern string into the vector of PatternActions,
142 /// which contains the message key, any text that needs to be written first
143 /// a property in case of %[] and required length.
144
145 void parsePriorityNames();
146
147 std::vector<PatternAction> _patternActions;
148 bool _localTime;
149 std::string _pattern;
150 std::string _priorityNames;
151 std::string _priorities[9];
152};
153
154
155} // namespace Poco
156
157
158#endif // Foundation_PatternFormatter_INCLUDED
159