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 CHROMEOS_DEVICE_EVENT_LOG_H_ | 5 #ifndef COMPONENTS_DEVICE_EVENT_LOG_DEVICE_EVENT_LOG_H_ |
6 #define CHROMEOS_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/timer/elapsed_timer.h" | 12 #include "base/timer/elapsed_timer.h" |
13 #include "chromeos/chromeos_export.h" | 13 #include "components/device_event_log/device_event_log_export.h" |
14 | 14 |
15 namespace chromeos { | 15 // These macros can be used to log device related events. |
16 | |
17 // These macros can be used to log chromeos device related events. | |
18 // The following values should be used for |level| in these macros: | 16 // The following values should be used for |level| in these macros: |
19 // ERROR Unexpected events, or device level failures. Use sparingly. | 17 // ERROR Unexpected events, or device level failures. Use sparingly. |
20 // USER Events initiated directly by a user (or Chrome) action. | 18 // USER Events initiated directly by a user (or Chrome) action. |
21 // EVENT Default event type. | 19 // EVENT Default event type. |
22 // DEBUG Debugging details that are usually not interesting. | 20 // DEBUG Debugging details that are usually not interesting. |
23 // Examples: | 21 // Examples: |
24 // NET_LOG(EVENT) << "NetworkState Changed " << name << ": " << state; | 22 // NET_LOG(EVENT) << "NetworkState Changed " << name << ": " << state; |
25 // POWER_LOG(USER) << "Suspend requested"; | 23 // POWER_LOG(USER) << "Suspend requested"; |
26 | 24 |
27 #define NET_LOG(level) \ | 25 #define NET_LOG(level) \ |
28 DEVICE_LOG(::chromeos::device_event_log::LOG_TYPE_NETWORK, \ | 26 DEVICE_LOG(::device_event_log::LOG_TYPE_NETWORK, \ |
29 ::chromeos::device_event_log::LOG_LEVEL_##level) | 27 ::device_event_log::LOG_LEVEL_##level) |
30 #define POWER_LOG(level) \ | 28 #define POWER_LOG(level) \ |
31 DEVICE_LOG(::chromeos::device_event_log::LOG_TYPE_POWER, \ | 29 DEVICE_LOG(::device_event_log::LOG_TYPE_POWER, \ |
32 ::chromeos::device_event_log::LOG_LEVEL_##level) | 30 ::device_event_log::LOG_LEVEL_##level) |
33 #define LOGIN_LOG(level) \ | 31 #define LOGIN_LOG(level) \ |
34 DEVICE_LOG(::chromeos::device_event_log::LOG_TYPE_LOGIN, \ | 32 DEVICE_LOG(::device_event_log::LOG_TYPE_LOGIN, \ |
35 ::chromeos::device_event_log::LOG_LEVEL_##level) | 33 ::device_event_log::LOG_LEVEL_##level) |
36 | 34 |
37 // Generally prefer the above macros unless |type| or |level| is not constant. | 35 // Generally prefer the above macros unless |type| or |level| is not constant. |
38 | 36 |
39 #define DEVICE_LOG(type, level) \ | 37 #define DEVICE_LOG(type, level) \ |
40 ::chromeos::device_event_log::internal::DeviceEventLogInstance( \ | 38 ::device_event_log::internal::DeviceEventLogInstance(__FILE__, __LINE__, \ |
41 __FILE__, __LINE__, type, level).stream() | 39 type, level).stream() |
42 | 40 |
43 // Declare {Type_LOG_IF_SLOW() at the top of a method to log slow methods | 41 // Declare {Type_LOG_IF_SLOW() at the top of a method to log slow methods |
44 // where "slow" is defined by kSlowMethodThresholdMs in the .cc file. | 42 // where "slow" is defined by kSlowMethodThresholdMs in the .cc file. |
45 #define SCOPED_NET_LOG_IF_SLOW() \ | 43 #define SCOPED_NET_LOG_IF_SLOW() \ |
46 SCOPED_DEVICE_LOG_IF_SLOW(::chromeos::device_event_log::LOG_TYPE_NETWORK) | 44 SCOPED_DEVICE_LOG_IF_SLOW(::device_event_log::LOG_TYPE_NETWORK) |
47 | 45 |
48 // Generally prefer the above macros unless |type| is not constant. | 46 // Generally prefer the above macros unless |type| is not constant. |
49 | 47 |
50 #define SCOPED_DEVICE_LOG_IF_SLOW(type) \ | 48 #define SCOPED_DEVICE_LOG_IF_SLOW(type) \ |
51 ::chromeos::device_event_log::internal::ScopedDeviceLogIfSlow \ | 49 ::device_event_log::internal::ScopedDeviceLogIfSlow \ |
52 scoped_device_log_if_slow(type, __FILE__, __func__) | 50 scoped_device_log_if_slow(type, __FILE__, __func__) |
53 | 51 |
54 namespace device_event_log { | 52 namespace device_event_log { |
55 | 53 |
56 // Used to specify the type of event. NOTE: Be sure to update LogTypeFromString | 54 // Used to specify the type of event. NOTE: Be sure to update LogTypeFromString |
57 // and GetLogTypeString when adding entries to this enum. Also consider | 55 // and GetLogTypeString when adding entries to this enum. Also consider |
58 // updating chrome://device-log (see device_log_ui.cc). | 56 // updating chrome://device-log (see device_log_ui.cc). |
59 enum LogType { | 57 enum LogType { |
60 // Shill / network configuration related events. | 58 // Shill / network configuration related events. |
61 LOG_TYPE_NETWORK, | 59 LOG_TYPE_NETWORK, |
(...skipping 13 matching lines...) Expand all Loading... |
75 LOG_LEVEL_USER = 1, | 73 LOG_LEVEL_USER = 1, |
76 LOG_LEVEL_EVENT = 2, | 74 LOG_LEVEL_EVENT = 2, |
77 LOG_LEVEL_DEBUG = 3 | 75 LOG_LEVEL_DEBUG = 3 |
78 }; | 76 }; |
79 | 77 |
80 // Used to specify which order to output event entries in GetAsString. | 78 // Used to specify which order to output event entries in GetAsString. |
81 enum StringOrder { OLDEST_FIRST, NEWEST_FIRST }; | 79 enum StringOrder { OLDEST_FIRST, NEWEST_FIRST }; |
82 | 80 |
83 // Initializes / shuts down device event logging. If |max_entries| = 0 the | 81 // Initializes / shuts down device event logging. If |max_entries| = 0 the |
84 // default value will be used. | 82 // default value will be used. |
85 CHROMEOS_EXPORT void Initialize(size_t max_entries); | 83 DEVICE_EVENT_LOG_EXPORT void Initialize(size_t max_entries); |
86 CHROMEOS_EXPORT void Shutdown(); | 84 DEVICE_EVENT_LOG_EXPORT void Shutdown(); |
87 | 85 |
88 // If the global instance is initialized, adds an entry to it. Regardless of | 86 // If the global instance is initialized, adds an entry to it. Regardless of |
89 // whether the global instance was intitialzed, this logs the event to | 87 // whether the global instance was intitialzed, this logs the event to |
90 // LOG(ERROR) if |type| = ERROR or VLOG(1) otherwise. | 88 // LOG(ERROR) if |type| = ERROR or VLOG(1) otherwise. |
91 CHROMEOS_EXPORT void AddEntry(const char* file, | 89 DEVICE_EVENT_LOG_EXPORT void AddEntry(const char* file, |
92 int line, | 90 int line, |
93 LogType type, | 91 LogType type, |
94 LogLevel level, | 92 LogLevel level, |
95 const std::string& event); | 93 const std::string& event); |
96 | 94 |
97 // For backwards compatibility with network_event_log. Combines |event| and | 95 // For backwards compatibility with network_event_log. Combines |event| and |
98 // |description| and calls AddEntry(). | 96 // |description| and calls AddEntry(). |
99 CHROMEOS_EXPORT void AddEntryWithDescription(const char* file, | 97 DEVICE_EVENT_LOG_EXPORT void AddEntryWithDescription( |
100 int line, | 98 const char* file, |
101 LogType type, | 99 int line, |
102 LogLevel level, | 100 LogType type, |
103 const std::string& event, | 101 LogLevel level, |
104 const std::string& description); | 102 const std::string& event, |
| 103 const std::string& description); |
105 | 104 |
106 // Outputs the log to a formatted string. | 105 // Outputs the log to a formatted string. |
107 // |order| determines which order to output the events. | 106 // |order| determines which order to output the events. |
108 // |format| is a comma-separated string that determines which elements to show. | 107 // |format| is a comma-separated string that determines which elements to show. |
109 // e.g. "time,desc". Note: order of the strings does not affect the output. | 108 // e.g. "time,desc". Note: order of the strings does not affect the output. |
110 // "time" - Include a timestamp. | 109 // "time" - Include a timestamp. |
111 // "file" - Include file and line number. | 110 // "file" - Include file and line number. |
112 // "type" - Include the event type. | 111 // "type" - Include the event type. |
113 // "html" - Include html tags. | 112 // "html" - Include html tags. |
114 // "json" - Return JSON format dictionaries containing entries for timestamp, | 113 // "json" - Return JSON format dictionaries containing entries for timestamp, |
115 // level, type, file, and event. | 114 // level, type, file, and event. |
116 // |types| lists the types included in the output. Prepend "non-" to disclude | 115 // |types| lists the types included in the output. Prepend "non-" to disclude |
117 // a type. e.g. "network,login" or "non-network". Use an empty string for | 116 // a type. e.g. "network,login" or "non-network". Use an empty string for |
118 // all types. | 117 // all types. |
119 // |max_level| determines the maximum log level to be included in the output. | 118 // |max_level| determines the maximum log level to be included in the output. |
120 // |max_events| limits how many events are output if > 0, otherwise all events | 119 // |max_events| limits how many events are output if > 0, otherwise all events |
121 // are included. | 120 // are included. |
122 CHROMEOS_EXPORT std::string GetAsString(StringOrder order, | 121 DEVICE_EVENT_LOG_EXPORT std::string GetAsString(StringOrder order, |
123 const std::string& format, | 122 const std::string& format, |
124 const std::string& types, | 123 const std::string& types, |
125 LogLevel max_level, | 124 LogLevel max_level, |
126 size_t max_events); | 125 size_t max_events); |
127 | 126 |
128 CHROMEOS_EXPORT extern const LogLevel kDefaultLogLevel; | 127 DEVICE_EVENT_LOG_EXPORT extern const LogLevel kDefaultLogLevel; |
129 | 128 |
130 namespace internal { | 129 namespace internal { |
131 | 130 |
132 // Implementation class for DEVICE_LOG macros. Provides a stream for creating | 131 // Implementation class for DEVICE_LOG macros. Provides a stream for creating |
133 // a log string and adds the event using device_event_log::AddEntry on | 132 // a log string and adds the event using device_event_log::AddEntry on |
134 // destruction. | 133 // destruction. |
135 class CHROMEOS_EXPORT DeviceEventLogInstance { | 134 class DEVICE_EVENT_LOG_EXPORT DeviceEventLogInstance { |
136 public: | 135 public: |
137 DeviceEventLogInstance(const char* file, | 136 DeviceEventLogInstance(const char* file, |
138 int line, | 137 int line, |
139 device_event_log::LogType type, | 138 device_event_log::LogType type, |
140 device_event_log::LogLevel level); | 139 device_event_log::LogLevel level); |
141 ~DeviceEventLogInstance(); | 140 ~DeviceEventLogInstance(); |
142 | 141 |
143 std::ostream& stream() { return stream_; } | 142 std::ostream& stream() { return stream_; } |
144 | 143 |
145 private: | 144 private: |
146 const char* file_; | 145 const char* file_; |
147 const int line_; | 146 const int line_; |
148 device_event_log::LogType type_; | 147 device_event_log::LogType type_; |
149 device_event_log::LogLevel level_; | 148 device_event_log::LogLevel level_; |
150 std::ostringstream stream_; | 149 std::ostringstream stream_; |
151 | 150 |
152 DISALLOW_COPY_AND_ASSIGN(DeviceEventLogInstance); | 151 DISALLOW_COPY_AND_ASSIGN(DeviceEventLogInstance); |
153 }; | 152 }; |
154 | 153 |
155 // Implementation class for SCOPED_LOG_IF_SLOW macros. Tests the elapsed time on | 154 // Implementation class for SCOPED_LOG_IF_SLOW macros. Tests the elapsed time on |
156 // destruction and adds a Debug or Error log entry if it exceeds the | 155 // destruction and adds a Debug or Error log entry if it exceeds the |
157 // corresponding expected maximum elapsed time. | 156 // corresponding expected maximum elapsed time. |
158 class CHROMEOS_EXPORT ScopedDeviceLogIfSlow { | 157 class DEVICE_EVENT_LOG_EXPORT ScopedDeviceLogIfSlow { |
159 public: | 158 public: |
160 ScopedDeviceLogIfSlow(LogType type, | 159 ScopedDeviceLogIfSlow(LogType type, |
161 const char* file, | 160 const char* file, |
162 const std::string& name); | 161 const std::string& name); |
163 ~ScopedDeviceLogIfSlow(); | 162 ~ScopedDeviceLogIfSlow(); |
164 | 163 |
165 private: | 164 private: |
166 const char* file_; | 165 const char* file_; |
167 LogType type_; | 166 LogType type_; |
168 std::string name_; | 167 std::string name_; |
169 base::ElapsedTimer timer_; | 168 base::ElapsedTimer timer_; |
170 }; | 169 }; |
171 | 170 |
172 } // namespace internal | 171 } // namespace internal |
173 | 172 |
174 } // namespace device_event_log | 173 } // namespace device_event_log |
175 | 174 |
176 } // namespace chromeos | 175 #endif // DEVICE_EVENT_LOG_DEVICE_EVENT_LOG_H_ |
177 | |
178 #endif // CHROMEOS_DEVICE_EVENT_LOG_H_ | |
OLD | NEW |