summaryrefslogtreecommitdiffstats
path: root/chrome/browser/resources/device_log_ui/device_log_ui.js
blob: acee79aa41d603abe20b08884e5828d4adce4f97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

var DeviceLogUI = (function() {
  'use strict';

  /**
   * Creates a tag for the log level.
   *
   * @param {string} level A string that represents log level.
   * @return {HTMLSpanElement} The created span element.
   */
  var createLevelTag = function(level) {
    var levelClassName = 'log-level-' + level.toLowerCase();
    var tag = document.createElement('span');
    tag.textContent = level;
    tag.className = 'level-tag ' + levelClassName;
    return tag;
  };

  /**
   * Creates a tag for the log type.
   *
   * @param {string} level A string that represents log type.
   * @return {HTMLSpanElement} The created span element.
   */
  var createTypeTag = function(type) {
    var typeClassName = 'log-type-' + type.toLowerCase();
    var tag = document.createElement('span');
    tag.textContent = type;
    tag.className = 'type-tag ' + typeClassName;
    return tag;
  };

  /**
   * Creates an element that contains the time, the event, the level and
   * the description of the given log entry.
   *
   * @param {Object} logEntry An object that represents a single line of log.
   * @return {?HTMLParagraphElement} The created p element that represents
   *     the log entry, or null if the entry should be skipped.
   */
  var createLogEntryText = function(logEntry) {
    var level = logEntry['level'];
    var levelCheckbox = 'log-level-' + level.toLowerCase();
    if ($(levelCheckbox) && !$(levelCheckbox).checked)
      return null;

    var type = logEntry['type'];
    var typeCheckbox = 'log-type-' + type.toLowerCase();
    if ($(typeCheckbox) && !$(typeCheckbox).checked)
      return null;

    var res = document.createElement('p');
    var textWrapper = document.createElement('span');
    var fileinfo = '';
    if ($('log-fileinfo').checked)
      fileinfo = logEntry['file'];
    var timestamp = '';
    if ($('log-timedetail').checked)
      timestamp = logEntry['timestamp'];
    else
      timestamp = logEntry['timestampshort'];
    textWrapper.textContent = loadTimeData.getStringF(
      'logEntryFormat',
      timestamp,
      fileinfo,
      logEntry['event']);
    res.appendChild(createTypeTag(type));
    res.appendChild(createLevelTag(level));
    res.appendChild(textWrapper);
    return res;
  };

  /**
   * Creates event log entries.
   *
   * @param {Array<string>} logEntries An array of strings that represent log
   *     log events in JSON format.
   */
  var createEventLog = function(logEntries) {
    var container = $('log-container');
    container.textContent = '';
    for (var i = 0; i < logEntries.length; ++i) {
      var entry = createLogEntryText(JSON.parse(logEntries[i]));
      if (entry)
        container.appendChild(entry);
    }
  };

  /**
   * Callback function, triggered when the log is received.
   *
   * @param {Object} data A JSON structure of event log entries.
   */
  var getLogCallback = function(data) {
    try {
      createEventLog(JSON.parse(data));
    } catch(e) {
      var container = $('log-container');
      container.textContent = 'No log entries';
    }
  };

  /**
   * Requests a log update.
   */
  var requestLog = function() {
    chrome.send('DeviceLog.getLog');
  };

  /**
   * Sets refresh rate if the interval is found in the url.
   */
  var setRefresh = function() {
    var interval = parseQueryParams(window.location)['refresh'];
    if (interval && interval != '')
      setInterval(requestLog, parseInt(interval) * 1000);
  };

  /**
   * Gets log information from WebUI.
   */
  document.addEventListener('DOMContentLoaded', function() {
    // Show all levels except 'debug' by default.
    $('log-level-error').checked = true;
    $('log-level-user').checked = true;
    $('log-level-event').checked = true;
    $('log-level-debug').checked = false;

    // Show all types by default.
    var checkboxes = document.querySelectorAll(
        '#log-checkbox-container input[type="checkbox"][id*="log-type"]');
    for (var i = 0; i < checkboxes.length; ++i)
      checkboxes[i].checked = true;

    $('log-fileinfo').checked = false;
    $('log-timedetail').checked = false;

    $('log-refresh').onclick = requestLog;
    checkboxes = document.querySelectorAll(
        '#log-checkbox-container input[type="checkbox"]');
    for (var i = 0; i < checkboxes.length; ++i)
      checkboxes[i].onclick = requestLog;

    setRefresh();
    requestLog();
  });

  return {
    getLogCallback: getLogCallback
  };
})();