Coverage Report

Created: 2025-07-11 07:00

/src/logging-log4cxx/src/main/cpp/jsonlayout.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/logstring.h>
19
#include <log4cxx/jsonlayout.h>
20
#include <log4cxx/spi/loggingevent.h>
21
#include <log4cxx/level.h>
22
#include <log4cxx/helpers/optionconverter.h>
23
#include <log4cxx/helpers/iso8601dateformat.h>
24
#include <log4cxx/helpers/stringhelper.h>
25
#include <log4cxx/helpers/transcoder.h>
26
27
#include <string.h>
28
29
using namespace LOG4CXX_NS;
30
using namespace LOG4CXX_NS::helpers;
31
using namespace LOG4CXX_NS::spi;
32
33
IMPLEMENT_LOG4CXX_OBJECT(JSONLayout)
34
35
struct JSONLayout::JSONLayoutPrivate
36
{
37
  JSONLayoutPrivate() :
38
1.63k
    locationInfo(false),
39
1.63k
    prettyPrint(false),
40
1.63k
    dateFormat(),
41
1.63k
    ppIndentL1(LOG4CXX_STR("  ")),
42
1.63k
    ppIndentL2(LOG4CXX_STR("    ")),
43
1.63k
    expectedPatternLength(100),
44
1.63k
    threadInfo(false) {}
45
46
  // Print no location info by default
47
  bool locationInfo; //= false
48
  bool prettyPrint; //= false
49
50
  helpers::ISO8601DateFormat dateFormat;
51
52
  LogString ppIndentL1;
53
  LogString ppIndentL2;
54
55
  // Expected length of a formatted event excluding the message text
56
  size_t expectedPatternLength;
57
58
  // Thread info is not included by default
59
  bool threadInfo; //= false
60
};
61
62
JSONLayout::JSONLayout() :
63
1.63k
  m_priv(std::make_unique<JSONLayoutPrivate>())
64
1.63k
{
65
1.63k
}
Unexecuted instantiation: log4cxx::JSONLayout::JSONLayout()
log4cxx::JSONLayout::JSONLayout()
Line
Count
Source
63
1.63k
  m_priv(std::make_unique<JSONLayoutPrivate>())
64
1.63k
{
65
1.63k
}
66
67
1.63k
JSONLayout::~JSONLayout(){}
68
69
void JSONLayout::setLocationInfo(bool locationInfoFlag)
70
1.05k
{
71
1.05k
  m_priv->locationInfo = locationInfoFlag;
72
1.05k
}
73
74
bool JSONLayout::getLocationInfo() const
75
0
{
76
0
  return m_priv->locationInfo;
77
0
}
78
79
void JSONLayout::setPrettyPrint(bool prettyPrintFlag)
80
1.01k
{
81
1.01k
  m_priv->prettyPrint = prettyPrintFlag;
82
1.01k
}
83
84
bool JSONLayout::getPrettyPrint() const
85
0
{
86
0
  return m_priv->prettyPrint;
87
0
}
88
89
void JSONLayout::setThreadInfo(bool newValue)
90
1.06k
{
91
1.06k
  m_priv->threadInfo = newValue;
92
1.06k
}
93
94
bool JSONLayout::getThreadInfo() const
95
0
{
96
0
  return m_priv->threadInfo;
97
0
}
98
99
LogString JSONLayout::getContentType() const
100
0
{
101
0
  return LOG4CXX_STR("application/json");
102
0
}
103
104
void JSONLayout::activateOptions(helpers::Pool& /* p */)
105
0
{
106
0
  m_priv->expectedPatternLength = getFormattedEventCharacterCount() * 2;
107
0
}
108
109
void JSONLayout::setOption(const LogString& option, const LogString& value)
110
3.14k
{
111
3.14k
  if (StringHelper::equalsIgnoreCase(option,
112
3.14k
      LOG4CXX_STR("LOCATIONINFO"), LOG4CXX_STR("locationinfo")))
113
1.05k
  {
114
1.05k
    setLocationInfo(OptionConverter::toBoolean(value, false));
115
1.05k
  }
116
2.08k
  else if (StringHelper::equalsIgnoreCase(option,
117
2.08k
      LOG4CXX_STR("THREADINFO"), LOG4CXX_STR("threadinfo")))
118
1.06k
  {
119
1.06k
    setThreadInfo(OptionConverter::toBoolean(value, false));
120
1.06k
  }
121
1.01k
  else if (StringHelper::equalsIgnoreCase(option,
122
1.01k
      LOG4CXX_STR("PRETTYPRINT"), LOG4CXX_STR("prettyprint")))
123
1.01k
  {
124
1.01k
    setPrettyPrint(OptionConverter::toBoolean(value, false));
125
1.01k
  }
126
3.14k
}
127
128
void JSONLayout::format(LogString& output,
129
  const spi::LoggingEventPtr& event,
130
  Pool& p) const
