MongoDB C++ Driver  legacy-1.1.2
log_test.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 <sstream>
19 #include <string>
20 #include <vector>
21 
22 #include "mongo/base/status.h"
23 #include "mongo/logger/appender.h"
24 #include "mongo/logger/log_severity.h"
25 #include "mongo/logger/logger.h"
26 #include "mongo/logger/message_log_domain.h"
27 #include "mongo/unittest/unittest.h"
28 
29 namespace mongo {
30 namespace logger {
31 
32 // Used for testing logging framework only.
33 // TODO(schwerin): Have logger write to a different log from the global log, so that tests can
34 // redirect their global log output for examination.
35 template <typename MessageEventEncoder>
36 class LogTest : public unittest::Test {
37  friend class LogTestAppender;
38 
39 public:
40  LogTest() : _severityOld(globalLogDomain()->getMinimumLogSeverity()) {
41  globalLogDomain()->clearAppenders();
42  _appenderHandle = globalLogDomain()->attachAppender(
43  MessageLogDomain::AppenderAutoPtr(new LogTestAppender(this)));
44  }
45 
46  virtual ~LogTest() {
47  globalLogDomain()->detachAppender(_appenderHandle);
48  globalLogDomain()->setMinimumLoggedSeverity(_severityOld);
49  }
50 
51 protected:
52  std::vector<std::string> _logLines;
53  LogSeverity _severityOld;
54 
55 private:
56  class LogTestAppender : public MessageLogDomain::EventAppender {
57  public:
58  explicit LogTestAppender(LogTest* ltest) : _ltest(ltest) {}
59  virtual ~LogTestAppender() {}
60  virtual Status append(const MessageLogDomain::Event& event) {
61  std::ostringstream _os;
62  if (!_encoder.encode(event, _os))
63  return Status(ErrorCodes::LogWriteFailed, "Failed to append to LogTestAppender.");
64  _ltest->_logLines.push_back(_os.str());
65  return Status::OK();
66  }
67 
68  private:
69  LogTest* _ltest;
70  MessageEventEncoder _encoder;
71  };
72 
73  MessageLogDomain::AppenderHandle _appenderHandle;
74 };
75 
76 } // namespace logger
77 } // namespace mongo
Status represents an error state or the absence thereof.
Definition: status.h:50
void clearAppenders()
Destroy all attached appenders, invalidating all handles.
Definition: log_domain-impl.h:84
Utility functions for parsing numbers from strings.
Definition: compare_numbers.h:20
AppenderHandle attachAppender(AppenderAutoPtr appender)
Attaches "appender" to this domain, taking ownership of it.
Definition: log_domain-impl.h:60
AppenderAutoPtr detachAppender(AppenderHandle handle)
Detaches the appender referenced by "handle" from this domain, releasing ownership of it...
Definition: log_domain-impl.h:75
Interface for sinks in a logging system.
Definition: appender.h:31
Opaque handle returned by attachAppender(), which can be subsequently passed to detachAppender() to d...
Definition: log_domain.h:61
Definition: log_test.h:36
void setMinimumLoggedSeverity(LogSeverity severity)
Sets the minimum severity of messages that should be sent to this LogDomain.
MONGO_CLIENT_API Status(MONGO_CLIENT_FUNC *saslClientAuthenticate)(DBClientWithCommands *client
Attempts to authenticate "client" using the SASL protocol.
Representation of the severity / priority of a log message.
Definition: log_severity.h:33