MongoDB C++ Driver  legacy-1.1.2
log.h
1 // @file log.h
2 
3 /* Copyright 2009 10gen Inc.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 // #pragma once is not used in this header.
19 // This header attempts to enforce the rule that no logging should be done in
20 // an inline function defined in a header.
21 // To enforce this "no logging in header" rule, we use #include guards with a validating #else
22 // clause.
23 // Also, this header relies on a preprocessor macro to determine the default component for the
24 // unconditional logging functions severe(), error(), warning() and log(). Disallowing multiple
25 // inclusion of log.h will ensure that the default component will be set correctly.
26 
27 #if defined(MONGO_UTIL_LOG_H_)
28 #error \
29  "mongo/util/log.h cannot be included multiple times. " \
30  "This may occur when log.h is included in a header. " \
31  "Please check your #include's."
32 #else // MONGO_UTIL_LOG_H_
33 #define MONGO_UTIL_LOG_H_
34 
35 #include "mongo/base/status.h"
36 #include "mongo/bson/util/builder.h"
37 #include "mongo/logger/log_component.h"
38 #include "mongo/logger/logger.h"
39 #include "mongo/logger/logstream_builder.h"
40 #include "mongo/logger/tee.h"
41 
42 // Provide log component in global scope so that MONGO_LOG will always have a valid component.
43 // Global log component will be kDefault unless overridden by MONGO_LOG_DEFAULT_COMPONENT.
44 #if defined(MONGO_LOG_DEFAULT_COMPONENT)
45 const ::mongo::logger::LogComponent MongoLogDefaultComponent_component =
46  MONGO_LOG_DEFAULT_COMPONENT;
47 #else
48 #error \
49  "mongo/util/log.h requires MONGO_LOG_DEFAULT_COMPONENT to be defined. " \
50  "Please see http://www.mongodb.org/about/contributors/reference/server-logging-rules/ "
51 #endif // MONGO_LOG_DEFAULT_COMPONENT
52 
53 namespace mongo {
54 
55 namespace logger {
56 typedef void (*ExtraLogContextFn)(BufBuilder& builder);
57 Status registerExtraLogContextFn(ExtraLogContextFn contextFn);
58 
59 } // namespace logger
60 
61 namespace {
62 
63 using logger::LogstreamBuilder;
64 using logger::LabeledLevel;
65 using logger::Tee;
66 
70 inline LogstreamBuilder severe() {
71  return LogstreamBuilder(logger::globalLogDomain(),
72  std::string(),
73  logger::LogSeverity::Severe(),
74  ::MongoLogDefaultComponent_component);
75 }
76 
77 inline LogstreamBuilder severe(logger::LogComponent component) {
78  return LogstreamBuilder(
79  logger::globalLogDomain(), std::string(), logger::LogSeverity::Severe(), component);
80 }
81 
85 inline LogstreamBuilder error() {
86  return LogstreamBuilder(logger::globalLogDomain(),
87  std::string(),
88  logger::LogSeverity::Error(),
89  ::MongoLogDefaultComponent_component);
90 }
91 
92 inline LogstreamBuilder error(logger::LogComponent component) {
93  return LogstreamBuilder(
94  logger::globalLogDomain(), std::string(), logger::LogSeverity::Error(), component);
95 }
96 
100 inline LogstreamBuilder warning() {
101  return LogstreamBuilder(logger::globalLogDomain(),
102  std::string(),
103  logger::LogSeverity::Warning(),
104  ::MongoLogDefaultComponent_component);
105 }
106 
107 inline LogstreamBuilder warning(logger::LogComponent component) {
108  return LogstreamBuilder(
109  logger::globalLogDomain(), std::string(), logger::LogSeverity::Warning(), component);
110 }
111 
115 inline LogstreamBuilder log() {
116  return LogstreamBuilder(logger::globalLogDomain(),
117  std::string(),
118  logger::LogSeverity::Log(),
119  ::MongoLogDefaultComponent_component);
120 }
121 
122 inline LogstreamBuilder log(logger::LogComponent component) {
123  return LogstreamBuilder(
124  logger::globalLogDomain(), std::string(), logger::LogSeverity::Log(), component);
125 }
126 
127 inline LogstreamBuilder log(logger::LogComponent::Value componentValue) {
128  return LogstreamBuilder(
129  logger::globalLogDomain(), std::string(), logger::LogSeverity::Log(), componentValue);
130 }
131 
135 inline bool shouldLog(logger::LogSeverity severity) {
136  return logger::globalLogDomain()->shouldLog(::MongoLogDefaultComponent_component, severity);
137 }
138 
139 } // namespace
140 
141 // MONGO_LOG uses log component from MongoLogDefaultComponent from current or global namespace.
142 #define MONGO_LOG(DLEVEL) \
143  if (!(::mongo::logger::globalLogDomain()) \
144  ->shouldLog(MongoLogDefaultComponent_component, \
145  ::mongo::LogstreamBuilder::severityCast(DLEVEL))) { \
146  } else \
147  ::mongo::logger::LogstreamBuilder(::mongo::logger::globalLogDomain(), \
148  std::string(), \
149  ::mongo::LogstreamBuilder::severityCast(DLEVEL), \
150  MongoLogDefaultComponent_component)
151 
152 #define LOG MONGO_LOG
153 
154 #define MONGO_LOG_COMPONENT(DLEVEL, COMPONENT1) \
155  if (!(::mongo::logger::globalLogDomain()) \
156  ->shouldLog((COMPONENT1), ::mongo::LogstreamBuilder::severityCast(DLEVEL))) { \
157  } else \
158  ::mongo::logger::LogstreamBuilder(::mongo::logger::globalLogDomain(), \
159  std::string(), \
160  ::mongo::LogstreamBuilder::severityCast(DLEVEL), \
161  (COMPONENT1))
162 
163 #define MONGO_LOG_COMPONENT2(DLEVEL, COMPONENT1, COMPONENT2) \
164  if (!(::mongo::logger::globalLogDomain()) \
165  ->shouldLog( \
166  (COMPONENT1), (COMPONENT2), ::mongo::LogstreamBuilder::severityCast(DLEVEL))) { \
167  } else \
168  ::mongo::logger::LogstreamBuilder(::mongo::logger::globalLogDomain(), \
169  std::string(), \
170  ::mongo::LogstreamBuilder::severityCast(DLEVEL), \
171  (COMPONENT1))
172 
173 #define MONGO_LOG_COMPONENT3(DLEVEL, COMPONENT1, COMPONENT2, COMPONENT3) \
174  if (!(::mongo::logger::globalLogDomain()) \
175  ->shouldLog((COMPONENT1), \
176  (COMPONENT2), \
177  (COMPONENT3), \
178  ::mongo::LogstreamBuilder::severityCast(DLEVEL))) { \
179  } else \
180  ::mongo::logger::LogstreamBuilder(::mongo::logger::globalLogDomain(), \
181  std::string(), \
182  ::mongo::LogstreamBuilder::severityCast(DLEVEL), \
183  (COMPONENT1))
184 
185 
189 std::string errnoWithPrefix(const char* prefix);
190 
191 std::string errnoWithDescription(int errorcode = -1);
192 
196 void logContext(const char* msg = NULL);
197 
198 } // namespace mongo
199 
200 #endif // MONGO_UTIL_LOG_H_
Utility functions for parsing numbers from strings.
Definition: compare_numbers.h:20
std::string errnoWithPrefix(const char *prefix)
output the error # and error message with prefix.
void logContext(const char *msg=NULL)
Write the optional "msg".
MONGO_CLIENT_API Status(MONGO_CLIENT_FUNC *saslClientAuthenticate)(DBClientWithCommands *client
Attempts to authenticate "client" using the SASL protocol.