/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 <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 | 20.0k | minLength(minLength1), |
29 | 20.0k | maxLength(maxLength1), |
30 | 20.0k | 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 | 20.0k | m_priv(std::make_unique<FormattingInfoPrivate>(leftAlign1, minLength1, maxLength1)) |
59 | 20.0k | { |
60 | 20.0k | } Unexecuted instantiation: log4cxx::pattern::FormattingInfo::FormattingInfo(bool, int, int) log4cxx::pattern::FormattingInfo::FormattingInfo(bool, int, int) Line | Count | Source | 58 | 20.0k | m_priv(std::make_unique<FormattingInfoPrivate>(leftAlign1, minLength1, maxLength1)) | 59 | 20.0k | { | 60 | 20.0k | } |
|
61 | | |
62 | 20.0k | FormattingInfo::~FormattingInfo() {} |
63 | | |
64 | | /** |
65 | | * Gets default instance. |
66 | | * @return default instance. |
67 | | */ |
68 | | FormattingInfoPtr FormattingInfo::getDefault() |
69 | 436k | { |
70 | 436k | static helpers::WideLife<FormattingInfoPtr> def= std::make_shared<FormattingInfo>(false, 0, INT_MAX); |
71 | 436k | return def; |
72 | 436k | } |
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 | if (INT_MAX < buffer.length()) |
83 | 0 | return; |
84 | 0 | int rawLength = static_cast<int>(buffer.length()) - fieldStart; |
85 | |
|
86 | 0 | if (rawLength > m_priv->maxLength) |
87 | 0 | { |
88 | 0 | buffer.erase(buffer.begin() + fieldStart, |
89 | 0 | buffer.begin() + fieldStart + (rawLength - m_priv->maxLength)); |
90 | 0 | } |
91 | 0 | else if (rawLength < m_priv->minLength) |
92 | 0 | { |
93 | 0 | if (m_priv->leftAlign) |
94 | 0 | { |
95 | 0 | buffer.append(m_priv->minLength - rawLength, (logchar) 0x20 /* ' ' */); |
96 | 0 | } |
97 | 0 | else |
98 | 0 | { |
99 | 0 | buffer.insert(fieldStart, m_priv->minLength - rawLength, 0x20 /* ' ' */); |
100 | 0 | } |
101 | 0 | } |
102 | 0 | } |
103 | | |
104 | | bool FormattingInfo::isLeftAligned() const |
105 | 9.34k | { |
106 | 9.34k | return m_priv->leftAlign; |
107 | 9.34k | } |
108 | | |
109 | | int FormattingInfo::getMinLength() const |
110 | 15.2k | { |
111 | 15.2k | return m_priv->minLength; |
112 | 15.2k | } |
113 | | |
114 | | int FormattingInfo::getMaxLength() const |
115 | 17.5k | { |
116 | 17.5k | return m_priv->maxLength; |
117 | 17.5k | } |