Coverage Report

Created: 2025-08-26 06:48

/src/logging-log4cxx/src/main/cpp/nameabbreviator.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/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.55k
{
34
9.55k
}
35
36
NameAbbreviator::~NameAbbreviator()
37
9.55k
{
38
9.55k
}
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
52
    {
68
52
    }
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
24
    {
68
24
    }
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
28
    {
68
28
    }
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
5.11k
    MaxElementAbbreviator(const int count1) : count(count1)
93
5.11k
    {
94
5.11k
    }
95
96
    /**
97
     * Abbreviate name.
98
     * @param buf buffer to append abbreviation.
99
     * @param nameStart start of name to abbreviate.
100
     */
101
    void abbreviate(LogString::size_type nameStart, LogString& buf) const override
102
18.6k
    {
103
      // We substract 1 from 'len' when assigning to 'end' to avoid out of
104
      // bounds exception in return r.substring(end+1, len). This can happen if
105
      // precision is 1 and the logger name ends with a dot.
106
18.6k
      LogString::size_type end = buf.length() - 1;
107
108
39.0k
      for (LogString::size_type i = count; i > 0; i--)
109
20.6k
      {
110
20.6k
        end = buf.rfind(0x2E /* '.' */, end - 1);
111
112
20.6k
        if ((end == LogString::npos) || (end < nameStart))
113
292
        {
114
292
          return;
115
292
        }
116
20.6k
      }
117
118
18.3k
      buf.erase(buf.begin() + nameStart, buf.begin() + (end + 1));
119
18.3k
    }
log4cxx::pattern::MaxElementAbbreviator::abbreviate(unsigned long, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&) const
Line
Count
Source
102
6.10k
    {
103
      // We substract 1 from 'len' when assigning to 'end' to avoid out of
104
      // bounds exception in return r.substring(end+1, len). This can happen if
105
      // precision is 1 and the logger name ends with a dot.
106
6.10k
      LogString::size_type end = buf.length() - 1;
107
108
13.1k
      for (LogString::size_type i = count; i > 0; i--)
109
7.13k
      {
110
7.13k
        end = buf.rfind(0x2E /* '.' */, end - 1);
111
112
7.13k
        if ((end == LogString::npos) || (end < nameStart))
113
134
        {
114
134
          return;
115
134
        }
116
7.13k
      }
117
118
5.97k
      buf.erase(buf.begin() + nameStart, buf.begin() + (end + 1));
119
5.97k
    }
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
102
12.5k
    {
103
      // We substract 1 from 'len' when assigning to 'end' to avoid out of
104
      // bounds exception in return r.substring(end+1, len). This can happen if
105
      // precision is 1 and the logger name ends with a dot.
106
12.5k
      LogString::size_type end = buf.length() - 1;
107
108
25.9k
      for (LogString::size_type i = count; i > 0; i--)
109
13.5k
      {
110
13.5k
        end = buf.rfind(0x2E /* '.' */, end - 1);
111
112
13.5k
        if ((end == LogString::npos) || (end < nameStart))
113
158
        {
114
158
          return;
115
158
        }
116
13.5k
      }
117
118
12.4k
      buf.erase(buf.begin() + nameStart, buf.begin() + (end + 1));
119
12.4k
    }
120
};
121
122
/**
123
 * Fragment of an pattern abbreviator.
124
 *
125
 */
