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/logging.h" |
13 #include "base/timer/elapsed_timer.h" | 13 #include "base/timer/elapsed_timer.h" |
14 #include "components/device_event_log/device_event_log_export.h" | |
15 | 14 |
16 // These macros can be used to log device related events. | 15 // These macros can be used to log device related events. |
17 // The following values should be used for |level| in these macros: | 16 // The following values should be used for |level| in these macros: |
18 // ERROR Unexpected events, or device level failures. Use sparingly. | 17 // ERROR Unexpected events, or device level failures. Use sparingly. |
19 // USER Events initiated directly by a user (or Chrome) action. | 18 // USER Events initiated directly by a user (or Chrome) action. |
20 // EVENT Default event type. | 19 // EVENT Default event type. |
21 // DEBUG Debugging details that are usually not interesting. | 20 // DEBUG Debugging details that are usually not interesting. |
22 // Examples: | 21 // Examples: |
23 // NET_LOG(EVENT) << "NetworkState Changed " << name << ": " << state; | 22 // NET_LOG(EVENT) << "NetworkState Changed " << name << ": " << state; |
24 // POWER_LOG(USER) << "Suspend requested"; | 23 // POWER_LOG(USER) << "Suspend requested"; |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 LOG_LEVEL_USER = 1, | 90 LOG_LEVEL_USER = 1, |
92 LOG_LEVEL_EVENT = 2, | 91 LOG_LEVEL_EVENT = 2, |
93 LOG_LEVEL_DEBUG = 3 | 92 LOG_LEVEL_DEBUG = 3 |
94 }; | 93 }; |
95 | 94 |
96 // Used to specify which order to output event entries in GetAsString. | 95 // Used to specify which order to output event entries in GetAsString. |
97 enum StringOrder { OLDEST_FIRST, NEWEST_FIRST }; | 96 enum StringOrder { OLDEST_FIRST, NEWEST_FIRST }; |
98 | 97 |
99 // Initializes / shuts down device event logging. If |max_entries| = 0 the | 98 // Initializes / shuts down device event logging. If |max_entries| = 0 the |
100 // default value will be used. | 99 // default value will be used. |
101 DEVICE_EVENT_LOG_EXPORT void Initialize(size_t max_entries); | 100 void Initialize(size_t max_entries); |
102 DEVICE_EVENT_LOG_EXPORT void Shutdown(); | 101 void Shutdown(); |
103 | 102 |
104 // If the global instance is initialized, adds an entry to it. Regardless of | 103 // If the global instance is initialized, adds an entry to it. Regardless of |
105 // whether the global instance was intitialzed, this logs the event to | 104 // whether the global instance was intitialzed, this logs the event to |
106 // LOG(ERROR) if |type| = ERROR or VLOG(1) otherwise. | 105 // LOG(ERROR) if |type| = ERROR or VLOG(1) otherwise. |
107 DEVICE_EVENT_LOG_EXPORT void AddEntry(const char* file, | 106 void AddEntry(const char* file, |
108 int line, | 107 int line, |
109 LogType type, | 108 LogType type, |
110 LogLevel level, | 109 LogLevel level, |
111 const std::string& event); | 110 const std::string& event); |
112 | 111 |
113 // For backwards compatibility with network_event_log. Combines |event| and | 112 // For backwards compatibility with network_event_log. Combines |event| and |
114 // |description| and calls AddEntry(). | 113 // |description| and calls AddEntry(). |
115 DEVICE_EVENT_LOG_EXPORT void AddEntryWithDescription( | 114 void AddEntryWithDescription( |
116 const char* file, | 115 const char* file, |
117 int line, | 116 int line, |
118 LogType type, | 117 LogType type, |
119 LogLevel level, | 118 LogLevel level, |
120 const std::string& event, | 119 const std::string& event, |
121 const std::string& description); | 120 const std::string& description); |
122 | 121 |
123 // Outputs the log to a formatted string. | 122 // Outputs the log to a formatted string. |
124 // |order| determines which order to output the events. | 123 // |order| determines which order to output the events. |
125 // |format| is a comma-separated string that determines which elements to show. | 124 // |format| is a comma-separated string that determines which elements to show. |
126 // e.g. "time,desc". Note: order of the strings does not affect the output. | 125 // e.g. "time,desc". Note: order of the strings does not affect the output. |
127 // "time" - Include a timestamp. | 126 // "time" - Include a timestamp. |
128 // "file" - Include file and line number. | 127 // "file" - Include file and line number. |
129 // "type" - Include the event type. | 128 // "type" - Include the event type. |
130 // "html" - Include html tags. | 129 // "html" - Include html tags. |
131 // "json" - Return JSON format dictionaries containing entries for timestamp, | 130 // "json" - Return JSON format dictionaries containing entries for timestamp, |
132 // level, type, file, and event. | 131 // level, type, file, and event. |
133 // |types| lists the types included in the output. Prepend "non-" to disclude | 132 // |types| lists the types included in the output. Prepend "non-" to disclude |
134 // a type. e.g. "network,login" or "non-network". Use an empty string for | 133 // a type. e.g. "network,login" or "non-network". Use an empty string for |
135 // all types. | 134 // all types. |
136 // |max_level| determines the maximum log level to be included in the output. | 135 // |max_level| determines the maximum log level to be included in the output. |
137 // |max_events| limits how many events are output if > 0, otherwise all events | 136 // |max_events| limits how many events are output if > 0, otherwise all events |
138 // are included. | 137 // are included. |
139 DEVICE_EVENT_LOG_EXPORT std::string GetAsString(StringOrder order, | 138 std::string GetAsString(StringOrder order, |
140 const std::string& format, | 139 const std::string& format, |
141 const std::string& types, | 140 const std::string& types, |
142 LogLevel max_level, | 141 LogLevel max_level, |
143 size_t max_events); | 142 size_t max_events); |
144 | 143 |
145 DEVICE_EVENT_LOG_EXPORT extern const LogLevel kDefaultLogLevel; | 144 extern const LogLevel kDefaultLogLevel; |
146 | 145 |
147 namespace internal { | 146 namespace internal { |
148 | 147 |
149 // Implementation class for DEVICE_LOG macros. Provides a stream for creating | 148 // Implementation class for DEVICE_LOG macros. Provides a stream for creating |
150 // a log string and adds the event using device_event_log::AddEntry on | 149 // a log string and adds the event using device_event_log::AddEntry on |
151 // destruction. | 150 // destruction. |
152 class DEVICE_EVENT_LOG_EXPORT DeviceEventLogInstance { | 151 class DeviceEventLogInstance { |
153 public: | 152 public: |
154 DeviceEventLogInstance(const char* file, | 153 DeviceEventLogInstance(const char* file, |
155 int line, | 154 int line, |
156 device_event_log::LogType type, | 155 device_event_log::LogType type, |
157 device_event_log::LogLevel level); | 156 device_event_log::LogLevel level); |
158 ~DeviceEventLogInstance(); | 157 ~DeviceEventLogInstance(); |
159 | 158 |
160 std::ostream& stream() { return stream_; } | 159 std::ostream& stream() { return stream_; } |
161 | 160 |
162 private: | 161 private: |
163 const char* file_; | 162 const char* file_; |
164 const int line_; | 163 const int line_; |
165 device_event_log::LogType type_; | 164 device_event_log::LogType type_; |
166 device_event_log::LogLevel level_; | 165 device_event_log::LogLevel level_; |
167 std::ostringstream stream_; | 166 std::ostringstream stream_; |
168 | 167 |
169 DISALLOW_COPY_AND_ASSIGN(DeviceEventLogInstance); | 168 DISALLOW_COPY_AND_ASSIGN(DeviceEventLogInstance); |
170 }; | 169 }; |
171 | 170 |
172 // Implementation class for DEVICE_PLOG macros. Provides a stream for creating | 171 // Implementation class for DEVICE_PLOG macros. Provides a stream for creating |
173 // a log string and adds the event, including system error code, using | 172 // a log string and adds the event, including system error code, using |
174 // device_event_log::AddEntry on destruction. | 173 // device_event_log::AddEntry on destruction. |
175 class DEVICE_EVENT_LOG_EXPORT DeviceEventSystemErrorLogInstance { | 174 class DeviceEventSystemErrorLogInstance { |
176 public: | 175 public: |
177 DeviceEventSystemErrorLogInstance(const char* file, | 176 DeviceEventSystemErrorLogInstance(const char* file, |
178 int line, | 177 int line, |
179 device_event_log::LogType type, | 178 device_event_log::LogType type, |
180 device_event_log::LogLevel level, | 179 device_event_log::LogLevel level, |
181 logging::SystemErrorCode err); | 180 logging::SystemErrorCode err); |
182 ~DeviceEventSystemErrorLogInstance(); | 181 ~DeviceEventSystemErrorLogInstance(); |
183 | 182 |
184 std::ostream& stream() { return log_instance_.stream(); } | 183 std::ostream& stream() { return log_instance_.stream(); } |
185 | 184 |
186 private: | 185 private: |
187 logging::SystemErrorCode err_; | 186 logging::SystemErrorCode err_; |
188 // Constructor parameters are passed to |log_instance_| which will update the | 187 // 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 | 188 // log when it is destroyed (after a string description of |err_| is appended |
190 // to the stream). | 189 // to the stream). |
191 DeviceEventLogInstance log_instance_; | 190 DeviceEventLogInstance log_instance_; |
192 | 191 |
193 DISALLOW_COPY_AND_ASSIGN(DeviceEventSystemErrorLogInstance); | 192 DISALLOW_COPY_AND_ASSIGN(DeviceEventSystemErrorLogInstance); |
194 }; | 193 }; |
195 | 194 |
196 // Implementation class for SCOPED_LOG_IF_SLOW macros. Tests the elapsed time on | 195 // Implementation class for SCOPED_LOG_IF_SLOW macros. Tests the elapsed time on |
197 // destruction and adds a Debug or Error log entry if it exceeds the | 196 // destruction and adds a Debug or Error log entry if it exceeds the |
198 // corresponding expected maximum elapsed time. | 197 // corresponding expected maximum elapsed time. |
199 class DEVICE_EVENT_LOG_EXPORT ScopedDeviceLogIfSlow { | 198 class ScopedDeviceLogIfSlow { |
200 public: | 199 public: |
201 ScopedDeviceLogIfSlow(LogType type, | 200 ScopedDeviceLogIfSlow(LogType type, |
202 const char* file, | 201 const char* file, |
203 const std::string& name); | 202 const std::string& name); |
204 ~ScopedDeviceLogIfSlow(); | 203 ~ScopedDeviceLogIfSlow(); |
205 | 204 |
206 private: | 205 private: |
207 const char* file_; | 206 const char* file_; |
208 LogType type_; | 207 LogType type_; |
209 std::string name_; | 208 std::string name_; |
210 base::ElapsedTimer timer_; | 209 base::ElapsedTimer timer_; |
211 }; | 210 }; |
212 | 211 |
213 } // namespace internal | 212 } // namespace internal |
214 | 213 |
215 } // namespace device_event_log | 214 } // namespace device_event_log |
216 | 215 |
217 #endif // DEVICE_EVENT_LOG_DEVICE_EVENT_LOG_H_ | 216 #endif // DEVICE_EVENT_LOG_DEVICE_EVENT_LOG_H_ |
OLD | NEW |