131
1.63k
{
132
1.63k
  output.reserve(m_priv->expectedPatternLength + event->getMessage().size());
133
1.63k
  output.append(LOG4CXX_STR("{"));
134
1.63k
  output.append(m_priv->prettyPrint ? LOG4CXX_EOL : LOG4CXX_STR(" "));
135
136
1.63k
  if (m_priv->prettyPrint)
137
1.01k
  {
138
1.01k
    output.append(m_priv->ppIndentL1);
139
1.01k
  }
140
141
1.63k
  appendQuotedEscapedString(output, LOG4CXX_STR("timestamp"));
142
1.63k
  output.append(LOG4CXX_STR(": "));
143
1.63k
  LogString timestamp;
144
1.63k
  m_priv->dateFormat.format(timestamp, event->getTimeStamp(), p);
145
1.63k
  appendQuotedEscapedString(output, timestamp);
146
1.63k
  output.append(LOG4CXX_STR(","));
147
1.63k
  output.append(m_priv->prettyPrint ? LOG4CXX_EOL : LOG4CXX_STR(" "));
148
149
1.63k
  if (m_priv->threadInfo)
150
1.06k
  {
151
1.06k
    if (m_priv->prettyPrint)
152
816
    {
153
816
      output.append(m_priv->ppIndentL1);
154
816
    }
155
1.06k
    appendQuotedEscapedString(output, LOG4CXX_STR("thread"));
156
1.06k
    output.append(LOG4CXX_STR(": "));
157
1.06k
    appendQuotedEscapedString(output, event->getThreadName());
158
1.06k
    output.append(LOG4CXX_STR(","));
159
1.06k
    output.append(m_priv->prettyPrint ? LOG4CXX_EOL : LOG4CXX_STR(" "));
160
1.06k
  }
161
162
1.63k
  if (m_priv->prettyPrint)
163
1.01k
  {
164
1.01k
    output.append(m_priv->ppIndentL1);
165
1.01k
  }
166
167
1.63k
  appendQuotedEscapedString(output, LOG4CXX_STR("level"));
168
1.63k
  output.append(LOG4CXX_STR(": "));
169
1.63k
  LogString level;
170
1.63k
  event->getLevel()->toString(level);
171
1.63k
  appendQuotedEscapedString(output, level);
172
1.63k
  output.append(LOG4CXX_STR(","));
173
1.63k
  output.append(m_priv->prettyPrint ? LOG4CXX_EOL : LOG4CXX_STR(" "));
174
175
1.63k
  if (m_priv->prettyPrint)
176
1.01k
  {
177
1.01k
    output.append(m_priv->ppIndentL1);
178
1.01k
  }
179
180
1.63k
  appendQuotedEscapedString(output, LOG4CXX_STR("logger"));
181
1.63k
  output.append(LOG4CXX_STR(": "));
182
1.63k
  appendQuotedEscapedString(output, event->getLoggerName());
183
1.63k
  output.append(LOG4CXX_STR(","));
184
1.63k
  output.append(m_priv->prettyPrint ? LOG4CXX_EOL : LOG4CXX_STR(" "));
185
186
1.63k
  if (m_priv->prettyPrint)
187
1.01k
  {
188
1.01k
    output.append(m_priv->ppIndentL1);
189
1.01k
  }
190
191
1.63k
  appendQuotedEscapedString(output, LOG4CXX_STR("message"));
192
1.63k
  output.append(LOG4CXX_STR(": "));
193
1.63k
  appendQuotedEscapedString(output, event->getMessage());
194
195
1.63k
  appendSerializedMDC(output, event);
196
1.63k
  appendSerializedNDC(output, event);
197
198
1.63k
  if (m_priv->locationInfo)
199
1.05k
  {
200
1.05k
    output.append(LOG4CXX_STR(","));
201
1.05k
    output.append(m_priv->prettyPrint ? LOG4CXX_EOL : LOG4CXX_STR(" "));
202
1.05k
    appendSerializedLocationInfo(output, event, p);
203
1.05k
  }
204
205
1.63k
  output.append(m_priv->prettyPrint ? LOG4CXX_EOL : LOG4CXX_STR(" "));
206
1.63k
  output.append(LOG4CXX_STR("}"));
207
1.63k
  output.append(LOG4CXX_EOL);
208
1.63k
}
209
210
void JSONLayout::appendQuotedEscapedString(LogString& buf,
211
  const LogString& input) const
