source: XMLIO_V2/external/src/POCO/Foundation/PatternFormatter.cpp @ 80

Last change on this file since 80 was 80, checked in by ymipsl, 14 years ago

ajout lib externe

  • Property svn:eol-style set to native
File size: 6.2 KB
Line 
1//
2// PatternFormatter.cpp
3//
4// $Id: //poco/1.3/Foundation/src/PatternFormatter.cpp#5 $
5//
6// Library: Foundation
7// Package: Logging
8// Module:  PatternFormatter
9//
10// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
11// and Contributors.
12//
13// Permission is hereby granted, free of charge, to any person or organization
14// obtaining a copy of the software and accompanying documentation covered by
15// this license (the "Software") to use, reproduce, display, distribute,
16// execute, and transmit the Software, and to prepare derivative works of the
17// Software, and to permit third-parties to whom the Software is furnished to
18// do so, all subject to the following:
19//
20// The copyright notices in the Software and this entire statement, including
21// the above license grant, this restriction and the following disclaimer,
22// must be included in all copies of the Software, in whole or in part, and
23// all derivative works of the Software, unless such copies or derivative
24// works are solely in the form of machine-executable object code generated by
25// a source language processor.
26//
27// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
30// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
31// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
32// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
33// DEALINGS IN THE SOFTWARE.
34//
35
36
37#include <Poco/PatternFormatter.h>
38#include <Poco/Message.h>
39#include <Poco/DateTimeFormat.h>
40#include <Poco/DateTimeFormatter.h>
41#include <Poco/DateTime.h>
42#include <Poco/Timestamp.h>
43#include <Poco/Timezone.h>
44#include <Poco/Environment.h>
45#include <cstdio>
46#include <cctype>
47
48
49namespace Poco {
50
51
52const std::string PatternFormatter::PROP_PATTERN = "pattern";
53const std::string PatternFormatter::PROP_TIMES   = "times";
54
55
56PatternFormatter::PatternFormatter():
57        _localTime(false)
58{
59}
60
61
62PatternFormatter::PatternFormatter(const std::string& format):
63        _localTime(false),
64        _pattern(format)
65{
66}
67
68
69PatternFormatter::~PatternFormatter()
70{
71}
72
73
74inline void PatternFormatter::fmt(std::string& str, int value)
75{
76        char buffer[64];
77        std::sprintf(buffer, "%d", value);
78        str.append(buffer);
79}
80
81
82inline void PatternFormatter::fmt(std::string& str, int value, int width)
83{
84        char buffer[64];
85        std::sprintf(buffer, "%*d", width, value);
86        str.append(buffer);
87}
88
89
90inline void PatternFormatter::fmt0(std::string& str, int value, int width)
91{
92        char buffer[64];
93        std::sprintf(buffer, "%0*d", width, value);
94        str.append(buffer);
95}
96
97
98void PatternFormatter::format(const Message& msg, std::string& text)
99{
100        Timestamp timestamp = msg.getTime();
101        if (_localTime)
102        {
103                timestamp += Timezone::utcOffset()*Timestamp::resolution();
104                timestamp += Timezone::dst()*Timestamp::resolution();
105        }
106        DateTime dateTime = timestamp;
107        std::string::const_iterator it  = _pattern.begin();
108        std::string::const_iterator end = _pattern.end();
109        while (it != end)
110        {
111                if (*it == '%')
112                {
113                        if (++it != end)
114                        {
115                                switch (*it)
116                                {
117                                case 's': text.append(msg.getSource()); break;
118                                case 't': text.append(msg.getText()); break;
119                                case 'l': fmt(text, (int) msg.getPriority()); break;
120                                case 'p': text.append(getPriorityName((int) msg.getPriority())); break;
121                                case 'q': text += getPriorityName((int) msg.getPriority()).at(0); break;
122                                case 'P': fmt(text, msg.getPid()); break;
123                                case 'T': text.append(msg.getThread()); break;
124                                case 'I': fmt(text, msg.getTid()); break;
125                                case 'N': text.append(Environment::nodeName()); break;
126                                case 'w': text.append(DateTimeFormat::WEEKDAY_NAMES[dateTime.dayOfWeek()], 0, 3); break;
127                                case 'W': text.append(DateTimeFormat::WEEKDAY_NAMES[dateTime.dayOfWeek()]); break;
128                                case 'b': text.append(DateTimeFormat::MONTH_NAMES[dateTime.month() - 1], 0, 3); break;
129                                case 'B': text.append(DateTimeFormat::MONTH_NAMES[dateTime.month() - 1]); break;
130                                case 'd': fmt0(text, dateTime.day(), 2); break;
131                                case 'e': fmt(text, dateTime.day()); break;
132                                case 'f': fmt(text, dateTime.day(), 2); break;
133                                case 'm': fmt0(text, dateTime.month(), 2); break;
134                                case 'n': fmt(text, dateTime.month()); break;
135                                case 'o': fmt(text, dateTime.month(), 2); break;
136                                case 'y': fmt0(text, dateTime.year() % 100, 2); break;
137                                case 'Y': fmt0(text, dateTime.year(), 4); break;
138                                case 'H': fmt0(text, dateTime.hour(), 2); break;
139                                case 'h': fmt0(text, dateTime.hourAMPM(), 2); break;
140                                case 'a': text.append(dateTime.isAM() ? "am" : "pm"); break;
141                                case 'A': text.append(dateTime.isAM() ? "AM" : "PM"); break;
142                                case 'M': fmt0(text, dateTime.minute(), 2); break;
143                                case 'S': fmt0(text, dateTime.second(), 2); break;
144                                case 'i': fmt0(text, dateTime.millisecond(), 3); break;
145                                case 'c': fmt(text, dateTime.millisecond()/100); break;
146                                case 'F': fmt0(text, dateTime.millisecond()*1000 + dateTime.microsecond(), 6); break;
147                                case 'z': text.append(DateTimeFormatter::tzdISO(_localTime ? Timezone::tzd() : DateTimeFormatter::UTC)); break;
148                                case 'Z': text.append(DateTimeFormatter::tzdRFC(_localTime ? Timezone::tzd() : DateTimeFormatter::UTC)); break;
149                                case '[':
150                                {
151                                        ++it;
152                                        std::string prop;
153                                        while (it != end && *it != ']') prop += *it++;
154                                        if (it == end) --it;
155                                        try
156                                        {
157                                                text.append(msg[prop]);
158                                        }
159                                        catch (...)
160                                        {
161                                        }
162                                        break;
163                                }
164                                default: text += *it;
165                                }
166                                ++it;
167                        }
168                }
169                else text += *it++;
170        }
171}
172
173       
174void PatternFormatter::setProperty(const std::string& name, const std::string& value)
175{
176        if (name == PROP_PATTERN)
177                _pattern = value;
178        else if (name == PROP_TIMES)
179                _localTime = (value == "local");
180        else 
181                Formatter::setProperty(name, value);
182}
183
184
185std::string PatternFormatter::getProperty(const std::string& name) const
186{
187        if (name == PROP_PATTERN)
188                return _pattern;
189        else if (name == PROP_TIMES)
190                return _localTime ? "local" : "UTC";
191        else
192                return Formatter::getProperty(name);
193}
194
195
196const std::string& PatternFormatter::getPriorityName(int prio)
197{
198        static std::string priorities[] = 
199        {
200                "",
201                "Fatal",
202                "Critical",
203                "Error",
204                "Warning",
205                "Notice",
206                "Information",
207                "Debug",
208                "Trace"
209        };
210
211        poco_assert (1 <= prio && prio <= 8);   
212        return priorities[prio];
213}
214
215
216} // namespace Poco
Note: See TracBrowser for help on using the repository browser.