File: device_event_log_impl.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (499 lines) | stat: -rw-r--r-- 16,501 bytes parent folder | download | duplicates (5)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.


#include "components/device_event_log/device_event_log_impl.h"

#include <algorithm>
#include <array>
#include <cmath>
#include <list>
#include <set>
#include <string_view>

#include "base/containers/adapters.h"
#include "base/functional/bind.h"
#include "base/i18n/time_formatting.h"
#include "base/json/json_string_value_serializer.h"
#include "base/json/json_writer.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/process/process_handle.h"
#include "base/strings/string_tokenizer.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/single_thread_task_runner.h"
#include "base/values.h"
#include "build/build_config.h"

namespace device_event_log {

namespace {

const auto kLogLevelName =
    std::to_array<const char*>({"Error", "User", "Event", "Debug"});

const char kLogTypeNetworkDesc[] = "Network";
const char kLogTypePowerDesc[] = "Power";
const char kLogTypeLoginDesc[] = "Login";
const char kLogTypeBluetoothDesc[] = "Bluetooth";
const char kLogTypeUsbDesc[] = "USB";
const char kLogTypeHidDesc[] = "HID";
const char kLogTypeMemoryDesc[] = "Memory";
const char kLogTypePrinterDesc[] = "Printer";
const char kLogTypeFidoDesc[] = "FIDO";
const char kLogTypeSerialDesc[] = "Serial";
const char kLogTypeCameraDesc[] = "Camera";
const char kLogTypeGeolocationDesc[] = "Geolocation";
const char kLogTypeExtensionsDesc[] = "Extensions";
const char kLogTypeDisplayDesc[] = "Display";
const char kLogTypeFirmwareDesc[] = "Firmware";

enum class ShowTime {
  kNone,
  kTimeWithMs,
  kUnix,
};

std::string GetLogTypeString(LogType type) {
  switch (type) {
    case LOG_TYPE_NETWORK:
      return kLogTypeNetworkDesc;
    case LOG_TYPE_POWER:
      return kLogTypePowerDesc;
    case LOG_TYPE_LOGIN:
      return kLogTypeLoginDesc;
    case LOG_TYPE_BLUETOOTH:
      return kLogTypeBluetoothDesc;
    case LOG_TYPE_USB:
      return kLogTypeUsbDesc;
    case LOG_TYPE_HID:
      return kLogTypeHidDesc;
    case LOG_TYPE_MEMORY:
      return kLogTypeMemoryDesc;
    case LOG_TYPE_PRINTER:
      return kLogTypePrinterDesc;
    case LOG_TYPE_FIDO:
      return kLogTypeFidoDesc;
    case LOG_TYPE_SERIAL:
      return kLogTypeSerialDesc;
    case LOG_TYPE_CAMERA:
      return kLogTypeCameraDesc;
    case LOG_TYPE_GEOLOCATION:
      return kLogTypeGeolocationDesc;
    case LOG_TYPE_EXTENSIONS:
      return kLogTypeExtensionsDesc;
    case LOG_TYPE_DISPLAY:
      return kLogTypeDisplayDesc;
    case LOG_TYPE_FIRMWARE:
      return kLogTypeFirmwareDesc;
    case LOG_TYPE_UNKNOWN:
      break;
  }
  NOTREACHED();
}

LogType GetLogTypeFromString(std::string_view desc) {
  std::string desc_lc = base::ToLowerASCII(desc);
  for (int i = 0; i < LOG_TYPE_UNKNOWN; ++i) {
    auto type = static_cast<LogType>(i);
    std::string log_desc_lc = base::ToLowerASCII(GetLogTypeString(type));
    if (desc_lc == log_desc_lc)
      return type;
  }
  NOTREACHED() << "Unrecogized LogType: " << desc;
}

std::string DateAndTimeWithMicroseconds(const base::Time& time) {
  return base::UnlocalizedTimeFormatWithPattern(time,
                                                "yyyy/MM/dd HH:mm:ss.SSSSSS");
}

std::string TimeWithSeconds(const base::Time& time) {
  return base::UnlocalizedTimeFormatWithPattern(time, "HH:mm:ss");
}

std::string TimeWithMillieconds(const base::Time& time) {
  return base::UnlocalizedTimeFormatWithPattern(time, "HH:mm:ss.SSS");
}

#if BUILDFLAG(IS_POSIX)
std::string UnixTime(const base::Time& time) {
  return base::UnlocalizedTimeFormatWithPattern(
      time, "yyyy-MM-dd'T'HH:mm:ss.SSSSSSxxx");
}
#endif

std::string LogEntryToString(const DeviceEventLogImpl::LogEntry& log_entry,
                             ShowTime show_time,
                             bool show_file,
                             bool show_type,
                             bool show_level) {
  std::string line;
  if (show_time == ShowTime::kTimeWithMs)
    line += "[" + TimeWithMillieconds(log_entry.time) + "] ";
#if BUILDFLAG(IS_POSIX)
  if (show_time == ShowTime::kUnix)
    line += UnixTime(log_entry.time) + " ";
#endif
  if (show_type)
    line += GetLogTypeString(log_entry.log_type) + ": ";
  if (show_level) {
    auto kLevelDesc =
        std::to_array<const char*>({"ERROR", "USER", "EVENT", "DEBUG"});
    line += std::string(kLevelDesc[log_entry.log_level]);
#if BUILDFLAG(IS_POSIX)
    if (show_time == ShowTime::kUnix) {
      // Format the level consistently with /var/log/messages.
      line += base::StringPrintf(" chrome[%d]", base::GetCurrentProcId());
    }
#endif
    line += ": ";
  }
  if (show_file) {
    line += base::StringPrintf("%s:%d ", log_entry.file.c_str(),
                               log_entry.file_line);
  }
  line += log_entry.event;
  if (log_entry.count > 1)
    line += base::StringPrintf(" (%d)", log_entry.count);
  return line;
}

base::Value::Dict LogEntryToDictionary(
    const DeviceEventLogImpl::LogEntry& log_entry) {
  base::Value::Dict entry_dict;
  entry_dict.Set("timestamp", DateAndTimeWithMicroseconds(log_entry.time));
  entry_dict.Set("timestampshort", TimeWithSeconds(log_entry.time));
  entry_dict.Set("level", kLogLevelName[log_entry.log_level]);
  entry_dict.Set("type", GetLogTypeString(log_entry.log_type));
  entry_dict.Set("file", base::StringPrintf("%s:%d ", log_entry.file.c_str(),
                                            log_entry.file_line));
  entry_dict.Set("event", log_entry.event);
  return entry_dict;
}

std::string LogEntryAsJSON(const DeviceEventLogImpl::LogEntry& log_entry) {
  std::string json;
  JSONStringValueSerializer serializer(&json);
  if (!serializer.Serialize(LogEntryToDictionary(log_entry))) {
    LOG(ERROR) << "Failed to serialize to JSON";
  }
  return json;
}

void SendLogEntryToVLogOrErrorLog(
    const DeviceEventLogImpl::LogEntry& log_entry) {
  if (log_entry.log_level != LOG_LEVEL_ERROR && !VLOG_IS_ON(1))
    return;
  const ShowTime show_time = ShowTime::kTimeWithMs;
  const bool show_file = true;
  const bool show_type = true;
  const bool show_level = log_entry.log_level != LOG_LEVEL_ERROR;
  std::string output =
      LogEntryToString(log_entry, show_time, show_file, show_type, show_level);
  if (log_entry.log_level == LOG_LEVEL_ERROR)
    LOG(ERROR) << output;
  else
    VLOG(1) << output;
}

bool LogEntryMatches(const DeviceEventLogImpl::LogEntry& first,
                     const DeviceEventLogImpl::LogEntry& second) {
  return first.file == second.file && first.file_line == second.file_line &&
         first.log_level == second.log_level &&
         first.log_type == second.log_type && first.event == second.event;
}

bool LogEntryMatchesTypes(const DeviceEventLogImpl::LogEntry& entry,
                          const std::set<LogType>& include_types,
                          const std::set<LogType>& exclude_types) {
  if (include_types.empty() && exclude_types.empty())
    return true;
  if (!include_types.empty() && include_types.count(entry.log_type))
    return true;
  if (!exclude_types.empty() && !exclude_types.count(entry.log_type))
    return true;
  return false;
}

void GetFormat(const std::string& format_string,
               ShowTime* show_time,
               bool* show_file,
               bool* show_type,
               bool* show_level,
               bool* format_json) {
  base::StringTokenizer tokens(format_string, ",");
  *show_time = ShowTime::kNone;
  *show_file = false;
  *show_type = false;
  *show_level = false;
  *format_json = false;
  while (tokens.GetNext()) {
    std::string_view tok = tokens.token_piece();
    if (tok == "time") {
      *show_time = ShowTime::kTimeWithMs;
    } else if (tok == "unixtime") {
#if BUILDFLAG(IS_POSIX)
      *show_time = ShowTime::kUnix;
#else
      *show_time = ShowTime::kTimeWithMs;
#endif
    } else if (tok == "file") {
      *show_file = true;
    } else if (tok == "type") {
      *show_type = true;
    } else if (tok == "level") {
      *show_level = true;
    } else if (tok == "json") {
      *format_json = true;
    }
  }
}

void GetLogTypes(const std::string& types,
                 std::set<LogType>* include_types,
                 std::set<LogType>* exclude_types) {
  base::StringTokenizer tokens(types, ",");
  while (tokens.GetNext()) {
    std::string_view tok = tokens.token_piece();
    if (base::StartsWith(tok, "non-")) {
      LogType type = GetLogTypeFromString(tok.substr(4));
      if (type != LOG_TYPE_UNKNOWN)
        exclude_types->insert(type);
    } else {
      LogType type = GetLogTypeFromString(tok);
      if (type != LOG_TYPE_UNKNOWN)
        include_types->insert(type);
    }
  }
}

// Update count and time for identical events to avoid log spam.
void IncreaseLogEntryCount(const DeviceEventLogImpl::LogEntry& new_entry,
                           DeviceEventLogImpl::LogEntry* cur_entry) {
  ++cur_entry->count;
  cur_entry->log_level = std::min(cur_entry->log_level, new_entry.log_level);
  cur_entry->time = base::Time::Now();
}

}  // namespace

// static
void DeviceEventLogImpl::SendToVLogOrErrorLog(const char* file,
                                              int file_line,
                                              LogType log_type,
                                              LogLevel log_level,
                                              const std::string& event) {
  LogEntry entry(file, file_line, log_type, log_level, event);
  SendLogEntryToVLogOrErrorLog(entry);
}

DeviceEventLogImpl::DeviceEventLogImpl(
    scoped_refptr<base::SingleThreadTaskRunner> task_runner,
    size_t max_entries)
    : task_runner_(task_runner), max_entries_(max_entries) {
  DCHECK(task_runner_);
}

DeviceEventLogImpl::~DeviceEventLogImpl() = default;

void DeviceEventLogImpl::AddEntry(const char* file,
                                  int file_line,
                                  LogType log_type,
                                  LogLevel log_level,
                                  const std::string& event) {
  LogEntry entry(file, file_line, log_type, log_level, event);
  if (!task_runner_->RunsTasksInCurrentSequence()) {
    task_runner_->PostTask(
        FROM_HERE, base::BindOnce(&DeviceEventLogImpl::AddLogEntry,
                                  weak_ptr_factory_.GetWeakPtr(), entry));
    return;
  }
  AddLogEntry(entry);
}

void DeviceEventLogImpl::AddEntryWithTimestampForTesting(
    const char* file,
    int file_line,
    LogType log_type,
    LogLevel log_level,
    const std::string& event,
    base::Time time) {
  LogEntry entry(file, file_line, log_type, log_level, event, time);
  if (!task_runner_->RunsTasksInCurrentSequence()) {
    task_runner_->PostTask(
        FROM_HERE, base::BindOnce(&DeviceEventLogImpl::AddLogEntry,
                                  weak_ptr_factory_.GetWeakPtr(), entry));
    return;
  }
  AddLogEntry(entry);
}

void DeviceEventLogImpl::AddLogEntry(const LogEntry& entry) {
  DCHECK(task_runner_->RunsTasksInCurrentSequence());
  if (!entries_.empty()) {
    LogEntry& last = entries_.back();
    if (LogEntryMatches(last, entry)) {
      IncreaseLogEntryCount(entry, &last);
      return;
    }
  }
  if (entries_.size() >= max_entries_)
    RemoveEntry();
  entries_.push_back(entry);
  SendLogEntryToVLogOrErrorLog(entry);
}

void DeviceEventLogImpl::RemoveEntry() {
  const size_t max_error_entries = max_entries_ / 2;
  DCHECK(max_error_entries < entries_.size());
  // Remove the first (oldest) non-error entry, or the oldest entry if more
  // than half the entries are errors.
  size_t error_count = 0;
  for (auto iter = entries_.begin(); iter != entries_.end(); ++iter) {
    if (iter->log_level != LOG_LEVEL_ERROR) {
      entries_.erase(iter);
      return;
    }
    if (++error_count > max_error_entries)
      break;
  }
  // Too many error entries, remove the oldest entry.
  entries_.pop_front();
}

std::string DeviceEventLogImpl::GetAsString(StringOrder order,
                                            const std::string& format,
                                            const std::string& types,
                                            LogLevel max_level,
                                            size_t max_events) {
  DCHECK(task_runner_->RunsTasksInCurrentSequence());
  if (entries_.empty())
    return "No Log Entries.";

  ShowTime show_time;
  bool show_file, show_type, show_level, format_json;
  GetFormat(format, &show_time, &show_file, &show_type, &show_level,
            &format_json);

  std::set<LogType> include_types, exclude_types;
  GetLogTypes(types, &include_types, &exclude_types);

  std::string result;
  base::Value::List log_entries;
  if (order == OLDEST_FIRST) {
    size_t offset = 0;
    if (max_events > 0 && max_events < entries_.size()) {
      // Iterate backwards through the list skipping uninteresting entries to
      // determine the first entry to include.
      size_t shown_events = 0;
      size_t num_entries = 0;
      for (const LogEntry& entry : base::Reversed(entries_)) {
        ++num_entries;
        if (!LogEntryMatchesTypes(entry, include_types, exclude_types))
          continue;
        if (entry.log_level > max_level)
          continue;
        if (++shown_events >= max_events)
          break;
      }
      offset = entries_.size() - num_entries;
    }
    for (const LogEntry& entry : entries_) {
      if (offset > 0) {
        --offset;
        continue;
      }
      if (!LogEntryMatchesTypes(entry, include_types, exclude_types))
        continue;
      if (entry.log_level > max_level)
        continue;
      if (format_json) {
        log_entries.Append(LogEntryAsJSON(entry));
      } else {
        result += LogEntryToString(entry, show_time, show_file, show_type,
                                   show_level);
        result += "\n";
      }
    }
  } else {
    size_t nlines = 0;
    // Iterate backwards through the list to show the most recent entries first.
    for (const LogEntry& entry : base::Reversed(entries_)) {
      if (!LogEntryMatchesTypes(entry, include_types, exclude_types))
        continue;
      if (entry.log_level > max_level)
        continue;
      if (format_json) {
        log_entries.Append(LogEntryAsJSON(entry));
      } else {
        result += LogEntryToString(entry, show_time, show_file, show_type,
                                   show_level);
        result += "\n";
      }
      if (max_events > 0 && ++nlines >= max_events)
        break;
    }
  }
  if (format_json) {
    JSONStringValueSerializer serializer(&result);
    serializer.Serialize(log_entries);
  }

  return result;
}

void DeviceEventLogImpl::ClearAll() {
  entries_.clear();
}

void DeviceEventLogImpl::Clear(const base::Time& begin, const base::Time& end) {
  entries_.erase(std::ranges::lower_bound(entries_, begin, {}, &LogEntry::time),
                 std::ranges::upper_bound(entries_, end, {}, &LogEntry::time));
}

int DeviceEventLogImpl::GetCountByLevelForTesting(LogLevel level) {
  int count = 0;
  for (const auto& entry : entries_) {
    if (entry.log_level == level)
      ++count;
  }
  return count;
}

DeviceEventLogImpl::LogEntry::LogEntry(const char* filedesc,
                                       int file_line,
                                       LogType log_type,
                                       LogLevel log_level,
                                       const std::string& event)
    : file_line(file_line),
      log_type(log_type),
      log_level(log_level),
      time(base::Time::Now()),
      count(1) {
  base::TrimWhitespaceASCII(event, base::TRIM_ALL, &this->event);
  if (filedesc) {
    file = filedesc;
    size_t last_slash_pos = file.find_last_of("\\/");
    if (last_slash_pos != std::string::npos) {
      file.erase(0, last_slash_pos + 1);
    }
  }
}

DeviceEventLogImpl::LogEntry::LogEntry(const char* filedesc,
                                       int file_line,
                                       LogType log_type,
                                       LogLevel log_level,
                                       const std::string& event,
                                       base::Time time_for_testing)
    : LogEntry(filedesc, file_line, log_type, log_level, event) {
  time = time_for_testing;
}

DeviceEventLogImpl::LogEntry::LogEntry(const LogEntry& other) = default;

}  // namespace device_event_log