/src/logging-log4cxx/src/main/cpp/mdcpatternconverter.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/pattern/mdcpatternconverter.h> | 
| 19 |  | #include <log4cxx/private/patternconverter_priv.h> | 
| 20 |  | #include <log4cxx/spi/loggingevent.h> | 
| 21 |  | #include <log4cxx/jsonlayout.h> | 
| 22 |  |  | 
| 23 |  | using namespace LOG4CXX_NS; | 
| 24 |  | using namespace LOG4CXX_NS::pattern; | 
| 25 |  |  | 
| 26 |  | IMPLEMENT_LOG4CXX_OBJECT(MDCPatternConverter) | 
| 27 |  |  | 
| 28 |  | MDCPatternConverter::MDCPatternConverter | 
| 29 |  |   ( const LogString&              name | 
| 30 |  |   , const LogString&              style | 
| 31 |  |   , const std::vector<LogString>& options | 
| 32 |  |   ) | 
| 33 | 0 |   : LoggingEventPatternConverter(std::make_unique<PatternConverter::PatternConverterPrivate>(name, style)) | 
| 34 | 0 | { | 
| 35 | 0 | } Unexecuted instantiation: log4cxx::pattern::MDCPatternConverter::MDCPatternConverter(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&)Unexecuted instantiation: log4cxx::pattern::MDCPatternConverter::MDCPatternConverter(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&) | 
| 36 |  |  | 
| 37 |  | PatternConverterPtr MDCPatternConverter::newInstance( | 
| 38 |  |   const std::vector<LogString>& options) | 
| 39 | 0 | { | 
| 40 | 0 |   if (options.empty()) | 
| 41 | 0 |   { | 
| 42 | 0 |     static helpers::WideLife<PatternConverterPtr> def = std::make_shared<MDCPatternConverter>(); | 
| 43 | 0 |     return def; | 
| 44 | 0 |   } | 
| 45 | 0 |   return std::make_shared<MDCPatternConverter>(LogString(), options.front()); | 
| 46 | 0 | } | 
| 47 |  |  | 
| 48 |  | void MDCPatternConverter::format | 
| 49 |  |   ( const spi::LoggingEventPtr& event | 
| 50 |  |   , LogString&                  toAppendTo | 
| 51 |  |   , helpers::Pool&           /* p */ | 
| 52 |  |   ) const | 
| 53 | 0 | { | 
| 54 | 0 |   size_t startIndex = toAppendTo.size(); | 
| 55 | 0 |   if (m_priv->name.empty()) // Full MDC required? | 
| 56 | 0 |   { | 
| 57 | 0 |     bool first = true; | 
| 58 | 0 |     for (auto key : event->getMDCKeySet()) | 
| 59 | 0 |     { | 
| 60 | 0 |       toAppendTo.append(first ? LOG4CXX_STR("{") : LOG4CXX_STR(",")); | 
| 61 | 0 |       JSONLayout::appendItem(key, toAppendTo); | 
| 62 | 0 |       toAppendTo.append(LOG4CXX_STR(":")); | 
| 63 | 0 |       LogString value; | 
| 64 | 0 |       event->getMDC(key, value); | 
| 65 | 0 |       JSONLayout::appendItem(value, toAppendTo); | 
| 66 | 0 |       first = false; | 
| 67 | 0 |     } | 
| 68 | 0 |     if (!first) | 
| 69 | 0 |       toAppendTo.append(LOG4CXX_STR("}")); | 
| 70 | 0 |   } | 
| 71 | 0 |   else | 
| 72 | 0 |     event->getMDC(m_priv->name, toAppendTo); | 
| 73 | 0 |   if (!m_priv->style.empty()) // In a quoted context? | 
| 74 | 0 |   { | 
| 75 | 0 |     auto quote = m_priv->style.front(); | 
| 76 | 0 |     size_t endIndex; | 
| 77 | 0 |     while ((endIndex = toAppendTo.find(quote, startIndex)) != toAppendTo.npos) | 
| 78 | 0 |     { | 
| 79 | 0 |       toAppendTo.insert(endIndex + 1, 1, quote); | 
| 80 | 0 |       startIndex = endIndex + 2; | 
| 81 | 0 |     } | 
| 82 | 0 |   } | 
| 83 | 0 | } |