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 | |
27 | namespace Poco { |
28 | |
29 | |
30 | class 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 | { |
80 | public: |
81 | PatternFormatter(); |
82 | /// Creates a PatternFormatter. |
83 | /// The format pattern must be specified with |
84 | /// a call to setProperty. |
85 | |
86 | PatternFormatter(const std::string& format); |
87 | /// Creates a PatternFormatter that uses the |
88 | /// given format pattern. |
89 | |
90 | ~PatternFormatter(); |
91 | /// Destroys the PatternFormatter. |
92 | |
93 | void format(const Message& msg, std::string& text); |
94 | /// Formats the message according to the specified |
95 | /// format pattern and places the result in text. |
96 | |
97 | void setProperty(const std::string& name, const std::string& value); |
98 | /// Sets the property with the given name to the given value. |
99 | /// |
100 | /// The following properties are supported: |
101 | /// |
102 | /// * pattern: The format pattern. See the PatternFormatter class |
103 | /// for details. |
104 | /// * times: Specifies whether times are adjusted for local time |
105 | /// or taken as they are in UTC. Supported values are "local" and "UTC". |
106 | /// * priorityNames: Provide a comma-separated list of custom priority names, |
107 | /// e.g. "Fatal, Critical, Error, Warning, Notice, Information, Debug, Trace" |
108 | /// |
109 | /// If any other property name is given, a PropertyNotSupported |
110 | /// exception is thrown. |
111 | |
112 | std::string getProperty(const std::string& name) const; |
113 | /// Returns the value of the property with the given name or |
114 | /// throws a PropertyNotSupported exception if the given |
115 | /// name is not recognized. |
116 | |
117 | static const std::string PROP_PATTERN; |
118 | static const std::string PROP_TIMES; |
119 | static const std::string PROP_PRIORITY_NAMES; |
120 | |
121 | protected: |
122 | const std::string& getPriorityName(int); |
123 | /// Returns a string for the given priority value. |
124 | |
125 | private: |
126 | struct PatternAction |
127 | { |
128 | PatternAction(): key(0), length(0) |
129 | { |
130 | } |
131 | |
132 | char key; |
133 | int length; |
134 | std::string property; |
135 | std::string prepend; |
136 | }; |
137 | |
138 | void parsePattern(); |
139 | /// Will parse the _pattern string into the vector of PatternActions, |
140 | /// which contains the message key, any text that needs to be written first |
141 | /// a property in case of %[] and required length. |
142 | |
143 | void parsePriorityNames(); |
144 | |
145 | std::vector<PatternAction> _patternActions; |
146 | bool _localTime; |
147 | std::string _pattern; |
148 | std::string _priorityNames; |
149 | std::string _priorities[9]; |
150 | }; |
151 | |
152 | |
153 | } // namespace Poco |
154 | |
155 | |
156 | #endif // Foundation_PatternFormatter_INCLUDED |
157 | |