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