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