1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 * LogGroupEntry is a wrapper around log entries, which makes it easier to
8 * find the corresponding start/end of events.
10 * This is used internally by the log and timeline views to pretty print
11 * collections of log entries.
14 // TODO(eroman): document these methods!
16 var LogGroupEntry
= (function() {
19 function LogGroupEntry(origEntry
, index
) {
20 this.orig
= origEntry
;
24 LogGroupEntry
.prototype = {
26 return this.orig
.phase
== EventPhase
.PHASE_BEGIN
;
30 return this.orig
.phase
== EventPhase
.PHASE_END
;
33 getDepth: function() {
35 var p
= this.parentEntry
;
44 function findParentIndex(parentStack
, eventType
) {
45 for (var i
= parentStack
.length
- 1; i
>= 0; --i
) {
46 if (parentStack
[i
].orig
.type
== eventType
)
53 * Returns a list of LogGroupEntrys. This basically wraps the original log
54 * entry, but makes it easier to find the start/end of the event.
56 LogGroupEntry
.createArrayFrom = function(origEntries
) {
57 var groupedEntries
= [];
59 // Stack of enclosing PHASE_BEGIN elements.
62 for (var i
= 0; i
< origEntries
.length
; ++i
) {
63 var origEntry
= origEntries
[i
];
65 var groupEntry
= new LogGroupEntry(origEntry
, i
);
66 groupedEntries
.push(groupEntry
);
68 // If this is the end of an event, match it to the start.
69 if (groupEntry
.isEnd()) {
70 // Walk up the parent stack to find the corresponding BEGIN for this
73 findParentIndex(parentStack
, groupEntry
.orig
.type
);
75 if (parentIndex
== -1) {
78 groupEntry
.begin
= parentStack
[parentIndex
];
80 // Consider this as the terminator for all open BEGINs up until
82 while (parentIndex
< parentStack
.length
) {
83 var p
= parentStack
.pop();
89 // Inherit the current parent.
90 if (parentStack
.length
> 0)
91 groupEntry
.parentEntry
= parentStack
[parentStack
.length
- 1];
93 if (groupEntry
.isBegin())
94 parentStack
.push(groupEntry
);
97 return groupedEntries
;
100 return LogGroupEntry
;