Debian Codegraph @

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

raw
1 // Copyright 2006 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 Definition of the Tracer class and associated classes.
17 *
18 * @see ../demos/tracer.html
19 */
20
21 goog.provide('goog.debug.Trace');
22
23 goog.require('goog.array');
24 goog.require('goog.asserts');
25 goog.require('goog.debug.Logger');
26 goog.require('goog.iter');
27 goog.require('goog.log');
28 goog.require('goog.structs.Map');
29 goog.require('goog.structs.SimplePool');
30
31
32
33 /**
34 * Class used for singleton goog.debug.Trace. Used for timing slow points in
35 * the code. Based on the java Tracer class but optimized for javascript.
36 * See com.google.common.tracing.Tracer.
37 * It is also possible to bridge from this class to other tracer classes via
38 * adding listeners.
39 * @constructor
40 * @private
41 */
42 goog.debug.Trace_ = function() {
43
44 /**
45 * Events in order.
46 * @private {!Array<!goog.debug.Trace_.Event_>}
47 */
48 this.events_ = [];
49
50 /**
51 * Outstanding events that have started but haven't yet ended. The keys are
52 * numeric ids and the values are goog.debug.Trace_.Event_ objects.
53 * @private {!goog.structs.Map<number, !goog.debug.Trace_.Event_>}
54 */
55 this.outstandingEvents_ = new goog.structs.Map();
56
57 /**
58 * Start time of the event trace
59 * @private {number}
60 */
61 this.startTime_ = 0;
62
63 /**
64 * Cummulative overhead of calls to startTracer
65 * @private {number}
66 */
67 this.tracerOverheadStart_ = 0;
68
69 /**
70 * Cummulative overhead of calls to endTracer
71 * @private {number}
72 */
73 this.tracerOverheadEnd_ = 0;
74
75 /**
76 * Cummulative overhead of calls to addComment
77 * @private {number}
78 */
79 this.tracerOverheadComment_ = 0;
80
81 /**
82 * Keeps stats on different types of tracers. The keys are strings and the
83 * values are goog.debug.Stat
84 * @private {!goog.structs.Map}
85 */
86 this.stats_ = new goog.structs.Map();
87
88 /**
89 * Total number of traces created in the trace.
90 * @private {number}
91 */
92 this.tracerCount_ = 0;
93
94 /**
95 * Total number of comments created in the trace.
96 * @private {number}
97 */
98 this.commentCount_ = 0;
99
100 /**
101 * Next id to use for the trace.
102 * @private {number}
103 */
104 this.nextId_ = 1;
105
106 /**
107 * A pool for goog.debug.Trace_.Event_ objects so we don't keep creating and
108 * garbage collecting these (which is very expensive in IE6).
109 * @private {!goog.structs.SimplePool}
110 */
111 this.eventPool_ = new goog.structs.SimplePool(0, 4000);
112 this.eventPool_.createObject = function() {
113 return new goog.debug.Trace_.Event_();
114 };
115
116
117 /**
118 * A pool for goog.debug.Trace_.Stat_ objects so we don't keep creating and
119 * garbage collecting these (which is very expensive in IE6).
120 * @private {!goog.structs.SimplePool}
121 */
122 this.statPool_ = new goog.structs.SimplePool(0, 50);
123 this.statPool_.createObject = function() {
124 return new goog.debug.Trace_.Stat_();
125 };
126
127 var self = this;
128
129 /** @private {!goog.structs.SimplePool} */
130 this.idPool_ = new goog.structs.SimplePool(0, 2000);
131
132 // TODO(nicksantos): SimplePool is supposed to only return objects.
133 // Reconcile this so that we don't have to cast to number below.
134 this.idPool_.createObject = function() { return String(self.nextId_++); };
135 this.idPool_.disposeObject = function(obj) {};
136
137 /**
138 * Default threshold below which a tracer shouldn't be reported
139 * @private {number}
140 */
141 this.defaultThreshold_ = 3;
142
143 /**
144 * An object containing three callback functions to be called when starting or
145 * stopping a trace, or creating a comment trace.
146 * @private {!goog.debug.Trace_.TracerCallbacks}
147 */
148 this.traceCallbacks_ = {};
149 };
150
151
152 /**
153 * Logger for the tracer
154 * @private @const {?goog.log.Logger}
155 */
156 goog.debug.Trace_.prototype.logger_ = goog.log.getLogger('goog.debug.Trace');
157
158
159 /**
160 * Maximum size of the trace before we discard events
161 * @type {number}
162 */
163 goog.debug.Trace_.prototype.MAX_TRACE_SIZE = 1000;
164
165
166 /**
167 * Event type supported by tracer
168 * @enum {number}
169 */
170 goog.debug.Trace_.EventType = {
171 /**
172 * Start event type
173 */
174 START: 0,
175
176 /**
177 * Stop event type
178 */
179 STOP: 1,
180
181 /**
182 * Comment event type
183 */
184 COMMENT: 2
185 };
186
187
188
189 /**
190 * Class to keep track of a stat of a single tracer type. Stores the count
191 * and cumulative time.
192 * @constructor
193 * @private
194 */
195 goog.debug.Trace_.Stat_ = function() {
196 /**
197 * Number of tracers
198 * @type {number}
199 */
200 this.count = 0;
201
202 /**
203 * Cumulative time of traces
204 * @type {number}
205 */
206 this.time = 0;
207
208 /**
209 * Total number of allocations for this tracer type
210 * @type {number}
211 */
212 this.varAlloc = 0;
213 };
214
215
216 /**
217 * @type {string|null|undefined}
218 */
219 goog.debug.Trace_.Stat_.prototype.type;
220
221
222 /**
223 * @return {string} A string describing the tracer stat.
224 * @override
225 */
226 goog.debug.Trace_.Stat_.prototype.toString = function() {
227 var sb = [];
228 sb.push(
229 this.type, ' ', this.count, ' (', Math.round(this.time * 10) / 10,
230 ' ms)');
231 if (this.varAlloc) {
232 sb.push(' [VarAlloc = ', this.varAlloc, ']');
233 }
234 return sb.join('');
235 };
236
237
238
239 /**
240 * Private class used to encapsulate a single event, either the start or stop
241 * of a tracer.
242 * @constructor
243 * @private
244 */
245 goog.debug.Trace_.Event_ = function() {
246 // the fields are different for different events - see usage in code
247 };
248
249
250 /**
251 * @type {string|null|undefined}
252 */
253 goog.debug.Trace_.Event_.prototype.type;
254
255
256 /**
257 * Returns a formatted string for the event.
258 * @param {number} startTime The start time of the trace to generate relative
259 * times.
260 * @param {number} prevTime The completion time of the previous event or -1.
261 * @param {string} indent Extra indent for the message
262 * if there was no previous event.
263 * @return {string} The formatted tracer string.
264 */
265 goog.debug.Trace_.Event_.prototype.toTraceString = function(
266 startTime, prevTime, indent) {
267 var sb = [];
268
269 if (prevTime == -1) {
270 sb.push(' ');
271 } else {
272 sb.push(goog.debug.Trace_.longToPaddedString_(this.eventTime - prevTime));
273 }
274
275 sb.push(' ', goog.debug.Trace_.formatTime_(this.eventTime - startTime));
276 if (this.eventType == goog.debug.Trace_.EventType.START) {
277 sb.push(' Start ');
278 } else if (this.eventType == goog.debug.Trace_.EventType.STOP) {
279 sb.push(' Done ');
280 var delta = this.stopTime - this.startTime;
281 sb.push(goog.debug.Trace_.longToPaddedString_(delta), ' ms ');
282 } else {
283 sb.push(' Comment ');
284 }
285
286 sb.push(indent, this);
287 if (this.totalVarAlloc > 0) {
288 sb.push('[VarAlloc ', this.totalVarAlloc, '] ');
289 }
290 return sb.join('');
291 };
292
293
294 /**
295 * @return {string} A string describing the tracer event.
296 * @override
297 */
298 goog.debug.Trace_.Event_.prototype.toString = function() {
299 if (this.type == null) {
300 return this.comment;
301 } else {
302 return '[' + this.type + '] ' + this.comment;
303 }
304 };
305
306
307 /**
308 * A class to specify the types of the callback functions used by
309 * {@code addTraceCallbacks}.
310 * @record
311 */
312 goog.debug.Trace_.TracerCallbacks = function() {
313 /**
314 * A callback function to be called at {@code startTrace} with two parameters:
315 * a number as the started trace id and a string as the comment on the trace.
316 * @type {function(number, string)|undefined}
317 */
318 this.start;
319 /**
320 * A callback function to be called at {@code stopTrace} with one parameter:
321 * a number as the trace id being stopped.
322 * @type {function(number)|undefined}
323 */
324 this.stop;
325 /**
326 * A callback function to be called at {@code addComment} with two parameters:
327 * a string as the comment on the trace and an optional time stamp number (in
328 * milliseconds since epoch) when the comment should be added as a trace.
329 * @type {function(string, ?number=)|undefined}
330 */
331 this.comment;
332 };
333
334 /**
335 * A function that combines two function with the same parameters in a sequence.
336 * @param {!Function|undefined} fn1 The first function to be combined.
337 * @param {!Function|undefined} fn2 The second function to be combined.
338 * @return {!Function|undefined} A function that calls the inputs in sequence.
339 * @private
340 */
341 goog.debug.Trace_.TracerCallbacks.sequence_ = function(fn1, fn2) {
342 return !fn1 ? fn2 : !fn2 ? fn1 : function() {
343 fn1.apply(undefined, arguments);
344 fn2.apply(undefined, arguments);
345 };
346 };
347
348
349 /**
350 * Removes all registered callback functions. Mainly used for testing.
351 */
352 goog.debug.Trace_.prototype.removeAllListeners = function() {
353 this.traceCallbacks_ = {};
354 };
355
356
357 /**
358 * Adds up to three callback functions which are called on {@code startTracer},
359 * {@code stopTracer} and {@code addComment} in order to bridge from the Closure
360 * tracer singleton object to any tracer class.
361 * @param {!goog.debug.Trace_.TracerCallbacks} callbacks An object literal
362 * containing the callback functions.
363 */
364 goog.debug.Trace_.prototype.addTraceCallbacks = function(callbacks) {
365 this.traceCallbacks_.start = goog.debug.Trace_.TracerCallbacks.sequence_(
366 this.traceCallbacks_.start, callbacks.start);
367 this.traceCallbacks_.stop = goog.debug.Trace_.TracerCallbacks.sequence_(
368 this.traceCallbacks_.stop, callbacks.stop);
369 this.traceCallbacks_.comment = goog.debug.Trace_.TracerCallbacks.sequence_(
370 this.traceCallbacks_.comment, callbacks.comment);
371 };
372
373
374 /**
375 * Add the ability to explicitly set the start time. This is useful for example
376 * for measuring initial load time where you can set a variable as soon as the
377 * main page of the app is loaded and then later call this function when the
378 * Tracer code has been loaded.
379 * @param {number} startTime The start time to set.
380 */
381 goog.debug.Trace_.prototype.setStartTime = function(startTime) {
382 this.startTime_ = startTime;
383 };
384
385
386 /**
387 * Initializes and resets the current trace
388 * @param {number} defaultThreshold The default threshold below which the
389 * tracer output will be suppressed. Can be overridden on a per-Tracer basis.
390 */
391 goog.debug.Trace_.prototype.initCurrentTrace = function(defaultThreshold) {
392 this.reset(defaultThreshold);
393 };
394
395
396 /**
397 * Clears the current trace
398 */
399 goog.debug.Trace_.prototype.clearCurrentTrace = function() {
400 this.reset(0);
401 };
402
403
404 /**
405 * Resets the trace.
406 * @param {number} defaultThreshold The default threshold below which the
407 * tracer output will be suppressed. Can be overridden on a per-Tracer basis.
408 */
409 goog.debug.Trace_.prototype.reset = function(defaultThreshold) {
410 this.defaultThreshold_ = defaultThreshold;
411
412 this.releaseEvents_();
413 this.outstandingEvents_.clear();
414 this.startTime_ = goog.debug.Trace_.now();
415 this.tracerOverheadStart_ = 0;
416 this.tracerOverheadEnd_ = 0;
417 this.tracerOverheadComment_ = 0;
418 this.tracerCount_ = 0;
419 this.commentCount_ = 0;
420
421 var keys = this.stats_.getKeys();
422 for (var i = 0; i < keys.length; i++) {
423 var key = keys[i];
424 var stat = this.stats_.get(key);
425 stat.count = 0;
426 stat.time = 0;
427 stat.varAlloc = 0;
428 this.statPool_.releaseObject(/** @type {Object} */ (stat));
429 }
430 this.stats_.clear();
431 };
432
433
434 /**
435 * @private
436 */
437 goog.debug.Trace_.prototype.releaseEvents_ = function() {
438 for (var i = 0; i < this.events_.length; i++) {
439 var event = this.events_[i];
440 if (event.id) {
441 this.idPool_.releaseObject(event.id);
442 }
443 this.eventPool_.releaseObject(event);
444 }
445 this.events_.length = 0;
446 };
447
448
449 /**
450 * Starts a tracer
451 * @param {string} comment A comment used to identify the tracer. Does not
452 * need to be unique.
453 * @param {string=} opt_type Type used to identify the tracer. If a Trace is
454 * given a type (the first argument to the constructor) and multiple Traces
455 * are done on that type then a "TOTAL line will be produced showing the
456 * total number of traces and the sum of the time
457 * ("TOTAL Database 2 (37 ms)" in our example). These traces should be
458 * mutually exclusive or else the sum won't make sense (the time will
459 * be double counted if the second starts before the first ends).
460 * @return {number} The identifier for the tracer that should be passed to the
461 * the stopTracer method.
462 */
463 goog.debug.Trace_.prototype.startTracer = function(comment, opt_type) {
464 var tracerStartTime = goog.debug.Trace_.now();
465 var varAlloc = this.getTotalVarAlloc();
466 var outstandingEventCount = this.outstandingEvents_.getCount();
467 if (this.events_.length + outstandingEventCount > this.MAX_TRACE_SIZE) {
468 goog.log.warning(
469 this.logger_, 'Giant thread trace. Clearing to avoid memory leak.');
470 // This is the more likely case. This usually means that we
471 // either forgot to clear the trace or else we are performing a
472 // very large number of events
473 if (this.events_.length > this.MAX_TRACE_SIZE / 2) {
474 this.releaseEvents_();
475 }
476
477 // This is less likely and probably indicates that a lot of traces
478 // aren't being closed. We want to avoid unnecessarily clearing
479 // this though in case the events do eventually finish.
480 if (outstandingEventCount > this.MAX_TRACE_SIZE / 2) {
481 this.outstandingEvents_.clear();
482 }
483 }
484
485 goog.debug.Logger.logToProfilers('Start : ' + comment);
486
487 /** @const */
488 var event =
489 /** @type {!goog.debug.Trace_.Event_} */ (this.eventPool_.getObject());
490 event.stopTime = undefined;
491 event.totalVarAlloc = varAlloc;
492 event.eventType = goog.debug.Trace_.EventType.START;
493 event.id = Number(this.idPool_.getObject());
494 event.comment = comment;
495 event.type = opt_type;
496 this.events_.push(event);
497 this.outstandingEvents_.set(String(event.id), event);
498 this.tracerCount_++;
499 var now = goog.debug.Trace_.now();
500 event.startTime = event.eventTime = now;
501 this.tracerOverheadStart_ += now - tracerStartTime;
502 if (this.traceCallbacks_.start) {
503 this.traceCallbacks_.start(event.id, event.toString());
504 }
505 return event.id;
506 };
507
508
509 /**
510 * Stops a tracer
511 * @param {number|undefined|null} id The id of the tracer that is ending.
512 * @param {number=} opt_silenceThreshold Threshold below which the tracer is
513 * silenced.
514 * @return {?number} The elapsed time for the tracer or null if the tracer
515 * identitifer was not recognized.
516 */
517 goog.debug.Trace_.prototype.stopTracer = function(id, opt_silenceThreshold) {
518 // this used to call goog.isDef(opt_silenceThreshold) but that causes an
519 // object allocation in IE for some reason (doh!). The following code doesn't
520 // cause an allocation
521 var now = goog.debug.Trace_.now();
522 var silenceThreshold;
523 if (opt_silenceThreshold === 0) {
524 silenceThreshold = 0;
525 } else if (opt_silenceThreshold) {
526 silenceThreshold = opt_silenceThreshold;
527 } else {
528 silenceThreshold = this.defaultThreshold_;
529 }
530
531 var startEvent = this.outstandingEvents_.get(String(id));
532 if (startEvent == null) {
533 return null;
534 }
535 goog.asserts.assertNumber(id);
536 if (this.traceCallbacks_.stop) {
537 this.traceCallbacks_.stop(Number(id));
538 }
539
540 this.outstandingEvents_.remove(String(id));
541
542 var stopEvent;
543 var elapsed = now - startEvent.startTime;
544 if (elapsed < silenceThreshold) {
545 var count = this.events_.length;
546 for (var i = count - 1; i >= 0; i--) {
547 var nextEvent = this.events_[i];
548 if (nextEvent == startEvent) {
549 this.events_.splice(i, 1);
550 this.idPool_.releaseObject(startEvent.id);
551 this.eventPool_.releaseObject(/** @type {Object} */ (startEvent));
552 break;
553 }
554 }
555 } else {
556 stopEvent =
557 /** @type {goog.debug.Trace_.Event_} */ (this.eventPool_.getObject());
558 stopEvent.id = undefined;
559 stopEvent.eventType = goog.debug.Trace_.EventType.STOP;
560 stopEvent.startTime = startEvent.startTime;
561 stopEvent.comment = startEvent.comment;
562 stopEvent.type = startEvent.type;
563 stopEvent.stopTime = stopEvent.eventTime = now;
564
565 this.events_.push(stopEvent);
566 }
567
568 var type = startEvent.type;
569 var stat = null;
570 if (type) {
571 stat = this.getStat_(type);
572 stat.count++;
573 stat.time += elapsed;
574 }
575 if (stopEvent) {
576 goog.debug.Logger.logToProfilers('Stop : ' + stopEvent.comment);
577
578 stopEvent.totalVarAlloc = this.getTotalVarAlloc();
579
580 if (stat) {
581 stat.varAlloc += (stopEvent.totalVarAlloc - startEvent.totalVarAlloc);
582 }
583 }
584 var tracerFinishTime = goog.debug.Trace_.now();
585 this.tracerOverheadEnd_ += tracerFinishTime - now;
586 return elapsed;
587 };
588
589
590 /**
591 * Sets the ActiveX object that can be used to get GC tracing in IE6.
592 * @param {Object} gcTracer GCTracer ActiveX object.
593 */
594 goog.debug.Trace_.prototype.setGcTracer = function(gcTracer) {
595 this.gcTracer_ = gcTracer;
596 };
597
598
599 /**
600 * Returns the total number of allocations since the GC stats were reset. Only
601 * works in IE.
602 * @return {number} The number of allocaitons or -1 if not supported.
603 */
604 goog.debug.Trace_.prototype.getTotalVarAlloc = function() {
605 var gcTracer = this.gcTracer_;
606 // isTracing is defined on the ActiveX object.
607 if (gcTracer && gcTracer['isTracing']()) {
608 return gcTracer['totalVarAlloc'];
609 }
610 return -1;
611 };
612
613
614 /**
615 * Adds a comment to the trace. Makes it possible to see when a specific event
616 * happened in relation to the traces.
617 * @param {string} comment A comment that is inserted into the trace.
618 * @param {?string=} opt_type Type used to identify the tracer. If a comment is
619 * given a type and multiple comments are done on that type then a "TOTAL
620 * line will be produced showing the total number of comments of that type.
621 * @param {?number=} opt_timeStamp The timestamp to insert the comment. If not
622 * specified, the current time wil be used.
623 */
624 goog.debug.Trace_.prototype.addComment = function(
625 comment, opt_type, opt_timeStamp) {
626 var now = goog.debug.Trace_.now();
627 var timeStamp = opt_timeStamp ? opt_timeStamp : now;
628
629 var eventComment =
630 /** @type {goog.debug.Trace_.Event_} */ (this.eventPool_.getObject());
631 eventComment.startTime = undefined;
632 eventComment.stopTime = undefined;
633 eventComment.id = undefined;
634 eventComment.eventType = goog.debug.Trace_.EventType.COMMENT;
635 eventComment.eventTime = timeStamp;
636 eventComment.type = opt_type;
637 eventComment.comment = comment;
638 eventComment.totalVarAlloc = this.getTotalVarAlloc();
639 this.commentCount_++;
640
641 if (opt_timeStamp) {
642 if (this.traceCallbacks_.comment) {
643 this.traceCallbacks_.comment(eventComment.toString(), opt_timeStamp);
644 }
645 var numEvents = this.events_.length;
646 for (var i = 0; i < numEvents; i++) {
647 var event = this.events_[i];
648 var eventTime = event.eventTime;
649
650 if (eventTime > timeStamp) {
651 goog.array.insertAt(this.events_, eventComment, i);
652 break;
653 }
654 }
655 if (i == numEvents) {
656 this.events_.push(eventComment);
657 }
658 } else { // No time_stamp
659 if (this.traceCallbacks_.comment) {
660 this.traceCallbacks_.comment(eventComment.toString());
661 }
662 this.events_.push(eventComment);
663 }
664
665 var type = eventComment.type;
666 if (type) {
667 var stat = this.getStat_(type);
668 stat.count++;
669 }
670
671 this.tracerOverheadComment_ += goog.debug.Trace_.now() - now;
672 };
673
674
675 /**
676 * Gets a stat object for a particular type. The stat object is created if it
677 * hasn't yet been.
678 * @param {string} type The type of stat.
679 * @return {goog.debug.Trace_.Stat_} The stat object.
680 * @private
681 */
682 goog.debug.Trace_.prototype.getStat_ = function(type) {
683 var stat = this.stats_.get(type);
684 if (!stat) {
685 stat = /** @type {goog.debug.Trace_.Event_} */ (this.statPool_.getObject());
686 stat.type = type;
687 this.stats_.set(type, stat);
688 }
689 return /** @type {goog.debug.Trace_.Stat_} */ (stat);
690 };
691
692
693 /**
694 * Returns a formatted string for the current trace
695 * @return {string} A formatted string that shows the timings of the current
696 * trace.
697 */
698 goog.debug.Trace_.prototype.getFormattedTrace = function() {
699 return this.toString();
700 };
701
702
703 /**
704 * Returns a formatted string that describes the thread trace.
705 * @return {string} A formatted string.
706 * @override
707 */
708 goog.debug.Trace_.prototype.toString = function() {
709 var sb = [];
710 var etime = -1;
711 var indent = [];
712 for (var i = 0; i < this.events_.length; i++) {
713 var e = this.events_[i];
714 if (e.eventType == goog.debug.Trace_.EventType.STOP) {
715 indent.pop();
716 }
717 sb.push(' ', e.toTraceString(this.startTime_, etime, indent.join('')));
718 etime = e.eventTime;
719 sb.push('\n');
720 if (e.eventType == goog.debug.Trace_.EventType.START) {
721 indent.push('| ');
722 }
723 }
724
725 if (this.outstandingEvents_.getCount() != 0) {
726 var now = goog.debug.Trace_.now();
727
728 sb.push(' Unstopped timers:\n');
729 goog.iter.forEach(this.outstandingEvents_, function(startEvent) {
730 sb.push(
731 ' ', startEvent, ' (', now - startEvent.startTime,
732 ' ms, started at ',
733 goog.debug.Trace_.formatTime_(startEvent.startTime), ')\n');
734 });
735 }
736
737 var statKeys = this.stats_.getKeys();
738 for (var i = 0; i < statKeys.length; i++) {
739 var stat = this.stats_.get(statKeys[i]);
740 if (stat.count > 1) {
741 sb.push(' TOTAL ', stat, '\n');
742 }
743 }
744
745 sb.push(
746 'Total tracers created ', this.tracerCount_, '\n',
747 'Total comments created ', this.commentCount_, '\n', 'Overhead start: ',
748 this.tracerOverheadStart_, ' ms\n', 'Overhead end: ',
749 this.tracerOverheadEnd_, ' ms\n', 'Overhead comment: ',
750 this.tracerOverheadComment_, ' ms\n');
751
752 return sb.join('');
753 };
754
755
756 /**
757 * Converts 'v' to a string and pads it with up to 3 spaces for
758 * improved alignment. TODO there must be a better way
759 * @param {number} v A number.
760 * @return {string} A padded string.
761 * @private
762 */
763 goog.debug.Trace_.longToPaddedString_ = function(v) {
764 v = Math.round(v);
765 // todo (pupius) - there should be a generic string in goog.string for this
766 var space = '';
767 if (v < 1000) space = ' ';
768 if (v < 100) space = ' ';
769 if (v < 10) space = ' ';
770 return space + v;
771 };
772
773
774 /**
775 * Return the sec.ms part of time (if time = "20:06:11.566", "11.566
776 * @param {number} time The time in MS.
777 * @return {string} A formatted string as sec.ms'.
778 * @private
779 */
780 goog.debug.Trace_.formatTime_ = function(time) {
781 time = Math.round(time);
782 var sec = (time / 1000) % 60;
783 var ms = time % 1000;
784
785 // TODO their must be a nicer way to get zero padded integers
786 return String(100 + sec).substring(1, 3) + '.' +
787 String(1000 + ms).substring(1, 4);
788 };
789
790
791 /**
792 * Returns the current time. Done through a wrapper function so it can be
793 * overridden by application code. Gmail has an ActiveX extension that provides
794 * higher precision timing info.
795 * @return {number} The current time in milliseconds.
796 */
797 goog.debug.Trace_.now = function() {
798 return goog.now();
799 };
800
801
802 /**
803 * Singleton trace object
804 * @type {goog.debug.Trace_}
805 */
806 goog.debug.Trace = new goog.debug.Trace_();