MongoDB C++ Driver  legacy-1.1.2
logstream_builder.h
1 /* Copyright 2013 10gen Inc.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #pragma once
17 
18 #include <boost/scoped_ptr.hpp>
19 #include <sstream>
20 #include <string>
21 
22 #include "mongo/client/export_macros.h"
23 #include "mongo/logger/labeled_level.h"
24 #include "mongo/logger/log_component.h"
25 #include "mongo/logger/log_severity.h"
26 #include "mongo/logger/message_log_domain.h"
27 
28 namespace mongo {
29 namespace logger {
30 
31 class Tee;
32 
36 class MONGO_CLIENT_API LogstreamBuilder {
37 public:
38  static LogSeverity MONGO_CLIENT_FUNC severityCast(int ll) {
39  return LogSeverity::cast(ll);
40  }
41  static LogSeverity MONGO_CLIENT_FUNC severityCast(LogSeverity ls) {
42  return ls;
43  }
44  static LabeledLevel MONGO_CLIENT_FUNC severityCast(const LabeledLevel& labeled) {
45  return labeled;
46  }
47 
55  const std::string& contextName,
56  LogSeverity severity);
57 
66  const std::string& contextName,
67  LogSeverity severity,
68  LogComponent component);
69 
74  const std::string& contextName,
75  LabeledLevel labeledLevel);
76 
84  LogstreamBuilder(const LogstreamBuilder& other);
85 
91 
92 
96  LogstreamBuilder& setBaseMessage(const std::string& baseMessage) {
97  _baseMessage = baseMessage;
98  return *this;
99  }
100 
101  std::ostream& stream() {
102  makeStream();
103  return *_os;
104  }
105 
106  LogstreamBuilder& operator<<(const char* x) {
107  stream() << x;
108  return *this;
109  }
110  LogstreamBuilder& operator<<(const std::string& x) {
111  stream() << x;
112  return *this;
113  }
114  LogstreamBuilder& operator<<(const StringData& x) {
115  stream() << x;
116  return *this;
117  }
118  LogstreamBuilder& operator<<(char* x) {
119  stream() << x;
120  return *this;
121  }
122  LogstreamBuilder& operator<<(char x) {
123  stream() << x;
124  return *this;
125  }
126  LogstreamBuilder& operator<<(int x) {
127  stream() << x;
128  return *this;
129  }
130  LogstreamBuilder& operator<<(long x) {
131  stream() << x;
132  return *this;
133  }
134  LogstreamBuilder& operator<<(unsigned long x) {
135  stream() << x;
136  return *this;
137  }
138  LogstreamBuilder& operator<<(unsigned x) {
139  stream() << x;
140  return *this;
141  }
142  LogstreamBuilder& operator<<(unsigned short x) {
143  stream() << x;
144  return *this;
145  }
146  LogstreamBuilder& operator<<(double x) {
147  stream() << x;
148  return *this;
149  }
150  LogstreamBuilder& operator<<(void* x) {
151  stream() << x;
152  return *this;
153  }
154  LogstreamBuilder& operator<<(const void* x) {
155  stream() << x;
156  return *this;
157  }
158  LogstreamBuilder& operator<<(long long x) {
159  stream() << x;
160  return *this;
161  }
162  LogstreamBuilder& operator<<(unsigned long long x) {
163  stream() << x;
164  return *this;
165  }
166  LogstreamBuilder& operator<<(bool x) {
167  stream() << x;
168  return *this;
169  }
170 
171  template <typename T>
172  LogstreamBuilder& operator<<(const T& x) {
173  stream() << x.toString();
174  return *this;
175  }
176 
177  LogstreamBuilder& operator<<(std::ostream&(MONGO_CLIENT_FUNC* manip)(std::ostream&)) {
178  stream() << manip;
179  return *this;
180  }
181  LogstreamBuilder& operator<<(std::ios_base&(MONGO_CLIENT_FUNC* manip)(std::ios_base&)) {
182  stream() << manip;
183  return *this;
184  }
185 
190  void operator<<(Tee* tee);
191 
192 private:
193  LogstreamBuilder& operator=(const LogstreamBuilder& other);
194 
195  void makeStream();
196 
197  MessageLogDomain* _domain;
198  std::string _contextName;
199  LogSeverity _severity;
200  LogComponent _component;
201  std::string _baseMessage;
202  std::ostringstream* _os;
203  Tee* _tee;
204 };
205 
206 
207 } // namespace logger
208 } // namespace mongo
Log components.
Definition: log_component.h:32
Utility functions for parsing numbers from strings.
Definition: compare_numbers.h:20
LogstreamBuilder & setBaseMessage(const std::string &baseMessage)
Sets an optional prefix for the message.
Definition: logstream_builder.h:96
Stream-ish object used to build and append log messages.
Definition: logstream_builder.h:36
static LogSeverity cast(int)
Casts an integer to a severity.
Definition: log_severity-inl.h:40
Logging domain for events of type E.
Definition: log_domain.h:50
Representation of the severity / priority of a log message.
Definition: log_severity.h:33
Deprecated utility for associating a string and log level together.
Definition: labeled_level.h:28