/src/logging-log4cxx/src/main/cpp/loggingevent.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 <chrono> | 
| 19 |  | #include <log4cxx/spi/loggingevent.h> | 
| 20 |  | #include <log4cxx/ndc.h> | 
| 21 |  |  | 
| 22 |  | #include <log4cxx/level.h> | 
| 23 |  | #include <log4cxx/helpers/loglog.h> | 
| 24 |  | #include <log4cxx/helpers/system.h> | 
| 25 |  | #include <log4cxx/helpers/socket.h> | 
| 26 |  | #if !defined(LOG4CXX) | 
| 27 |  |   #define LOG4CXX 1 | 
| 28 |  | #endif | 
| 29 |  | #include <log4cxx/helpers/aprinitializer.h> | 
| 30 |  | #include <log4cxx/helpers/threadspecificdata.h> | 
| 31 |  | #include <log4cxx/helpers/bytebuffer.h> | 
| 32 |  | #include <log4cxx/helpers/messagebuffer.h> | 
| 33 |  | #include <log4cxx/helpers/date.h> | 
| 34 |  | #include <log4cxx/helpers/optional.h> | 
| 35 |  |  | 
| 36 |  | using namespace LOG4CXX_NS; | 
| 37 |  | using namespace LOG4CXX_NS::spi; | 
| 38 |  | using namespace LOG4CXX_NS::helpers; | 
| 39 |  |  | 
| 40 |  | struct LoggingEvent::LoggingEventPrivate | 
| 41 |  | { | 
| 42 |  |   LoggingEventPrivate(const ThreadSpecificData::NamePairPtr p = ThreadSpecificData::getNames()) : | 
| 43 | 0 |     timeStamp(0), | 
| 44 | 0 |     pNames(p) | 
| 45 | 0 |   { | 
| 46 | 0 |   } | 
| 47 |  |  | 
| 48 |  |   LoggingEventPrivate | 
| 49 |  |     ( const LogString& logger1 | 
| 50 |  |     , const LevelPtr& level1 | 
| 51 |  |     , const LocationInfo& locationInfo1 | 
| 52 |  |     , LogString&& message1 | 
| 53 |  |     , const ThreadSpecificData::NamePairPtr p = ThreadSpecificData::getNames() | 
| 54 |  |     ) : | 
| 55 | 0 |     logger(logger1), | 
| 56 | 0 |     level(level1), | 
| 57 | 0 |     message(std::move(message1)), | 
| 58 | 0 |     timeStamp(Date::currentTime()), | 
| 59 | 0 |     locationInfo(locationInfo1), | 
| 60 | 0 |     chronoTimeStamp(std::chrono::microseconds(timeStamp)), | 
| 61 | 0 |     pNames(p) | 
| 62 | 0 |   { | 
| 63 | 0 |   } | 
| 64 |  |  | 
| 65 |  |   LoggingEventPrivate | 
| 66 |  |     ( const LogString& logger1 | 
| 67 |  |     , const LevelPtr& level1 | 
| 68 |  |     , const LocationInfo& locationInfo1 | 
| 69 |  |     , helpers::AsyncBuffer&& messageAppenderArg | 
| 70 |  |     , const ThreadSpecificData::NamePairPtr p = ThreadSpecificData::getNames() | 
| 71 |  |     ) | 
| 72 | 0 |     : logger(logger1) | 
| 73 | 0 |     , level(level1) | 
| 74 | 0 |     , timeStamp(Date::currentTime()) | 
| 75 | 0 |     , locationInfo(locationInfo1) | 
| 76 | 0 |     , chronoTimeStamp(std::chrono::microseconds(timeStamp)) | 
| 77 | 0 |     , pNames(p) | 
| 78 | 0 |     , messageAppender(std::move(messageAppenderArg)) | 
| 79 | 0 |   { | 
| 80 | 0 |   } | 
| 81 |  |  | 
| 82 |  |   LoggingEventPrivate( | 
| 83 |  |     const LogString& logger1, const LevelPtr& level1, | 
| 84 |  |     const LogString& message1, const LocationInfo& locationInfo1, | 
| 85 |  |     const ThreadSpecificData::NamePairPtr& p = ThreadSpecificData::getNames() | 
| 86 |  |     ) : | 
| 87 | 3.76k |     logger(logger1), | 
| 88 | 3.76k |     level(level1), | 
| 89 | 3.76k |     message(message1), | 
| 90 | 3.76k |     timeStamp(Date::currentTime()), | 
| 91 | 3.76k |     locationInfo(locationInfo1), | 
| 92 | 3.76k |     chronoTimeStamp(std::chrono::microseconds(timeStamp)), | 
| 93 | 3.76k |     pNames(p) | 
| 94 | 3.76k |   { | 
| 95 | 3.76k |   } | 
| 96 |  |  | 
| 97 |  |   ~LoggingEventPrivate() | 
| 98 | 3.76k |   { | 
| 99 | 3.76k |     delete properties; | 
| 100 | 3.76k |   } | 
| 101 |  |  | 
| 102 |  |   /** | 
| 103 |  |   * The name of the logger used to make the logging request | 
| 104 |  |   **/ | 
| 105 |  |   LogString logger; | 
| 106 |  |  | 
| 107 |  |   /** severity level of logging event. */ | 
| 108 |  |   LevelPtr level; | 
| 109 |  |  | 
| 110 |  |   /** | 
| 111 |  |   * A map of String keys and String values. | 
| 112 |  |   */ | 
| 113 |  |   std::map<LogString, LogString>* properties{NULL}; | 
| 114 |  |  | 
| 115 |  |   /** The application supplied message. */ | 
| 116 |  |   LogString message; | 
| 117 |  |  | 
| 118 |  |  | 
| 119 |  |   /** The number of microseconds elapsed since 1970-01-01 | 
| 120 |  |    *  at the time this logging event was created. | 
| 121 |  |    */ | 
| 122 |  |   log4cxx_time_t timeStamp; | 
| 123 |  |  | 
| 124 |  |   /** The source code location where the logging request was made. */ | 
| 125 |  |   const spi::LocationInfo locationInfo; | 
| 126 |  |  | 
| 127 |  |   std::chrono::time_point<std::chrono::system_clock> chronoTimeStamp; | 
| 128 |  |  | 
| 129 |  |   /** | 
| 130 |  |    *  Thread names that remain valid for the lifetime of this LoggingEvent | 
| 131 |  |    *  (i.e. even after thread termination). | 
| 132 |  |    */ | 
| 133 |  |   ThreadSpecificData::NamePairPtr pNames; | 
| 134 |  |  | 
| 135 |  |   struct DiagnosticContext | 
| 136 |  |   { | 
| 137 |  |     Optional<NDC::DiagnosticContext> ctx; | 
| 138 |  |     MDC::Map map; | 
| 139 |  |   }; | 
| 140 |  |   /** | 
| 141 |  |    *  Used to hold the diagnostic context when the lifetime | 
| 142 |  |    *  of this LoggingEvent exceeds the duration of the logging request. | 
| 143 |  |    */ | 
| 144 |  |   mutable std::unique_ptr<DiagnosticContext> dc; | 
| 145 |  |  | 
| 146 |  |   /** Application supplied message builders. | 
| 147 |  |    */ | 
| 148 |  |   helpers::AsyncBuffer messageAppender; | 
| 149 |  |  | 
| 150 |  |   void renderMessage() | 
| 151 | 0 |   { | 
| 152 | 0 |     if (!this->messageAppender.empty()) | 
| 153 | 0 |     { | 
| 154 | 0 |       helpers::LogCharMessageBuffer buf; | 
| 155 | 0 |       this->messageAppender.renderMessage(buf); | 
| 156 | 0 |       this->message = buf.extract_str(buf); | 
| 157 | 0 |       this->messageAppender.clear(); | 
| 158 | 0 |     } | 
| 159 | 0 |   } | 
| 160 |  | }; | 
| 161 |  |  | 
| 162 |  | IMPLEMENT_LOG4CXX_OBJECT(LoggingEvent) | 
| 163 |  |  | 
| 164 |  |  | 
| 165 |  | // | 
| 166 |  | //   Accessor for start time. | 
| 167 |  | // | 
| 168 |  | log4cxx_time_t LoggingEvent::getStartTime() | 
| 169 | 0 | { | 
| 170 | 0 |   return APRInitializer::getStartTime(); | 
| 171 | 0 | } | 
| 172 |  |  | 
| 173 |  | LoggingEvent::LoggingEvent() : | 
| 174 | 0 |   m_priv(std::make_unique<LoggingEventPrivate>()) | 
| 175 | 0 | { | 
| 176 | 0 | } Unexecuted instantiation: log4cxx::spi::LoggingEvent::LoggingEvent()Unexecuted instantiation: log4cxx::spi::LoggingEvent::LoggingEvent() | 
| 177 |  |  | 
| 178 |  | LoggingEvent::LoggingEvent | 
| 179 |  |   ( const LogString&    logger | 
| 180 |  |   , const LevelPtr&     level | 
| 181 |  |   , const LocationInfo& location | 
| 182 |  |   , LogString&&         message | 
| 183 |  |   ) | 
| 184 | 0 |   : m_priv(std::make_unique<LoggingEventPrivate>(logger, level, location, std::move(message))) | 
| 185 | 0 | { | 
| 186 | 0 | } Unexecuted instantiation: log4cxx::spi::LoggingEvent::LoggingEvent(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&, std::__1::shared_ptr<log4cxx::Level> const&, log4cxx::spi::LocationInfo const&, std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> >&&)Unexecuted instantiation: log4cxx::spi::LoggingEvent::LoggingEvent(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&, std::__1::shared_ptr<log4cxx::Level> const&, log4cxx::spi::LocationInfo const&, std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> >&&) | 
| 187 |  |  | 
| 188 |  | LoggingEvent::LoggingEvent | 
| 189 |  |   ( const LogString&    logger | 
| 190 |  |   , const LevelPtr&     level | 
| 191 |  |   , const LocationInfo& location | 
| 192 |  |   , helpers::AsyncBuffer&& messageAppender | 
| 193 |  |   ) | 
| 194 | 0 |   : m_priv(std::make_unique<LoggingEventPrivate>(logger, level, location, std::move(messageAppender))) | 
| 195 | 0 | { | 
| 196 | 0 | } Unexecuted instantiation: log4cxx::spi::LoggingEvent::LoggingEvent(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&, std::__1::shared_ptr<log4cxx::Level> const&, log4cxx::spi::LocationInfo const&, log4cxx::helpers::AsyncBuffer&&)Unexecuted instantiation: log4cxx::spi::LoggingEvent::LoggingEvent(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&, std::__1::shared_ptr<log4cxx::Level> const&, log4cxx::spi::LocationInfo const&, log4cxx::helpers::AsyncBuffer&&) | 
| 197 |  |  | 
| 198 |  | LoggingEvent::LoggingEvent( | 
| 199 |  |   const LogString& logger1, const LevelPtr& level1, | 
| 200 |  |   const LogString& message1, const LocationInfo& locationInfo1) : | 
| 201 | 3.76k |   m_priv(std::make_unique<LoggingEventPrivate>(logger1, level1, message1, locationInfo1)) | 
| 202 | 3.76k | { | 
| 203 | 3.76k | } Unexecuted instantiation: log4cxx::spi::LoggingEvent::LoggingEvent(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&, std::__1::shared_ptr<log4cxx::Level> const&, std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&, log4cxx::spi::LocationInfo const&)log4cxx::spi::LoggingEvent::LoggingEvent(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&, std::__1::shared_ptr<log4cxx::Level> const&, std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&, log4cxx::spi::LocationInfo const&)| Line | Count | Source |  | 201 | 3.76k |   m_priv(std::make_unique<LoggingEventPrivate>(logger1, level1, message1, locationInfo1)) |  | 202 | 3.76k | { |  | 203 | 3.76k | } | 
 | 
