/src/logging-log4cxx/src/main/cpp/cyclicbuffer.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 | | #include <log4cxx/logstring.h> |
18 | | #include <log4cxx/helpers/cyclicbuffer.h> |
19 | | #include <log4cxx/spi/loggingevent.h> |
20 | | #include <log4cxx/helpers/exception.h> |
21 | | #include <log4cxx/helpers/pool.h> |
22 | | #include <log4cxx/helpers/stringhelper.h> |
23 | | |
24 | | using namespace LOG4CXX_NS; |
25 | | using namespace LOG4CXX_NS::helpers; |
26 | | using namespace LOG4CXX_NS::spi; |
27 | | |
28 | | struct CyclicBuffer::CyclicBufferPriv |
29 | | { |
30 | | CyclicBufferPriv(int maxSize1) : |
31 | 0 | ea(maxSize1), first(0), last(0), numElems(0), maxSize(maxSize1) {} |
32 | | |
33 | | LOG4CXX_NS::spi::LoggingEventList ea; |
34 | | int first; |
35 | | int last; |
36 | | int numElems; |
37 | | int maxSize; |
38 | | }; |
39 | | |
40 | | /** |
41 | | Instantiate a new CyclicBuffer of at most <code>maxSize</code> events. |
42 | | The <code>maxSize</code> argument must a positive integer. |
43 | | @param maxSize The maximum number of elements in the buffer. |
44 | | */ |
45 | | CyclicBuffer::CyclicBuffer(int maxSize1) |
46 | 0 | : m_priv(std::make_unique<CyclicBufferPriv>(maxSize1)) |
47 | 0 | { |
48 | 0 | if (maxSize1 < 1) |
49 | 0 | { |
50 | 0 | LogString msg(LOG4CXX_STR("The maxSize argument (")); |
51 | 0 | Pool p; |
52 | 0 | StringHelper::toString(maxSize1, p, msg); |
53 | 0 | msg.append(LOG4CXX_STR(") is not a positive integer.")); |
54 | 0 | throw IllegalArgumentException(msg); |
55 | 0 | } |
56 | 0 | } |
57 | | |
58 | | CyclicBuffer::~CyclicBuffer() |
59 | 0 | { |
60 | 0 | } |
61 | | |
62 | | /** |
63 | | Add an <code>event</code> as the last event in the buffer. |
64 | | */ |
65 | | void CyclicBuffer::add(const spi::LoggingEventPtr& event) |
66 | 0 | { |
67 | 0 | if (m_priv->ea.empty()) |
68 | 0 | { |
69 | 0 | return; |
70 | 0 | } |
71 | | |
72 | 0 | m_priv->ea[m_priv->last] = event; |
73 | |
|
74 | 0 | if (++m_priv->last == m_priv->maxSize) |
75 | 0 | { |
76 | 0 | m_priv->last = 0; |
77 | 0 | } |
78 | |
|
79 | 0 | if (m_priv->numElems < m_priv->maxSize) |
80 | 0 | { |
81 | 0 | m_priv->numElems++; |
82 | 0 | } |
83 | 0 | else if (++m_priv->first == m_priv->maxSize) |
84 | 0 | { |
85 | 0 | m_priv->first = 0; |
86 | 0 | } |
87 | 0 | } |
88 | | |
89 | | |
90 | | /** |
91 | | Get the <i>i</i>th oldest event currently in the buffer. If |
92 | | <em>i</em> is outside the range 0 to the number of elements |
93 | | currently in the buffer, then <code>null</code> is returned. |
94 | | */ |
95 | | spi::LoggingEventPtr CyclicBuffer::get(int i) |
96 | 0 | { |
97 | 0 | if (i < 0 || i >= m_priv->numElems) |
98 | 0 | { |
99 | 0 | return 0; |
100 | 0 | } |
101 | | |
102 | 0 | return m_priv->ea[(m_priv->first + i) % m_priv->maxSize]; |
103 | 0 | } |
104 | | |
105 | | /** |
106 | | Get the oldest (first) element in the buffer. The oldest element |
107 | | is removed from the buffer. |
108 | | */ |
109 | | spi::LoggingEventPtr CyclicBuffer::get() |
110 | 0 | { |
111 | 0 | LoggingEventPtr r; |
112 | |
|
113 | 0 | if (m_priv->numElems > 0) |
114 | 0 | { |
115 | 0 | m_priv->numElems--; |
116 | 0 | r = m_priv->ea[m_priv->first]; |
117 | 0 | m_priv->ea[m_priv->first] = 0; |
118 | |
|
119 | 0 | if (++m_priv->first == m_priv->maxSize) |
120 | 0 | { |
121 | 0 | m_priv->first = 0; |
122 | 0 | } |
123 | 0 | } |
124 | |
|
125 | 0 | return r; |
126 | 0 | } |
127 | | |
128 | | /** |
129 | | Resize the cyclic buffer to <code>newSize</code>. |
130 | | @throws IllegalArgumentException if <code>newSize</code> is negative. |
131 | | */ |
132 | | void CyclicBuffer::resize(int newSize) |
133 | 0 | { |
134 | 0 | if (newSize < 0) |
135 | 0 | { |
136 | 0 | LogString msg(LOG4CXX_STR("Negative array size [")); |
137 | 0 | Pool p; |
138 | 0 | StringHelper::toString(newSize, p, msg); |
139 | 0 | msg.append(LOG4CXX_STR("] not allowed.")); |
140 | 0 | throw IllegalArgumentException(msg); |
141 | 0 | } |
142 | | |
143 | 0 | if (newSize == m_priv->numElems) |
144 | 0 | { |
145 | 0 | return; // nothing to do |
146 | 0 | } |
147 | | |
148 | 0 | LoggingEventList temp(newSize); |
149 | |
|
150 | 0 | int loopLen = newSize < m_priv->numElems ? newSize : m_priv->numElems; |
151 | 0 | int i; |
152 | |
|
153 | 0 | for (i = 0; i < loopLen; i++) |
154 | 0 | { |
155 | 0 | temp[i] = m_priv->ea[m_priv->first]; |
156 | 0 | m_priv->ea[m_priv->first] = 0; |
157 | |
|
158 | 0 | if (++m_priv->first == m_priv->numElems) |
159 | 0 | { |
160 | 0 | m_priv->first = 0; |
161 | 0 | } |
162 | 0 | } |
163 | |
|
164 | 0 | m_priv->ea = temp; |
165 | 0 | m_priv->first = 0; |
166 | 0 | m_priv->numElems = loopLen; |
167 | 0 | m_priv->maxSize = newSize; |
168 | |
|
169 | 0 | if (loopLen == newSize) |
170 | 0 | { |
171 | 0 | m_priv->last = 0; |
172 | 0 | } |
173 | 0 | else |
174 | 0 | { |
175 | 0 | m_priv->last = loopLen; |
176 | 0 | } |
177 | 0 | } |
178 | | |
179 | | int CyclicBuffer::getMaxSize() const |
180 | 0 | { |
181 | 0 | return m_priv->maxSize; |
182 | 0 | } |
183 | | |
184 | | int CyclicBuffer::length() const |
185 | 0 | { |
186 | 0 | return m_priv->numElems; |
187 | 0 | } |