blob: f415bcebda0f601090cc4d0bc432396feff9a29b [file] [log] [blame]
Ben Chanbc49ac72012-04-10 19:59:45 -07001// 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 Stewart5581d072012-12-17 17:30:20 -08009#include <vector>
Ben Chanbc49ac72012-04-10 19:59:45 -070010#include <string>
11
12#include <base/basictypes.h>
13#include <base/lazy_instance.h>
Ben Chanbc49ac72012-04-10 19:59:45 -070014#include <gtest/gtest_prod.h>
15
Paul Stewart5581d072012-12-17 17:30:20 -080016#include "shill/callbacks.h"
17
Ben Chanbc49ac72012-04-10 19:59:45 -070018namespace shill {
19
20// A class that enables logging based on scope and verbose level. It is not
Christopher Wileyb691efd2012-08-09 13:51:51 -070021// intended to be used directly but via the SLOG() macros in shill/logging.h
Ben Chanbc49ac72012-04-10 19:59:45 -070022class ScopeLogger {
23 public:
24 // Logging scopes.
25 //
26 // Update kScopeNames in scope_logger.cc after changing this enumerated type.
Ben Chanfad4a0b2012-04-18 15:49:59 -070027 // These scope identifiers are sorted by their scope names alphabetically.
Ben Chanbc49ac72012-04-10 19:59:45 -070028 enum Scope {
29 kCellular = 0,
30 kConnection,
31 kCrypto,
Ben Chanfad4a0b2012-04-18 15:49:59 -070032 kDaemon,
Ben Chanbc49ac72012-04-10 19:59:45 -070033 kDBus,
34 kDevice,
Ben Chanfad4a0b2012-04-18 15:49:59 -070035 kDHCP,
36 kDNS,
Ben Chanbc49ac72012-04-10 19:59:45 -070037 kEthernet,
Ben Chanfad4a0b2012-04-18 15:49:59 -070038 kHTTP,
39 kHTTPProxy,
Ben Chanbc49ac72012-04-10 19:59:45 -070040 kInet,
Paul Stewart6c72c972012-07-27 11:29:20 -070041 kLink,
Ben Chanbc49ac72012-04-10 19:59:45 -070042 kManager,
43 kMetrics,
44 kModem,
45 kPortal,
Ben Chanfad4a0b2012-04-18 15:49:59 -070046 kPower,
mukesh agrawal9da07772013-05-15 14:15:17 -070047 kPPP,
Ben Chanbc49ac72012-04-10 19:59:45 -070048 kProfile,
Ben Chanfad4a0b2012-04-18 15:49:59 -070049 kProperty,
50 kResolver,
51 kRoute,
Ben Chanbc49ac72012-04-10 19:59:45 -070052 kRTNL,
53 kService,
54 kStorage,
55 kTask,
56 kVPN,
57 kWiFi,
Darin Petkov096b3472012-05-15 10:26:22 +020058 kWiMax,
Ben Chanbc49ac72012-04-10 19:59:45 -070059 kNumScopes
60 };
61
Paul Stewart5581d072012-12-17 17:30:20 -080062 typedef base::Callback<void(bool)> ScopeEnableChangedCallback;
63 typedef std::vector<ScopeEnableChangedCallback>ScopeEnableChangedCallbacks;
64
Ben Chanbc49ac72012-04-10 19:59:45 -070065 // Returns a singleton of this class.
66 static ScopeLogger *GetInstance();
67
68 ~ScopeLogger();
69
70 // Returns true if logging is enabled for |scope| and |verbose_level|, i.e.
71 // scope_enable_[|scope|] is true and |verbose_level| <= |verbose_level_|
72 bool IsLogEnabled(Scope scope, int verbose_level) const;
73
Paul Stewart5581d072012-12-17 17:30:20 -080074 // Returns true if logging is enabled for |scope| at any verbosity level.
75 bool IsScopeEnabled(Scope scope) const;
76
Ben Chanbc49ac72012-04-10 19:59:45 -070077 // Returns a string comprising the names, separated by commas, of all scopes.
78 std::string GetAllScopeNames() const;
79
80 // Returns a string comprising the names, separated by plus signs, of all
81 // scopes that are enabled for logging.
82 std::string GetEnabledScopeNames() const;
83
84 // Enables/disables scopes as specified by |expression|.
85 //
86 // |expression| is a string comprising a sequence of scope names, each
87 // prefixed by a plus '+' or minus '-' sign. A scope prefixed by a plus
88 // sign is enabled for logging, whereas a scope prefixed by a minus sign
89 // is disabled for logging. Scopes that are not mentioned in |expression|
90 // remain the same state.
91 //
92 // To allow resetting the state of all scopes, an exception is made for the
93 // first scope name in the sequence, which may not be prefixed by any sign.
94 // That is considered as an implicit plus sign for that scope and also
95 // indicates that all scopes are first disabled before enabled by
96 // |expression|.
97 //
98 // If |expression| is an empty string, all scopes are disabled. Any unknown
99 // scope name found in |expression| is ignored.
100 void EnableScopesByName(const std::string &expression);
101
Paul Stewart5581d072012-12-17 17:30:20 -0800102 // Register for log scope enable/disable state changes for |scope|.
103 void RegisterScopeEnableChangedCallback(
104 Scope scope, ScopeEnableChangedCallback callback);
105
Ben Chanbc49ac72012-04-10 19:59:45 -0700106 // Sets the verbose level for all scopes to |verbose_level|.
107 void set_verbose_level(int verbose_level) { verbose_level_ = verbose_level; }
108
109 private:
110 // Required for constructing LazyInstance<ScopeLogger>.
111 friend struct base::DefaultLazyInstanceTraits<ScopeLogger>;
112 friend class ScopeLoggerTest;
113 FRIEND_TEST(ScopeLoggerTest, GetEnabledScopeNames);
114 FRIEND_TEST(ScopeLoggerTest, SetScopeEnabled);
115 FRIEND_TEST(ScopeLoggerTest, SetVerboseLevel);
116
117 // Disables logging for all scopes.
118 void DisableAllScopes();
119
120 // Enables or disables logging for |scope|.
121 void SetScopeEnabled(Scope scope, bool enabled);
122
123 // Boolean values to indicate whether logging is enabled for each scope.
124 std::bitset<kNumScopes> scope_enabled_;
125
126 // Verbose level that is applied to all scopes.
127 int verbose_level_;
128
Paul Stewart5581d072012-12-17 17:30:20 -0800129 // Hooks to notify interested parties of changes to log scopes.
130 ScopeEnableChangedCallbacks log_scope_callbacks_[kNumScopes];
131
Ben Chanbc49ac72012-04-10 19:59:45 -0700132 DISALLOW_IMPLICIT_CONSTRUCTORS(ScopeLogger);
133};
134
135} // namespace shill
136
137#endif // SHILL_SCOPE_LOGGER_H_