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