1 | // |
2 | // PatternFormatter.cpp |
3 | // |
4 | // Library: Foundation |
5 | // Package: Logging |
6 | // Module: PatternFormatter |
7 | // |
8 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
9 | // and Contributors. |
10 | // |
11 | // SPDX-License-Identifier: BSL-1.0 |
12 | // |
13 | |
14 | |
15 | #include "Poco/PatternFormatter.h" |
16 | #include "Poco/Message.h" |
17 | #include "Poco/NumberFormatter.h" |
18 | #include "Poco/DateTimeFormat.h" |
19 | #include "Poco/DateTimeFormatter.h" |
20 | #include "Poco/DateTime.h" |
21 | #include "Poco/Timestamp.h" |
22 | #include "Poco/Timezone.h" |
23 | #include "Poco/Environment.h" |
24 | #include "Poco/NumberParser.h" |
25 | #include "Poco/StringTokenizer.h" |
26 | |
27 | |
28 | namespace Poco { |
29 | |
30 | |
31 | const std::string PatternFormatter::PROP_PATTERN = "pattern" ; |
32 | const std::string PatternFormatter::PROP_TIMES = "times" ; |
33 | const std::string PatternFormatter::PROP_PRIORITY_NAMES = "priorityNames" ; |
34 | |
35 | |
36 | PatternFormatter::PatternFormatter(): |
37 | _localTime(false) |
38 | { |
39 | parsePriorityNames(); |
40 | } |
41 | |
42 | |
43 | PatternFormatter::PatternFormatter(const std::string& rFormat): |
44 | _localTime(false), |
45 | _pattern(rFormat) |
46 | { |
47 | parsePriorityNames(); |
48 | parsePattern(); |
49 | } |
50 | |
51 | |
52 | PatternFormatter::~PatternFormatter() |
53 | { |
54 | } |
55 | |
56 | |
57 | void PatternFormatter::(const Message& msg, std::string& text) |
58 | { |
59 | Timestamp timestamp = msg.getTime(); |
60 | bool localTime = _localTime; |
61 | if (localTime) |
62 | { |
63 | timestamp += Timezone::utcOffset()*Timestamp::resolution(); |
64 | timestamp += Timezone::dst()*Timestamp::resolution(); |
65 | } |
66 | DateTime dateTime = timestamp; |
67 | for (std::vector<PatternAction>::iterator ip = _patternActions.begin(); ip != _patternActions.end(); ++ip) |
68 | { |
69 | text.append(ip->prepend); |
70 | switch (ip->key) |
71 | { |
72 | case 's': text.append(msg.getSource()); break; |
73 | case 't': text.append(msg.getText()); break; |
74 | case 'l': NumberFormatter::append(text, (int) msg.getPriority()); break; |
75 | case 'p': text.append(getPriorityName((int) msg.getPriority())); break; |
76 | case 'q': text += getPriorityName((int) msg.getPriority()).at(0); break; |
77 | case 'P': NumberFormatter::append(text, static_cast<Poco::Int64>(msg.getPid())); break; |
78 | case 'I': NumberFormatter::append(text, static_cast<Poco::Int64>(msg.getTid())); break; |
79 | case 'T': text.append(msg.getThread()); break; |
80 | case 'O': NumberFormatter::append(text, msg.getOsTid()); break; |
81 | case 'N': text.append(Environment::nodeName()); break; |
82 | case 'U': text.append(msg.getSourceFile() ? msg.getSourceFile() : "" ); break; |
83 | case 'u': NumberFormatter::append(text, msg.getSourceLine()); break; |
84 | case 'w': text.append(DateTimeFormat::WEEKDAY_NAMES[dateTime.dayOfWeek()], 0, 3); break; |
85 | case 'W': text.append(DateTimeFormat::WEEKDAY_NAMES[dateTime.dayOfWeek()]); break; |
86 | case 'b': text.append(DateTimeFormat::MONTH_NAMES[dateTime.month() - 1], 0, 3); break; |
87 | case 'B': text.append(DateTimeFormat::MONTH_NAMES[dateTime.month() - 1]); break; |
88 | case 'd': NumberFormatter::append0(text, dateTime.day(), 2); break; |
89 | case 'e': NumberFormatter::append(text, dateTime.day()); break; |
90 | case 'f': NumberFormatter::append(text, dateTime.day(), 2); break; |
91 | case 'm': NumberFormatter::append0(text, dateTime.month(), 2); break; |
92 | case 'n': NumberFormatter::append(text, dateTime.month()); break; |
93 | case 'o': NumberFormatter::append(text, dateTime.month(), 2); break; |
94 | case 'y': NumberFormatter::append0(text, dateTime.year() % 100, 2); break; |
95 | case 'Y': NumberFormatter::append0(text, dateTime.year(), 4); break; |
96 | case 'H': NumberFormatter::append0(text, dateTime.hour(), 2); break; |
97 | case 'h': NumberFormatter::append0(text, dateTime.hourAMPM(), 2); break; |
98 | case 'a': text.append(dateTime.isAM() ? "am" : "pm" ); break; |
99 | case 'A': text.append(dateTime.isAM() ? "AM" : "PM" ); break; |
100 | case 'M': NumberFormatter::append0(text, dateTime.minute(), 2); break; |
101 | case 'S': NumberFormatter::append0(text, dateTime.second(), 2); break; |
102 | case 'i': NumberFormatter::append0(text, dateTime.millisecond(), 3); break; |
103 | case 'c': NumberFormatter::append(text, dateTime.millisecond()/100); break; |
104 | case 'F': NumberFormatter::append0(text, dateTime.millisecond()*1000 + dateTime.microsecond(), 6); break; |
105 | case 'z': text.append(DateTimeFormatter::tzdISO(localTime ? Timezone::tzd() : DateTimeFormatter::UTC)); break; |
106 | case 'Z': text.append(DateTimeFormatter::tzdRFC(localTime ? Timezone::tzd() : DateTimeFormatter::UTC)); break; |
107 | case 'E': NumberFormatter::append(text, static_cast<Poco::Int64>(msg.getTime().epochTime())); break; |
108 | case 'v': |
109 | if (ip->length > msg.getSource().length()) //append spaces |
110 | text.append(msg.getSource()).append(ip->length - msg.getSource().length(), ' '); |
111 | else if (ip->length && ip->length < msg.getSource().length()) // crop |
112 | text.append(msg.getSource(), msg.getSource().length()-ip->length, ip->length); |
113 | else |
114 | text.append(msg.getSource()); |
115 | break; |
116 | case 'x': |
117 | try |
118 | { |
119 | text.append(msg[ip->property]); |
120 | } |
121 | catch (...) |
122 | { |
123 | } |
124 | break; |
125 | case 'L': |
126 | if (!localTime) |
127 | { |
128 | localTime = true; |
129 | timestamp += Timezone::utcOffset()*Timestamp::resolution(); |
130 | timestamp += Timezone::dst()*Timestamp::resolution(); |
131 | dateTime = timestamp; |
132 | } |
133 | break; |
134 | } |
135 | } |
136 | } |
137 | |
138 | |
139 | void PatternFormatter::parsePattern() |
140 | { |
141 | _patternActions.clear(); |
142 | std::string::const_iterator it = _pattern.begin(); |
143 | std::string::const_iterator end = _pattern.end(); |
144 | PatternAction endAct; |
145 | while (it != end) |
146 | { |
147 | if (*it == '%') |
148 | { |
149 | if (++it != end) |
150 | { |
151 | PatternAction act; |
152 | act.prepend = endAct.prepend; |
153 | endAct.prepend.clear(); |
154 | |
155 | if (*it == '[') |
156 | { |
157 | act.key = 'x'; |
158 | ++it; |
159 | std::string prop; |
160 | while (it != end && *it != ']') prop += *it++; |
161 | if (it == end) --it; |
162 | act.property = prop; |
163 | } |
164 | else |
165 | { |
166 | act.key = *it; |
167 | if ((it + 1) != end && *(it + 1) == '[') |
168 | { |
169 | it += 2; |
170 | std::string number; |
171 | while (it != end && *it != ']') number += *it++; |
172 | if (it == end) --it; |
173 | try |
174 | { |
175 | act.length = NumberParser::parse(number); |
176 | } |
177 | catch (...) |
178 | { |
179 | } |
180 | } |
181 | } |
182 | _patternActions.push_back(act); |
183 | ++it; |
184 | } |
185 | } |
186 | else |
187 | { |
188 | endAct.prepend += *it++; |
189 | } |
190 | } |
191 | if (endAct.prepend.size()) |
192 | { |
193 | _patternActions.push_back(endAct); |
194 | } |
195 | } |
196 | |
197 | |
198 | void PatternFormatter::setProperty(const std::string& name, const std::string& value) |
199 | { |
200 | if (name == PROP_PATTERN) |
201 | { |
202 | _pattern = value; |
203 | parsePattern(); |
204 | } |
205 | else if (name == PROP_TIMES) |
206 | { |
207 | _localTime = (value == "local" ); |
208 | } |
209 | else if (name == PROP_PRIORITY_NAMES) |
210 | { |
211 | _priorityNames = value; |
212 | parsePriorityNames(); |
213 | } |
214 | else |
215 | { |
216 | Formatter::setProperty(name, value); |
217 | } |
218 | } |
219 | |
220 | |
221 | std::string PatternFormatter::getProperty(const std::string& name) const |
222 | { |
223 | if (name == PROP_PATTERN) |
224 | return _pattern; |
225 | else if (name == PROP_TIMES) |
226 | return _localTime ? "local" : "UTC" ; |
227 | else if (name == PROP_PRIORITY_NAMES) |
228 | return _priorityNames; |
229 | else |
230 | return Formatter::getProperty(name); |
231 | } |
232 | |
233 | |
234 | namespace |
235 | { |
236 | static std::string priorities[] = |
237 | { |
238 | "" , |
239 | "Fatal" , |
240 | "Critical" , |
241 | "Error" , |
242 | "Warning" , |
243 | "Notice" , |
244 | "Information" , |
245 | "Debug" , |
246 | "Trace" |
247 | }; |
248 | } |
249 | |
250 | |
251 | void PatternFormatter::parsePriorityNames() |
252 | { |
253 | for (int i = 0; i <= 8; i++) |
254 | { |
255 | _priorities[i] = priorities[i]; |
256 | } |
257 | if (!_priorityNames.empty()) |
258 | { |
259 | StringTokenizer st(_priorityNames, ",;" , StringTokenizer::TOK_TRIM); |
260 | if (st.count() == 8) |
261 | { |
262 | for (int i = 1; i <= 8; i++) |
263 | { |
264 | _priorities[i] = st[i - 1]; |
265 | } |
266 | } |
267 | else throw Poco::SyntaxException("priorityNames property must specify a comma-separated list of 8 property names" ); |
268 | } |
269 | } |
270 | |
271 | |
272 | const std::string& PatternFormatter::getPriorityName(int prio) |
273 | { |
274 | poco_assert (1 <= prio && prio <= 8); |
275 | return _priorities[prio]; |
276 | } |
277 | |
278 | |
279 | } // namespace Poco |
280 | |