126
class PatternAbbreviatorFragment
127
{
128
    /**
129
     * Count of initial characters of element to output.
130
     */
131
    LogString::size_type charCount;
132
133
    /**
134
     *  Character used to represent dropped characters.
135
     * '\0' indicates no representation of dropped characters.
136
     */
137
    logchar ellipsis;
138
139
  public:
140
    /**
141
     * Creates a PatternAbbreviatorFragment.
142
     * @param charCount number of initial characters to preserve.
143
     * @param ellipsis character to represent elimination of characters,
144
     *    '\0' if no ellipsis is desired.
145
     */
146
    PatternAbbreviatorFragment(
147
      const int charCount1, const logchar ellipsis1)
148
148k
      : charCount(charCount1), ellipsis(ellipsis1)
149
148k
    {
150
148k
    }
log4cxx::pattern::PatternAbbreviatorFragment::PatternAbbreviatorFragment(int, char)
Line
Count
Source
148
79.1k
      : charCount(charCount1), ellipsis(ellipsis1)
149
79.1k
    {
150
79.1k
    }
log4cxx::pattern::PatternAbbreviatorFragment::PatternAbbreviatorFragment(int, wchar_t)
Line
Count
Source
148
69.0k
      : charCount(charCount1), ellipsis(ellipsis1)
149
69.0k
    {
150
69.0k
    }
151
    PatternAbbreviatorFragment() : charCount(0), ellipsis(0)
152
0
    {
153
0
    }
154
155
    PatternAbbreviatorFragment(const PatternAbbreviatorFragment& src)
156
487k
      : charCount(src.charCount), ellipsis(src.ellipsis)
157
487k
    {
158
487k
    }
159
160
    PatternAbbreviatorFragment& operator=(const PatternAbbreviatorFragment& src)
161
0
    {
162
0
      charCount = src.charCount;
163
0
      ellipsis = src.ellipsis;
164
0
      return *this;
165
0
    }
166
167
    /**
168
     * Abbreviate element of name.
169
     * @param buf buffer to receive element.
170
     * @param startPos starting index of name element.
171
     * @return starting index of next element.
172
     */
173
    LogString::size_type abbreviate(LogString& buf, LogString::size_type startPos) const
174
108k
    {
175
108k
      LogString::size_type nextDot = buf.find(0x2E /* '.' */, startPos);
176
177
108k
      if (nextDot != LogString::npos)
178
108k
      {
179
108k
        if ((nextDot - startPos) > charCount)
180
72.2k
        {
181
72.2k
          buf.erase(buf.begin() + (startPos + charCount), buf.begin() + nextDot);
182
72.2k
          nextDot = startPos + charCount;
183
184
72.2k
          if (ellipsis != 0x00)
185
71.2k
          {
186
71.2k
            buf.insert(nextDot, 1, ellipsis);
187
71.2k
            nextDot++;
188
71.2k
          }
189
72.2k
        }
190
191
108k
        nextDot++;
192
108k
      }
193
194
108k
      return nextDot;
195
108k
    }
log4cxx::pattern::PatternAbbreviatorFragment::abbreviate(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&, unsigned long) const
Line
Count
Source
174
73.5k
    {
175
73.5k
      LogString::size_type nextDot = buf.find(0x2E /* '.' */, startPos);
176
177
73.5k
      if (nextDot != LogString::npos)
178
73.4k
      {
179
73.4k
        if ((nextDot - startPos) > charCount)
180
70.9k
        {
181
70.9k
          buf.erase(buf.begin() + (startPos + charCount), buf.begin() + nextDot);
182
70.9k
          nextDot = startPos + charCount;
183
184
70.9k
          if (ellipsis != 0x00)
185
70.6k
          {
186
70.6k
            buf.insert(nextDot, 1, ellipsis);
187
70.6k
            nextDot++;
188
70.6k
          }
189
70.9k
        }
190
191
73.4k
        nextDot++;
192
73.4k
      }
193
194
73.5k
      return nextDot;
195
73.5k
    }
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
174
35.2k
    {
175
35.2k
      LogString::size_type nextDot = buf.find(0x2E /* '.' */, startPos);
176
177
35.2k
      if (nextDot != LogString::npos)
178
35.1k
      {
179
35.1k
        if ((nextDot - startPos) > charCount)
180
1.30k
        {
181
1.30k
          buf.erase(buf.begin() + (startPos + charCount), buf.begin() + nextDot);
182
1.30k
          nextDot = startPos + charCount;
183
184
1.30k
          if (ellipsis != 0x00)
185
616
          {
186
616
            buf.insert(nextDot, 1, ellipsis);
187
616
            nextDot++;
188
616
          }
189
1.30k
        }
190
191
35.1k
        nextDot++;
192
35.1k
      }
193
194
35.2k
      return nextDot;
195
35.2k
    }
196
};
197
198
/**
199
 * Pattern abbreviator.
200
 *
201
 *
202
 */
