Coverage Report

Created: 2025-07-18 06:17

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