Revert of Fix device_event_log component build, make NonThreadSafe (patchset #6 id...
[chromium-blink-merge.git] / components / device_event_log / device_event_log.cc
blob8e720c0f31d2aa56923a319b6e4f73f28d50ab6d
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 "components/device_event_log/device_event_log_impl.h"
12 namespace device_event_log {
14 namespace {
16 const size_t kDefaultMaxEntries = 4000;
18 const int kSlowMethodThresholdMs = 10;
19 const int kVerySlowMethodThresholdMs = 50;
21 DeviceEventLogImpl* g_device_event_log = NULL;
23 } // namespace
25 const LogLevel kDefaultLogLevel = LOG_LEVEL_EVENT;
27 void Initialize(size_t max_entries) {
28 CHECK(!g_device_event_log);
29 if (max_entries == 0)
30 max_entries = kDefaultMaxEntries;
31 g_device_event_log = new DeviceEventLogImpl(max_entries);
34 void Shutdown() {
35 delete g_device_event_log;
36 g_device_event_log = NULL;
39 void AddEntry(const char* file,
40 int line,
41 LogType type,
42 LogLevel level,
43 const std::string& event) {
44 if (g_device_event_log) {
45 g_device_event_log->AddEntry(file, line, type, level, event);
46 } else {
47 DeviceEventLogImpl::SendToVLogOrErrorLog(file, line, type, level, event);
51 void AddEntryWithDescription(const char* file,
52 int line,
53 LogType type,
54 LogLevel level,
55 const std::string& event,
56 const std::string& desc) {
57 std::string event_with_desc = event;
58 if (!desc.empty())
59 event_with_desc += ": " + desc;
60 AddEntry(file, line, type, level, event_with_desc);
63 std::string GetAsString(StringOrder order,
64 const std::string& format,
65 const std::string& types,
66 LogLevel max_level,
67 size_t max_events) {
68 if (!g_device_event_log)
69 return "DeviceEventLog not initialized.";
70 return g_device_event_log->GetAsString(order, format, types, max_level,
71 max_events);
74 namespace internal {
76 DeviceEventLogInstance::DeviceEventLogInstance(const char* file,
77 int line,
78 device_event_log::LogType type,
79 device_event_log::LogLevel level)
80 : file_(file), line_(line), type_(type), level_(level) {
83 DeviceEventLogInstance::~DeviceEventLogInstance() {
84 device_event_log::AddEntry(file_, line_, type_, level_, stream_.str());
87 DeviceEventSystemErrorLogInstance::DeviceEventSystemErrorLogInstance(
88 const char* file,
89 int line,
90 device_event_log::LogType type,
91 device_event_log::LogLevel level,
92 logging::SystemErrorCode err)
93 : err_(err), log_instance_(file, line, type, level) {
96 DeviceEventSystemErrorLogInstance::~DeviceEventSystemErrorLogInstance() {
97 stream() << ": " << ::logging::SystemErrorCodeToString(err_);
100 ScopedDeviceLogIfSlow::ScopedDeviceLogIfSlow(LogType type,
101 const char* file,
102 const std::string& name)
103 : file_(file), type_(type), name_(name) {
106 ScopedDeviceLogIfSlow::~ScopedDeviceLogIfSlow() {
107 if (timer_.Elapsed().InMilliseconds() >= kSlowMethodThresholdMs) {
108 LogLevel level(LOG_LEVEL_DEBUG);
109 if (timer_.Elapsed().InMilliseconds() >= kVerySlowMethodThresholdMs)
110 level = LOG_LEVEL_ERROR;
111 DEVICE_LOG(type_, level) << "@@@ Slow method: " << file_ << ":" << name_
112 << ": " << timer_.Elapsed().InMilliseconds()
113 << "ms";
117 } // namespace internal
119 } // namespace device_event_log