Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 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 SHILL_SCOPE_LOGGER_H_ |
| 6 | #define SHILL_SCOPE_LOGGER_H_ |
| 7 | |
| 8 | #include <bitset> |
Paul Stewart | 5581d07 | 2012-12-17 17:30:20 -0800 | [diff] [blame] | 9 | #include <vector> |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 10 | #include <string> |
| 11 | |
| 12 | #include <base/basictypes.h> |
| 13 | #include <base/lazy_instance.h> |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 14 | #include <gtest/gtest_prod.h> |
| 15 | |
Paul Stewart | 5581d07 | 2012-12-17 17:30:20 -0800 | [diff] [blame] | 16 | #include "shill/callbacks.h" |
| 17 | |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 18 | namespace shill { |
| 19 | |
| 20 | // A class that enables logging based on scope and verbose level. It is not |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 21 | // intended to be used directly but via the SLOG() macros in shill/logging.h |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 22 | class ScopeLogger { |
| 23 | public: |
| 24 | // Logging scopes. |
| 25 | // |
| 26 | // Update kScopeNames in scope_logger.cc after changing this enumerated type. |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 27 | // These scope identifiers are sorted by their scope names alphabetically. |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 28 | enum Scope { |
| 29 | kCellular = 0, |
| 30 | kConnection, |
| 31 | kCrypto, |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 32 | kDaemon, |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 33 | kDBus, |
| 34 | kDevice, |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 35 | kDHCP, |
| 36 | kDNS, |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 37 | kEthernet, |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 38 | kHTTP, |
| 39 | kHTTPProxy, |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 40 | kInet, |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 41 | kLink, |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 42 | kManager, |
| 43 | kMetrics, |
| 44 | kModem, |
| 45 | kPortal, |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 46 | kPower, |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 47 | kProfile, |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 48 | kProperty, |
| 49 | kResolver, |
| 50 | kRoute, |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 51 | kRTNL, |
| 52 | kService, |
| 53 | kStorage, |
| 54 | kTask, |
| 55 | kVPN, |
| 56 | kWiFi, |
Darin Petkov | 096b347 | 2012-05-15 10:26:22 +0200 | [diff] [blame] | 57 | kWiMax, |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 58 | kNumScopes |
| 59 | }; |
| 60 | |
Paul Stewart | 5581d07 | 2012-12-17 17:30:20 -0800 | [diff] [blame] | 61 | typedef base::Callback<void(bool)> ScopeEnableChangedCallback; |
| 62 | typedef std::vector<ScopeEnableChangedCallback>ScopeEnableChangedCallbacks; |
| 63 | |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 64 | // Returns a singleton of this class. |
| 65 | static ScopeLogger *GetInstance(); |
| 66 | |
| 67 | ~ScopeLogger(); |
| 68 | |
| 69 | // Returns true if logging is enabled for |scope| and |verbose_level|, i.e. |
| 70 | // scope_enable_[|scope|] is true and |verbose_level| <= |verbose_level_| |
| 71 | bool IsLogEnabled(Scope scope, int verbose_level) const; |
| 72 | |
Paul Stewart | 5581d07 | 2012-12-17 17:30:20 -0800 | [diff] [blame] | 73 | // Returns true if logging is enabled for |scope| at any verbosity level. |
| 74 | bool IsScopeEnabled(Scope scope) const; |
| 75 | |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 76 | // Returns a string comprising the names, separated by commas, of all scopes. |
| 77 | std::string GetAllScopeNames() const; |
| 78 | |
| 79 | // Returns a string comprising the names, separated by plus signs, of all |
| 80 | // scopes that are enabled for logging. |
| 81 | std::string GetEnabledScopeNames() const; |
| 82 | |
| 83 | // Enables/disables scopes as specified by |expression|. |
| 84 | // |
| 85 | // |expression| is a string comprising a sequence of scope names, each |
| 86 | // prefixed by a plus '+' or minus '-' sign. A scope prefixed by a plus |
| 87 | // sign is enabled for logging, whereas a scope prefixed by a minus sign |
| 88 | // is disabled for logging. Scopes that are not mentioned in |expression| |
| 89 | // remain the same state. |
| 90 | // |
| 91 | // To allow resetting the state of all scopes, an exception is made for the |
| 92 | // first scope name in the sequence, which may not be prefixed by any sign. |
| 93 | // That is considered as an implicit plus sign for that scope and also |
| 94 | // indicates that all scopes are first disabled before enabled by |
| 95 | // |expression|. |
| 96 | // |
| 97 | // If |expression| is an empty string, all scopes are disabled. Any unknown |
| 98 | // scope name found in |expression| is ignored. |
| 99 | void EnableScopesByName(const std::string &expression); |
| 100 | |
Paul Stewart | 5581d07 | 2012-12-17 17:30:20 -0800 | [diff] [blame] | 101 | // Register for log scope enable/disable state changes for |scope|. |
| 102 | void RegisterScopeEnableChangedCallback( |
| 103 | Scope scope, ScopeEnableChangedCallback callback); |
| 104 | |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 105 | // Sets the verbose level for all scopes to |verbose_level|. |
| 106 | void set_verbose_level(int verbose_level) { verbose_level_ = verbose_level; } |
| 107 | |
| 108 | private: |
| 109 | // Required for constructing LazyInstance<ScopeLogger>. |
| 110 | friend struct base::DefaultLazyInstanceTraits<ScopeLogger>; |
| 111 | friend class ScopeLoggerTest; |
| 112 | FRIEND_TEST(ScopeLoggerTest, GetEnabledScopeNames); |
| 113 | FRIEND_TEST(ScopeLoggerTest, SetScopeEnabled); |
| 114 | FRIEND_TEST(ScopeLoggerTest, SetVerboseLevel); |
| 115 | |
| 116 | // Disables logging for all scopes. |
| 117 | void DisableAllScopes(); |
| 118 | |
| 119 | // Enables or disables logging for |scope|. |
| 120 | void SetScopeEnabled(Scope scope, bool enabled); |
| 121 | |
| 122 | // Boolean values to indicate whether logging is enabled for each scope. |
| 123 | std::bitset<kNumScopes> scope_enabled_; |
| 124 | |
| 125 | // Verbose level that is applied to all scopes. |
| 126 | int verbose_level_; |
| 127 | |
Paul Stewart | 5581d07 | 2012-12-17 17:30:20 -0800 | [diff] [blame] | 128 | // Hooks to notify interested parties of changes to log scopes. |
| 129 | ScopeEnableChangedCallbacks log_scope_callbacks_[kNumScopes]; |
| 130 | |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 131 | DISALLOW_IMPLICIT_CONSTRUCTORS(ScopeLogger); |
| 132 | }; |
| 133 | |
| 134 | } // namespace shill |
| 135 | |
| 136 | #endif // SHILL_SCOPE_LOGGER_H_ |