MongoDB  2.7.0
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
19 
20 #include "mongo/base/status.h"
21 #include "mongo/bson/util/builder.h"
22 #include "mongo/logger/logger.h"
23 #include "mongo/logger/logstream_builder.h"
24 #include "mongo/logger/tee.h"
25 #include "mongo/util/concurrency/thread_name.h"
26 
27 namespace mongo {
28 
29 namespace logger {
30  typedef void (*ExtraLogContextFn)(BufBuilder& builder);
31  Status registerExtraLogContextFn(ExtraLogContextFn contextFn);
32 
33 } // namespace logger
34 
35  using logger::LogstreamBuilder;
36  using logger::LabeledLevel;
37  using logger::Tee;
38 
42  inline LogstreamBuilder severe() {
43  return LogstreamBuilder(logger::globalLogDomain(),
44  getThreadName(),
45  logger::LogSeverity::Severe());
46  }
47 
51  inline LogstreamBuilder error() {
52  return LogstreamBuilder(logger::globalLogDomain(),
53  getThreadName(),
54  logger::LogSeverity::Error());
55  }
56 
60  inline LogstreamBuilder warning() {
61  return LogstreamBuilder(logger::globalLogDomain(),
62  getThreadName(),
63  logger::LogSeverity::Warning());
64  }
65 
69  inline LogstreamBuilder log() {
70  return LogstreamBuilder(logger::globalLogDomain(),
71  getThreadName(),
72  logger::LogSeverity::Log());
73  }
74 
75 
79  inline LogstreamBuilder out() { return log(); }
80 
85  extern int tlogLevel;
86 
87 #define MONGO_LOG(DLEVEL) \
88  if (!(::mongo::logger::globalLogDomain())->shouldLog(::mongo::LogstreamBuilder::severityCast(DLEVEL))) {} \
89  else LogstreamBuilder(::mongo::logger::globalLogDomain(), getThreadName(), ::mongo::LogstreamBuilder::severityCast(DLEVEL))
90 
91 #define LOG MONGO_LOG
92 
93 #define MONGO_DLOG(DLEVEL) \
94  if (!(DEBUG_BUILD) && !::mongo::logger::globalLogDomain()->shouldLog(::mongo::LogstreamBuilder::severityCast(DLEVEL))) {} \
95  else LogstreamBuilder(::mongo::logger::globalLogDomain(), getThreadName(), ::mongo::LogstreamBuilder::severityCast(DLEVEL))
96 
97 #define MONGO_TLOG(DLEVEL) \
98  if ((!::mongo::debug && ((DLEVEL) > tlogLevel)) || !::mongo::logger::globalLogDomain()->shouldLog(::mongo::LogstreamBuilder::severityCast(DLEVEL))) {} \
99  else LogstreamBuilder(::mongo::logger::globalLogDomain(), getThreadName(), ::mongo::LogstreamBuilder::severityCast(DLEVEL))
100 
101  inline LogstreamBuilder problem() {
102  return log();
103  }
104 
108  bool rotateLogs();
109 
113  string errnoWithPrefix( const char * prefix );
114 
115  // Guard that alters the indentation level used by log messages on the current thread.
116  // Used only by mongodump (mongo/tools/dump.cpp). Do not introduce new uses.
117  struct LogIndentLevel {
118  LogIndentLevel();
119  ~LogIndentLevel();
120  };
121 
122  extern Tee* const warnings; // Things put here go in serverStatus
123  extern Tee* const startupWarningsLog; // Things put here get reported in MMS
124 
125  string errnoWithDescription(int errorcode = -1);
126 
130  void logContext(const char *msg = NULL);
131 
132 } // namespace mongo
string errnoWithPrefix(const char *prefix)
output the error # and error message with prefix.
Definition: assert_util.cpp:229
LogstreamBuilder error()
Returns a LogstreamBuilder for logging a message with LogSeverity::Error().
Definition: log.h:51
LogstreamBuilder out()
Synonym for log().
Definition: log.h:79
int tlogLevel
For logging which we may not want during unit tests (dbtests) runs.
Definition: log.cpp:57
LogstreamBuilder warning()
Returns a LogstreamBuilder for logging a message with LogSeverity::Warning().
Definition: log.h:60
Definition: log.h:117
bool rotateLogs()
Rotates the log files.
Definition: log.cpp:59
const std::string & getThreadName()
Retrieves the name of the current thread, as previously set, or "" if no name was previously set...
Definition: thread_name.cpp:69
LogstreamBuilder log()
Returns a LogstreamBuilder for logging a message with LogSeverity::Log().
Definition: log.h:69
void logContext(const char *errmsg)
Write the current context (backtrace), along with the optional "msg".
Definition: log.cpp:120
LogstreamBuilder severe()
Returns a LogstreamBuilder for logging a message with LogSeverity::Severe().
Definition: log.h:42