1 | // |
---|
2 | // PatternFormatter.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/include/Poco/PatternFormatter.h#3 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: Logging |
---|
8 | // Module: PatternFormatter |
---|
9 | // |
---|
10 | // Definition of the PatternFormatter class. |
---|
11 | // |
---|
12 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
---|
13 | // and Contributors. |
---|
14 | // |
---|
15 | // Permission is hereby granted, free of charge, to any person or organization |
---|
16 | // obtaining a copy of the software and accompanying documentation covered by |
---|
17 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
18 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
19 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
20 | // do so, all subject to the following: |
---|
21 | // |
---|
22 | // The copyright notices in the Software and this entire statement, including |
---|
23 | // the above license grant, this restriction and the following disclaimer, |
---|
24 | // must be included in all copies of the Software, in whole or in part, and |
---|
25 | // all derivative works of the Software, unless such copies or derivative |
---|
26 | // works are solely in the form of machine-executable object code generated by |
---|
27 | // a source language processor. |
---|
28 | // |
---|
29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
35 | // DEALINGS IN THE SOFTWARE. |
---|
36 | // |
---|
37 | |
---|
38 | |
---|
39 | #ifndef Foundation_PatternFormatter_INCLUDED |
---|
40 | #define Foundation_PatternFormatter_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Foundation.h" |
---|
44 | #include "Poco/Formatter.h" |
---|
45 | #include "Poco/Message.h" |
---|
46 | |
---|
47 | |
---|
48 | namespace Poco { |
---|
49 | |
---|
50 | |
---|
51 | class Foundation_API PatternFormatter: public Formatter |
---|
52 | /// This Formatter allows for custom formatting of |
---|
53 | /// log messages based on format patterns. |
---|
54 | /// |
---|
55 | /// The format pattern is used as a template to format the message and |
---|
56 | /// is copied character by character except for the following special characters, |
---|
57 | /// which are replaced by the corresponding value. |
---|
58 | /// |
---|
59 | /// * %s - message source |
---|
60 | /// * %t - message text |
---|
61 | /// * %l - message priority level (1 .. 7) |
---|
62 | /// * %p - message priority (Fatal, Critical, Error, Warning, Notice, Information, Debug, Trace) |
---|
63 | /// * %q - abbreviated message priority (F, C, E, W,N, I, D, T) |
---|
64 | /// * %P - message process identifier |
---|
65 | /// * %T - message thread name |
---|
66 | /// * %I - message thread identifier (numeric) |
---|
67 | /// * %N - node or host name |
---|
68 | /// * %w - message date/time abbreviated weekday (Mon, Tue, ...) |
---|
69 | /// * %W - message date/time full weekday (Monday, Tuesday, ...) |
---|
70 | /// * %b - message date/time abbreviated month (Jan, Feb, ...) |
---|
71 | /// * %B - message date/time full month (January, February, ...) |
---|
72 | /// * %d - message date/time zero-padded day of month (01 .. 31) |
---|
73 | /// * %e - message date/time day of month (1 .. 31) |
---|
74 | /// * %f - message date/time space-padded day of month ( 1 .. 31) |
---|
75 | /// * %m - message date/time zero-padded month (01 .. 12) |
---|
76 | /// * %n - message date/time month (1 .. 12) |
---|
77 | /// * %o - message date/time space-padded month ( 1 .. 12) |
---|
78 | /// * %y - message date/time year without century (70) |
---|
79 | /// * %Y - message date/time year with century (1970) |
---|
80 | /// * %H - message date/time hour (00 .. 23) |
---|
81 | /// * %h - message date/time hour (00 .. 12) |
---|
82 | /// * %a - message date/time am/pm |
---|
83 | /// * %A - message date/time AM/PM |
---|
84 | /// * %M - message date/time minute (00 .. 59) |
---|
85 | /// * %S - message date/time second (00 .. 59) |
---|
86 | /// * %i - message date/time millisecond (000 .. 999) |
---|
87 | /// * %c - message date/time centisecond (0 .. 9) |
---|
88 | /// * %F - message date/time fractional seconds/microseconds (000000 - 999999) |
---|
89 | /// * %z - time zone differential in ISO 8601 format (Z or +NN.NN). |
---|
90 | /// * %Z - time zone differential in RFC format (GMT or +NNNN) |
---|
91 | /// * %[name] - the value of the message parameter with the given name |
---|
92 | /// * %% - percent sign |
---|
93 | |
---|
94 | { |
---|
95 | public: |
---|
96 | PatternFormatter(); |
---|
97 | /// Creates a PatternFormatter. |
---|
98 | /// The format pattern must be specified with |
---|
99 | /// a call to setProperty. |
---|
100 | |
---|
101 | PatternFormatter(const std::string& format); |
---|
102 | /// Creates a PatternFormatter that uses the |
---|
103 | /// given format pattern. |
---|
104 | |
---|
105 | ~PatternFormatter(); |
---|
106 | /// Destroys the PatternFormatter. |
---|
107 | |
---|
108 | void format(const Message& msg, std::string& text); |
---|
109 | /// Formats the message according to the specified |
---|
110 | /// format pattern and places the result in text. |
---|
111 | |
---|
112 | void setProperty(const std::string& name, const std::string& value); |
---|
113 | /// Sets the property with the given name to the given value. |
---|
114 | /// |
---|
115 | /// The following properties are supported: |
---|
116 | /// |
---|
117 | /// * pattern: The format pattern. See the PatternFormatter class |
---|
118 | /// for details. |
---|
119 | /// * times: Specifies whether times are adjusted for local time |
---|
120 | /// or taken as they are in UTC. Supported values are "local" and "UTC". |
---|
121 | /// |
---|
122 | /// If any other property name is given, a PropertyNotSupported |
---|
123 | /// exception is thrown. |
---|
124 | |
---|
125 | std::string getProperty(const std::string& name) const; |
---|
126 | /// Returns the value of the property with the given name or |
---|
127 | /// throws a PropertyNotSupported exception if the given |
---|
128 | /// name is not recognized. |
---|
129 | |
---|
130 | static const std::string PROP_PATTERN; |
---|
131 | static const std::string PROP_TIMES; |
---|
132 | |
---|
133 | protected: |
---|
134 | static const std::string& getPriorityName(int); |
---|
135 | /// Returns a string for the given priority value. |
---|
136 | |
---|
137 | static void fmt(std::string& str, int value); |
---|
138 | static void fmt(std::string& str, int value, int width); |
---|
139 | static void fmt0(std::string& str, int value, int width); |
---|
140 | |
---|
141 | private: |
---|
142 | bool _localTime; |
---|
143 | std::string _pattern; |
---|
144 | }; |
---|
145 | |
---|
146 | |
---|
147 | } // namespace Poco |
---|
148 | |
---|
149 | |
---|
150 | #endif // Foundation_PatternFormatter_INCLUDED |
---|