212
34.6k
{
213
34.6k
  appendItem(input, buf);
214
34.6k
}
215
216
void JSONLayout::appendItem(const LogString& input, LogString& buf)
217
34.6k
{
218
  /* add leading quote */
219
34.6k
  buf.push_back(0x22);
220
221
34.6k
  logchar specialChars[] =
222
34.6k
  {
223
34.6k
    0x08,   /* \b backspace         */
224
34.6k
    0x09,   /* \t tab               */
225
34.6k
    0x0a,   /* \n newline           */
226
34.6k
    0x0c,   /* \f form feed         */
227
34.6k
    0x0d,   /* \r carriage return   */
228
34.6k
    0x22,   /* \" double quote      */
229
34.6k
    0x5c,   /* \\ backslash         */
230
34.6k
    0x00    /* terminating NULL for C-strings */
231
34.6k
  };
232
233
34.6k
  size_t start = 0;
234
34.6k
  size_t found = input.find_first_of(specialChars, start);
235
236
408k
  while (found != LogString::npos)
237
373k
  {
238
373k
    if (found > start)
239
5.76k
    {
240
5.76k
      buf.append(input, start, found - start);
241
5.76k
    }
242
243
373k
    switch (input[found])
244
373k
    {
245
18.0k
      case 0x08:
246
        /* \b backspace */
247
18.0k
        buf.push_back(0x5c);
248
18.0k
        buf.push_back('b');
249
18.0k
        break;
250
251
129k
      case 0x09:
252
        /* \t tab */
253
129k
        buf.push_back(0x5c);
254
129k
        buf.push_back('t');
255
129k
        break;
256
257
15.6k
      case 0x0a:
258
        /* \n newline */
259
15.6k
        buf.push_back(0x5c);
260
15.6k
        buf.push_back('n');
261
15.6k
        break;
262
263
116k
      case 0x0c:
264
        /* \f form feed */
265
116k
        buf.push_back(0x5c);
266
116k
        buf.push_back('f');
267
116k
        break;
268
269
57.9k
      case 0x0d:
270
        /* \r carriage return */
271
57.9k
        buf.push_back(0x5c);
272
57.9k
        buf.push_back('r');
273
57.9k
        break;
274
275
35.2k
      case 0x22:
276
        /* \" double quote */
277
35.2k
        buf.push_back(0x5c);
278
35.2k
        buf.push_back(0x22);
279
35.2k
        break;
280
281
541
      case 0x5c:
282
        /* \\ backslash */
283
541
        buf.push_back(0x5c);
284
541
        buf.push_back(0x5c);
285
541
        break;
286
287
0
      default:
288
0
        buf.push_back(input[found]);
289
0
        break;
290
373k
    }
291
292
373k
    start = found + 1;
293
294
373k
    if (found < input.size())
295
373k
    {
296
373k
      found = input.find_first_of(specialChars, start);
297
373k
    }
298
0
    else
299
0
    {
300
0
      found = LogString::npos;
301
0
    }
302
373k
  }
303
304
34.6k
  if (start < input.size())
305
24.8k
  {
306
24.8k
    buf.append(input, start, input.size() - start);
307
24.8k
  }
308
309
  /* add trailing quote */
310
34.6k
  buf.push_back(0x22);
311
34.6k
}
312
313
void JSONLayout::appendSerializedMDC(LogString& buf,
314
  const LoggingEventPtr& event) const
