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"
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
{
17 const size_t kDefaultMaxEntries
= 4000;
19 const int kSlowMethodThresholdMs
= 10;
20 const int kVerySlowMethodThresholdMs
= 50;
22 DeviceEventLogImpl
* g_device_event_log
= NULL
;
26 const LogLevel kDefaultLogLevel
= LOG_LEVEL_EVENT
;
28 void Initialize(size_t max_entries
) {
29 CHECK(!g_device_event_log
);
31 max_entries
= kDefaultMaxEntries
;
33 new DeviceEventLogImpl(base::ThreadTaskRunnerHandle::Get(), max_entries
);
36 bool IsInitialized() {
37 return !!g_device_event_log
;
41 delete g_device_event_log
;
42 g_device_event_log
= NULL
;
45 void AddEntry(const char* file
,
49 const std::string
& event
) {
50 if (g_device_event_log
) {
51 g_device_event_log
->AddEntry(file
, line
, type
, level
, event
);
53 DeviceEventLogImpl::SendToVLogOrErrorLog(file
, line
, type
, level
, event
);
57 void AddEntryWithDescription(const char* file
,
61 const std::string
& event
,
62 const std::string
& desc
) {
63 std::string event_with_desc
= event
;
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
,
74 if (!g_device_event_log
)
75 return "DeviceEventLog not initialized.";
76 return g_device_event_log
->GetAsString(order
, format
, types
, max_level
,
82 DeviceEventLogInstance::DeviceEventLogInstance(const char* file
,
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(
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
,
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()
123 } // namespace internal
125 } // namespace device_event_log