Coverage Report

Created: 2026-09-04 06:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/logging-log4cxx/src/main/cpp/nameabbreviator.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 <log4cxx/pattern/nameabbreviator.h>
19
#include <log4cxx/helpers/exception.h>
20
#include <log4cxx/helpers/stringhelper.h>
21
#include <log4cxx/helpers/loglog.h>
22
#include <vector>
23
#include <limits.h>
24
#include <stdexcept>
25
26
using namespace LOG4CXX_NS;
27
using namespace LOG4CXX_NS::pattern;
28
using namespace LOG4CXX_NS::helpers;
29
30
IMPLEMENT_LOG4CXX_OBJECT(NameAbbreviator)
31
32
NameAbbreviator::NameAbbreviator()
33
9.18k
{
34
9.18k
}
35
36
NameAbbreviator::~NameAbbreviator()
37
9.18k
{
38
9.18k
}
39
40
namespace LOG4CXX_NS
41
{
42
namespace pattern
43
{
44
/**
45
 * Abbreviator that simply appends full name to buffer.
46
 */
47
class NOPAbbreviator : public NameAbbreviator
48
{
49
  public:
50
    DECLARE_ABSTRACT_LOG4CXX_OBJECT(NOPAbbreviator)
51
0
    BEGIN_LOG4CXX_CAST_MAP()
52
0
    LOG4CXX_CAST_ENTRY(NOPAbbreviator)
53
0
    LOG4CXX_CAST_ENTRY_CHAIN(NameAbbreviator)
54
0
    END_LOG4CXX_CAST_MAP()
55
56
    /**
57
     * Constructor.
58
     */
59
    NOPAbbreviator()
60
4
    {
61
4
    }
62
63
    /**
64
     * {@inheritDoc}
65
     */
66
    void abbreviate(LogString::size_type /* nameStart */, LogString& /* buf */) const override
67
68
    {
68
68
    }
log4cxx::pattern::NOPAbbreviator::abbreviate(unsigned long, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&) const
Line
Count
Source
67
62
    {
68
62
    }
log4cxx::pattern::NOPAbbreviator::abbreviate(unsigned long, std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> >&) const
Line
Count
Source
67
6
    {
68
6
    }
69
};
70
71
72
/**
73
 * Abbreviator that drops starting path elements.
74
 */
75
class MaxElementAbbreviator : public NameAbbreviator
76
{
77
    /**
78
     * Maximum number of path elements to output.
79
     */
80
    const int count;
81
82
  public:
83
    DECLARE_ABSTRACT_LOG4CXX_OBJECT(MaxElementAbbreviator)
84
0
    BEGIN_LOG4CXX_CAST_MAP()
85
0
    LOG4CXX_CAST_ENTRY(MaxElementAbbreviator)
86
0
    LOG4CXX_CAST_ENTRY_CHAIN(NameAbbreviator)
87
0
    END_LOG4CXX_CAST_MAP()
88
    /**
89
     * Create new instance.
90
     * @param count maximum number of path elements to output.
91
     */
92
4.38k
    MaxElementAbbreviator(const int count1) : count(count1 < 1 ? 1 : count1)
93
4.38k
    {
94
4.38k
    }
95
96
    /**
97
     * {@inheritDoc}
98
     */
99
    void abbreviate(LogString::size_type nameStart, LogString& buf) const override
100
12.5k
    {
101
12.5k
      logchar separ = 0x2E; // '.'
102
12.5k
      LogString::size_type end = buf.length();
103
25.2k
      for (LogString::size_type i = count; nameStart < end && 0 < i; --i)
104
12.7k
      {
105
12.7k
        end = buf.rfind(separ, end - 1);
106
12.7k
        if (LogString::npos == end)
107
35
        {
108
35
          return;
109
35
        }
110
12.7k
      }
111
12.4k
      if (nameStart < end + 1 && end + 1 < buf.length())
112
12.4k
        buf.erase(buf.begin() + nameStart, buf.begin() + (end + 1));
113
12.4k
    }
log4cxx::pattern::MaxElementAbbreviator::abbreviate(unsigned long, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&) const
Line
Count
Source
100
5.11k
    {
101
5.11k
      logchar separ = 0x2E; // '.'
102
5.11k
      LogString::size_type end = buf.length();
103
10.3k
      for (LogString::size_type i = count; nameStart < end && 0 < i; --i)
104
5.28k
      {
105
5.28k
        end = buf.rfind(separ, end - 1);
106
5.28k
        if (LogString::npos == end)
107
16
        {
108
16
          return;
109
16
        }
110
5.28k
      }
111
5.10k
      if (nameStart < end + 1 && end + 1 < buf.length())
112
5.09k
        buf.erase(buf.begin() + nameStart, buf.begin() + (end + 1));
113
5.10k
    }
log4cxx::pattern::MaxElementAbbreviator::abbreviate(unsigned long, std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> >&) const
Line
Count
Source
100
7.38k
    {
101
7.38k
      logchar separ = 0x2E; // '.'
102
7.38k
      LogString::size_type end = buf.length();
103
14.8k
      for (LogString::size_type i = count; nameStart < end && 0 < i; --i)
104
7.49k
      {
105
7.49k
        end = buf.rfind(separ, end - 1);
106
7.49k
        if (LogString::npos == end)
107
19
        {
108
19
          return;
109
19
        }
110
7.49k
      }
111
7.36k
      if (nameStart < end + 1 && end + 1 < buf.length())
112
7.35k
        buf.erase(buf.begin() + nameStart, buf.begin() + (end + 1));
113
7.36k
    }
114
};
115
116
/**
117
 * Fragment of an pattern abbreviator.
118
 *
119
 */
120
class PatternAbbreviatorFragment
121
{
122
    /**
123
     * Count of initial characters of element to output.
124
     */
125
    LogString::size_type charCount;
126
127
    /**
128
     *  Character used to represent dropped characters.
129
     * '\0' indicates no representation of dropped characters.
130
     */
131
    logchar ellipsis;
132
133
  public:
134
    /**
135
     * Creates a PatternAbbreviatorFragment.
136
     * @param charCount number of initial characters to preserve.
137
     * @param ellipsis character to represent elimination of characters,
138
     *    '\0' if no ellipsis is desired.
139
     */
140
    PatternAbbreviatorFragment(
141
      const int charCount1, const logchar ellipsis1)
142
168k
      : charCount(charCount1), ellipsis(ellipsis1)
143
168k
    {
144
168k
    }
log4cxx::pattern::PatternAbbreviatorFragment::PatternAbbreviatorFragment(int, char)
Line
Count
Source
142
99.4k
      : charCount(charCount1), ellipsis(ellipsis1)
143
99.4k
    {
144
99.4k
    }
log4cxx::pattern::PatternAbbreviatorFragment::PatternAbbreviatorFragment(int, wchar_t)
Line
Count
Source
142
68.6k
      : charCount(charCount1), ellipsis(ellipsis1)
143
68.6k
    {
144
68.6k
    }
145
    PatternAbbreviatorFragment() : charCount(0), ellipsis(0)
146
0
    {
147
0
    }
148
149
    PatternAbbreviatorFragment(const PatternAbbreviatorFragment& src)
150
550k
      : charCount(src.charCount), ellipsis(src.ellipsis)
151
550k
    {
152
550k
    }
153
154
    PatternAbbreviatorFragment& operator=(const PatternAbbreviatorFragment& src)
155
0
    {
156
0
      charCount = src.charCount;
157
0
      ellipsis = src.ellipsis;
158
0
      return *this;
159
0
    }
160
161
    /**
162
     * Abbreviate element of name.
163
     * @param buf buffer to receive element.
164
     * @param startPos starting index of name element.
165
     * @return starting index of next element.
166
     */
167
    LogString::size_type abbreviate(LogString& buf, LogString::size_type startPos) const
168
1.13k
    {
169
1.13k
      LogString::size_type nextDot = buf.find(0x2E /* '.' */, startPos);
170
171
1.13k
      if (nextDot != LogString::npos)
172
1.08k
      {
173
1.08k
        if ((nextDot - startPos) > charCount)
174
595
        {
175
595
          buf.erase(buf.begin() + (startPos + charCount), buf.begin() + nextDot);
176
595
          nextDot = startPos + charCount;
177
178
595
          if (ellipsis != 0x00)
179
353
          {
180
353
            buf.insert(nextDot, 1, ellipsis);
181
353
            nextDot++;
182
353
          }
183
595
        }
184
185
1.08k
        nextDot++;
186
1.08k
      }
187
188
1.13k
      return nextDot;
189
1.13k
    }
log4cxx::pattern::PatternAbbreviatorFragment::abbreviate(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&, unsigned long) const
Line
Count
Source
168
690
    {
169
690
      LogString::size_type nextDot = buf.find(0x2E /* '.' */, startPos);
170
171
690
      if (nextDot != LogString::npos)
172
652
      {
173
652
        if ((nextDot - startPos) > charCount)
174
391
        {
175
391
          buf.erase(buf.begin() + (startPos + charCount), buf.begin() + nextDot);
176
391
          nextDot = startPos + charCount;
177
178
391
          if (ellipsis != 0x00)
179
256
          {
180
256
            buf.insert(nextDot, 1, ellipsis);
181
256
            nextDot++;
182
256
          }
183
391
        }
184
185
652
        nextDot++;
186
652
      }
187
188
690
      return nextDot;
189
690
    }
log4cxx::pattern::PatternAbbreviatorFragment::abbreviate(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> >&, unsigned long) const
Line
Count
Source
168
449
    {
169
449
      LogString::size_type nextDot = buf.find(0x2E /* '.' */, startPos);
170
171
449
      if (nextDot != LogString::npos)
172
433
      {
173
433
        if ((nextDot - startPos) > charCount)
174
204
        {
175
204
          buf.erase(buf.begin() + (startPos + charCount), buf.begin() + nextDot);
176
204
          nextDot = startPos + charCount;
177
178
204
          if (ellipsis != 0x00)
179
97
          {
180
97
            buf.insert(nextDot, 1, ellipsis);
181
97
            nextDot++;
182
97
          }
183
204
        }
184
185
433
        nextDot++;
186
433
      }
187
188
449
      return nextDot;
189
449
    }
190
191
    /**
192
     * Abbreviate all '.' separated elements of \c buf from \c startPos.
193
     * @param buf buffer to receive element.
194
     * @param startPos starting index of name element.
195
     */
196
    void abbreviateAll(LogString& buf, LogString::size_type startPos) const
197
317
    {
198
317
      logchar separ = 0x2E; /* '.' */
199
317
      LogString abbreviation;
200
317
      abbreviation.reserve(buf.length());
201
317
      auto pos = startPos;
202
317
      LogString::size_type nextDot;
203
2.04k
      while (pos < buf.length() && buf.npos != (nextDot = buf.find(separ, pos)))
204
1.73k
      {
205
1.73k
        if (pos + charCount < nextDot)
206
495
        {
207
495
          abbreviation.append(buf.begin() + pos, buf.begin() + (pos + charCount));
208
495
          if (ellipsis != 0x00)
209
302
            abbreviation.append(1, ellipsis);
210
495
        }
211
1.23k
        else
212
1.23k
          abbreviation.append(buf.begin() + pos, buf.begin() + nextDot);
213
1.73k
        abbreviation.append(1, separ);
214
1.73k
        pos = nextDot + 1;
215
1.73k
      }
216
317
      if (pos < buf.length())
217
191
        abbreviation.append(buf.begin() + pos, buf.end());
218
317
      buf.erase(buf.begin() + startPos, buf.end());
219
317
      buf.append(abbreviation);
220
317
    }
log4cxx::pattern::PatternAbbreviatorFragment::abbreviateAll(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&, unsigned long) const
Line
Count
Source
197
189
    {
198
189
      logchar separ = 0x2E; /* '.' */
199
189
      LogString abbreviation;
200
189
      abbreviation.reserve(buf.length());
201
189
      auto pos = startPos;
202
189
      LogString::size_type nextDot;
203
1.09k
      while (pos < buf.length() && buf.npos != (nextDot = buf.find(separ, pos)))
204
909
      {
205
909
        if (pos + charCount < nextDot)
206
261
        {
207
261
          abbreviation.append(buf.begin() + pos, buf.begin() + (pos + charCount));
208
261
          if (ellipsis != 0x00)
209
152
            abbreviation.append(1, ellipsis);
210
261
        }
211
648
        else
212
648
          abbreviation.append(buf.begin() + pos, buf.begin() + nextDot);
213
909
        abbreviation.append(1, separ);
214
909
        pos = nextDot + 1;
215
909
      }
216
189
      if (pos < buf.length())
217
130
        abbreviation.append(buf.begin() + pos, buf.end());
218
189
      buf.erase(buf.begin() + startPos, buf.end());
219
189
      buf.append(abbreviation);
220
189
    }
log4cxx::pattern::PatternAbbreviatorFragment::abbreviateAll(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> >&, unsigned long) const
Line
Count
Source
197
128
    {
198
128
      logchar separ = 0x2E; /* '.' */
199
128
      LogString abbreviation;
200
128
      abbreviation.reserve(buf.length());
201
128
      auto pos = startPos;
202
128
      LogString::size_type nextDot;
203
949
      while (pos < buf.length() && buf.npos != (nextDot = buf.find(separ, pos)))
204
821
      {
205
821
        if (pos + charCount < nextDot)
206
234
        {
207
234
          abbreviation.append(buf.begin() + pos, buf.begin() + (pos + charCount));
208
234
          if (ellipsis != 0x00)
209
150
            abbreviation.append(1, ellipsis);
210
234
        }
211
587
        else
212
587
          abbreviation.append(buf.begin() + pos, buf.begin() + nextDot);
213
821
        abbreviation.append(1, separ);
214
821
        pos = nextDot + 1;
215
821
      }
216
128
      if (pos < buf.length())
217
61
        abbreviation.append(buf.begin() + pos, buf.end());
218
128
      buf.erase(buf.begin() + startPos, buf.end());
219
128
      buf.append(abbreviation);
220
128
    }
221
};
222
223
/**
224
 * Pattern abbreviator.
225
 *
226
 *
227
 */
228
class PatternAbbreviator : public NameAbbreviator
229
{
230
    /**
231
     * Element abbreviation patterns.
232
     */
233
    std::vector<PatternAbbreviatorFragment> fragments;
234
235
  public:
236
    DECLARE_ABSTRACT_LOG4CXX_OBJECT(PatternAbbreviator)
237
0
    BEGIN_LOG4CXX_CAST_MAP()
238
0
    LOG4CXX_CAST_ENTRY(PatternAbbreviator)
239
0
    LOG4CXX_CAST_ENTRY_CHAIN(NameAbbreviator)
240
0
    END_LOG4CXX_CAST_MAP()
241
    /**
242
     * Create PatternAbbreviator.
243
     *
244
     * @param fragments element abbreviation patterns.
245
     */
246
    PatternAbbreviator(const std::vector<PatternAbbreviatorFragment>& fragments1) :
247
4.79k
      fragments(fragments1)
248
4.79k
    {
249
4.79k
      if (fragments1.size() == 0)
250
0
      {
251
0
        throw IllegalArgumentException(LOG4CXX_STR("fragments parameter must contain at least one element"));
252
0
      }
253
4.79k
    }
254
255
    /**
256
     * {@inheritDoc}
257
     */
258
    void abbreviate(LogString::size_type nameStart, LogString& buf) const override
259
436
    {
260
      //
261
      //  all non-terminal patterns are executed once
262
      //
263
436
      LogString::size_type pos = nameStart;
264
265
1.57k
      for (LogString::size_type i = 0; (i < (fragments.size() - 1)) && (pos < buf.length());
266
1.13k
        i++)
267
1.13k
      {
268
1.13k
        pos = fragments[i].abbreviate(buf, pos);
269
1.13k
      }
270
271
      //
272
      //   apply the last pattern to all the remaining name parts
273
      //
274
436
      if (pos < buf.length())
275
317
        fragments.back().abbreviateAll(buf, pos);
276
436
    }
log4cxx::pattern::PatternAbbreviator::abbreviate(unsigned long, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&) const
Line
Count
Source
259
238
    {
260
      //
261
      //  all non-terminal patterns are executed once
262
      //
263
238
      LogString::size_type pos = nameStart;
264
265
928
      for (LogString::size_type i = 0; (i < (fragments.size() - 1)) && (pos < buf.length());
266
690
        i++)
267
690
      {
268
690
        pos = fragments[i].abbreviate(buf, pos);
269
690
      }
270
271
      //
272
      //   apply the last pattern to all the remaining name parts
273
      //
274
238
      if (pos < buf.length())
275
189
        fragments.back().abbreviateAll(buf, pos);
276
238
    }
log4cxx::pattern::PatternAbbreviator::abbreviate(unsigned long, std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> >&) const
Line
Count
Source
259
198
    {
260
      //
261
      //  all non-terminal patterns are executed once
262
      //
263
198
      LogString::size_type pos = nameStart;
264
265
647
      for (LogString::size_type i = 0; (i < (fragments.size() - 1)) && (pos < buf.length());
266
449
        i++)
267
449
      {
268
449
        pos = fragments[i].abbreviate(buf, pos);
269
449
      }
270
271
      //
272
      //   apply the last pattern to all the remaining name parts
273
      //
274
198
      if (pos < buf.length())
275
128
        fragments.back().abbreviateAll(buf, pos);
276
198
    }
277
};
278
}
279
}
280
281
IMPLEMENT_LOG4CXX_OBJECT(NOPAbbreviator)
282
IMPLEMENT_LOG4CXX_OBJECT(MaxElementAbbreviator)
283
IMPLEMENT_LOG4CXX_OBJECT(PatternAbbreviator)
284
285
286
287
NameAbbreviatorPtr NameAbbreviator::getAbbreviator(const LogString& pattern)
288
10.3k
{
289
10.3k
  if (pattern.length() > 0)
290
9.63k
  {
291
    //  if pattern is just spaces and numbers then
292
    //     use MaxElementAbbreviator
293
9.63k
    LogString trimmed(StringHelper::trim(pattern));
294
295
9.63k
    if (trimmed.length() == 0)
296
455
    {
297
455
      return getDefaultAbbreviator();
298
455
    }
299
300
9.17k
    LogString::size_type i = 0;
301
302
9.17k
    while (
303
94.4k
      (i < trimmed.length()) && (trimmed[i] >= 0x30 /* '0' */)
304
86.4k
      && (trimmed[i] <= 0x39 /* '9' */))
305
85.2k
    {
306
85.2k
      i++;
307
85.2k
    }
308
309
    //
310
    //  if all blanks and digits
311
    //
312
9.17k
    if (i == trimmed.length())
313
4.38k
    {
314
4.38k
      int len = 256;
315
4.38k
      try
316
4.38k
      {
317
4.38k
        len = StringHelper::toInt(trimmed);
318
4.38k
      }
319
4.38k
      catch (const std::out_of_range& ex)
320
4.38k
      {
321
540
        LogLog::warn(LOG4CXX_STR("Invalid name abreviator pattern: ") + pattern, ex);
322
540
      }
323
324
4.38k
      if(len > 256){
325
643
        len = 256;
326
3.74k
      }else if( len < 0 ){
327
0
        len = 0;
328
0
      }
329
330
4.38k
      return std::make_shared<MaxElementAbbreviator>(len);
331
4.38k
    }
332
333
4.79k
    std::vector<PatternAbbreviatorFragment> fragments;
334
4.79k
    logchar ellipsis;
335
4.79k
    int charCount;
336
4.79k
    LogString::size_type pos = 0;
337
338
169k
    while (pos < trimmed.length())
339
168k
    {
340
168k
      LogString::size_type ellipsisPos = pos;
341
342
168k
      if (trimmed[pos] == 0x2A /* '*' */)
343
5.26k
      {
344
5.26k
        charCount = INT_MAX;
345
5.26k
        ellipsisPos++;
346
5.26k
      }
347
162k
      else
348
162k
      {
349
162k
        if ((trimmed[pos] >= 0x30 /* '0' */)
350
6.78k
          && (trimmed[pos] <= 0x39 /* '9' */))
351
3.42k
        {
352
3.42k
          charCount = trimmed[pos] - 0x30 /* '0' */;
353
3.42k
          ellipsisPos++;
354
3.42k
        }
355
159k
        else
356
159k
        {
357
159k
          charCount = 0;
358
159k
        }
359
162k
      }
360
361
168k
      ellipsis = 0;
362
363
168k
      if (ellipsisPos < trimmed.length())
364
167k
      {
365
167k
        ellipsis = trimmed[ellipsisPos];
366
367
167k
        if (ellipsis == 0x2E /* '.' */)
368
155k
        {
369
155k
          ellipsis = 0;
370
155k
        }
371
167k
      }
372
373
168k
      fragments.push_back(PatternAbbreviatorFragment(charCount, ellipsis));
374
168k
      pos = trimmed.find(0x2E /* '.' */, pos);
375
376
168k
      if (pos == LogString::npos)
377
3.82k
      {
378
3.82k
        break;
379
3.82k
      }
380
381
164k
      pos++;
382
164k
    }
383
384
4.79k
    return std::make_shared<PatternAbbreviator>(fragments);
385
9.17k
  }
386
387
  //
388
  //  no matching abbreviation, return defaultAbbreviator
389
  //
390
691
  return getDefaultAbbreviator();
391
10.3k
}
log4cxx::pattern::NameAbbreviator::getAbbreviator(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Line
Count
Source
288
5.12k
{
289
5.12k
  if (pattern.length() > 0)
290
4.75k
  {
291
    //  if pattern is just spaces and numbers then
292
    //     use MaxElementAbbreviator
293
4.75k
    LogString trimmed(StringHelper::trim(pattern));
294
295
4.75k
    if (trimmed.length() == 0)
296
249
    {
297
249
      return getDefaultAbbreviator();
298
249
    }
299
300
4.50k
    LogString::size_type i = 0;
301
302
4.50k
    while (
303
30.0k
      (i < trimmed.length()) && (trimmed[i] >= 0x30 /* '0' */)
304
26.1k
      && (trimmed[i] <= 0x39 /* '9' */))
305
25.5k
    {
306
25.5k
      i++;
307
25.5k
    }
308
309
    //
310
    //  if all blanks and digits
311
    //
312
4.50k
    if (i == trimmed.length())
313
1.75k
    {
314
1.75k
      int len = 256;
315
1.75k
      try
316
1.75k
      {
317
1.75k
        len = StringHelper::toInt(trimmed);
318
1.75k
      }
319
1.75k
      catch (const std::out_of_range& ex)
320
1.75k
      {
321
286
        LogLog::warn(LOG4CXX_STR("Invalid name abreviator pattern: ") + pattern, ex);
322
286
      }
323
324
1.75k
      if(len > 256){
325
248
        len = 256;
326
1.50k
      }else if( len < 0 ){
327
0
        len = 0;
328
0
      }
329
330
1.75k
      return std::make_shared<MaxElementAbbreviator>(len);
331
1.75k
    }
332
333
2.75k
    std::vector<PatternAbbreviatorFragment> fragments;
334
2.75k
    logchar ellipsis;
335
2.75k
    int charCount;
336
2.75k
    LogString::size_type pos = 0;
337
338
99.8k
    while (pos < trimmed.length())
339
99.4k
    {
340
99.4k
      LogString::size_type ellipsisPos = pos;
341
342
99.4k
      if (trimmed[pos] == 0x2A /* '*' */)
343
3.40k
      {
344
3.40k
        charCount = INT_MAX;
345
3.40k
        ellipsisPos++;
346
3.40k
      }
347
96.0k
      else
348
96.0k
      {
349
96.0k
        if ((trimmed[pos] >= 0x30 /* '0' */)
350
4.47k
          && (trimmed[pos] <= 0x39 /* '9' */))
351
2.14k
        {
352
2.14k
          charCount = trimmed[pos] - 0x30 /* '0' */;
353
2.14k
          ellipsisPos++;
354
2.14k
        }
355
93.8k
        else
356
93.8k
        {
357
93.8k
          charCount = 0;
358
93.8k
        }
359
96.0k
      }
360
361
99.4k
      ellipsis = 0;
362
363
99.4k
      if (ellipsisPos < trimmed.length())
364
99.1k
      {
365
99.1k
        ellipsis = trimmed[ellipsisPos];
366
367
99.1k
        if (ellipsis == 0x2E /* '.' */)
368
89.7k
        {
369
89.7k
          ellipsis = 0;
370
89.7k
        }
371
99.1k
      }
372
373
99.4k
      fragments.push_back(PatternAbbreviatorFragment(charCount, ellipsis));
374
99.4k
      pos = trimmed.find(0x2E /* '.' */, pos);
375
376
99.4k
      if (pos == LogString::npos)
377
2.29k
      {
378
2.29k
        break;
379
2.29k
      }
380
381
97.1k
      pos++;
382
97.1k
    }
383
384
2.75k
    return std::make_shared<PatternAbbreviator>(fragments);
385
4.50k
  }
386
387
  //
388
  //  no matching abbreviation, return defaultAbbreviator
389
  //
390
373
  return getDefaultAbbreviator();
391
5.12k
}
log4cxx::pattern::NameAbbreviator::getAbbreviator(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&)
Line
Count
Source
288
5.19k
{
289
5.19k
  if (pattern.length() > 0)
290
4.87k
  {
291
    //  if pattern is just spaces and numbers then
292
    //     use MaxElementAbbreviator
293
4.87k
    LogString trimmed(StringHelper::trim(pattern));
294
295
4.87k
    if (trimmed.length() == 0)
296
206
    {
297
206
      return getDefaultAbbreviator();
298
206
    }
299
300
4.67k
    LogString::size_type i = 0;
301
302
4.67k
    while (
303
64.3k
      (i < trimmed.length()) && (trimmed[i] >= 0x30 /* '0' */)
304
60.2k
      && (trimmed[i] <= 0x39 /* '9' */))
305
59.6k
    {
306
59.6k
      i++;
307
59.6k
    }
308
309
    //
310
    //  if all blanks and digits
311
    //
312
4.67k
    if (i == trimmed.length())
313
2.63k
    {
314
2.63k
      int len = 256;
315
2.63k
      try
316
2.63k
      {
317
2.63k
        len = StringHelper::toInt(trimmed);
318
2.63k
      }
319
2.63k
      catch (const std::out_of_range& ex)
320
2.63k
      {
321
254
        LogLog::warn(LOG4CXX_STR("Invalid name abreviator pattern: ") + pattern, ex);
322
254
      }
323
324
2.63k
      if(len > 256){
325
395
        len = 256;
326
2.23k
      }else if( len < 0 ){
327
0
        len = 0;
328
0
      }
329
330
2.63k
      return std::make_shared<MaxElementAbbreviator>(len);
331
2.63k
    }
332
333
2.04k
    std::vector<PatternAbbreviatorFragment> fragments;
334
2.04k
    logchar ellipsis;
335
2.04k
    int charCount;
336
2.04k
    LogString::size_type pos = 0;
337
338
69.1k
    while (pos < trimmed.length())
339
68.6k
    {
340
68.6k
      LogString::size_type ellipsisPos = pos;
341
342
68.6k
      if (trimmed[pos] == 0x2A /* '*' */)
343
1.85k
      {
344
1.85k
        charCount = INT_MAX;
345
1.85k
        ellipsisPos++;
346
1.85k
      }
347
66.7k
      else
348
66.7k
      {
349
66.7k
        if ((trimmed[pos] >= 0x30 /* '0' */)
350
2.30k
          && (trimmed[pos] <= 0x39 /* '9' */))
351
1.28k
        {
352
1.28k
          charCount = trimmed[pos] - 0x30 /* '0' */;
353
1.28k
          ellipsisPos++;
354
1.28k
        }
355
65.4k
        else
356
65.4k
        {
357
65.4k
          charCount = 0;
358
65.4k
        }
359
66.7k
      }
360
361
68.6k
      ellipsis = 0;
362
363
68.6k
      if (ellipsisPos < trimmed.length())
364
68.0k
      {
365
68.0k
        ellipsis = trimmed[ellipsisPos];
366
367
68.0k
        if (ellipsis == 0x2E /* '.' */)
368
65.4k
        {
369
65.4k
          ellipsis = 0;
370
65.4k
        }
371
68.0k
      }
372
373
68.6k
      fragments.push_back(PatternAbbreviatorFragment(charCount, ellipsis));
374
68.6k
      pos = trimmed.find(0x2E /* '.' */, pos);
375
376
68.6k
      if (pos == LogString::npos)
377
1.53k
      {
378
1.53k
        break;
379
1.53k
      }
380
381
67.0k
      pos++;
382
67.0k
    }
383
384
2.04k
    return std::make_shared<PatternAbbreviator>(fragments);
385
4.67k
  }
386
387
  //
388
  //  no matching abbreviation, return defaultAbbreviator
389
  //
390
318
  return getDefaultAbbreviator();
391
5.19k
}
392
393
/**
394
 * Gets default abbreviator.
395
 *
396
 * @return default abbreviator.
397
 */
398
NameAbbreviatorPtr NameAbbreviator::getDefaultAbbreviator()
399
23.2k
{
400
23.2k
  static WideLife<NameAbbreviatorPtr> def = std::make_shared<NOPAbbreviator>();
401
23.2k
  return def;
402
23.2k
}
403