File: PatternLayout.cpp

package info (click to toggle)
log4cpp 1.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 4,196 kB
  • sloc: sh: 10,465; cpp: 6,695; ansic: 1,049; makefile: 288
file content (438 lines) | stat: -rw-r--r-- 14,887 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/*
 * PatternLayout.cpp
 *
 * Copyright 2002, Bastiaan Bakker. All rights reserved.
 *
 * See the COPYING file for the terms of usage and distribution.
 */

#include "PortabilityImpl.hh"

#include <log4cpp/PatternLayout.hh>
#include <log4cpp/Priority.hh>
#include <log4cpp/NDC.hh>
#include <log4cpp/TimeStamp.hh>
#include <log4cpp/FactoryParams.hh>
#include <memory>

#ifdef LOG4CPP_HAVE_SSTREAM
#include <sstream>
#else
#include <strstream>
#endif

#include <iomanip>
#include <ctime>
#include <cstdlib>
#include "Localtime.hh"

#ifdef LOG4CPP_HAVE_INT64_T
#ifdef LOG4CPP_HAVE_STDINT_H
#include <stdint.h>
#endif // LOG4CPP_HAVE_STDINT_H

#ifdef LOG4CPP_MISSING_INT64_OSTREAM_OP
/* workaround suggested at:
   http://support.microsoft.com/default.aspx?scid=kb;EN-US;q168440
*/

#include <stdio.h>
 
std::ostream& operator<<(std::ostream& os, int64_t i) {
    char buf[20];
    ::sprintf(buf,"%I64d", i);
    return os << buf;
}
#endif // LOG4CPP_MISSING_INT64_OSTREAM_OP
#endif // LOG4CPP_HAVE_INT64_T

namespace log4cpp {

    struct StringLiteralComponent : public PatternLayout::PatternComponent {
        StringLiteralComponent(const std::string& literal) :
            _literal(literal) {
        }

        virtual void append(std::ostringstream& out, const LoggingEvent& event) {
            out << _literal;
        }

        private:
        std::string _literal;
    };

    struct CategoryNameComponent : public PatternLayout::PatternComponent {
        CategoryNameComponent(std::string specifier) {
            if (specifier == "") {
                _precision = -1;
            } else {
#ifdef LOG4CPP_HAVE_SSTREAM 
                std::istringstream s(specifier);
#else
                std::istrstream s(specifier.c_str());
#endif
                s >> _precision;
            }
        }

        virtual void append(std::ostringstream& out, const LoggingEvent& event) {
            if (_precision == -1) {
                out << event.categoryName;
            } else {
                std::string::size_type begin = std::string::npos;
                for(int i = 0; i < _precision; i  ) {
                    begin = event.categoryName.rfind('.', begin - 2);
                    if (begin == std::string::npos) {
                        begin = 0;
                        break;
                    }
                    begin  ;
                }
                if (begin == std::string::npos) {
                    begin = 0;
                }
                out << event.categoryName.substr(begin);
            }
        }

        private:
        int _precision;
    };

    struct MessageComponent : public PatternLayout::PatternComponent {
        virtual void append(std::ostringstream& out, const LoggingEvent& event) {
            out << event.message;
        }
    };

    struct NDCComponent : public PatternLayout::PatternComponent {
        virtual void append(std::ostringstream& out, const LoggingEvent& event) {
            out << event.ndc;
        }
    };

    struct PriorityComponent : public PatternLayout::PatternComponent {
        virtual void append(std::ostringstream& out, const LoggingEvent& event) {
            out << Priority::getPriorityName(event.priority);
        }
    };

    struct ThreadNameComponent : public PatternLayout::PatternComponent {
        virtual void append(std::ostringstream& out, const LoggingEvent& event) {
            out << event.threadName;
        }
    };

    struct ProcessorTimeComponent : public PatternLayout::PatternComponent {
        virtual void append(std::ostringstream& out, const LoggingEvent& event) {
            out << std::clock();
        }
    };

    struct TimeStampComponent : public PatternLayout::PatternComponent {
        static const char* const FORMAT_ISO8601;
        static const char* const FORMAT_ABSOLUTE;
        static const char* const FORMAT_DATE;

        TimeStampComponent(std::string timeFormat) {
            if ((timeFormat == "") || (timeFormat == "ISO8601")) {
                timeFormat = FORMAT_ISO8601;
            } else if (timeFormat == "ABSOLUTE") {
                timeFormat = FORMAT_ABSOLUTE;
            } else if (timeFormat == "DATE") {
                timeFormat = FORMAT_DATE;
            }
            std::string::size_type pos = timeFormat.find("%l");
            if (pos == std::string::npos) {
                _printMillis = false;
                _timeFormat1 = timeFormat; 
            } else {
                _printMillis = true;
                _timeFormat1 = timeFormat.substr(0, pos);
                _timeFormat2 = timeFormat.substr(pos   2);
            }
        }

