Coverage Report

Created: 2025-07-18 06:17

/src/logging-log4cxx/src/main/cpp/ndc.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 <log4cxx/ndc.h>
19
#include <log4cxx/helpers/transcoder.h>
20
#include <log4cxx/helpers/threadspecificdata.h>
21
22
using namespace LOG4CXX_NS;
23
using namespace LOG4CXX_NS::helpers;
24
25
NDC::NDC(const std::string& message)
26
0
{
27
0
  push(message);
28
0
}
29
30
NDC::~NDC()
31
0
{
32
0
  pop();
33
0
}
34
35
36
const LogString& NDC::getMessage(const DiagnosticContext& ctx)
37
0
{
38
0
  return ctx.first;
39
0
}
40
41
const LogString& NDC::getFullMessage(const DiagnosticContext& ctx)
42
0
{
43
0
  return ctx.second;
44
0
}
45
46
void NDC::clear()
47
0
{
48
0
  ThreadSpecificData* data = ThreadSpecificData::getCurrentData();
49
50
0
  if (data != 0)
51
0
  {
52
0
    Stack& stack = data->getStack();
53
54
0
    while (!stack.empty())
55
0
    {
56
0
      stack.pop();
57
0
    }
58
59
0
    data->recycle();
60
0
  }
61
0
}
62
63
NDC::Stack* NDC::cloneStack()
64
0
{
65
0
  ThreadSpecificData* data = ThreadSpecificData::getCurrentData();
66
67
0
  if (data != 0)
68
0
  {
69
0
    Stack& stack = data->getStack();
70
71
0
    if (!stack.empty())
72
0
    {
73
0
      return new Stack(stack);
74
0
    }
75
0
  }
76
77
0
  return new Stack();
78
0
}
79
80
void NDC::inherit(NDC::Stack* stack)
81
0
{
82
0
  if (stack != NULL)
83
0
  {
84
0
    ThreadSpecificData::inherit(*stack);
85
0
    delete stack;
86
0
  }
87
0
}
88
89
90
bool NDC::get(LogString& dest)
91
10
{
92
10
  ThreadSpecificData* data = ThreadSpecificData::getCurrentData();
93
94
10
  if (data != 0)
95
10
  {
96
10
    Stack& stack = data->getStack();
97
98
10
    if (!stack.empty())
99
0
    {
100
0
      dest.append(getFullMessage(stack.top()));
101
0
      return true;
102
0
    }
103
104
10
    data->recycle();
105
10
  }
106
107
10
  return false;
108
10
}
109
110
int NDC::getDepth()
111
0
{
112
0
  int size = 0;
113
0
  ThreadSpecificData* data = ThreadSpecificData::getCurrentData();
114
115
0
  if (data != 0)
116
0
  {
117
0
    size = (int)data->getStack().size();
118
119
0
    if (size == 0)
120
0
    {
121
0
      data->recycle();
122
0
    }
123
0
  }
124
125
0
  return size;
126
0
}
127
128
LogString NDC::pop()
129
0
{
130
0
  ThreadSpecificData* data = ThreadSpecificData::getCurrentData();
131
132
0
  if (data != 0)
133
0
  {
134
0
    Stack& stack = data->getStack();
135
136
0
    if (!stack.empty())
137
0
    {
138
0
      LogString value(getMessage(stack.top()));
139
0
      stack.pop();
140
0
      data->recycle();
141
0
      return value;
142
0
    }
143
144
0
    data->recycle();
145
0
  }
146
147
0
  return LogString();
148
0
}
149
150
bool NDC::pop(std::string& dst)
151
0
{
152
0
  bool retval = false;
153
0
  ThreadSpecificData* data = ThreadSpecificData::getCurrentData();
154
155
0
  if (data != 0)
156
0
  {
157
0
    Stack& stack = data->getStack();
158
159
0
    if (!stack.empty())
160
0
    {
161
0
      Transcoder::encode(getMessage(stack.top()), dst);
162
0
      stack.pop();
163
0
      retval = true;
164
0
    }
165
166
0
    data->recycle();
167
0
  }
168
169
0
  return retval;
170
0
}
171
172
LogString NDC::peek()
173
0
{
174
0
  ThreadSpecificData* data = ThreadSpecificData::getCurrentData();
175
176
0
  if (data != 0)
177
0
  {
178
0
    Stack& stack = data->getStack();
179
180
0
    if (!stack.empty())
181
0
    {
182
0
      return getMessage(stack.top());
183
0
    }
184
185
0
    data->recycle();
186
0
  }
187
188
0
  return LogString();
189
0
}
190
191
bool NDC::peek(std::string& dst)
192
0
{
193
0
  ThreadSpecificData* data = ThreadSpecificData::getCurrentData();
194
195
0
  if (data != 0)
196
0
  {
197
0
    Stack& stack = data->getStack();
198
199
0
    if (!stack.empty())
200
0
    {
201
0
      Transcoder::encode(getMessage(stack.top()), dst);
202
0
      return true;
203
0
    }
204
205
0
    data->recycle();
206
0
  }
207
208
0
  return false;
209
0
}
210
211
void NDC::pushLS(const LogString& message)
212
0
{
213
0
  ThreadSpecificData::push(message);
214
0
}
215
216
void NDC::push(const std::string& message)
217
0
{
218
0
  LOG4CXX_DECODE_CHAR(msg, message);
219
0
  pushLS(msg);
220
0
}
221
222
void NDC::remove()
223
0
{
224
0
  clear();
225
0
}
226
227
bool NDC::empty()
228
0
{
229
0
  bool empty = true;
230
0
  ThreadSpecificData* data = ThreadSpecificData::getCurrentData();
231
232
0
  if (data != 0)
233
0
  {
234
0
    Stack& stack = data->getStack();
235
0
    empty = stack.empty();
236
237
0
    if (empty)
238
0
    {
239
0
      data->recycle();
240
0
    }
241
0
  }
242
243
0
  return empty;
244
0
}
245
246
#if LOG4CXX_WCHAR_T_API
247
NDC::NDC(const std::wstring& message)
248
0
{
249
0
  push(message);
250
0
}
251
252
void NDC::push(const std::wstring& message)
253
0
{
254
0
  LOG4CXX_DECODE_WCHAR(msg, message);
255
0
  pushLS(msg);
256
0
}
257
258
bool NDC::pop(std::wstring& dst)
259
0
{
260
0
  ThreadSpecificData* data = ThreadSpecificData::getCurrentData();
261
262
0
  if (data != 0)
263
0
  {
264
0
    Stack& stack = data->getStack();
265
266
0
    if (!stack.empty())
267
0
    {
268
0
      Transcoder::encode(getMessage(stack.top()), dst);
269
0
      stack.pop();
270
0
      data->recycle();
271
0
      return true;
272
0
    }
273
274
0
    data->recycle();
275
0
  }
276
277
0
  return false;
278
0
}
279
280
bool NDC::peek(std::wstring& dst)
281
0
{
282
0
  ThreadSpecificData* data = ThreadSpecificData::getCurrentData();
283
284
0
  if (data != 0)
285
0
  {
286
0
    Stack& stack = data->getStack();
287
288
0
    if (!stack.empty())
289
0
    {
290
0
      Transcoder::encode(getMessage(stack.top()), dst);
291
0
      return true;
292
0
    }
293
294
0
    data->recycle();
295
0
  }
296
297
0
  return false;
298
0
}
299
300
#endif
301
302
303
#if LOG4CXX_UNICHAR_API
304
NDC::NDC(const std::basic_string<UniChar>& message)
305
{
306
  push(message);
307
}
308
309
void NDC::push(const std::basic_string<UniChar>& message)
310
{
311
  LOG4CXX_DECODE_UNICHAR(msg, message);
312
  pushLS(msg);
313
}
314
315
bool NDC::pop(std::basic_string<UniChar>& dst)
316
{
317
  ThreadSpecificData* data = ThreadSpecificData::getCurrentData();
318
319
  if (data != 0)
320
  {
321
    Stack& stack = data->getStack();
322
323
    if (!stack.empty())
324
    {
325
      Transcoder::encode(getMessage(stack.top()), dst);
326
      stack.pop();
327
      data->recycle();
328
      return true;
329
    }
330
331
    data->recycle();
332
  }
333
334
  return false;
335
}
336
337
bool NDC::peek(std::basic_string<UniChar>& dst)
338
{
339
  ThreadSpecificData* data = ThreadSpecificData::getCurrentData();
340
341
  if (data != 0)
342
  {
343
    Stack& stack = data->getStack();
344
345
    if (!stack.empty())
346
    {
347
      Transcoder::encode(getMessage(stack.top()), dst);
348
      return true;
349
    }
350
351
    data->recycle();
352
  }
353
354
  return false;
355
}
356
357
#endif
358
359
360
#if LOG4CXX_CFSTRING_API
361
NDC::NDC(const CFStringRef& message)
362
{
363
  push(message);
364
}
365
366
void NDC::push(const CFStringRef& message)
367
{
368
  LOG4CXX_DECODE_CFSTRING(msg, message);
369
  pushLS(msg);
370
}
371
372
bool NDC::pop(CFStringRef& dst)
373
{
374
  ThreadSpecificData* data = ThreadSpecificData::getCurrentData();
375
376
  if (data != 0)
377
  {
378
    Stack& stack = data->getStack();
379
380
    if (!stack.empty())
381
    {
382
      dst = Transcoder::encode(getMessage(stack.top()));
383
      stack.pop();
384
      data->recycle();
385
      return true;
386
    }
387
388
    data->recycle();
389
  }
390
391
  return false;
392
}
393
394
bool NDC::peek(CFStringRef& dst)
395
{
396
  ThreadSpecificData* data = ThreadSpecificData::getCurrentData();
397
398
  if (data != 0)
399
  {
400
    Stack& stack = data->getStack();
401
402
    if (!stack.empty())
403
    {
404
      dst = Transcoder::encode(getMessage(stack.top()));
405
      return true;
406
    }
407
408
    data->recycle();
409
  }
410
411
  return false;
412
}
413
414
#endif
415