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