| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "chromeos/device_event_log.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "chromeos/device_event_log_impl.h" | |
| 11 | |
| 12 namespace chromeos { | |
| 13 | |
| 14 namespace device_event_log { | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 const size_t kDefaultMaxEntries = 4000; | |
| 19 | |
| 20 const int kSlowMethodThresholdMs = 10; | |
| 21 const int kVerySlowMethodThresholdMs = 50; | |
| 22 | |
| 23 DeviceEventLogImpl* g_device_event_log = NULL; | |
| 24 | |
| 25 } // namespace | |
| 26 | |
| 27 const LogLevel kDefaultLogLevel = LOG_LEVEL_EVENT; | |
| 28 | |
| 29 void Initialize(size_t max_entries) { | |
| 30 CHECK(!g_device_event_log); | |
| 31 if (max_entries == 0) | |
| 32 max_entries = kDefaultMaxEntries; | |
| 33 g_device_event_log = new DeviceEventLogImpl(max_entries); | |
| 34 } | |
| 35 | |
| 36 void Shutdown() { | |
| 37 delete g_device_event_log; | |
| 38 g_device_event_log = NULL; | |
| 39 } | |
| 40 | |
| 41 void AddEntry(const char* file, | |
| 42 int line, | |
| 43 LogType type, | |
| 44 LogLevel level, | |
| 45 const std::string& event) { | |
| 46 if (g_device_event_log) { | |
| 47 g_device_event_log->AddEntry(file, line, type, level, event); | |
| 48 } else { | |
| 49 DeviceEventLogImpl::SendToVLogOrErrorLog(file, line, type, level, event); | |
| 50 } | |
| 51 } | |
| 52 | |
| 53 void AddEntryWithDescription(const char* file, | |
| 54 int line, | |
| 55 LogType type, | |
| 56 LogLevel level, | |
| 57 const std::string& event, | |
| 58 const std::string& desc) { | |
| 59 std::string event_with_desc = event; | |
| 60 if (!desc.empty()) | |
| 61 event_with_desc += ": " + desc; | |
| 62 AddEntry(file, line, type, level, event_with_desc); | |
| 63 } | |
| 64 | |
| 65 std::string GetAsString(StringOrder order, | |
| 66 const std::string& format, | |
| 67 const std::string& types, | |
| 68 LogLevel max_level, | |
| 69 size_t max_events) { | |
| 70 if (!g_device_event_log) | |
| 71 return "DeviceEventLog not initialized."; | |
| 72 return g_device_event_log->GetAsString(order, format, types, max_level, | |
| 73 max_events); | |
| 74 } | |
| 75 | |
| 76 namespace internal { | |
| 77 | |
| 78 DeviceEventLogInstance::DeviceEventLogInstance(const char* file, | |
| 79 int line, | |
| 80 device_event_log::LogType type, | |
| 81 device_event_log::LogLevel level) | |
| 82 : file_(file), line_(line), type_(type), level_(level) { | |
| 83 } | |
| 84 | |
| 85 DeviceEventLogInstance::~DeviceEventLogInstance() { | |
| 86 device_event_log::AddEntry(file_, line_, type_, level_, stream_.str()); | |
| 87 } | |
| 88 | |
| 89 ScopedDeviceLogIfSlow::ScopedDeviceLogIfSlow(LogType type, | |
| 90 const char* file, | |
| 91 const std::string& name) | |
| 92 : file_(file), type_(type), name_(name) { | |
| 93 } | |
| 94 | |
| 95 ScopedDeviceLogIfSlow::~ScopedDeviceLogIfSlow() { | |
| 96 if (timer_.Elapsed().InMilliseconds() >= kSlowMethodThresholdMs) { | |
| 97 LogLevel level(LOG_LEVEL_DEBUG); | |
| 98 if (timer_.Elapsed().InMilliseconds() >= kVerySlowMethodThresholdMs) | |
| 99 level = LOG_LEVEL_ERROR; | |
| 100 DEVICE_LOG(type_, level) << "@@@ Slow method: " << file_ << ":" << name_ | |
| 101 << ": " << timer_.Elapsed().InMilliseconds() | |
| 102 << "ms"; | |
| 103 } | |
| 104 } | |
| 105 | |
| 106 } // namespace internal | |
| 107 | |
| 108 } // namespace device_event_log | |
| 109 | |
| 110 } // namespace chromeos | |
| OLD | NEW |