|
| 1 | +// |
| 2 | +// Copyright (c) 2004-2021 Jaroslaw Kowalski <jaak@jkowalski.net>, Kim Christensen, Julian Verdurmen |
| 3 | +// |
| 4 | +// All rights reserved. |
| 5 | +// |
| 6 | +// Redistribution and use in source and binary forms, with or without |
| 7 | +// modification, are permitted provided that the following conditions |
| 8 | +// are met: |
| 9 | +// |
| 10 | +// * Redistributions of source code must retain the above copyright notice, |
| 11 | +// this list of conditions and the following disclaimer. |
| 12 | +// |
| 13 | +// * Redistributions in binary form must reproduce the above copyright notice, |
| 14 | +// this list of conditions and the following disclaimer in the documentation |
| 15 | +// and/or other materials provided with the distribution. |
| 16 | +// |
| 17 | +// * Neither the name of Jaroslaw Kowalski nor the names of its |
| 18 | +// contributors may be used to endorse or promote products derived from this |
| 19 | +// software without specific prior written permission. |
| 20 | +// |
| 21 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 22 | +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 | +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 | +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 25 | +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 26 | +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 27 | +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 28 | +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 29 | +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 30 | +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 31 | +// THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | +// |
| 33 | + |
| 34 | +namespace NLog.Web.Internal |
| 35 | +{ |
| 36 | + using NLog.Common; |
| 37 | + using System; |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// A cyclic buffer of <see cref="LogEventInfo"/> object. |
| 41 | + /// </summary> |
| 42 | + internal class LogEventInfoBuffer |
| 43 | + { |
| 44 | + private readonly object _lockObject = new object(); |
| 45 | + private readonly bool _growAsNeeded; |
| 46 | + private readonly int _growLimit; |
| 47 | + |
| 48 | + private AsyncLogEventInfo[] _buffer; |
| 49 | + private int _getPointer; |
| 50 | + private int _putPointer; |
| 51 | + private int _count; |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Initializes a new instance of the <see cref="LogEventInfoBuffer" /> class. |
| 55 | + /// </summary> |
| 56 | + /// <param name="size">Buffer size.</param> |
| 57 | + /// <param name="growAsNeeded">Whether buffer should grow as it becomes full.</param> |
| 58 | + /// <param name="growLimit">The maximum number of items that the buffer can grow to.</param> |
| 59 | + public LogEventInfoBuffer(int size, bool growAsNeeded, int growLimit) |
| 60 | + { |
| 61 | + _growAsNeeded = growAsNeeded; |
| 62 | + _buffer = new AsyncLogEventInfo[size]; |
| 63 | + _growLimit = growLimit; |
| 64 | + _getPointer = 0; |
| 65 | + _putPointer = 0; |
| 66 | + } |
| 67 | + |
| 68 | + /// <summary> |
| 69 | + /// Gets the capacity of the buffer |
| 70 | + /// </summary> |
| 71 | + public int Size => _buffer.Length; |
| 72 | + |
| 73 | + /// <summary> |
| 74 | + /// Gets the number of items in the buffer |
| 75 | + /// </summary> |
| 76 | + internal int Count { get { lock (_lockObject) return _count; } } |
| 77 | + |
| 78 | + /// <summary> |
| 79 | + /// Adds the specified log event to the buffer. |
| 80 | + /// </summary> |
| 81 | + /// <param name="eventInfo">Log event.</param> |
| 82 | + /// <returns>The number of items in the buffer.</returns> |
| 83 | + public int Append(AsyncLogEventInfo eventInfo) |
| 84 | + { |
| 85 | + lock (_lockObject) |
| 86 | + { |
| 87 | + // make room for additional item |
| 88 | + if (_count >= _buffer.Length) |
| 89 | + { |
| 90 | + if (_growAsNeeded && _buffer.Length < _growLimit) |
| 91 | + { |
| 92 | + // create a new buffer, copy data from current |
| 93 | + int newLength = _buffer.Length * 2; |
| 94 | + if (newLength >= _growLimit) |
| 95 | + { |
| 96 | + newLength = _growLimit; |
| 97 | + } |
| 98 | + |
| 99 | + InternalLogger.Trace("Enlarging LogEventInfoBuffer from {0} to {1}", _buffer.Length, newLength); |
| 100 | + var newBuffer = new AsyncLogEventInfo[newLength]; |
| 101 | + Array.Copy(_buffer, 0, newBuffer, 0, _buffer.Length); |
| 102 | + _buffer = newBuffer; |
| 103 | + } |
| 104 | + else |
| 105 | + { |
| 106 | + // lose the oldest item |
| 107 | + _getPointer = _getPointer + 1; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + // put the item |
| 112 | + _putPointer = _putPointer % _buffer.Length; |
| 113 | + _buffer[_putPointer] = eventInfo; |
| 114 | + _putPointer = _putPointer + 1; |
| 115 | + _count++; |
| 116 | + if (_count >= _buffer.Length) |
| 117 | + { |
| 118 | + _count = _buffer.Length; |
| 119 | + } |
| 120 | + |
| 121 | + return _count; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + /// <summary> |
| 126 | + /// Gets the array of events accumulated in the buffer and clears the buffer as one atomic operation. |
| 127 | + /// </summary> |
| 128 | + /// <returns>Events in the buffer.</returns> |
| 129 | + public AsyncLogEventInfo[] GetEventsAndClear() |
| 130 | + { |
| 131 | + lock (_lockObject) |
| 132 | + { |
| 133 | + int cnt = _count; |
| 134 | + if (cnt == 0) |
| 135 | + return new AsyncLogEventInfo[0]; |
| 136 | + |
| 137 | + var returnValue = new AsyncLogEventInfo[cnt]; |
| 138 | + |
| 139 | + for (int i = 0; i < cnt; ++i) |
| 140 | + { |
| 141 | + int p = (_getPointer + i) % _buffer.Length; |
| 142 | + var e = _buffer[p]; |
| 143 | + _buffer[p] = default(AsyncLogEventInfo); // we don't want memory leaks |
| 144 | + returnValue[i] = e; |
| 145 | + } |
| 146 | + |
| 147 | + _count = 0; |
| 148 | + _getPointer = 0; |
| 149 | + _putPointer = 0; |
| 150 | + |
| 151 | + return returnValue; |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments