/src/logging-log4cxx/src/main/cpp/messagepatternconverter.cpp
Line | Count | Source |
1 | | /* |
2 | | * Licensed to the Apache Software Foundation (ASF) under one or more |
3 | | * contributor license agreements. See the NOTICE file distributed with |
4 | | * this work for additional information regarding copyright ownership. |
5 | | * The ASF licenses this file to You under the Apache License, Version 2.0 |
6 | | * (the "License"); you may not use this file except in compliance with |
7 | | * the License. You may obtain a copy of the License at |
8 | | * |
9 | | * http://www.apache.org/licenses/LICENSE-2.0 |
10 | | * |
11 | | * Unless required by applicable law or agreed to in writing, software |
12 | | * distributed under the License is distributed on an "AS IS" BASIS, |
13 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 | | * See the License for the specific language governing permissions and |
15 | | * limitations under the License. |
16 | | */ |
17 | | |
18 | | #include <log4cxx/logstring.h> |
19 | | #include <log4cxx/pattern/messagepatternconverter.h> |
20 | | #include <log4cxx/spi/loggingevent.h> |
21 | | #include <log4cxx/spi/location/locationinfo.h> |
22 | | |
23 | | |
24 | | using namespace LOG4CXX_NS; |
25 | | using namespace LOG4CXX_NS::pattern; |
26 | | |
27 | | IMPLEMENT_LOG4CXX_OBJECT(MessagePatternConverter) |
28 | | |
29 | | namespace { |
30 | | /** |
31 | | * Formats the message of an logging event for a quoted context |
32 | | */ |
33 | | class QuotedMessagePatternConverter : public LoggingEventPatternConverter |
34 | | { |
35 | | logchar m_quote; |
36 | | public: |
37 | | QuotedMessagePatternConverter(logchar quote) |
38 | 940 | : LoggingEventPatternConverter(LOG4CXX_STR("Message"), LOG4CXX_STR("quoted")) |
39 | 940 | , m_quote(quote) |
40 | 940 | {} |
41 | | |
42 | | using LoggingEventPatternConverter::format; |
43 | | |
44 | | // Duplicate any quote character in the event message |
45 | | void format( LOG4CXX_FORMAT_EVENT_FORMAL_PARAMETERS ) const override |
46 | 0 | { |
47 | 0 | auto& input = event->getRenderedMessage(); |
48 | 0 | size_t endIndex, startIndex = 0; |
49 | 0 | while ((endIndex = input.find(m_quote, startIndex)) != input.npos) |
50 | 0 | { |
51 | 0 | toAppendTo.append(input.substr(startIndex, endIndex - startIndex + 1)); |
52 | 0 | toAppendTo += m_quote; |
53 | 0 | startIndex = endIndex + 1; |
54 | 0 | } |
55 | 0 | toAppendTo.append(input.substr(startIndex)); |
56 | 0 | } |
57 | | }; |
58 | | } |
59 | | |
60 | | MessagePatternConverter::MessagePatternConverter() |
61 | 20.6k | : LoggingEventPatternConverter(LOG4CXX_STR("Message") |
62 | 20.6k | , LOG4CXX_STR("message")) |
63 | 20.6k | { |
64 | 20.6k | } Unexecuted instantiation: log4cxx::pattern::MessagePatternConverter::MessagePatternConverter() log4cxx::pattern::MessagePatternConverter::MessagePatternConverter() Line | Count | Source | 61 | 20.6k | : LoggingEventPatternConverter(LOG4CXX_STR("Message") | 62 | 20.6k | , LOG4CXX_STR("message")) | 63 | 20.6k | { | 64 | 20.6k | } |
|
65 | | |
66 | | PatternConverterPtr MessagePatternConverter::newInstance( |
67 | | const std::vector<LogString>& options) |
68 | 21.5k | { |
69 | 21.5k | if (options.empty() || options.front().empty()) |
70 | 20.6k | { |
71 | 20.6k | return std::make_shared<MessagePatternConverter>(); |
72 | 20.6k | } |
73 | 940 | return std::make_shared<QuotedMessagePatternConverter>(options.front().front()); |
74 | 21.5k | } |
75 | | |
76 | | void MessagePatternConverter::format( LOG4CXX_FORMAT_EVENT_FORMAL_PARAMETERS ) const |
77 | 0 | { |
78 | 0 | auto& msg = event->getRenderedMessage(); |
79 | 0 | auto& info = getFormattingInfo(); |
80 | 0 | if (info.getMaxLength() < msg.length()) |
81 | 0 | toAppendTo.append(&msg[msg.length() - info.getMaxLength()], info.getMaxLength()); |
82 | 0 | else |
83 | 0 | toAppendTo.append(msg); |
84 | 0 | } |
85 | | |