OLD | NEW |
| (Empty) |
1 // Copyright 2014 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 var DeviceLogUI = (function() { | |
6 'use strict'; | |
7 | |
8 /** | |
9 * Creates a tag for the log level. | |
10 * | |
11 * @param {string} level A string that represents log level. | |
12 * @return {HTMLSpanElement} The created span element. | |
13 */ | |
14 var createLevelTag = function(level) { | |
15 var levelClassName = 'log-level-' + level.toLowerCase(); | |
16 var tag = document.createElement('span'); | |
17 tag.textContent = level; | |
18 tag.className = 'level-tag ' + levelClassName; | |
19 return tag; | |
20 }; | |
21 | |
22 /** | |
23 * Creates a tag for the log type. | |
24 * | |
25 * @param {string} level A string that represents log type. | |
26 * @return {HTMLSpanElement} The created span element. | |
27 */ | |
28 var createTypeTag = function(type) { | |
29 var typeClassName = 'log-type-' + type.toLowerCase(); | |
30 var tag = document.createElement('span'); | |
31 tag.textContent = type; | |
32 tag.className = 'type-tag ' + typeClassName; | |
33 return tag; | |
34 }; | |
35 | |
36 /** | |
37 * Creates an element that contains the time, the event, the level and | |
38 * the description of the given log entry. | |
39 * | |
40 * @param {Object} logEntry An object that represents a single line of log. | |
41 * @return {?HTMLParagraphElement} The created p element that represents | |
42 * the log entry, or null if the entry should be skipped. | |
43 */ | |
44 var createLogEntryText = function(logEntry) { | |
45 var level = logEntry['level']; | |
46 var levelCheckbox = 'log-level-' + level.toLowerCase(); | |
47 if ($(levelCheckbox) && !$(levelCheckbox).checked) | |
48 return null; | |
49 | |
50 var type = logEntry['type']; | |
51 var typeCheckbox = 'log-type-' + type.toLowerCase(); | |
52 if ($(typeCheckbox) && !$(typeCheckbox).checked) | |
53 return null; | |
54 | |
55 var res = document.createElement('p'); | |
56 var textWrapper = document.createElement('span'); | |
57 var fileinfo = ''; | |
58 if ($('log-fileinfo').checked) | |
59 fileinfo = logEntry['file']; | |
60 var timestamp = ''; | |
61 if ($('log-timedetail').checked) | |
62 timestamp = logEntry['timestamp']; | |
63 else | |
64 timestamp = logEntry['timestampshort']; | |
65 textWrapper.textContent = loadTimeData.getStringF( | |
66 'logEntryFormat', | |
67 timestamp, | |
68 fileinfo, | |
69 logEntry['event']); | |
70 res.appendChild(createTypeTag(type)); | |
71 res.appendChild(createLevelTag(level)); | |
72 res.appendChild(textWrapper); | |
73 return res; | |
74 }; | |
75 | |
76 /** | |
77 * Creates event log entries. | |
78 * | |
79 * @param {Array.<string>} logEntries An array of strings that represent log | |
80 * log events in JSON format. | |
81 */ | |
82 var createEventLog = function(logEntries) { | |
83 var container = $('log-container'); | |
84 container.textContent = ''; | |
85 for (var i = 0; i < logEntries.length; ++i) { | |
86 var entry = createLogEntryText(JSON.parse(logEntries[i])); | |
87 if (entry) | |
88 container.appendChild(entry); | |
89 } | |
90 }; | |
91 | |
92 /** | |
93 * Callback function, triggered when the log is received. | |
94 * | |
95 * @param {Object} data A JSON structure of event log entries. | |
96 */ | |
97 var getLogCallback = function(data) { | |
98 createEventLog(JSON.parse(data)); | |
99 }; | |
100 | |
101 /** | |
102 * Requests a log update. | |
103 */ | |
104 var requestLog = function() { | |
105 chrome.send('DeviceLog.getLog'); | |
106 }; | |
107 | |
108 /** | |
109 * Sets refresh rate if the interval is found in the url. | |
110 */ | |
111 var setRefresh = function() { | |
112 var interval = parseQueryParams(window.location)['refresh']; | |
113 if (interval && interval != '') | |
114 setInterval(requestLog, parseInt(interval) * 1000); | |
115 }; | |
116 | |
117 /** | |
118 * Gets log information from WebUI. | |
119 */ | |
120 document.addEventListener('DOMContentLoaded', function() { | |
121 // Show all levels except 'debug' by default. | |
122 $('log-level-error').checked = true; | |
123 $('log-level-user').checked = true; | |
124 $('log-level-event').checked = true; | |
125 $('log-level-debug').checked = false; | |
126 | |
127 // Show all types by default. | |
128 var checkboxes = document.querySelectorAll( | |
129 '#log-checkbox-container input[type="checkbox"][id*="log-type"]'); | |
130 for (var i = 0; i < checkboxes.length; ++i) | |
131 checkboxes[i].checked = true; | |
132 | |
133 $('log-fileinfo').checked = false; | |
134 $('log-timedetail').checked = false; | |
135 | |
136 $('log-refresh').onclick = requestLog; | |
137 checkboxes = document.querySelectorAll( | |
138 '#log-checkbox-container input[type="checkbox"]'); | |
139 for (var i = 0; i < checkboxes.length; ++i) | |
140 checkboxes[i].onclick = requestLog; | |
141 | |
142 setRefresh(); | |
143 requestLog(); | |
144 }); | |
145 | |
146 return { | |
147 getLogCallback: getLogCallback | |
148 }; | |
149 })(); | |
OLD | NEW |