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.
5 var DeviceLogUI = (function() {
9 * Creates a tag for the log level.
11 * @param {string} level A string that represents log level.
12 * @return {HTMLSpanElement} The created span element.
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;
23 * Creates a tag for the log type.
25 * @param {string} level A string that represents log type.
26 * @return {HTMLSpanElement} The created span element.
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;
37 * Creates an element that contains the time, the event, the level and
38 * the description of the given log entry.
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.
44 var createLogEntryText = function(logEntry) {
45 var level = logEntry['level'];
46 var levelCheckbox = 'log-level-' + level.toLowerCase();
47 if ($(levelCheckbox) && !$(levelCheckbox).checked)
50 var type = logEntry['type'];
51 var typeCheckbox = 'log-type-' + type.toLowerCase();
52 if ($(typeCheckbox) && !$(typeCheckbox).checked)
55 var res = document.createElement('p');
56 var textWrapper = document.createElement('span');
58 if ($('log-fileinfo').checked)
59 fileinfo = logEntry['file'];
61 if ($('log-timedetail').checked)
62 timestamp = logEntry['timestamp'];
64 timestamp = logEntry['timestampshort'];
65 textWrapper.textContent = loadTimeData.getStringF(
70 res.appendChild(createTypeTag(type));
71 res.appendChild(createLevelTag(level));
72 res.appendChild(textWrapper);
77 * Creates event log entries.
79 * @param {Array<string>} logEntries An array of strings that represent log
80 * log events in JSON format.
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]));
88 container.appendChild(entry);
93 * Callback function, triggered when the log is received.
95 * @param {Object} data A JSON structure of event log entries.
97 var getLogCallback = function(data) {
98 createEventLog(JSON.parse(data));
102 * Requests a log update.
104 var requestLog = function() {
105 chrome.send('DeviceLog.getLog');
109 * Sets refresh rate if the interval is found in the url.
111 var setRefresh = function() {
112 var interval = parseQueryParams(window.location)['refresh'];
113 if (interval && interval != '')
114 setInterval(requestLog, parseInt(interval) * 1000);
118 * Gets log information from WebUI.
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;
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;
133 $('log-fileinfo').checked = false;
134 $('log-timedetail').checked = false;
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;
147 getLogCallback: getLogCallback