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> |
| 9 | #include <string> |
Alex Vakulenko | a41ab51 | 2014-07-23 14:24:23 -0700 | [diff] [blame] | 10 | #include <vector> |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 11 | |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 12 | #include <base/lazy_instance.h> |
Ben Chan | cc67c52 | 2014-09-03 07:19:18 -0700 | [diff] [blame] | 13 | #include <base/macros.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, |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 47 | kPPP, |
Garret Kelly | 2dc218e | 2015-01-30 11:16:34 -0500 | [diff] [blame] | 48 | kPPPoE, |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 49 | kProfile, |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 50 | kProperty, |
| 51 | kResolver, |
| 52 | kRoute, |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 53 | kRTNL, |
| 54 | kService, |
| 55 | kStorage, |
| 56 | kTask, |
| 57 | kVPN, |
| 58 | kWiFi, |
Darin Petkov | 096b347 | 2012-05-15 10:26:22 +0200 | [diff] [blame] | 59 | kWiMax, |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 60 | kNumScopes |
| 61 | }; |
| 62 | |
Paul Stewart | 5581d07 | 2012-12-17 17:30:20 -0800 | [diff] [blame] | 63 | typedef base::Callback<void(bool)> ScopeEnableChangedCallback; |
| 64 | typedef std::vector<ScopeEnableChangedCallback>ScopeEnableChangedCallbacks; |
| 65 | |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 66 | // Returns a singleton of this class. |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 67 | static ScopeLogger* GetInstance(); |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 68 | |
| 69 | ~ScopeLogger(); |
| 70 | |
| 71 | // Returns true if logging is enabled for |scope| and |verbose_level|, i.e. |
| 72 | // scope_enable_[|scope|] is true and |verbose_level| <= |verbose_level_| |
| 73 | bool IsLogEnabled(Scope scope, int verbose_level) const; |
| 74 | |
Paul Stewart | 5581d07 | 2012-12-17 17:30:20 -0800 | [diff] [blame] | 75 | // Returns true if logging is enabled for |scope| at any verbosity level. |
| 76 | bool IsScopeEnabled(Scope scope) const; |
| 77 | |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 78 | // Returns a string comprising the names, separated by commas, of all scopes. |
| 79 | std::string GetAllScopeNames() const; |
| 80 | |
| 81 | // Returns a string comprising the names, separated by plus signs, of all |
| 82 | // scopes that are enabled for logging. |
| 83 | std::string GetEnabledScopeNames() const; |
| 84 | |
| 85 | // Enables/disables scopes as specified by |expression|. |
| 86 | // |
| 87 | // |expression| is a string comprising a sequence of scope names, each |
| 88 | // prefixed by a plus '+' or minus '-' sign. A scope prefixed by a plus |
| 89 | // sign is enabled for logging, whereas a scope prefixed by a minus sign |
| 90 | // is disabled for logging. Scopes that are not mentioned in |expression| |
| 91 | // remain the same state. |
| 92 | // |
| 93 | // To allow resetting the state of all scopes, an exception is made for the |
| 94 | // first scope name in the sequence, which may not be prefixed by any sign. |
| 95 | // That is considered as an implicit plus sign for that scope and also |
| 96 | // indicates that all scopes are first disabled before enabled by |
| 97 | // |expression|. |
| 98 | // |
| 99 | // If |expression| is an empty string, all scopes are disabled. Any unknown |
| 100 | // scope name found in |expression| is ignored. |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 101 | void EnableScopesByName(const std::string& expression); |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 102 | |
Paul Stewart | 5581d07 | 2012-12-17 17:30:20 -0800 | [diff] [blame] | 103 | // Register for log scope enable/disable state changes for |scope|. |
| 104 | void RegisterScopeEnableChangedCallback( |
| 105 | Scope scope, ScopeEnableChangedCallback callback); |
| 106 | |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 107 | // Sets the verbose level for all scopes to |verbose_level|. |
| 108 | void set_verbose_level(int verbose_level) { verbose_level_ = verbose_level; } |
| 109 | |
| 110 | private: |
| 111 | // Required for constructing LazyInstance<ScopeLogger>. |
| 112 | friend struct base::DefaultLazyInstanceTraits<ScopeLogger>; |
| 113 | friend class ScopeLoggerTest; |
| 114 | FRIEND_TEST(ScopeLoggerTest, GetEnabledScopeNames); |
| 115 | FRIEND_TEST(ScopeLoggerTest, SetScopeEnabled); |
| 116 | FRIEND_TEST(ScopeLoggerTest, SetVerboseLevel); |
| 117 | |
| 118 | // Disables logging for all scopes. |
| 119 | void DisableAllScopes(); |
| 120 | |
| 121 | // Enables or disables logging for |scope|. |
| 122 | void SetScopeEnabled(Scope scope, bool enabled); |
| 123 | |
| 124 | // Boolean values to indicate whether logging is enabled for each scope. |
| 125 | std::bitset<kNumScopes> scope_enabled_; |
| 126 | |
| 127 | // Verbose level that is applied to all scopes. |
| 128 | int verbose_level_; |
| 129 | |
Paul Stewart | 5581d07 | 2012-12-17 17:30:20 -0800 | [diff] [blame] | 130 | // Hooks to notify interested parties of changes to log scopes. |
| 131 | ScopeEnableChangedCallbacks log_scope_callbacks_[kNumScopes]; |
| 132 | |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 133 | DISALLOW_IMPLICIT_CONSTRUCTORS(ScopeLogger); |
| 134 | }; |
| 135 | |
| 136 | } // namespace shill |
| 137 | |
| 138 | #endif // SHILL_SCOPE_LOGGER_H_ |