OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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. |
| 4 |
| 5 /** |
| 6 * LogGroupEntry is a wrapper around log entries, which makes it easier to |
| 7 * find the corresponding start/end of events. |
| 8 * |
| 9 * This is used internally by the log and timeline views to pretty print |
| 10 * collections of log entries. |
| 11 * |
| 12 * @fileoverview |
| 13 */ |
| 14 |
| 15 // TODO(eroman): document these methods! |
| 16 |
| 17 function LogGroupEntry(origEntry, index) { |
| 18 this.orig = origEntry; |
| 19 this.index = index; |
| 20 } |
| 21 |
| 22 LogGroupEntry.prototype.isBegin = function() { |
| 23 return this.orig.type == LogEntryType.TYPE_EVENT && |
| 24 this.orig.event.phase == LogEventPhase.PHASE_BEGIN; |
| 25 }; |
| 26 |
| 27 LogGroupEntry.prototype.isEnd = function() { |
| 28 return this.orig.type == LogEntryType.TYPE_EVENT && |
| 29 this.orig.event.phase == LogEventPhase.PHASE_END |
| 30 }; |
| 31 |
| 32 LogGroupEntry.prototype.getDepth = function() { |
| 33 var depth = 0; |
| 34 var p = this.parentEntry; |
| 35 while (p) { |
| 36 depth += 1; |
| 37 p = p.parentEntry; |
| 38 } |
| 39 return depth; |
| 40 }; |
| 41 |
| 42 function findParentIndex(parentStack, eventType) { |
| 43 for (var i = parentStack.length - 1; i >= 0; --i) { |
| 44 if (parentStack[i].orig.event.type == eventType) |
| 45 return i; |
| 46 } |
| 47 return -1; |
| 48 } |
| 49 |
| 50 /** |
| 51 * Returns a list of LogGroupEntrys. This basically wraps the original log |
| 52 * entry, but makes it easier to find the start/end of the event. |
| 53 */ |
| 54 LogGroupEntry.createArrayFrom = function(origEntries) { |
| 55 var groupedEntries = []; |
| 56 |
| 57 // Stack of enclosing PHASE_BEGIN elements. |
| 58 var parentStack = []; |
| 59 |
| 60 for (var i = 0; i < origEntries.length; ++i) { |
| 61 var origEntry = origEntries[i]; |
| 62 |
| 63 var groupEntry = new LogGroupEntry(origEntry, i); |
| 64 groupedEntries.push(groupEntry); |
| 65 |
| 66 // If this is the end of an event, match it to the start. |
| 67 if (groupEntry.isEnd()) { |
| 68 // Walk up the parent stack to find the corresponding BEGIN for this END. |
| 69 var parentIndex = |
| 70 findParentIndex(parentStack, groupEntry.orig.event.type); |
| 71 |
| 72 if (parentIndex == -1) { |
| 73 // Unmatched end. |
| 74 } else { |
| 75 groupEntry.begin = parentStack[parentIndex]; |
| 76 |
| 77 // Consider this as the terminator for all open BEGINs up until |
| 78 // parentIndex. |
| 79 for (var j = 0; j < parentStack.length - parentIndex; ++j) { |
| 80 var p = parentStack.pop(); |
| 81 p.end = groupEntry; |
| 82 } |
| 83 } |
| 84 } |
| 85 |
| 86 // Inherit the current parent. |
| 87 if (parentStack.length > 0) |
| 88 groupEntry.parentEntry = parentStack[parentStack.length - 1]; |
| 89 |
| 90 if (groupEntry.isBegin()) |
| 91 parentStack.push(groupEntry); |
| 92 } |
| 93 |
| 94 return groupedEntries; |
| 95 } |
OLD | NEW |