| 204 |  |  | 
| 205 |  | LoggingEvent::~LoggingEvent() | 
| 206 | 3.76k | { | 
| 207 | 3.76k | } | 
| 208 |  |  | 
| 209 |  | const LogString& LoggingEvent::getThreadUserName() const | 
| 210 | 0 | { | 
| 211 | 0 |   return m_priv->pNames->threadName; | 
| 212 | 0 | } | 
| 213 |  |  | 
| 214 |  | bool LoggingEvent::getNDC(LogString& dest) const | 
| 215 | 0 | { | 
| 216 | 0 |   bool result = false; | 
| 217 |  |   // Use the copy of the diagnostic context if it exists. | 
| 218 |  |   // Otherwise use the NDC that is associated with the thread. | 
| 219 | 0 |   if (m_priv->dc) | 
| 220 | 0 |   { | 
| 221 | 0 |     result = bool(m_priv->dc->ctx); | 
| 222 | 0 |     if (result) | 
| 223 | 0 |       dest.append(NDC::getFullMessage(m_priv->dc->ctx.value())); | 
| 224 | 0 |   } | 
| 225 | 0 |   else | 
| 226 | 0 |     result = NDC::get(dest); | 
| 227 | 0 |   return result; | 
| 228 | 0 | } | 
| 229 |  |  | 
| 230 |  | bool LoggingEvent::getMDC(const LogString& key, LogString& dest) const | 
| 231 | 0 | { | 
| 232 | 0 |   bool result = false; | 
| 233 |  |   // Use the copy of the diagnostic context if it exists. | 
| 234 |  |   // Otherwise use the MDC that is associated with the thread. | 
| 235 | 0 |   if (m_priv->dc) | 
| 236 | 0 |   { | 
| 237 | 0 |     auto& map = m_priv->dc->map; | 
| 238 | 0 |     auto it = map.find(key); | 
| 239 | 0 |     if (it != map.end() && !it->second.empty()) | 
| 240 | 0 |     { | 
| 241 | 0 |       dest.append(it->second); | 
| 242 | 0 |       result = true; | 
| 243 | 0 |     } | 
| 244 | 0 |   } | 
| 245 | 0 |   else | 
| 246 | 0 |     result = MDC::get(key, dest); | 
| 247 | 0 |   return result; | 
| 248 | 0 | } | 
| 249 |  |  | 
| 250 |  | LoggingEvent::KeySet LoggingEvent::getMDCKeySet() const | 
| 251 | 0 | { | 
| 252 | 0 |   LoggingEvent::KeySet result; | 
| 253 | 0 |   if (m_priv->dc) | 
| 254 | 0 |   { | 
| 255 | 0 |     for (auto const& item : m_priv->dc->map) | 
| 256 | 0 |       result.push_back(item.first); | 
| 257 | 0 |   } | 
| 258 | 0 |   else if (auto pData = ThreadSpecificData::getCurrentData()) | 
| 259 | 0 |   { | 
| 260 | 0 |     for (auto const& item : pData->getMap()) | 
| 261 | 0 |       result.push_back(item.first); | 
| 262 | 0 |   } | 
| 263 | 0 |   return result; | 
| 264 | 0 | } | 
| 265 |  |  | 
| 266 |  | void LoggingEvent::LoadDC() const | 
| 267 | 0 | { | 
| 268 | 0 |   m_priv->dc = std::make_unique<LoggingEventPrivate::DiagnosticContext>(); | 
| 269 | 0 |   if (auto pData = ThreadSpecificData::getCurrentData()) | 
| 270 | 0 |   { | 
| 271 | 0 |     m_priv->dc->map = pData->getMap(); | 
| 272 | 0 |     auto& stack = pData->getStack(); | 
| 273 | 0 |     if (!stack.empty()) | 
| 274 | 0 |       m_priv->dc->ctx = stack.top(); | 
| 275 | 0 |   } | 
| 276 | 0 | } | 
| 277 |  |  | 
| 278 |  | #if LOG4CXX_ABI_VERSION <= 15 | 
| 279 |  | void LoggingEvent::getMDCCopy() const | 
| 280 | 0 | { | 
| 281 | 0 |   if (!m_priv->dc) | 
| 282 | 0 |     LoadDC(); | 
| 283 | 0 | } | 
| 284 |  | #endif | 
| 285 |  |  | 
| 286 |  | bool LoggingEvent::getProperty(const LogString& key, LogString& dest) const | 
| 287 | 0 | { | 
| 288 | 0 |   if (m_priv->properties == 0) | 
| 289 | 0 |   { | 
| 290 | 0 |     return false; | 
| 291 | 0 |   } | 
| 292 |  |  | 
| 293 | 0 |   std::map<LogString, LogString>::const_iterator  it = m_priv->properties->find(key); | 
| 294 |  | 
 | 
| 295 | 0 |   if (it != m_priv->properties->end()) | 
| 296 | 0 |   { | 
| 297 | 0 |     dest.append(it->second); | 
| 298 | 0 |     return true; | 
| 299 | 0 |   } | 
| 300 |  |  | 
| 301 | 0 |   return false; | 
| 302 | 0 | } | 
| 303 |  |  | 
| 304 |  | LoggingEvent::KeySet LoggingEvent::getPropertyKeySet() const | 
| 305 | 0 | { | 
| 306 | 0 |   LoggingEvent::KeySet set; | 
| 307 |  | 
 | 
| 308 | 0 |   if (m_priv->properties) | 
| 309 | 0 |   { | 
| 310 | 0 |     for (auto item : *m_priv->properties) | 
| 311 | 0 |     { | 
| 312 | 0 |       set.push_back(item.first); | 
| 313 | 0 |     } | 
| 314 | 0 |   } | 
| 315 |  | 
 | 
| 316 | 0 |   return set; | 
| 317 | 0 | } | 
| 318 |  |  | 
| 319 |  | void LoggingEvent::renderMessage() | 
| 320 | 0 | { | 
| 321 | 0 |   m_priv->renderMessage(); | 
| 322 | 0 | } | 
| 323 |  |  | 
| 324 |  | void LoggingEvent::setProperty(const LogString& key, const LogString& value) | 
| 325 | 0 | { | 
| 326 | 0 |   if (m_priv->properties == 0) | 
| 327 | 0 |   { | 
| 328 | 0 |     m_priv->properties = new std::map<LogString, LogString>; | 
| 329 | 0 |   } | 
| 330 |  | 
 | 
| 331 | 0 |   (*m_priv->properties)[key] = value; | 
| 332 | 0 | } | 
| 333 |  |  | 
| 334 |  | const LevelPtr& LoggingEvent::getLevel() const | 
| 335 | 0 | { | 
| 336 | 0 |   return m_priv->level; | 
| 337 | 0 | } | 
| 338 |  |  | 
| 339 |  | const LogString& LoggingEvent::getLoggerName() const | 
| 340 | 0 | { | 
| 341 | 0 |   return m_priv->logger; | 
| 342 | 0 | } | 
| 343 |  |  | 
| 344 |  | const LogString& LoggingEvent::getMessage() const | 
| 345 | 0 | { | 
| 346 | 0 |   return m_priv->message; | 
| 347 | 0 | } | 
| 348 |  |  | 
| 349 |  | const LogString& LoggingEvent::getRenderedMessage() const | 
| 350 | 0 | { | 
| 351 | 0 |   m_priv->renderMessage(); | 
| 352 | 0 |   return m_priv->message; | 
| 353 | 0 | } | 
| 354 |  |  | 
| 355 |  | const LogString& LoggingEvent::getThreadName() const | 
| 356 | 0 | { | 
| 357 | 0 |   return m_priv->pNames->idString; | 
| 358 | 0 | } | 
| 359 |  |  | 
| 360 |  | log4cxx_time_t LoggingEvent::getTimeStamp() const | 
| 361 | 0 | { | 
| 362 | 0 |   return m_priv->timeStamp; | 
| 363 | 0 | } | 
| 364 |  |  | 
| 365 |  | const LOG4CXX_NS::spi::LocationInfo& LoggingEvent::getLocationInformation() const | 
| 366 | 0 | { | 
| 367 | 0 |   return m_priv->locationInfo; | 
| 368 | 0 | } | 
| 369 |  |  | 
| 370 | 0 | std::chrono::time_point<std::chrono::system_clock> LoggingEvent::getChronoTimeStamp() const{ | 
| 371 | 0 |   return m_priv->chronoTimeStamp; | 
| 372 | 0 | } | 
| 373 |  |  |