Componentize the IDR_LOCATION_HTTP resource
[chromium-blink-merge.git] / components / device_event_log / device_event_log.cc
blob6e70eeb3a2bfd4e8cf77e6fde39f2824efb85651
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 #include "components/device_event_log/device_event_log.h"
7 #include <string>
9 #include "base/logging.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "components/device_event_log/device_event_log_impl.h"
13 namespace device_event_log {
15 namespace {
17 const size_t kDefaultMaxEntries = 4000;
19 const int kSlowMethodThresholdMs = 10;
20 const int kVerySlowMethodThresholdMs = 50;
22 DeviceEventLogImpl* g_device_event_log = NULL;
24 } // namespace
26 const LogLevel kDefaultLogLevel = LOG_LEVEL_EVENT;
28 void Initialize(size_t max_entries) {
29 CHECK(!g_device_event_log);
30 if (max_entries == 0)
31 max_entries = kDefaultMaxEntries;
32 g_device_event_log =
33 new DeviceEventLogImpl(base::ThreadTaskRunnerHandle::Get(), max_entries);
36 bool IsInitialized() {
37 return !!g_device_event_log;
40 void Shutdown() {
41 delete g_device_event_log;
42 g_device_event_log = NULL;
45 void AddEntry(const char* file,
46 int line,
47 LogType type,
48 LogLevel level,
49 const std::string& event) {
50 if (g_device_event_log) {
51 g_device_event_log->AddEntry(file, line, type, level, event);
52 } else {
53 DeviceEventLogImpl::SendToVLogOrErrorLog(file, line, type, level, event);
57 void AddEntryWithDescription(const char* file,
58 int line,
59 LogType type,
60 LogLevel level,
61 const std::string& event,
62 const std::string& desc) {
63 std::string event_with_desc = event;
64 if (!desc.empty())
65 event_with_desc += ": " + desc;
66 AddEntry(file, line, type, level, event_with_desc);
69 std::string GetAsString(StringOrder order,
70 const std::string& format,
71 const std::string& types,
72 LogLevel max_level,
73 size_t max_events) {
74 if (!g_device_event_log)
75 return "DeviceEventLog not initialized.";
76 return g_device_event_log->GetAsString(order, format, types, max_level,
77 max_events);
80 namespace internal {
82 DeviceEventLogInstance::DeviceEventLogInstance(const char* file,
83 int line,
84 device_event_log::LogType type,
85 device_event_log::LogLevel level)
86 : file_(file), line_(line), type_(type), level_(level) {
89 DeviceEventLogInstance::~DeviceEventLogInstance() {
90 device_event_log::AddEntry(file_, line_, type_, level_, stream_.str());
93 DeviceEventSystemErrorLogInstance::DeviceEventSystemErrorLogInstance(
94 const char* file,
95 int line,
96 device_event_log::LogType type,
97 device_event_log::LogLevel level,
98 logging::SystemErrorCode err)
99 : err_(err), log_instance_(file, line, type, level) {
102 DeviceEventSystemErrorLogInstance::~DeviceEventSystemErrorLogInstance() {
103 stream() << ": " << ::logging::SystemErrorCodeToString(err_);
106 ScopedDeviceLogIfSlow::ScopedDeviceLogIfSlow(LogType type,
107 const char* file,
108 const std::string& name)
109 : file_(file), type_(type), name_(name) {
112 ScopedDeviceLogIfSlow::~ScopedDeviceLogIfSlow() {
113 if (timer_.Elapsed().InMilliseconds() >= kSlowMethodThresholdMs) {
114 LogLevel level(LOG_LEVEL_DEBUG);
115 if (timer_.Elapsed().InMilliseconds() >= kVerySlowMethodThresholdMs)
116 level = LOG_LEVEL_ERROR;
117 DEVICE_LOG(type_, level) << "@@@ Slow method: " << file_ << ":" << name_
118 << ": " << timer_.Elapsed().InMilliseconds()
119 << "ms";
123 } // namespace internal
125 } // namespace device_event_log