OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_NETWORK_NETWORK_EVENT_LOG_H_ | 5 #ifndef CHROMEOS_NETWORK_NETWORK_EVENT_LOG_H_ |
6 #define CHROMEOS_NETWORK_NETWORK_EVENT_LOG_H_ | 6 #define CHROMEOS_NETWORK_NETWORK_EVENT_LOG_H_ |
7 | 7 |
8 #include <deque> | 8 #include <deque> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/stringprintf.h" | 12 #include "base/stringprintf.h" |
13 #include "base/time.h" | 13 #include "base/time.h" |
14 #include "chromeos/chromeos_export.h" | 14 #include "chromeos/chromeos_export.h" |
15 | 15 |
| 16 namespace base { |
| 17 class Value; |
| 18 } |
| 19 |
16 namespace chromeos { | 20 namespace chromeos { |
17 | 21 |
18 // Namespace for functions for logging network events. | 22 // Namespace for functions for logging network events. |
19 namespace network_event_log { | 23 namespace network_event_log { |
20 | 24 |
21 // Used to determine which order to output event entries in GetAsString. | 25 // Used to determine which order to output event entries in GetAsString. |
22 enum StringOrder { | 26 enum StringOrder { |
23 OLDEST_FIRST, | 27 OLDEST_FIRST, |
24 NEWEST_FIRST | 28 NEWEST_FIRST |
25 }; | 29 }; |
26 | 30 |
| 31 // Used to set the detail level for logging. |
| 32 enum LogLevel { |
| 33 LOG_LEVEL_ERROR = 0, |
| 34 LOG_LEVEL_EVENT = 1, |
| 35 LOG_LEVEL_DEBUG = 2 |
| 36 }; |
| 37 |
27 // Maximum number of event log entries, exported for testing. | 38 // Maximum number of event log entries, exported for testing. |
28 CHROMEOS_EXPORT extern const size_t kMaxNetworkEventLogEntries; | 39 CHROMEOS_EXPORT extern const size_t kMaxNetworkEventLogEntries; |
29 | 40 |
| 41 // Default log level. |
| 42 CHROMEOS_EXPORT extern const LogLevel kDefaultLogLevel; |
| 43 |
30 // Initializes / shuts down network event logging. Calling Initialize more than | 44 // Initializes / shuts down network event logging. Calling Initialize more than |
31 // once will reset the log. | 45 // once will reset the log. |
32 CHROMEOS_EXPORT void Initialize(); | 46 CHROMEOS_EXPORT void Initialize(); |
33 CHROMEOS_EXPORT void Shutdown(); | 47 CHROMEOS_EXPORT void Shutdown(); |
34 | 48 |
35 // Returns true if network event logging has been initialized. | 49 // Returns true if network event logging has been initialized. |
36 CHROMEOS_EXPORT bool IsInitialized(); | 50 CHROMEOS_EXPORT bool IsInitialized(); |
37 | 51 |
38 // Adds an entry to the event log. A maximum number of events are recorded | 52 namespace internal { |
39 // after which new events replace old ones. Does nothing unless Initialize() | 53 |
40 // has been called. | 54 // Adds an entry to the event log at level specified by |log_level|. |
41 CHROMEOS_EXPORT void AddEntry(const std::string& module, | 55 // A maximum number of events are recorded after which new events replace |
| 56 // old ones. Does nothing unless Initialize() has been called. |
| 57 // NOTE: Generally use NET_LOG instead. |
| 58 CHROMEOS_EXPORT void AddEntry(const char* file, |
| 59 int file_line, |
| 60 LogLevel log_level, |
42 const std::string& event, | 61 const std::string& event, |
43 const std::string& description); | 62 const std::string& description); |
44 | 63 |
45 // Outputs the log to a formatted string. |order| determines which order to | 64 } // namespace internal |
46 // output the events. If |max_events| > 0, limits how many events are output. | |
47 CHROMEOS_EXPORT std::string GetAsString(StringOrder order, size_t max_events); | |
48 | 65 |
49 // Macros to make logging format more consistent. | 66 // Outputs the log to a formatted string. |
50 #define NET_LOG(module, message) \ | 67 // |order| determines which order to output the events. |
51 ::chromeos::network_event_log::AddEntry( \ | 68 // |format| is a string that determines which elements to show. Elements |
52 module, \ | 69 // must be comma-separated, e.g. "time,desc". |
53 std::string(__FILE__) + ":" + ::base::StringPrintf("%d",__LINE__) + \ | 70 // Note: order of the format strings does not affect the output. |
54 " (" + std::string(__func__) + ")", \ | 71 // "time" - Include a timestamp. |
55 message) | 72 // "file" - Include file and line number. |
| 73 // "desc" - Include the description. |
| 74 // "html" - Include html tags. |
| 75 // Only events with |log_level| <= |max_level| are included in the output. |
| 76 // If |max_events| > 0, limits how many events are output. |
| 77 CHROMEOS_EXPORT std::string GetAsString(StringOrder order, |
| 78 const std::string& format, |
| 79 LogLevel max_level, |
| 80 size_t max_events); |
56 | 81 |
57 #define NET_LOG_WARNING(module, message) \ | 82 // Helper function for displaying a value as a string. |
58 NET_LOG(module, std::string("WARNING:") + message) | 83 CHROMEOS_EXPORT std::string ValueAsString(const base::Value& value); |
59 #define NET_LOG_ERROR(module, message) \ | 84 |
60 NET_LOG(module, std::string("ERROR:") + message) | 85 // Errors |
| 86 #define NET_LOG_ERROR(event, desc) NET_LOG_LEVEL( \ |
| 87 ::chromeos::network_event_log::LOG_LEVEL_ERROR, event, desc) |
| 88 |
| 89 // Important events, e.g. connection request |
| 90 #define NET_LOG_EVENT(event, desc) NET_LOG_LEVEL( \ |
| 91 ::chromeos::network_event_log::LOG_LEVEL_EVENT, event, desc) |
| 92 |
| 93 // Non-essential debugging events |
| 94 #define NET_LOG_DEBUG(event, desc) NET_LOG_LEVEL( \ |
| 95 ::chromeos::network_event_log::LOG_LEVEL_DEBUG, event, desc) |
| 96 |
| 97 // Macro to include file and line number info in the event log. |
| 98 #define NET_LOG_LEVEL(log_level, event, description) \ |
| 99 ::chromeos::network_event_log::internal::AddEntry( \ |
| 100 __FILE__, __LINE__, log_level, event, description) |
61 | 101 |
62 } // namespace network_event_log | 102 } // namespace network_event_log |
63 | 103 |
64 } // namespace chromeos | 104 } // namespace chromeos |
65 | 105 |
66 #endif // CHROMEOS_NETWORK_NETWORK_EVENT_LOG_H_ | 106 #endif // CHROMEOS_NETWORK_NETWORK_EVENT_LOG_H_ |
OLD | NEW |