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