        virtual void append(std::ostringstream& out, const LoggingEvent& event) {
            struct std::tm currentTime;
            std::time_t t = event.timeStamp.getSeconds();
            localtime(&t, &currentTime);
            char formatted[100];
            std::string timeFormat;
            if (_printMillis) {
                std::ostringstream formatStream;
                formatStream << _timeFormat1 
                             << std::setw(3) << std::setfill('0')
                             << event.timeStamp.getMilliSeconds()
                             << _timeFormat2;
                timeFormat = formatStream.str();
            } else {
                timeFormat = _timeFormat1;
            }
            std::strftime(formatted, sizeof(formatted), timeFormat.c_str(), &currentTime);
            out << formatted;
        }

        private:
        std::string _timeFormat1;
        std::string _timeFormat2;
        bool _printMillis;
    };

    const char* const TimeStampComponent::FORMAT_ISO8601 = "%Y-%m-%d %H:%M:%S,%l";
    const char* const TimeStampComponent::FORMAT_ABSOLUTE = "%H:%M:%S,%l";
    const char* const TimeStampComponent::FORMAT_DATE = "%d %b %Y %H:%M:%S,%l";

    struct SecondsSinceEpochComponent : public PatternLayout::PatternComponent {
        virtual void append(std::ostringstream& out, const LoggingEvent& event) {
            out << event.timeStamp.getSeconds();
        }
    };

    struct MillisSinceEpochComponent : public PatternLayout::PatternComponent {
        virtual void append(std::ostringstream& out, const LoggingEvent& event) {
#ifdef LOG4CPP_HAVE_INT64_T
            int64_t t = event.timeStamp.getSeconds() -
                TimeStamp::getStartTime().getSeconds();
            t *= 1000;
            t  = event.timeStamp.getMilliSeconds() -
                TimeStamp::getStartTime().getMilliSeconds();
            
            out << t;
#else
            double t = event.timeStamp.getSeconds() -
                TimeStamp::getStartTime().getSeconds();
            t *= 1000;
            t  = event.timeStamp.getMilliSeconds() -
                TimeStamp::getStartTime().getMilliSeconds();
            
            out << std::setiosflags(std::ios::fixed)
                << std::setprecision(0) << t;
#endif
        }
    };

    struct FormatModifierComponent : public PatternLayout::PatternComponent {
        FormatModifierComponent(PatternLayout::PatternComponent* component,
                                size_t minWidth, size_t maxWidth, bool alignLeft) :
            _component(component) , 
            _minWidth(minWidth),
            _maxWidth(maxWidth),
            _alignLeft(alignLeft) {
        }

        virtual ~FormatModifierComponent() {
            delete _component;
        }

        virtual void append(std::ostringstream& out, const LoggingEvent& event) {
            std::ostringstream s;
            _component->append(s, event);
            std::string msg = s.str();
            if (_maxWidth > 0 && _maxWidth < msg.length()) {
                msg.erase(_maxWidth);
            }
            size_t fillCount = _minWidth - msg.length();
            if (_minWidth > msg.length()) {
                if (_alignLeft) {
                    out << msg << std::string(fillCount, ' ');
                } else {
                    out << std::string(fillCount, ' ') << msg;
                }
            } else {
                out << msg;
            }
        }

        private:
        PatternLayout::PatternComponent* _component;
        size_t _minWidth;
        size_t _maxWidth;
        bool _alignLeft;
    };

    const char* PatternLayout::DEFAULT_CONVERSION_PATTERN = "%m%n";
    const char* PatternLayout::SIMPLE_CONVERSION_PATTERN = "%p - %m%n";
    const char* PatternLayout::BASIC_CONVERSION_PATTERN = "%R %p %c %x: %m%n";
    const char* PatternLayout::TTCC_CONVERSION_PATTERN = "%r [%t] %p %c %x - %m%n";

    PatternLayout::PatternLayout() {
        try {
            setConversionPattern(DEFAULT_CONVERSION_PATTERN);
        } catch(ConfigureFailure&) {
        }
    }

    PatternLayout::~PatternLayout() {
        clearConversionPattern();
    }

    void PatternLayout::clearConversionPattern() {
        for(ComponentVector::const_iterator i = _components.begin();
            i != _components.end();   i) {
            delete (*i);
        }
        _components.clear();
        _conversionPattern = "";
    }

