Debian Codegraph @

closure-library/closure/goog/debug/logbuffer.js

raw
1 // Copyright 2010 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 /**
16 * @fileoverview A buffer for log records. The purpose of this is to improve
17 * logging performance by re-using old objects when the buffer becomes full and
18 * to eliminate the need for each app to implement their own log buffer. The
19 * disadvantage to doing this is that log handlers cannot maintain references to
20 * log records and expect that they are not overwriten at a later point.
21 *
22 * @author [email protected] (Andrew Grieve)
23 */
24
25 goog.provide('goog.debug.LogBuffer');
26
27 goog.require('goog.asserts');
28 goog.require('goog.debug.LogRecord');
29
30
31
32 /**
33 * Creates the log buffer.
34 * @constructor
35 * @final
36 */
37 goog.debug.LogBuffer = function() {
38 goog.asserts.assert(
39 goog.debug.LogBuffer.isBufferingEnabled(),
40 'Cannot use goog.debug.LogBuffer without defining ' +
41 'goog.debug.LogBuffer.CAPACITY.');
42 this.clear();
43 };
44
45
46 /**
47 * A static method that always returns the same instance of LogBuffer.
48 * @return {!goog.debug.LogBuffer} The LogBuffer singleton instance.
49 */
50 goog.debug.LogBuffer.getInstance = function() {
51 if (!goog.debug.LogBuffer.instance_) {
52 // This function is written with the return statement after the assignment
53 // to avoid the jscompiler StripCode bug described in http://b/2608064.
54 // After that bug is fixed this can be refactored.
55 goog.debug.LogBuffer.instance_ = new goog.debug.LogBuffer();
56 }
57 return goog.debug.LogBuffer.instance_;
58 };
59
60
61 /**
62 * @define {number} The number of log records to buffer. 0 means disable
63 * buffering.
64 */
65 goog.define('goog.debug.LogBuffer.CAPACITY', 0);
66
67
68 /**
69 * The array to store the records.
70 * @type {!Array<!goog.debug.LogRecord|undefined>}
71 * @private
72 */
73 goog.debug.LogBuffer.prototype.buffer_;
74
75
76 /**
77 * The index of the most recently added record or -1 if there are no records.
78 * @type {number}
79 * @private
80 */
81 goog.debug.LogBuffer.prototype.curIndex_;
82
83
84 /**
85 * Whether the buffer is at capacity.
86 * @type {boolean}
87 * @private
88 */
89 goog.debug.LogBuffer.prototype.isFull_;
90
91
92 /**
93 * Adds a log record to the buffer, possibly overwriting the oldest record.
94 * @param {goog.debug.Logger.Level} level One of the level identifiers.
95 * @param {string} msg The string message.
96 * @param {string} loggerName The name of the source logger.
97 * @return {!goog.debug.LogRecord} The log record.
98 */
99 goog.debug.LogBuffer.prototype.addRecord = function(level, msg, loggerName) {
100 var curIndex = (this.curIndex_ + 1) % goog.debug.LogBuffer.CAPACITY;
101 this.curIndex_ = curIndex;
102 if (this.isFull_) {
103 var ret = this.buffer_[curIndex];
104 ret.reset(level, msg, loggerName);
105 return ret;
106 }
107 this.isFull_ = curIndex == goog.debug.LogBuffer.CAPACITY - 1;
108 return this.buffer_[curIndex] =
109 new goog.debug.LogRecord(level, msg, loggerName);
110 };
111
112
113 /**
114 * @return {boolean} Whether the log buffer is enabled.
115 */
116 goog.debug.LogBuffer.isBufferingEnabled = function() {
117 return goog.debug.LogBuffer.CAPACITY > 0;
118 };
119
120
121 /**
122 * Removes all buffered log records.
123 */
124 goog.debug.LogBuffer.prototype.clear = function() {
125 this.buffer_ = new Array(goog.debug.LogBuffer.CAPACITY);
126 this.curIndex_ = -1;
127 this.isFull_ = false;
128 };
129
130
131 /**
132 * Calls the given function for each buffered log record, starting with the
133 * oldest one.
134 * @param {function(!goog.debug.LogRecord)} func The function to call.
135 */
136 goog.debug.LogBuffer.prototype.forEachRecord = function(func) {
137 var buffer = this.buffer_;
138 // Corner case: no records.
139 if (!buffer[0]) {
140 return;
141 }
142 var curIndex = this.curIndex_;
143 var i = this.isFull_ ? curIndex : -1;
144 do {
145 i = (i + 1) % goog.debug.LogBuffer.CAPACITY;
146 func(/** @type {!goog.debug.LogRecord} */ (buffer[i]));
147 } while (i != curIndex);
148 };