Coverage Report

Created: 2025-07-01 06:08

/src/logging-log4cxx/src/main/cpp/mdc.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/mdc.h>
19
#include <log4cxx/helpers/transcoder.h>
20
#include <log4cxx/helpers/threadspecificdata.h>
21
22
#if LOG4CXX_CFSTRING_API
23
  #include <CoreFoundation/CFString.h>
24
#endif
25
26
27
using namespace LOG4CXX_NS;
28
using namespace LOG4CXX_NS::helpers;
29
30
0
MDC::MDC(const std::string& key1, const std::string& value) : key()
31
0
{
32
0
  Transcoder::decode(key1, key);
33
0
  LOG4CXX_DECODE_CHAR(v, value);
34
0
  putLS(key, v);
35
0
}
36
37
MDC::~MDC()
38
0
{
39
0
  LogString prevVal;
40
0
  remove(key, prevVal);
41
0
}
42
43
void MDC::putLS(const LogString& key, const LogString& value)
44
0
{
45
0
  ThreadSpecificData::put(key, value);
46
0
}
47
48
void MDC::put(const std::string& key, const std::string& value)
49
0
{
50
0
  LOG4CXX_DECODE_CHAR(lkey, key);
51
0
  LOG4CXX_DECODE_CHAR(lvalue, value);
52
0
  putLS(lkey, lvalue);
53
0
}
54
55
bool MDC::get(const LogString& key, LogString& value)
56
0
{
57
0
  ThreadSpecificData* data = ThreadSpecificData::getCurrentData();
58
59
0
  if (data != 0)
60
0
  {
61
0
    Map& map = data->getMap();
62
63
0
    Map::iterator it = map.find(key);
64
65
0
    if (it != map.end())
66
0
    {
67
0
      value.append(it->second);
68
0
      return true;
69
0
    }
70
71
0
    data->recycle();
72
0
  }
73
74
0
  return false;
75
0
}
76
77
std::string MDC::get(const std::string& key)
78
0
{
79
0
  LOG4CXX_DECODE_CHAR(lkey, key);
80
0
  LogString lvalue;
81
82
0
  if (get(lkey, lvalue))
83
0
  {
84
0
    LOG4CXX_ENCODE_CHAR(value, lvalue);
85
0
    return value;
86
0
  }
87
88
0
  return std::string();
89
0
}
90
91
bool MDC::remove(const LogString& key, LogString& value)
92
0
{
93
0
  ThreadSpecificData* data = ThreadSpecificData::getCurrentData();
94
95
0
  if (data != 0)
96
0
  {
97
0
    Map& map = data->getMap();
98
0
    Map::iterator it;
99
100
0
    if ((it = map.find(key)) != map.end())
101
0
    {
102
0
      value = it->second;
103
0
      map.erase(it);
104
0
      data->recycle();
105
0
      return true;
106
0
    }
107
0
  }
108
109
0
  return false;
110
0
}
111
112
std::string MDC::remove(const std::string& key)
113
0
{
114
0
  LOG4CXX_DECODE_CHAR(lkey, key);
115
0
  LogString lvalue;
116
117
0
  if (remove(lkey, lvalue))
118
0
  {
119
0
    LOG4CXX_ENCODE_CHAR(value, lvalue);
120
0
    return value;
121
0
  }
122
123
0
  return std::string();
124
0
}
125
126
127
void MDC::clear()
128
0
{
129
0
  ThreadSpecificData* data = ThreadSpecificData::getCurrentData();
130
131
0
  if (data != 0)
132
0
  {
133
0
    Map& map = data->getMap();
134
0
    map.erase(map.begin(), map.end());
135
0
    data->recycle();
136
0
  }
137
0
}
138
139
140
#if LOG4CXX_WCHAR_T_API
141
0
MDC::MDC(const std::wstring& key1, const std::wstring& value) : key()
142
0
{
143
0
  Transcoder::decode(key1, key);
144
0
  LOG4CXX_DECODE_WCHAR(v, value);
145
0
  putLS(key, v);
146
0
}
147
148
std::wstring MDC::get(const std::wstring& key)
149
0
{
150
0
  LOG4CXX_DECODE_WCHAR(lkey, key);
151
0
  LogString lvalue;
152
153
0
  if (get(lkey, lvalue))
154
0
  {
155
0
    LOG4CXX_ENCODE_WCHAR(value, lvalue);
156
0
    return value;
157
0
  }
158
159
0
  return std::wstring();
160
0
}
161
162
void MDC::put(const std::wstring& key, const std::wstring& value)
163
0
{
164
0
  LOG4CXX_DECODE_WCHAR(lkey, key);
165
0
  LOG4CXX_DECODE_WCHAR(lvalue, value);
166
0
  putLS(lkey, lvalue);
167
0
}
168
169
170
std::wstring MDC::remove(const std::wstring& key)
171
0
{
172
0
  LOG4CXX_DECODE_WCHAR(lkey, key);
173
0
  LogString lvalue;
174
175
0
  if (remove(lkey, lvalue))
176
0
  {
177
0
    LOG4CXX_ENCODE_WCHAR(value, lvalue);
178
0
    return value;
179
0
  }
180
181
0
  return std::wstring();
182
0
}
183
#endif
184
185
#if LOG4CXX_UNICHAR_API
186
MDC::MDC(const std::basic_string<UniChar>& key1, const std::basic_string<UniChar>& value)
187
{
188
  Transcoder::decode(key1, key);
189
  LOG4CXX_DECODE_UNICHAR(v, value);
190
  putLS(key, v);
191
}
192
193
std::basic_string<LOG4CXX_NS::UniChar> MDC::get(const std::basic_string<LOG4CXX_NS::UniChar>& key)
194
{
195
  LOG4CXX_DECODE_UNICHAR(lkey, key);
196
  LogString lvalue;
197
198
  if (get(lkey, lvalue))
199
  {
200
    LOG4CXX_ENCODE_UNICHAR(value, lvalue);
201
    return value;
202
  }
203
204
  return std::basic_string<UniChar>();
205
}
206
207
void MDC::put(const std::basic_string<UniChar>& key, const std::basic_string<LOG4CXX_NS::UniChar>& value)
208
{
209
  LOG4CXX_DECODE_UNICHAR(lkey, key);
210
  LOG4CXX_DECODE_UNICHAR(lvalue, value);
211
  putLS(lkey, lvalue);
212
}
213
214
215
std::basic_string<LOG4CXX_NS::UniChar> MDC::remove(const std::basic_string<LOG4CXX_NS::UniChar>& key)
216
{
217
  LOG4CXX_DECODE_UNICHAR(lkey, key);
218
  LogString lvalue;
219
220
  if (remove(lkey, lvalue))
221
  {
222
    LOG4CXX_ENCODE_UNICHAR(value, lvalue);
223
    return value;
224
  }
225
226
  return std::basic_string<UniChar>();
227
}
228
#endif
229
230
#if LOG4CXX_CFSTRING_API
231
232
MDC::MDC(const CFStringRef& key1, const CFStringRef& value)
233
{
234
  Transcoder::decode(key1, key);
235
  LOG4CXX_DECODE_CFSTRING(v, value);
236
  putLS(key, v);
237
}
238
239
CFStringRef MDC::get(const CFStringRef& key)
240
{
241
  LOG4CXX_DECODE_CFSTRING(lkey, key);
242
  LogString lvalue;
243
244
  if (get(lkey, lvalue))
245
  {
246
    LOG4CXX_ENCODE_CFSTRING(value, lvalue);
247
    return value;
248
  }
249
250
  return CFSTR("");
251
}
252
253
void MDC::put(const CFStringRef& key, const CFStringRef& value)
254
{
255
  LOG4CXX_DECODE_CFSTRING(lkey, key);
256
  LOG4CXX_DECODE_CFSTRING(lvalue, value);
257
  putLS(lkey, lvalue);
258
}
259
260
261
CFStringRef MDC::remove(const CFStringRef& key)
262
{
263
  LOG4CXX_DECODE_CFSTRING(lkey, key);
264
  LogString lvalue;
265
266
  if (remove(lkey, lvalue))
267
  {
268
    LOG4CXX_ENCODE_CFSTRING(value, lvalue);
269
    return value;
270
  }
271
272
  return CFSTR("");
273
}
274
#endif
275