315
1.63k
{
316
1.63k
  LoggingEvent::KeySet keys = event->getMDCKeySet();
317
318
1.63k
  if (keys.empty())
319
0
  {
320
0
    return;
321
0
  }
322
323
1.63k
  buf.append(LOG4CXX_STR(","));
324
1.63k
  buf.append(m_priv->prettyPrint ? LOG4CXX_EOL : LOG4CXX_STR(" "));
325
326
1.63k
  if (m_priv->prettyPrint)
327
1.01k
  {
328
1.01k
    buf.append(m_priv->ppIndentL1);
329
1.01k
  }
330
331
1.63k
  appendQuotedEscapedString(buf, LOG4CXX_STR("context_map"));
332
1.63k
  buf.append(LOG4CXX_STR(": {"));
333
1.63k
  buf.append(m_priv->prettyPrint ? LOG4CXX_EOL : LOG4CXX_STR(" "));
334
335
1.63k
  for (LoggingEvent::KeySet::iterator it = keys.begin();
336
4.11k
    it != keys.end(); ++it)
337
2.47k
  {
338
2.47k
    if (m_priv->prettyPrint)
339
1.62k
    {
340
1.62k
      buf.append(m_priv->ppIndentL2);
341
1.62k
    }
342
343
2.47k
    appendQuotedEscapedString(buf, *it);
344
2.47k
    buf.append(LOG4CXX_STR(": "));
345
2.47k
    LogString value;
346
2.47k
    event->getMDC(*it, value);
347
2.47k
    appendQuotedEscapedString(buf, value);
348
349
    /* if this isn't the last k:v pair, we need a comma */
350
2.47k
    if (it + 1 != keys.end())
351
842
    {
352
842
      buf.append(LOG4CXX_STR(","));
353
842
      buf.append(m_priv->prettyPrint ? LOG4CXX_EOL : LOG4CXX_STR(" "));
354
842
    }
355
1.63k
    else
356
1.63k
    {
357
1.63k
      buf.append(m_priv->prettyPrint ? LOG4CXX_EOL : LOG4CXX_STR(" "));
358
1.63k
    }
359
2.47k
  }
360
361
1.63k
  if (m_priv->prettyPrint)
362
1.01k
  {
363
1.01k
    buf.append(m_priv->ppIndentL1);
364
1.01k
  }
365
366
1.63k
  buf.append(LOG4CXX_STR("}"));
367
1.63k
}
368
369
void JSONLayout::appendSerializedNDC(LogString& buf,
370
  const LoggingEventPtr& event) const
371
1.63k
{
372
1.63k
  LogString ndcVal;
373
374
1.63k
  if (!event->getNDC(ndcVal))
375
0
  {
376
0
    return;
377
0
  }
378
379
1.63k
  buf.append(LOG4CXX_STR(","));
380
1.63k
  buf.append(m_priv->prettyPrint ? LOG4CXX_EOL : LOG4CXX_STR(" "));
381
382
1.63k
  if (m_priv->prettyPrint)
383
1.01k
  {
384
1.01k
    buf.append(m_priv->ppIndentL1);
385
1.01k
  }
386
387
1.63k
  appendQuotedEscapedString(buf, LOG4CXX_STR("context_stack"));
388
1.63k
  buf.append(LOG4CXX_STR(": ["));
389
1.63k
  buf.append(m_priv->prettyPrint ? LOG4CXX_EOL : LOG4CXX_STR(" "));
390
391
1.63k
  if (m_priv->prettyPrint)
392
1.01k
  {
393
1.01k
    buf.append(m_priv->ppIndentL2);
394
1.01k
  }
395
396
1.63k
  appendQuotedEscapedString(buf, ndcVal);
397
1.63k
  buf.append(m_priv->prettyPrint ? LOG4CXX_EOL : LOG4CXX_STR(" "));
398
399
1.63k
  if (m_priv->prettyPrint)
400
1.01k
  {
401
1.01k
    buf.append(m_priv->ppIndentL1);
402
1.01k
  }
403
404
1.63k
  buf.append(LOG4CXX_STR("]"));
405
1.63k
}
406
407
void JSONLayout::appendSerializedLocationInfo(LogString& buf,
408
  const LoggingEventPtr& event, Pool& p) const
