Consistently have the namespace closing comment
[folly.git] / folly / experimental / logging / Logger.h
1 /*
2  * Copyright 2004-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/Format.h>
20 #include <folly/experimental/logging/LogCategory.h>
21 #include <folly/experimental/logging/LogLevel.h>
22 #include <folly/experimental/logging/LogStream.h>
23 #include <folly/experimental/logging/LogStreamProcessor.h>
24
25 /**
26  * Helper macro for implementing FB_LOG() and FB_LOGF().
27  *
28  * This macro generally should not be used directly by end users.
29  */
30 /* clang-format off */
31 #define FB_LOG_IMPL(logger, level, type, ...)                                \
32   (!(logger).getCategory()->logCheck(level))                                 \
33       ? ::folly::logDisabledHelper(                                          \
34             std::integral_constant<bool, ::folly::isLogLevelFatal(level)>{}) \
35       : ::folly::LogStreamVoidify< ::folly::isLogLevelFatal(level)>{} &      \
36           ::folly::LogStreamProcessor{(logger).getCategory(),                \
37                                       (level),                               \
38                                       __FILE__,                              \
39                                       __LINE__,                              \
40                                       (type),                                \
41                                       ##__VA_ARGS__}                         \
42               .stream()
43 /* clang-format on */
44
45 /**
46  * Log a message to the specified logger.
47  *
48  * This macro avoids evaluating the log arguments unless the log level check
49  * succeeds.
50  *
51  * Beware that the logger argument is evaluated twice, so this argument should
52  * be an expression with no side-effects.
53  */
54 #define FB_LOG(logger, level, ...)         \
55   FB_LOG_IMPL(                             \
56       logger,                              \
57       ::folly::LogLevel::level,            \
58       ::folly::LogStreamProcessor::APPEND, \
59       ##__VA_ARGS__)
60
61 /**
62  * Log a message to the specified logger, using a folly::format() string.
63  *
64  * The arguments will be processed using folly::format().  The format syntax
65  * is similar to Python format strings.
66  *
67  * This macro avoids evaluating the log arguments unless the log level check
68  * succeeds.
69  *
70  * Beware that the logger argument is evaluated twice, so this argument should
71  * be an expression with no side-effects.
72  */
73 #define FB_LOGF(logger, level, fmt, arg1, ...) \
74   FB_LOG_IMPL(                                 \
75       logger,                                  \
76       ::folly::LogLevel::level,                \
77       ::folly::LogStreamProcessor::FORMAT,     \
78       fmt,                                     \
79       arg1,                                    \
80       ##__VA_ARGS__)
81
82 namespace folly {
83
84 class LoggerDB;
85 class LogMessage;
86
87 /**
88  * Logger is the class you will normally use to log messages.
89  *
90  * The Logger is really just a small wrapper class that contains a pointer
91  * to the appropriate LogCategory object.  It exists to allow for easy static
92  * initialization of log categories, as well as to provide fast checking of the
93  * current effective log level.
94  */
95 class Logger {
96  public:
97   /**
98    * Construct a Logger for the given category name.
99    *
100    * A LogCategory object for this category will be created if one does not
101    * already exist.
102    */
103   explicit Logger(folly::StringPiece name);
104
105   /**
106    * Construct a Logger pointing to an existing LogCategory object.
107    */
108   explicit Logger(LogCategory* cat);
109
110   /**
111    * Construct a Logger for a specific LoggerDB object, rather than the main
112    * singleton.
113    *
114    * This is primarily intended for use in unit tests.
115    */
116   Logger(LoggerDB* db, folly::StringPiece name);
117
118   /**
119    * Get the LogCategory that this Logger refers to.
120    */
121   LogCategory* getCategory() const {
122     return category_;
123   }
124
125  private:
126   LogCategory* const category_{nullptr};
127 };
128 } // namespace folly