scope_logger.h revision 1a212a6b98b22ad1d69652bb26a9e94138635476
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> 10#include <vector> 11 12#include <base/lazy_instance.h> 13#include <base/macros.h> 14#include <gtest/gtest_prod.h> 15 16#include "shill/callbacks.h" 17 18namespace shill { 19 20// A class that enables logging based on scope and verbose level. It is not 21// intended to be used directly but via the SLOG() macros in shill/logging.h 22class ScopeLogger { 23 public: 24 // Logging scopes. 25 // 26 // Update kScopeNames in scope_logger.cc after changing this enumerated type. 27 // These scope identifiers are sorted by their scope names alphabetically. 28 enum Scope { 29 kCellular = 0, 30 kConnection, 31 kCrypto, 32 kDaemon, 33 kDBus, 34 kDevice, 35 kDHCP, 36 kDNS, 37 kEthernet, 38 kHTTP, 39 kHTTPProxy, 40 kInet, 41 kLink, 42 kManager, 43 kMetrics, 44 kModem, 45 kPortal, 46 kPower, 47 kPPP, 48 kPPPoE, 49 kProfile, 50 kProperty, 51 kResolver, 52 kRoute, 53 kRTNL, 54 kService, 55 kStorage, 56 kTask, 57 kVPN, 58 kWiFi, 59 kWiMax, 60 kNumScopes 61 }; 62 63 typedef base::Callback<void(bool)> ScopeEnableChangedCallback; 64 typedef std::vector<ScopeEnableChangedCallback>ScopeEnableChangedCallbacks; 65 66 // Returns a singleton of this class. 67 static ScopeLogger* GetInstance(); 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 75 // Returns true if logging is enabled for |scope| at any verbosity level. 76 bool IsScopeEnabled(Scope scope) const; 77 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. 101 void EnableScopesByName(const std::string& expression); 102 103 // Register for log scope enable/disable state changes for |scope|. 104 void RegisterScopeEnableChangedCallback( 105 Scope scope, ScopeEnableChangedCallback callback); 106 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 130 // Hooks to notify interested parties of changes to log scopes. 131 ScopeEnableChangedCallbacks log_scope_callbacks_[kNumScopes]; 132 133 DISALLOW_IMPLICIT_CONSTRUCTORS(ScopeLogger); 134}; 135 136} // namespace shill 137 138#endif // SHILL_SCOPE_LOGGER_H_ 139