OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_DEVICE_EVENT_LOG_DEVICE_EVENT_LOG_H_ | 5 #ifndef COMPONENTS_DEVICE_EVENT_LOG_DEVICE_EVENT_LOG_H_ |
6 #define COMPONENTS_DEVICE_EVENT_LOG_DEVICE_EVENT_LOG_H_ | 6 #define COMPONENTS_DEVICE_EVENT_LOG_DEVICE_EVENT_LOG_H_ |
7 | 7 |
8 #include <cstring> | 8 #include <cstring> |
9 #include <sstream> | 9 #include <sstream> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/logging.h" |
12 #include "base/timer/elapsed_timer.h" | 13 #include "base/timer/elapsed_timer.h" |
13 #include "components/device_event_log/device_event_log_export.h" | 14 #include "components/device_event_log/device_event_log_export.h" |
14 | 15 |
15 // These macros can be used to log device related events. | 16 // These macros can be used to log device related events. |
16 // The following values should be used for |level| in these macros: | 17 // The following values should be used for |level| in these macros: |
17 // ERROR Unexpected events, or device level failures. Use sparingly. | 18 // ERROR Unexpected events, or device level failures. Use sparingly. |
18 // USER Events initiated directly by a user (or Chrome) action. | 19 // USER Events initiated directly by a user (or Chrome) action. |
19 // EVENT Default event type. | 20 // EVENT Default event type. |
20 // DEBUG Debugging details that are usually not interesting. | 21 // DEBUG Debugging details that are usually not interesting. |
21 // Examples: | 22 // Examples: |
22 // NET_LOG(EVENT) << "NetworkState Changed " << name << ": " << state; | 23 // NET_LOG(EVENT) << "NetworkState Changed " << name << ": " << state; |
23 // POWER_LOG(USER) << "Suspend requested"; | 24 // POWER_LOG(USER) << "Suspend requested"; |
24 | 25 |
25 #define NET_LOG(level) \ | 26 #define NET_LOG(level) \ |
26 DEVICE_LOG(::device_event_log::LOG_TYPE_NETWORK, \ | 27 DEVICE_LOG(::device_event_log::LOG_TYPE_NETWORK, \ |
27 ::device_event_log::LOG_LEVEL_##level) | 28 ::device_event_log::LOG_LEVEL_##level) |
28 #define POWER_LOG(level) \ | 29 #define POWER_LOG(level) \ |
29 DEVICE_LOG(::device_event_log::LOG_TYPE_POWER, \ | 30 DEVICE_LOG(::device_event_log::LOG_TYPE_POWER, \ |
30 ::device_event_log::LOG_LEVEL_##level) | 31 ::device_event_log::LOG_LEVEL_##level) |
31 #define LOGIN_LOG(level) \ | 32 #define LOGIN_LOG(level) \ |
32 DEVICE_LOG(::device_event_log::LOG_TYPE_LOGIN, \ | 33 DEVICE_LOG(::device_event_log::LOG_TYPE_LOGIN, \ |
33 ::device_event_log::LOG_LEVEL_##level) | 34 ::device_event_log::LOG_LEVEL_##level) |
34 #define USB_LOG(level) \ | 35 #define USB_LOG(level) \ |
35 DEVICE_LOG(::device_event_log::LOG_TYPE_USB, \ | 36 DEVICE_LOG(::device_event_log::LOG_TYPE_USB, \ |
36 ::device_event_log::LOG_LEVEL_##level) | 37 ::device_event_log::LOG_LEVEL_##level) |
| 38 #define HID_LOG(level) \ |
| 39 DEVICE_LOG(::device_event_log::LOG_TYPE_HID, \ |
| 40 ::device_event_log::LOG_LEVEL_##level) |
| 41 #define HID_PLOG(level) \ |
| 42 DEVICE_PLOG(::device_event_log::LOG_TYPE_HID, \ |
| 43 ::device_event_log::LOG_LEVEL_##level) |
37 | 44 |
38 // Generally prefer the above macros unless |type| or |level| is not constant. | 45 // Generally prefer the above macros unless |type| or |level| is not constant. |
39 | 46 |
40 #define DEVICE_LOG(type, level) \ | 47 #define DEVICE_LOG(type, level) \ |
41 ::device_event_log::internal::DeviceEventLogInstance(__FILE__, __LINE__, \ | 48 ::device_event_log::internal::DeviceEventLogInstance(__FILE__, __LINE__, \ |
42 type, level).stream() | 49 type, level).stream() |
| 50 #define DEVICE_PLOG(type, level) \ |
| 51 ::device_event_log::internal::DeviceEventSystemErrorLogInstance( \ |
| 52 __FILE__, __LINE__, type, level, ::logging::GetLastSystemErrorCode()) \ |
| 53 .stream() |
43 | 54 |
44 // Declare {Type_LOG_IF_SLOW() at the top of a method to log slow methods | 55 // Declare {Type_LOG_IF_SLOW() at the top of a method to log slow methods |
45 // where "slow" is defined by kSlowMethodThresholdMs in the .cc file. | 56 // where "slow" is defined by kSlowMethodThresholdMs in the .cc file. |
46 #define SCOPED_NET_LOG_IF_SLOW() \ | 57 #define SCOPED_NET_LOG_IF_SLOW() \ |
47 SCOPED_DEVICE_LOG_IF_SLOW(::device_event_log::LOG_TYPE_NETWORK) | 58 SCOPED_DEVICE_LOG_IF_SLOW(::device_event_log::LOG_TYPE_NETWORK) |
48 | 59 |
49 // Generally prefer the above macros unless |type| is not constant. | 60 // Generally prefer the above macros unless |type| is not constant. |
50 | 61 |
51 #define SCOPED_DEVICE_LOG_IF_SLOW(type) \ | 62 #define SCOPED_DEVICE_LOG_IF_SLOW(type) \ |
52 ::device_event_log::internal::ScopedDeviceLogIfSlow \ | 63 ::device_event_log::internal::ScopedDeviceLogIfSlow \ |
53 scoped_device_log_if_slow(type, __FILE__, __func__) | 64 scoped_device_log_if_slow(type, __FILE__, __func__) |
54 | 65 |
55 namespace device_event_log { | 66 namespace device_event_log { |
56 | 67 |
57 // Used to specify the type of event. NOTE: Be sure to update LogTypeFromString | 68 // Used to specify the type of event. NOTE: Be sure to update LogTypeFromString |
58 // and GetLogTypeString when adding entries to this enum. Also consider | 69 // and GetLogTypeString when adding entries to this enum. Also consider |
59 // updating chrome://device-log (see device_log_ui.cc). | 70 // updating chrome://device-log (see device_log_ui.cc). |
60 enum LogType { | 71 enum LogType { |
61 // Shill / network configuration related events. | 72 // Shill / network configuration related events. |
62 LOG_TYPE_NETWORK, | 73 LOG_TYPE_NETWORK, |
63 // Power manager related events. | 74 // Power manager related events. |
64 LOG_TYPE_POWER, | 75 LOG_TYPE_POWER, |
65 // Login related events. | 76 // Login related events. |
66 LOG_TYPE_LOGIN, | 77 LOG_TYPE_LOGIN, |
67 // USB device related events (i.e. device/usb). | 78 // USB device related events (i.e. device/usb). |
68 LOG_TYPE_USB, | 79 LOG_TYPE_USB, |
| 80 // Human-interface device related events (i.e. device/hid). |
| 81 LOG_TYPE_HID, |
69 // Used internally | 82 // Used internally |
70 LOG_TYPE_UNKNOWN | 83 LOG_TYPE_UNKNOWN |
71 }; | 84 }; |
72 | 85 |
73 // Used to specify the detail level for logging. In GetAsString, used to | 86 // Used to specify the detail level for logging. In GetAsString, used to |
74 // specify the maximum detail level (i.e. EVENT will include USER and ERROR). | 87 // specify the maximum detail level (i.e. EVENT will include USER and ERROR). |
75 // See top-level comment for guidelines for each type. | 88 // See top-level comment for guidelines for each type. |
76 enum LogLevel { | 89 enum LogLevel { |
77 LOG_LEVEL_ERROR = 0, | 90 LOG_LEVEL_ERROR = 0, |
78 LOG_LEVEL_USER = 1, | 91 LOG_LEVEL_USER = 1, |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 private: | 162 private: |
150 const char* file_; | 163 const char* file_; |
151 const int line_; | 164 const int line_; |
152 device_event_log::LogType type_; | 165 device_event_log::LogType type_; |
153 device_event_log::LogLevel level_; | 166 device_event_log::LogLevel level_; |
154 std::ostringstream stream_; | 167 std::ostringstream stream_; |
155 | 168 |
156 DISALLOW_COPY_AND_ASSIGN(DeviceEventLogInstance); | 169 DISALLOW_COPY_AND_ASSIGN(DeviceEventLogInstance); |
157 }; | 170 }; |
158 | 171 |
| 172 // Implementation class for DEVICE_PLOG macros. Provides a stream for creating |
| 173 // a log string and adds the event, including system error code, using |
| 174 // device_event_log::AddEntry on destruction. |
| 175 class DEVICE_EVENT_LOG_EXPORT DeviceEventSystemErrorLogInstance { |
| 176 public: |
| 177 DeviceEventSystemErrorLogInstance(const char* file, |
| 178 int line, |
| 179 device_event_log::LogType type, |
| 180 device_event_log::LogLevel level, |
| 181 logging::SystemErrorCode err); |
| 182 ~DeviceEventSystemErrorLogInstance(); |
| 183 |
| 184 std::ostream& stream() { return log_instance_.stream(); } |
| 185 |
| 186 private: |
| 187 logging::SystemErrorCode err_; |
| 188 // Constructor parameters are passed to |log_instance_| which will update the |
| 189 // log when it is destroyed (after a string description of |err_| is appended |
| 190 // to the stream). |
| 191 DeviceEventLogInstance log_instance_; |
| 192 |
| 193 DISALLOW_COPY_AND_ASSIGN(DeviceEventSystemErrorLogInstance); |
| 194 }; |
| 195 |
159 // Implementation class for SCOPED_LOG_IF_SLOW macros. Tests the elapsed time on | 196 // Implementation class for SCOPED_LOG_IF_SLOW macros. Tests the elapsed time on |
160 // destruction and adds a Debug or Error log entry if it exceeds the | 197 // destruction and adds a Debug or Error log entry if it exceeds the |
161 // corresponding expected maximum elapsed time. | 198 // corresponding expected maximum elapsed time. |
162 class DEVICE_EVENT_LOG_EXPORT ScopedDeviceLogIfSlow { | 199 class DEVICE_EVENT_LOG_EXPORT ScopedDeviceLogIfSlow { |
163 public: | 200 public: |
164 ScopedDeviceLogIfSlow(LogType type, | 201 ScopedDeviceLogIfSlow(LogType type, |
165 const char* file, | 202 const char* file, |
166 const std::string& name); | 203 const std::string& name); |
167 ~ScopedDeviceLogIfSlow(); | 204 ~ScopedDeviceLogIfSlow(); |
168 | 205 |
169 private: | 206 private: |
170 const char* file_; | 207 const char* file_; |
171 LogType type_; | 208 LogType type_; |
172 std::string name_; | 209 std::string name_; |
173 base::ElapsedTimer timer_; | 210 base::ElapsedTimer timer_; |
174 }; | 211 }; |
175 | 212 |
176 } // namespace internal | 213 } // namespace internal |
177 | 214 |
178 } // namespace device_event_log | 215 } // namespace device_event_log |
179 | 216 |
180 #endif // DEVICE_EVENT_LOG_DEVICE_EVENT_LOG_H_ | 217 #endif // DEVICE_EVENT_LOG_DEVICE_EVENT_LOG_H_ |
OLD | NEW |