    void PatternLayout::setConversionPattern(const std::string& conversionPattern) {
#ifdef LOG4CPP_HAVE_SSTREAM 
        std::istringstream conversionStream(conversionPattern);
#else
        std::istrstream conversionStream(conversionPattern.c_str());
#endif
        std::string literal;

        char ch;
        PatternLayout::PatternComponent* component = NULL;
        int minWidth = 0;
        size_t maxWidth = 0;
        clearConversionPattern();
        while (conversionStream.get(ch)) {
            if (ch == '%') {
                // readPrefix;
                {
                    char ch2;
                    conversionStream.get(ch2);
                    if ((ch2 == '-') || ((ch2 >= '0') && (ch2 <= '9'))) {
                        conversionStream.putback(ch2);
                        conversionStream >> minWidth;
                        conversionStream.get(ch2);
                    } 
                    if (ch2 == '.') {
                        conversionStream >> maxWidth;
                    } else {
                        conversionStream.putback(ch2);
                    }                        
                }
                if (!conversionStream.get(ch)) {
                    std::ostringstream msg;
                    msg << "unterminated conversion specifier in '" << conversionPattern << "' at index " << conversionStream.tellg();
                    throw ConfigureFailure(msg.str());
                }
                std::string specPostfix = "";
                // read postfix
                {
                    char ch2;
                    if (conversionStream.get(ch2)) {
                        if (ch2 == '{') {
                            while(conversionStream.get(ch2) && (ch2 != '}'))
                                specPostfix  = ch2;
                        } else {
                            conversionStream.putback(ch2);
                        }
                    }
                }
                switch (ch) {
                case '%':
                    literal  = ch;
                    break;
                case 'm':
                    component = new MessageComponent();
                    break;
                case 'n':
                    {
                        std::ostringstream endline;
                        endline << std::endl;
                        literal  = endline.str();
                    }
                    break;
                case 'c':
                    component = new CategoryNameComponent(specPostfix);
                    break;
                case 'd':
                    component = new TimeStampComponent(specPostfix);
                    break;
                case 'p':
                    component = new PriorityComponent();
                    break;
                case 'r':
                    component = new MillisSinceEpochComponent();
                    break;
                case 'R':
                    component = new SecondsSinceEpochComponent();
                    break;
                case 't':
                    component = new ThreadNameComponent();
                    break;
                case 'u':
                    component = new ProcessorTimeComponent();
                    break;
                case 'x':
                    component = new NDCComponent();
                    break;
                default:
                    std::ostringstream msg;
                    msg << "unknown conversion specifier '" << ch << "' in '" << conversionPattern << "' at index " << conversionStream.tellg();
                    throw ConfigureFailure(msg.str());                    
                }
                if (component) {
                    if (!literal.empty()) {
                        _components.push_back(new StringLiteralComponent(literal));
                        literal = "";
                    }
                    if ((minWidth != 0) || (maxWidth != 0)) {
                        component = new FormatModifierComponent(component, std::abs(minWidth), maxWidth, minWidth < 0);
                        minWidth = maxWidth = 0;
                    }
                    _components.push_back(component);
                    component = NULL;
                }
            } else {
                literal  = ch;
            }
        }
        if (!literal.empty()) {
            _components.push_back(new StringLiteralComponent(literal));
        }

        _conversionPattern = conversionPattern;
    }

    std::string PatternLayout::getConversionPattern() const {
        return _conversionPattern;
    }

    std::string PatternLayout::format(const LoggingEvent& event) {
        std::ostringstream message;

        for(ComponentVector::const_iterator i = _components.begin();
            i != _components.end();   i) {
            (*i)->append(message, event);
        }

        return message.str();
    }

    std::auto_ptr<Layout> create_pattern_layout(const FactoryParams& params)
    {
       std::string pattern;
       params.get_for("pattern layout").optional("pattern", pattern);
       std::auto_ptr<Layout> result(new PatternLayout);
       PatternLayout* l = static_cast<PatternLayout*>(result.get());
       if (pattern.empty() || pattern == "default")
          return result;

       if (pattern == "simple")
       {
          l->setConversionPattern(PatternLayout::SIMPLE_CONVERSION_PATTERN);
          return result;
       }

       if (pattern == "basic")
       {
          l->setConversionPattern(PatternLayout::BASIC_CONVERSION_PATTERN);
          return result;
       }

       if (pattern == "ttcc")
       {
          l->setConversionPattern(PatternLayout::TTCC_CONVERSION_PATTERN);
          return result;
       }
       
       l->setConversionPattern(pattern);
       return result;
   }
}