proxygen
LoggerDB.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 
18 #include <folly/Conv.h>
19 #include <folly/CppAttributes.h>
20 #include <folly/Range.h>
21 #include <folly/Synchronized.h>
22 #include <memory>
23 #include <string>
24 #include <unordered_map>
25 #include <vector>
26 
27 #include <folly/logging/LogName.h>
28 
29 namespace folly {
30 
31 class LogCategory;
32 class LogConfig;
33 class LogHandler;
34 class LogHandlerFactory;
35 enum class LogLevel : uint32_t;
36 
40 class LoggerDB {
41  public:
45  static LoggerDB& get();
46 
47  ~LoggerDB();
48 
55  LogCategory* getCategory(folly::StringPiece name);
56 
63  LogCategory* FOLLY_NULLABLE getCategoryOrNull(folly::StringPiece name);
64 
77  void setLevel(folly::StringPiece name, LogLevel level, bool inherit = true);
78  void setLevel(LogCategory* category, LogLevel level, bool inherit = true);
79 
83  LogConfig getConfig() const;
84 
92  LogConfig getFullConfig() const;
93 
101  void updateConfig(const LogConfig& config);
102 
115  void resetConfig(const LogConfig& config);
116 
122  void cleanupHandlers();
123 
130  size_t flushAllHandlers();
131 
147  void registerHandlerFactory(
148  std::unique_ptr<LogHandlerFactory> factory,
149  bool replaceExisting = false);
150 
159  void unregisterHandlerFactory(folly::StringPiece type);
160 
167  LogLevel xlogInit(
168  folly::StringPiece categoryName,
169  std::atomic<LogLevel>* xlogCategoryLevel,
170  LogCategory** xlogCategory);
171  LogCategory* xlogInitCategory(
172  folly::StringPiece categoryName,
173  LogCategory** xlogCategory,
174  std::atomic<bool>* isInitialized);
175 
176  enum TestConstructorArg { TESTING };
177 
187  explicit LoggerDB(TestConstructorArg);
188 
200  template <typename... Args>
201  static void internalWarning(
202  folly::StringPiece file,
203  int lineNumber,
204  Args&&... args) noexcept {
205  internalWarningImpl(
206  file, lineNumber, folly::to<std::string>(std::forward<Args>(args)...));
207  }
208 
209  using InternalWarningHandler =
210  void (*)(folly::StringPiece file, int lineNumber, std::string&&);
211 
224  static void setInternalWarningHandler(InternalWarningHandler handler);
225 
226  private:
227  using LoggerNameMap = std::unordered_map<
229  std::unique_ptr<LogCategory>,
232 
233  using HandlerFactoryMap =
234  std::unordered_map<std::string, std::unique_ptr<LogHandlerFactory>>;
235  using HandlerMap = std::unordered_map<std::string, std::weak_ptr<LogHandler>>;
236  struct HandlerInfo {
239  };
240 
241  // Forbidden copy constructor and assignment operator
242  LoggerDB(LoggerDB const&) = delete;
243  LoggerDB& operator=(LoggerDB const&) = delete;
244 
245  LoggerDB();
246  LogCategory* getOrCreateCategoryLocked(
247  LoggerNameMap& loggersByName,
248  folly::StringPiece name);
249  LogCategory* createCategoryLocked(
250  LoggerNameMap& loggersByName,
251  folly::StringPiece name,
253 
254  using NewHandlerMap =
255  std::unordered_map<std::string, std::shared_ptr<LogHandler>>;
256  using OldToNewHandlerMap = std::
257  unordered_map<std::shared_ptr<LogHandler>, std::shared_ptr<LogHandler>>;
258  LogConfig getConfigImpl(bool includeAllCategories) const;
259  void startConfigUpdate(
260  const Synchronized<HandlerInfo>::LockedPtr& handlerInfo,
261  const LogConfig& config,
262  NewHandlerMap* handlers,
263  OldToNewHandlerMap* oldToNewHandlerMap);
264  void finishConfigUpdate(
265  const Synchronized<HandlerInfo>::LockedPtr& handlerInfo,
266  NewHandlerMap* handlers,
267  OldToNewHandlerMap* oldToNewHandlerMap);
268  std::vector<std::shared_ptr<LogHandler>> buildCategoryHandlerList(
269  const NewHandlerMap& handlerMap,
270  StringPiece categoryName,
271  const std::vector<std::string>& categoryHandlerNames);
272 
273  static void internalWarningImpl(
274  folly::StringPiece filename,
275  int lineNumber,
276  std::string&& msg) noexcept;
277  static void defaultInternalWarningImpl(
278  folly::StringPiece filename,
279  int lineNumber,
280  std::string&& msg) noexcept;
281 
288  folly::Synchronized<LoggerNameMap> loggersByName_;
289 
297 
298  static std::atomic<InternalWarningHandler> warningHandler_;
299 };
300 
328 void initializeLoggerDB(LoggerDB& db);
329 
330 } // namespace folly
FOLLY_ATTR_WEAK void initializeLoggerDB(LoggerDB &db)
Definition: LoggerDB.cpp:43
std::unordered_map< std::shared_ptr< LogHandler >, std::shared_ptr< LogHandler >> OldToNewHandlerMap
Definition: LoggerDB.h:257
PskType type
std::unordered_map< std::string, std::unique_ptr< LogHandlerFactory >> HandlerFactoryMap
Definition: LoggerDB.h:234
STL namespace.
#define FOLLY_NULLABLE
internal::ArgsMatcher< InnerMatcher > Args(const InnerMatcher &matcher)
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
requires E e noexcept(noexcept(s.error(std::move(e))))
AHArrayT::Config config
void handler(int, siginfo_t *, void *)
typename Base::LockedPtr LockedPtr
Definition: Synchronized.h:453
const char * name
Definition: http_parser.c:437
std::unordered_map< std::string, std::weak_ptr< LogHandler >> HandlerMap
Definition: LoggerDB.h:235
HandlerFactoryMap factories
Definition: LoggerDB.h:237
LogLevel
Definition: LogLevel.h:38
const char * string
Definition: Conv.cpp:212
void(*)(folly::StringPiece file, int lineNumber, std::string &&) InternalWarningHandler
Definition: LoggerDB.h:210
Range< const char * > StringPiece
folly::Function< void()> parent
Definition: AtFork.cpp:34
std::unordered_map< folly::StringPiece, std::unique_ptr< LogCategory >, LogName::Hash, LogName::Equals > LoggerNameMap
Definition: LoggerDB.h:231
std::unordered_map< std::string, std::shared_ptr< LogHandler >> NewHandlerMap
Definition: LoggerDB.h:255
static void internalWarning(folly::StringPiece file, int lineNumber, Args &&...args) noexcept
Definition: LoggerDB.h:201