409
1.05k
{
410
1.05k
  if (m_priv->prettyPrint)
411
762
  {
412
762
    buf.append(m_priv->ppIndentL1);
413
762
  }
414
415
1.05k
  appendQuotedEscapedString(buf, LOG4CXX_STR("location_info"));
416
1.05k
  buf.append(LOG4CXX_STR(": {"));
417
1.05k
  buf.append(m_priv->prettyPrint ? LOG4CXX_EOL : LOG4CXX_STR(" "));
418
1.05k
  const LocationInfo& locInfo = event->getLocationInformation();
419
420
1.05k
  if (m_priv->prettyPrint)
421
762
  {
422
762
    buf.append(m_priv->ppIndentL2);
423
762
  }
424
425
1.05k
  appendQuotedEscapedString(buf, LOG4CXX_STR("file"));
426
1.05k
  buf.append(LOG4CXX_STR(": "));
427
1.05k
  LOG4CXX_DECODE_CHAR(fileName, locInfo.getFileName());
428
1.05k
  appendQuotedEscapedString(buf, fileName);
429
1.05k
  buf.append(LOG4CXX_STR(","));
430
1.05k
  buf.append(m_priv->prettyPrint ? LOG4CXX_EOL : LOG4CXX_STR(" "));
431
432
1.05k
  if (m_priv->prettyPrint)
433
762
  {
434
762
    buf.append(m_priv->ppIndentL2);
435
762
  }
436
437
1.05k
  appendQuotedEscapedString(buf, LOG4CXX_STR("line"));
438
1.05k
  buf.append(LOG4CXX_STR(": "));
439
1.05k
  LogString lineNumber;
440
1.05k
  StringHelper::toString(locInfo.getLineNumber(), p, lineNumber);
441
1.05k
  appendQuotedEscapedString(buf, lineNumber);
442
1.05k
  buf.append(LOG4CXX_STR(","));
443
1.05k
  buf.append(m_priv->prettyPrint ? LOG4CXX_EOL : LOG4CXX_STR(" "));
444
445
1.05k
  if (m_priv->prettyPrint)
446
762
  {
447
762
    buf.append(m_priv->ppIndentL2);
448
762
  }
449
450
1.05k
  appendQuotedEscapedString(buf, LOG4CXX_STR("class"));
451
1.05k
  buf.append(LOG4CXX_STR(": "));
452
1.05k
  LOG4CXX_DECODE_CHAR(className, locInfo.getClassName());
453
1.05k
  appendQuotedEscapedString(buf, className);
454
1.05k
  buf.append(LOG4CXX_STR(","));
455
1.05k
  buf.append(m_priv->prettyPrint ? LOG4CXX_EOL : LOG4CXX_STR(" "));
456
457
1.05k
  if (m_priv->prettyPrint)
458
762
  {
459
762
    buf.append(m_priv->ppIndentL2);
460
762
  }
461
462
1.05k
  appendQuotedEscapedString(buf, LOG4CXX_STR("method"));
463
1.05k
  buf.append(LOG4CXX_STR(": "));
464
1.05k
  LOG4CXX_DECODE_CHAR(methodName, locInfo.getMethodName());
465
1.05k
  appendQuotedEscapedString(buf, methodName);
466
1.05k
  buf.append(m_priv->prettyPrint ? LOG4CXX_EOL : LOG4CXX_STR(" "));
467
468
1.05k
  if (m_priv->prettyPrint)
469
762
  {
470
762
    buf.append(m_priv->ppIndentL1);
471
762
  }
472
473
1.05k
  buf.append(LOG4CXX_STR("}"));
474
1.05k
}
475