/src/logging-log4cxx/src/main/cpp/formattinginfo.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/logstring.h> |
19 | | #include <log4cxx/pattern/formattinginfo.h> |
20 | | #include <algorithm> |
21 | | #include <limits.h> |
22 | | |
23 | | using namespace LOG4CXX_NS; |
24 | | using namespace LOG4CXX_NS::pattern; |
25 | | |
26 | | struct FormattingInfo::FormattingInfoPrivate |
27 | | { |
28 | | FormattingInfoPrivate(const bool leftAlign1, const int minLength1, const int maxLength1): |
29 | 89.9k | minLength(minLength1), |
30 | 89.9k | maxLength(maxLength1), |
31 | 89.9k | leftAlign(leftAlign1) {} |
32 | | |
33 | | /** |
34 | | * Minimum length. |
35 | | */ |
36 | | const int minLength; |
37 | | |
38 | | /** |
39 | | * Maximum length. |
40 | | */ |
41 | | const int maxLength; |
42 | | |
43 | | /** |
44 | | * Alignment. |
45 | | */ |
46 | | const bool leftAlign; |
47 | | }; |
48 | | |
49 | | IMPLEMENT_LOG4CXX_OBJECT(FormattingInfo) |
50 | | |
51 | | /** |
52 | | * Creates new instance. |
53 | | * @param leftAlign left align if true. |
54 | | * @param minLength minimum length. |
55 | | * @param maxLength maximum length. |
56 | | */ |
57 | | FormattingInfo::FormattingInfo( |
58 | | const bool leftAlign1, const int minLength1, const int maxLength1) : |
59 | 89.9k | m_priv(std::make_unique<FormattingInfoPrivate>(leftAlign1, minLength1, maxLength1)) |
60 | 89.9k | { |
61 | 89.9k | } Unexecuted instantiation: log4cxx::pattern::FormattingInfo::FormattingInfo(bool, int, int) log4cxx::pattern::FormattingInfo::FormattingInfo(bool, int, int) Line | Count | Source | 59 | 89.9k | m_priv(std::make_unique<FormattingInfoPrivate>(leftAlign1, minLength1, maxLength1)) | 60 | 89.9k | { | 61 | 89.9k | } |
|
62 | | |
63 | 89.9k | FormattingInfo::~FormattingInfo() {} |
64 | | |
65 | | /** |
66 | | * Gets default instance. |
67 | | * @return default instance. |
68 | | */ |
69 | | FormattingInfoPtr FormattingInfo::getDefault() |
70 | 975k | { |
71 | 975k | static helpers::WideLife<FormattingInfoPtr> def= std::make_shared<FormattingInfo>(false, 0, INT_MAX); |
72 | 975k | return def; |
73 | 975k | } |
74 | | |
75 | | /** |
76 | | * Adjust the content of the buffer based on the specified lengths and alignment. |
77 | | * |
78 | | * @param fieldStart start of field in buffer. |
79 | | * @param buffer buffer to be modified. |
80 | | */ |
81 | | #if LOG4CXX_ABI_VERSION <= 15 |
82 | | void FormattingInfo::format(const int fieldStart, LogString& buffer) const |
83 | 0 | { |
84 | 0 | adjustField(static_cast<LogString::size_type>(std::max(fieldStart, 0)), buffer); |
85 | 0 | } Unexecuted instantiation: log4cxx::pattern::FormattingInfo::format(int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&) const Unexecuted instantiation: log4cxx::pattern::FormattingInfo::format(int, std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> >&) const |
86 | | #endif |
87 | | void FormattingInfo::adjustField(const LogString::size_type fieldStart, LogString& buffer) const |
88 | 1.17M | { |
89 | 1.17M | if (fieldStart > buffer.length()) |
90 | 0 | { |
91 | 0 | return; |
92 | 0 | } |
93 | | |
94 | 1.17M | const LogString::size_type rawLength = buffer.length() - fieldStart; |
95 | 1.17M | const LogString::size_type maxLength = static_cast<LogString::size_type>(m_priv->maxLength); |
96 | 1.17M | const LogString::size_type minLength = static_cast<LogString::size_type>(m_priv->minLength); |
97 | | |
98 | 1.17M | if (rawLength > maxLength) |
99 | 0 | { |
100 | 0 | buffer.erase(buffer.begin() + fieldStart, |
101 | 0 | buffer.begin() + fieldStart + (rawLength - maxLength)); |
102 | 0 | } |
103 | 1.17M | else if (rawLength < minLength) |
104 | 108k | { |
105 | 108k | if (m_priv->leftAlign) |
106 | 0 | { |
107 | 0 | buffer.append(minLength - rawLength, (logchar) 0x20 /* ' ' */); |
108 | 0 | } |
109 | 108k | else |
110 | 108k | { |
111 | 108k | buffer.insert(fieldStart, minLength - rawLength, 0x20 /* ' ' */); |
112 | 108k | } |
113 | 108k | } |
114 | 1.17M | } log4cxx::pattern::FormattingInfo::adjustField(unsigned long, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&) const Line | Count | Source | 88 | 479k | { | 89 | 479k | if (fieldStart > buffer.length()) | 90 | 0 | { | 91 | 0 | return; | 92 | 0 | } | 93 | | | 94 | 479k | const LogString::size_type rawLength = buffer.length() - fieldStart; | 95 | 479k | const LogString::size_type maxLength = static_cast<LogString::size_type>(m_priv->maxLength); | 96 | 479k | const LogString::size_type minLength = static_cast<LogString::size_type>(m_priv->minLength); | 97 | | | 98 | 479k | if (rawLength > maxLength) | 99 | 0 | { | 100 | 0 | buffer.erase(buffer.begin() + fieldStart, | 101 | 0 | buffer.begin() + fieldStart + (rawLength - maxLength)); | 102 | 0 | } | 103 | 479k | else if (rawLength < minLength) | 104 | 44.7k | { | 105 | 44.7k | if (m_priv->leftAlign) | 106 | 0 | { | 107 | 0 | buffer.append(minLength - rawLength, (logchar) 0x20 /* ' ' */); | 108 | 0 | } | 109 | 44.7k | else | 110 | 44.7k | { | 111 | 44.7k | buffer.insert(fieldStart, minLength - rawLength, 0x20 /* ' ' */); | 112 | 44.7k | } | 113 | 44.7k | } | 114 | 479k | } |
log4cxx::pattern::FormattingInfo::adjustField(unsigned long, std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> >&) const Line | Count | Source | 88 | 700k | { | 89 | 700k | if (fieldStart > buffer.length()) | 90 | 0 | { | 91 | 0 | return; | 92 | 0 | } | 93 | | | 94 | 700k | const LogString::size_type rawLength = buffer.length() - fieldStart; | 95 | 700k | const LogString::size_type maxLength = static_cast<LogString::size_type>(m_priv->maxLength); | 96 | 700k | const LogString::size_type minLength = static_cast<LogString::size_type>(m_priv->minLength); | 97 | | | 98 | 700k | if (rawLength > maxLength) | 99 | 0 | { | 100 | 0 | buffer.erase(buffer.begin() + fieldStart, | 101 | 0 | buffer.begin() + fieldStart + (rawLength - maxLength)); | 102 | 0 | } | 103 | 700k | else if (rawLength < minLength) | 104 | 63.5k | { | 105 | 63.5k | if (m_priv->leftAlign) | 106 | 0 | { | 107 | 0 | buffer.append(minLength - rawLength, (logchar) 0x20 /* ' ' */); | 108 | 0 | } | 109 | 63.5k | else | 110 | 63.5k | { | 111 | 63.5k | buffer.insert(fieldStart, minLength - rawLength, 0x20 /* ' ' */); | 112 | 63.5k | } | 113 | 63.5k | } | 114 | 700k | } |
|
115 | | |
116 | | bool FormattingInfo::isLeftAligned() const |
117 | 21.4k | { |
118 | 21.4k | return m_priv->leftAlign; |
119 | 21.4k | } |
120 | | |
121 | | int FormattingInfo::getMinLength() const |
122 | 78.3k | { |
123 | 78.3k | return m_priv->minLength; |
124 | 78.3k | } |
125 | | |
126 | | int FormattingInfo::getMaxLength() const |
127 | 208k | { |
128 | 208k | return m_priv->maxLength; |
129 | 208k | } |