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
|
// 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.h"
#include <string>
#include "base/logging.h"
#include "base/task/single_thread_task_runner.h"
#include "components/device_event_log/device_event_log_impl.h"
namespace device_event_log {
namespace {
const size_t kDefaultMaxEntries = 4000;
const int kSlowMethodThresholdMs = 10;
// Loaded builders may perform badly, set this to a fairly high value to catch
// extreme cases.
const int kVerySlowMethodThresholdMs = 250;
DeviceEventLogImpl* g_device_event_log = nullptr;
} // namespace
const LogLevel kDefaultLogLevel = LOG_LEVEL_EVENT;
void Initialize(size_t max_entries) {
CHECK(!g_device_event_log);
if (max_entries == 0)
max_entries = kDefaultMaxEntries;
g_device_event_log = new DeviceEventLogImpl(
base::SingleThreadTaskRunner::GetCurrentDefault(), max_entries);
}
bool IsInitialized() {
return !!g_device_event_log;
}
void Shutdown() {
delete g_device_event_log;
g_device_event_log = nullptr;
}
void AddEntry(const char* file,
int line,
LogType type,
LogLevel level,
const std::string& event) {
if (g_device_event_log) {
g_device_event_log->AddEntry(file, line, type, level, event);
} else {
DeviceEventLogImpl::SendToVLogOrErrorLog(file, line, type, level, event);
}
}
void AddEntryWithDescription(const char* file,
int line,
LogType type,
LogLevel level,
const std::string& event,
const std::string& desc) {
std::string event_with_desc = event;
if (!desc.empty())
event_with_desc += ": " + desc;
AddEntry(file, line, type, level, event_with_desc);
}
std::string GetAsString(StringOrder order,
const std::string& format,
const std::string& types,
LogLevel max_level,
size_t max_events) {
if (!g_device_event_log)
return "DeviceEventLog not initialized.";
return g_device_event_log->GetAsString(order, format, types, max_level,
max_events);
}
void ClearAll() {
if (g_device_event_log)
g_device_event_log->ClearAll();
}
void Clear(const base::Time& begin, const base::Time& end) {
if (g_device_event_log)
g_device_event_log->Clear(begin, end);
}
int GetCountByLevelForTesting(LogLevel level) {
return g_device_event_log->GetCountByLevelForTesting(level);
}
namespace internal {
DeviceEventLogInstance::DeviceEventLogInstance(const char* file,
int line,
device_event_log::LogType type,
device_event_log::LogLevel level)
: file_(file), line_(line), type_(type), level_(level) {
}
DeviceEventLogInstance::~DeviceEventLogInstance() {
device_event_log::AddEntry(file_, line_, type_, level_, stream_.str());
}
DeviceEventSystemErrorLogInstance::DeviceEventSystemErrorLogInstance(
const char* file,
int line,
device_event_log::LogType type,
device_event_log::LogLevel level,
logging::SystemErrorCode err)
: err_(err), log_instance_(file, line, type, level) {
}
DeviceEventSystemErrorLogInstance::~DeviceEventSystemErrorLogInstance() {
stream() << ": " << ::logging::SystemErrorCodeToString(err_);
}
ScopedDeviceLogIfSlow::ScopedDeviceLogIfSlow(LogType type,
const char* file,
const std::string& name)
: file_(file), type_(type), name_(name) {
}
ScopedDeviceLogIfSlow::~ScopedDeviceLogIfSlow() {
if (timer_.Elapsed().InMilliseconds() >= kSlowMethodThresholdMs) {
LogLevel level(LOG_LEVEL_DEBUG);
if (timer_.Elapsed().InMilliseconds() >= kVerySlowMethodThresholdMs)
level = LOG_LEVEL_ERROR;
DEVICE_LOG(type_, level) << "@@@ Slow method: " << file_ << ":" << name_
<< ": " << timer_.Elapsed().InMilliseconds()
<< "ms";
}
}
} // namespace internal
} // namespace device_event_log
|