Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | |
|
Ben Chan
2014/12/02 21:42:53
drop (c)
http://www.chromium.org/developers/codin
stevenjb
2014/12/03 00:16:04
Done.
| |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CHROMEOS_DEVICE_EVENT_LOG_H_ | |
| 6 #define CHROMEOS_DEVICE_EVENT_LOG_H_ | |
| 7 | |
| 8 #include <cstring> | |
| 9 #include <sstream> | |
| 10 | |
| 11 #include "base/basictypes.h" | |
| 12 #include "chromeos/chromeos_export.h" | |
| 13 | |
| 14 namespace chromeos { | |
| 15 | |
| 16 // This macro can be used to log chromeos device related events. | |
| 17 // The following values should be used for |level| in these macros: | |
| 18 // ERROR An unexpected event, or a low level failure. Use sparingly. | |
| 19 // USER An event that was initiated directly by a user (or Chrome) action. | |
| 20 // EVENT A default event type. | |
| 21 // DEBUG Useful debugging details that may not always be interesting. | |
| 22 // Examples: | |
| 23 // NET_LOG(EVENT) << "NetworkState Changed " << name << ": " << state; | |
| 24 // POWER_LOG(USER) << "Suspend requested"; | |
| 25 | |
| 26 #define NET_LOG(level) \ | |
| 27 DEVICE_LOG(::chromeos::device_event_log::LOG_TYPE_NETWORK, \ | |
| 28 ::chromeos::device_event_log::LOG_LEVEL_##level) | |
| 29 #define POWER_LOG(level) \ | |
| 30 DEVICE_LOG(::chromeos::device_event_log::LOG_TYPE_POWER, \ | |
| 31 ::chromeos::device_event_log::LOG_LEVEL_##level) | |
| 32 | |
| 33 #define DEVICE_LOG(type, level) \ | |
|
Ben Chan
2014/12/02 21:42:53
nit: perhaps default DEVICE_LOG first
stevenjb
2014/12/03 00:16:03
I put this last since usually one of the above wil
| |
| 34 ::chromeos::device_event_log::internal::DeviceEventLogInstance( \ | |
| 35 __FILE__, __LINE__, type, level).stream() | |
| 36 | |
| 37 namespace device_event_log { | |
| 38 | |
| 39 // Used to specify the type of event. | |
| 40 enum LogType { | |
| 41 LOG_TYPE_NETWORK, | |
| 42 LOG_TYPE_POWER, | |
| 43 LOG_TYPE_NON_NETWORK, // Match all non NETWORK log types | |
|
Ben Chan
2014/12/02 21:42:53
why is NETWORK log a special case? do we expect t
stevenjb
2014/12/03 00:16:03
I'll add a comment. Currently there is much more n
| |
| 44 LOG_TYPE_ALL, // Match all log types | |
| 45 }; | |
| 46 | |
| 47 // Used to specify the detail level for logging. In GetAsString, used to | |
| 48 // specify the maximum detail level (i.e. EVENT will include USER and ERROR). | |
| 49 enum LogLevel { | |
| 50 LOG_LEVEL_ERROR = 0, | |
| 51 LOG_LEVEL_USER = 1, | |
| 52 LOG_LEVEL_EVENT = 2, | |
|
Daniel Erat
2014/12/02 20:43:16
please add comments describing how to use these le
stevenjb
2014/12/02 21:14:38
I put comments above, I will refer to that here.
Daniel Erat
2014/12/02 21:20:39
ah, thanks. i scanned a bit above but missed that
| |
| 53 LOG_LEVEL_DEBUG = 3 | |
| 54 }; | |
| 55 | |
| 56 // Used to specify which order to output event entries in GetAsString. | |
| 57 enum StringOrder { OLDEST_FIRST, NEWEST_FIRST }; | |
| 58 | |
| 59 // Initializes / shuts down device event logging. Calling Initialize more than | |
| 60 // once will reset the log. If |max_entries| = 0 the default value will be used. | |
| 61 CHROMEOS_EXPORT void Initialize(size_t max_entries); | |
| 62 CHROMEOS_EXPORT void Shutdown(); | |
| 63 | |
| 64 // Returns true if device event logging has been initialized. | |
| 65 CHROMEOS_EXPORT bool IsInitialized(); | |
| 66 | |
| 67 // Adds an entry to the global instance if initialized. Also always logs the | |
|
Ben Chan
2014/12/02 21:42:53
nit: perhaps describe what happens if the instance
stevenjb
2014/12/03 00:16:04
I thought I did. I will rephrase this.
| |
| 68 // event to LOG(ERROR) or |type| = ERROR or VLOG(1) otherwise. | |
| 69 CHROMEOS_EXPORT void AddEntry(const char* file, | |
| 70 int line, | |
| 71 LogType type, | |
| 72 LogLevel level, | |
| 73 const std::string& event); | |
| 74 | |
| 75 // For backwards compatibility with network_event_log. Combines |event| and | |
| 76 // |description| and calls AddEntry(). | |
|
Ben Chan
2014/12/02 21:42:53
will this function be deprecated after the migrati
stevenjb
2014/12/03 00:16:04
Someday maybe? There are 352 instances of NET_LOG_
| |
| 77 CHROMEOS_EXPORT void AddEntryWithDescription(const char* file, | |
| 78 int line, | |
| 79 LogType type, | |
| 80 LogLevel level, | |
| 81 const std::string& event, | |
| 82 const std::string& description); | |
| 83 | |
| 84 // Outputs the log to a formatted string. | |
| 85 // |order| determines which order to output the events. | |
| 86 // |format| is a string that determines which elements to show. Elements | |
| 87 // must be comma-separated, e.g. "time,desc". | |
| 88 // Note: order of the format strings does not affect the output. | |
| 89 // "time" - Include a timestamp. | |
| 90 // "file" - Include file and line number. | |
| 91 // "type" - Include the event type. | |
| 92 // "html" - Include html tags. | |
| 93 // "json" - Return as JSON format | |
| 94 // Only events matching |log_type| are included in the output. | |
| 95 // Only events with |log_level| <= |max_level| are included in the output. | |
| 96 // If |max_events| > 0, limits how many events are output. | |
| 97 // If |json| is specified, returns a JSON list of dictionaries containing time, | |
| 98 // level, file, event, and description. | |
| 99 CHROMEOS_EXPORT std::string GetAsString(StringOrder order, | |
| 100 const std::string& format, | |
| 101 LogType log_type, | |
| 102 LogLevel max_level, | |
| 103 size_t max_events); | |
| 104 | |
| 105 CHROMEOS_EXPORT extern const LogLevel kDefaultLogLevel; | |
| 106 | |
| 107 namespace internal { | |
| 108 | |
| 109 class CHROMEOS_EXPORT DeviceEventLogInstance { | |
| 110 public: | |
| 111 DeviceEventLogInstance(const char* file, | |
| 112 int line, | |
| 113 device_event_log::LogType type, | |
| 114 device_event_log::LogLevel level); | |
| 115 ~DeviceEventLogInstance(); | |
| 116 | |
| 117 std::ostream& stream() { return stream_; } | |
| 118 | |
| 119 private: | |
| 120 const char* file_; | |
| 121 const int line_; | |
| 122 device_event_log::LogType type_; | |
| 123 device_event_log::LogLevel level_; | |
| 124 std::ostringstream stream_; | |
| 125 | |
| 126 DISALLOW_COPY_AND_ASSIGN(DeviceEventLogInstance); | |
| 127 }; | |
| 128 | |
| 129 } // namespace internal | |
| 130 | |
| 131 } // namespace device_event_log | |
| 132 | |
| 133 } // namespace chromeos | |
| 134 | |
| 135 #endif // CHROMEOS_DEVICE_EVENT_LOG_H_ | |
| OLD | NEW |