203
class PatternAbbreviator : public NameAbbreviator
204
{
205
    /**
206
     * Element abbreviation patterns.
207
     */
208
    std::vector<PatternAbbreviatorFragment> fragments;
209
210
  public:
211
    DECLARE_ABSTRACT_LOG4CXX_OBJECT(PatternAbbreviator)
212
0
    BEGIN_LOG4CXX_CAST_MAP()
213
0
    LOG4CXX_CAST_ENTRY(PatternAbbreviator)
214
0
    LOG4CXX_CAST_ENTRY_CHAIN(NameAbbreviator)
215
0
    END_LOG4CXX_CAST_MAP()
216
    /**
217
     * Create PatternAbbreviator.
218
     *
219
     * @param fragments element abbreviation patterns.
220
     */
221
    PatternAbbreviator(const std::vector<PatternAbbreviatorFragment>& fragments1) :
222
4.43k
      fragments(fragments1)
223
4.43k
    {
224
4.43k
      if (fragments1.size() == 0)
225
0
      {
226
0
        throw IllegalArgumentException(LOG4CXX_STR("fragments parameter must contain at least one element"));
227
0
      }
228
4.43k
    }
229
230
    /**
231
     * Abbreviate name.
232
     * @param buf buffer that abbreviated name is appended.
233
     * @param nameStart start of name.
234
     */
235
    void abbreviate(LogString::size_type nameStart, LogString& buf) const override
236
710
    {
237
      //
238
      //  all non-terminal patterns are executed once
239
      //
240
710
      LogString::size_type pos = nameStart;
241
242
4.78k
      for (LogString::size_type i = 0; (i < (fragments.size() - 1)) && (pos < buf.length());
243
4.07k
        i++)
244
4.07k
      {
245
4.07k
        pos = fragments[i].abbreviate(buf, pos);
246
4.07k
      }
247
248
      //
249
      //   last pattern in executed repeatedly
250
      //
251
710
      PatternAbbreviatorFragment terminalFragment =
252
710
        fragments[fragments.size() - 1];
253
254
105k
      while (pos < buf.length())
255
104k
      {
256
104k
        pos = terminalFragment.abbreviate(buf, pos);
257
104k
      }
258
710
    }
log4cxx::pattern::PatternAbbreviator::abbreviate(unsigned long, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&) const
Line
Count
Source
236
351
    {
237
      //
238
      //  all non-terminal patterns are executed once
239
      //
240
351
      LogString::size_type pos = nameStart;
241
242
1.37k
      for (LogString::size_type i = 0; (i < (fragments.size() - 1)) && (pos < buf.length());
243
1.02k
        i++)
244
1.02k
      {
245
1.02k
        pos = fragments[i].abbreviate(buf, pos);
246
1.02k
      }
247
248
      //
249
      //   last pattern in executed repeatedly
250
      //
251
351
      PatternAbbreviatorFragment terminalFragment =
252
351
        fragments[fragments.size() - 1];
253
254
72.8k
      while (pos < buf.length())
255
72.4k
      {
256
72.4k
        pos = terminalFragment.abbreviate(buf, pos);
257
72.4k
      }
258
351
    }
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
236
359
    {
237
      //
238
      //  all non-terminal patterns are executed once
239
      //
240
359
      LogString::size_type pos = nameStart;
241
242
3.41k
      for (LogString::size_type i = 0; (i < (fragments.size() - 1)) && (pos < buf.length());
243
3.05k
        i++)
244
3.05k
      {
245
3.05k
        pos = fragments[i].abbreviate(buf, pos);
246
3.05k
      }
247
248
      //
249
      //   last pattern in executed repeatedly
250
      //
251
359
      PatternAbbreviatorFragment terminalFragment =
252
359
        fragments[fragments.size() - 1];
253
254
32.5k
      while (pos < buf.length())
255
32.2k
      {
256
32.2k
        pos = terminalFragment.abbreviate(buf, pos);
257
32.2k
      }
258
359
    }
259
};
260
}
261
}
262
263
IMPLEMENT_LOG4CXX_OBJECT(NOPAbbreviator)
264
IMPLEMENT_LOG4CXX_OBJECT(MaxElementAbbreviator)
265
IMPLEMENT_LOG4CXX_OBJECT(PatternAbbreviator)
266
267
268
269
NameAbbreviatorPtr NameAbbreviator::getAbbreviator(const LogString& pattern)
270
19.8k
{
271
19.8k
  if (pattern.length() > 0)
272
10.1k
  {
273
    //  if pattern is just spaces and numbers then
274
    //     use MaxElementAbbreviator
275
10.1k
    LogString trimmed(StringHelper::trim(pattern));
276
277
10.1k
    if (trimmed.length() == 0)
278
620
    {
279
620
      return getDefaultAbbreviator();
280
620
    }
281
282
9.55k
    LogString::size_type i = 0;
283
284
9.55k
    while (
285
136k
      (i < trimmed.length()) && (trimmed[i] >= 0x30 /* '0' */)
286
136k
      && (trimmed[i] <= 0x39 /* '9' */))
287
126k
    {
288
126k
      i++;
289
126k
    }
290
291
    //
292
    //  if all blanks and digits
293
    //
294
9.55k
    if (i == trimmed.length())
295
5.11k
    {
296
5.11k
      int len = 256;
297
5.11k
      try
298
5.11k
      {
299
5.11k
        len = StringHelper::toInt(trimmed);
300
5.11k
      }
301
5.11k
      catch (const std::out_of_range& ex)
302
5.11k
      {
303
569
        LogLog::warn(LOG4CXX_STR("Invalid name abreviator pattern: ") + pattern, ex);
304
569
      }
305
306
5.11k
      if(len > 256){
307
635
        len = 256;
308
4.47k
      }else if( len < 0 ){
309
0
        len = 0;
310
0
      }
311
312
5.11k
      return std::make_shared<MaxElementAbbreviator>(len);
313
5.11k
    }
314
315
4.43k
    std::vector<PatternAbbreviatorFragment> fragments;
316
4.43k
    logchar ellipsis;
317
4.43k
    int charCount;
318
4.43k
    LogString::size_type pos = 0;
319
320
149k
    while (pos < trimmed.length())
321
148k
    {
322
148k
      LogString::size_type ellipsisPos = pos;
323
324
148k
      if (trimmed[pos] == 0x2A /* '*' */)
325
2.50k
      {
326
2.50k
        charCount = INT_MAX;
327
2.50k
        ellipsisPos++;
328
2.50k
      }
329
145k
      else
330
145k
      {
331
145k
        if ((trimmed[pos] >= 0x30 /* '0' */)
332
145k
          && (trimmed[pos] <= 0x39 /* '9' */))
333
2.67k
        {
334
2.67k
          charCount = trimmed[pos] - 0x30 /* '0' */;
335
2.67k
          ellipsisPos++;
336
2.67k
        }
337
143k
        else
338
143k
        {
339
143k
          charCount = 0;
340
143k
        }
341
145k
      }
342
343
148k
      ellipsis = 0;
344
345
148k
      if (ellipsisPos < trimmed.length())
346
147k
      {
347
147k
        ellipsis = trimmed[ellipsisPos];
348
349
147k
        if (ellipsis == 0x2E /* '.' */)
350
138k
        {
351
138k
          ellipsis = 0;
352
138k
        }
353
147k
      }
354
355
148k
      fragments.push_back(PatternAbbreviatorFragment(charCount, ellipsis));
356
148k
      pos = trimmed.find(0x2E /* '.' */, pos);
357
358
148k
      if (pos == LogString::npos)
359
3.66k
      {
360
3.66k
        break;
361
3.66k
      }
362
363
144k
      pos++;
364
144k
    }
365
366
4.43k
    return std::make_shared<PatternAbbreviator>(fragments);
367
9.55k
  }
368
369
  //
370
  //  no matching abbreviation, return defaultAbbreviator
371
  //
372
9.68k
  return getDefaultAbbreviator();
373
19.8k
}
log4cxx::pattern::NameAbbreviator::getAbbreviator(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Line
Count
Source
270
8.85k
{
271
8.85k
  if (pattern.length() > 0)
272
4.32k
  {
273
    //  if pattern is just spaces and numbers then
274
    //     use MaxElementAbbreviator
275
4.32k
    LogString trimmed(StringHelper::trim(pattern));
276
277
4.32k
    if (trimmed.length() == 0)
278
307
    {
279
307
      return getDefaultAbbreviator();
280
307
    }
281
282
4.01k
    LogString::size_type i = 0;
283
284
4.01k
    while (
285
44.3k
      (i < trimmed.length()) && (trimmed[i] >= 0x30 /* '0' */)
286
44.3k
      && (trimmed[i] <= 0x39 /* '9' */))
287
40.2k
    {
288
40.2k
      i++;
289
40.2k
    }
290
291
    //
292
    //  if all blanks and digits
293
    //
294
4.01k
    if (i == trimmed.length())
295
1.70k
    {
296
1.70k
      int len = 256;
297
1.70k
      try
298
1.70k
      {
299
1.70k
        len = StringHelper::toInt(trimmed);
300
1.70k
      }
301
1.70k
      catch (const std::out_of_range& ex)
302
1.70k
      {
303
280
        LogLog::warn(LOG4CXX_STR("Invalid name abreviator pattern: ") + pattern, ex);
304
280
      }
305
306
1.70k
      if(len > 256){
307
260
        len = 256;
308
1.44k
      }else if( len < 0 ){
309
0
        len = 0;
310
0
      }
311
312
1.70k
      return std::make_shared<MaxElementAbbreviator>(len);
313
1.70k
    }
314
315
2.30k
    std::vector<PatternAbbreviatorFragment> fragments;
316
2.30k
    logchar ellipsis;
317
2.30k
    int charCount;
318
2.30k
    LogString::size_type pos = 0;
319
320
79.5k
    while (pos < trimmed.length())
321
79.1k
    {
322
79.1k
      LogString::size_type ellipsisPos = pos;
323
324
79.1k
      if (trimmed[pos] == 0x2A /* '*' */)
325
800
      {
326
800
        charCount = INT_MAX;
327
800
        ellipsisPos++;
328
800
      }
329
78.3k
      else
330
78.3k
      {
331
78.3k
        if ((trimmed[pos] >= 0x30 /* '0' */)
332
78.3k
          && (trimmed[pos] <= 0x39 /* '9' */))
333
1.79k
        {
334
1.79k
          charCount = trimmed[pos] - 0x30 /* '0' */;
335
1.79k
          ellipsisPos++;
336
1.79k
        }
337
76.5k
        else
338
76.5k
        {
339
76.5k
          charCount = 0;
340
76.5k
        }
341
78.3k
      }
342
343
79.1k
      ellipsis = 0;
344
345
79.1k
      if (ellipsisPos < trimmed.length())
346
78.8k
      {
347
78.8k
        ellipsis = trimmed[ellipsisPos];
348
349
78.8k
        if (ellipsis == 0x2E /* '.' */)
350
73.5k
        {
351
73.5k
          ellipsis = 0;
352
73.5k
        }
353
78.8k
      }
354
355
79.1k
      fragments.push_back(PatternAbbreviatorFragment(charCount, ellipsis));
356
79.1k
      pos = trimmed.find(0x2E /* '.' */, pos);
357
358
79.1k
      if (pos == LogString::npos)
359
1.94k
      {
360
1.94k
        break;
361
1.94k
      }
362
363
77.2k
      pos++;
364
77.2k
    }
365
366
2.30k
    return std::make_shared<PatternAbbreviator>(fragments);
367
4.01k
  }
368
369
  //
370
  //  no matching abbreviation, return defaultAbbreviator
371
  //
372
4.53k
  return getDefaultAbbreviator();
373
8.85k
}
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
270
11.0k
{
271
11.0k
  if (pattern.length() > 0)
272
5.85k
  {
273
    //  if pattern is just spaces and numbers then
274
    //     use MaxElementAbbreviator
275
5.85k
    LogString trimmed(StringHelper::trim(pattern));
276
277
5.85k
    if (trimmed.length() == 0)
278
313
    {
279
313
      return getDefaultAbbreviator();
280
313
    }
281
282
5.53k
    LogString::size_type i = 0;
283
284
5.53k
    while (
285
91.7k
      (i < trimmed.length()) && (trimmed[i] >= 0x30 /* '0' */)
286
91.7k
      && (trimmed[i] <= 0x39 /* '9' */))
287
86.2k
    {
288
86.2k
      i++;
289
86.2k
    }
290
291
    //
292
    //  if all blanks and digits
293
    //
294
5.53k
    if (i == trimmed.length())
295
3.40k
    {
296
3.40k
      int len = 256;
297
3.40k
      try
298
3.40k
      {
299
3.40k
        len = StringHelper::toInt(trimmed);
300
3.40k
      }
301
3.40k
      catch (const std::out_of_range& ex)
302
3.40k
      {
303
289
        LogLog::warn(LOG4CXX_STR("Invalid name abreviator pattern: ") + pattern, ex);
304
289
      }
305
306
3.40k
      if(len > 256){
307
375
        len = 256;
308
3.03k
      }else if( len < 0 ){
309
0
        len = 0;
310
0
      }
311
312
3.40k
      return std::make_shared<MaxElementAbbreviator>(len);
313
3.40k
    }
314
315
2.13k
    std::vector<PatternAbbreviatorFragment> fragments;
316
2.13k
    logchar ellipsis;
317
2.13k
    int charCount;
318
2.13k
    LogString::size_type pos = 0;
319
320
69.4k
    while (pos < trimmed.length())
321
69.0k
    {
322
69.0k
      LogString::size_type ellipsisPos = pos;
323
324
69.0k
      if (trimmed[pos] == 0x2A /* '*' */)
325
1.70k
      {
326
1.70k
        charCount = INT_MAX;
327
1.70k
        ellipsisPos++;
328
1.70k
      }
329
67.3k
      else
330
67.3k
      {
331
67.3k
        if ((trimmed[pos] >= 0x30 /* '0' */)
332
67.3k
          && (trimmed[pos] <= 0x39 /* '9' */))
333
888
        {
334
888
          charCount = trimmed[pos] - 0x30 /* '0' */;
335
888
          ellipsisPos++;
336
888
        }
337
66.4k
        else
338
66.4k
        {
339
66.4k
          charCount = 0;
340
66.4k
        }
341
67.3k
      }
342
343
69.0k
      ellipsis = 0;
344
345
69.0k
      if (ellipsisPos < trimmed.length())
346
68.6k
      {
347
68.6k
        ellipsis = trimmed[ellipsisPos];
348
349
68.6k
        if (ellipsis == 0x2E /* '.' */)
350
65.1k
        {
351
65.1k
          ellipsis = 0;
352
65.1k
        }
353
68.6k
      }
354
355
69.0k
      fragments.push_back(PatternAbbreviatorFragment(charCount, ellipsis));
356
69.0k
      pos = trimmed.find(0x2E /* '.' */, pos);
357
358
69.0k
      if (pos == LogString::npos)
359
1.72k
      {
360
1.72k
        break;
361
1.72k
      }
362
363
67.3k
      pos++;
364
67.3k
    }
365
366
2.13k
    return std::make_shared<PatternAbbreviator>(fragments);
367
5.53k
  }
368
369
  //
370
  //  no matching abbreviation, return defaultAbbreviator
371
  //
372
5.15k
  return getDefaultAbbreviator();
373
11.0k
}
374
375
/**
376
 * Gets default abbreviator.
377
 *
378
 * @return default abbreviator.
379
 */
380
NameAbbreviatorPtr NameAbbreviator::getDefaultAbbreviator()
381
10.3k
{
382
10.3k
  static WideLife<NameAbbreviatorPtr> def = std::make_shared<NOPAbbreviator>();
383
10.3k
  return def;
384
10.3k
}
385