/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 | 0 | minLength(minLength1), |
30 | 0 | maxLength(maxLength1), |
31 | 0 | 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 | 0 | m_priv(std::make_unique<FormattingInfoPrivate>(leftAlign1, minLength1, maxLength1)) |
60 | 0 | { |
61 | 0 | } Unexecuted instantiation: log4cxx::pattern::FormattingInfo::FormattingInfo(bool, int, int) Unexecuted instantiation: log4cxx::pattern::FormattingInfo::FormattingInfo(bool, int, int) |
62 | | |
63 | 0 | FormattingInfo::~FormattingInfo() {} |
64 | | |
65 | | /** |
66 | | * Gets default instance. |
67 | | * @return default instance. |
68 | | */ |
69 | | FormattingInfoPtr FormattingInfo::getDefault() |
70 | 0 | { |
71 | 0 | static helpers::WideLife<FormattingInfoPtr> def= std::make_shared<FormattingInfo>(false, 0, INT_MAX); |
72 | 0 | return def; |
73 | 0 | } |
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 | } |
86 | | #endif |
87 | | void FormattingInfo::adjustField(const LogString::size_type fieldStart, LogString& buffer) const |
88 | 0 | { |
89 | 0 | if (fieldStart > buffer.length()) |
90 | 0 | { |
91 | 0 | return; |
92 | 0 | } |
93 | | |
94 | 0 | const LogString::size_type rawLength = buffer.length() - fieldStart; |
95 | 0 | const LogString::size_type maxLength = static_cast<LogString::size_type>(m_priv->maxLength); |
96 | 0 | const LogString::size_type minLength = static_cast<LogString::size_type>(m_priv->minLength); |
97 | |
|
98 | 0 | if (rawLength > maxLength) |
99 | 0 | { |
100 | 0 | buffer.erase(buffer.begin() + fieldStart, |
101 | 0 | buffer.begin() + fieldStart + (rawLength - maxLength)); |
102 | 0 | } |
103 | 0 | else if (rawLength < minLength) |
104 | 0 | { |
105 | 0 | if (m_priv->leftAlign) |
106 | 0 | { |
107 | 0 | buffer.append(minLength - rawLength, (logchar) 0x20 /* ' ' */); |
108 | 0 | } |
109 | 0 | else |
110 | 0 | { |
111 | 0 | buffer.insert(fieldStart, minLength - rawLength, 0x20 /* ' ' */); |
112 | 0 | } |
113 | 0 | } |
114 | 0 | } |
115 | | |
116 | | bool FormattingInfo::isLeftAligned() const |
117 | 0 | { |
118 | 0 | return m_priv->leftAlign; |
119 | 0 | } |
120 | | |
121 | | int FormattingInfo::getMinLength() const |
122 | 0 | { |
123 | 0 | return m_priv->minLength; |
124 | 0 | } |
125 | | |
126 | | int FormattingInfo::getMaxLength() const |
127 | 0 | { |
128 | 0 | return m_priv->maxLength; |
129 | 0 | } |