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 "components/device_event_log/device_event_log_impl.h"
12 namespace device_event_log
{
16 const size_t kDefaultMaxEntries
= 4000;
18 const int kSlowMethodThresholdMs
= 10;
19 const int kVerySlowMethodThresholdMs
= 50;
21 DeviceEventLogImpl
* g_device_event_log
= NULL
;
25 const LogLevel kDefaultLogLevel
= LOG_LEVEL_EVENT
;
27 void Initialize(size_t max_entries
) {
28 CHECK(!g_device_event_log
);
30 max_entries
= kDefaultMaxEntries
;
31 g_device_event_log
= new DeviceEventLogImpl(max_entries
);
35 delete g_device_event_log
;
36 g_device_event_log
= NULL
;
39 void AddEntry(const char* file
,
43 const std::string
& event
) {
44 if (g_device_event_log
) {
45 g_device_event_log
->AddEntry(file
, line
, type
, level
, event
);
47 DeviceEventLogImpl::SendToVLogOrErrorLog(file
, line
, type
, level
, event
);
51 void AddEntryWithDescription(const char* file
,
55 const std::string
& event
,
56 const std::string
& desc
) {
57 std::string event_with_desc
= event
;
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
,
68 if (!g_device_event_log
)
69 return "DeviceEventLog not initialized.";
70 return g_device_event_log
->GetAsString(order
, format
, types
, max_level
,
76 DeviceEventLogInstance::DeviceEventLogInstance(const char* file
,
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(
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
,
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()
117 } // namespace internal
119 } // namespace device_event_log