mock-log.h
Go to the documentation of this file.
1 // Copyright (c) 2007, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: Zhanyong Wan
31 //
32 // Defines the ScopedMockLog class (using Google C++ Mocking
33 // Framework), which is convenient for testing code that uses LOG().
34 
35 #ifndef GLOG_SRC_MOCK_LOG_H_
36 #define GLOG_SRC_MOCK_LOG_H_
37 
38 // For GOOGLE_NAMESPACE. This must go first so we get _XOPEN_SOURCE.
39 #include "utilities.h"
40 
41 #include <string>
42 
43 #include <gmock/gmock.h>
44 
45 #include <glog/logging.h>
46 
48 namespace glog_testing {
49 
50 // A ScopedMockLog object intercepts LOG() messages issued during its
51 // lifespan. Using this together with Google C++ Mocking Framework,
52 // it's very easy to test how a piece of code calls LOG(). The
53 // typical usage:
54 //
55 // TEST(FooTest, LogsCorrectly) {
56 // ScopedMockLog log;
57 //
58 // // We expect the WARNING "Something bad!" exactly twice.
59 // EXPECT_CALL(log, Log(WARNING, _, "Something bad!"))
60 // .Times(2);
61 //
62 // // We allow foo.cc to call LOG(INFO) any number of times.
63 // EXPECT_CALL(log, Log(INFO, HasSubstr("/foo.cc"), _))
64 // .Times(AnyNumber());
65 //
66 // Foo(); // Exercises the code under test.
67 // }
68 class ScopedMockLog : public GOOGLE_NAMESPACE::LogSink {
69  public:
70  // When a ScopedMockLog object is constructed, it starts to
71  // intercept logs.
73 
74  // When the object is destructed, it stops intercepting logs.
76 
77  // Implements the mock method:
78  //
79  // void Log(LogSeverity severity, const string& file_path,
80  // const string& message);
81  //
82  // The second argument to Send() is the full path of the source file
83  // in which the LOG() was issued.
84  //
85  // Note, that in a multi-threaded environment, all LOG() messages from a
86  // single thread will be handled in sequence, but that cannot be guaranteed
87  // for messages from different threads. In fact, if the same or multiple
88  // expectations are matched on two threads concurrently, their actions will
89  // be executed concurrently as well and may interleave.
91  const std::string& file_path,
92  const std::string& message));
93 
94  private:
95  // Implements the send() virtual function in class LogSink.
96  // Whenever a LOG() statement is executed, this function will be
97  // invoked with information presented in the LOG().
98  //
99  // The method argument list is long and carries much information a
100  // test usually doesn't care about, so we trim the list before
101  // forwarding the call to Log(), which is much easier to use in
102  // tests.
103  //
104  // We still cannot call Log() directly, as it may invoke other LOG()
105  // messages, either due to Invoke, or due to an error logged in
106  // Google C++ Mocking Framework code, which would trigger a deadlock
107  // since a lock is held during send().
108  //
109  // Hence, we save the message for WaitTillSent() which will be called after
110  // the lock on send() is released, and we'll call Log() inside
111  // WaitTillSent(). Since while a single send() call may be running at a
112  // time, multiple WaitTillSent() calls (along with the one send() call) may
113  // be running simultaneously, we ensure thread-safety of the exchange between
114  // send() and WaitTillSent(), and that for each message, LOG(), send(),
115  // WaitTillSent() and Log() are executed in the same thread.
117  const char* full_filename,
118  const char* /*base_filename*/, int /*line*/,
119  const LogMessageTime & /*logmsgtime*/,
120  const char* message, size_t message_len) {
121  // We are only interested in the log severity, full file name, and
122  // log message.
124  message_info_.file_path = full_filename;
125  message_info_.message = std::string(message, message_len);
126  }
127 
128  // Implements the WaitTillSent() virtual function in class LogSink.
129  // It will be executed after send() and after the global logging lock is
130  // released, so calls within it (or rather within the Log() method called
131  // within) may also issue LOG() statements.
132  //
133  // LOG(), send(), WaitTillSent() and Log() will occur in the same thread for
134  // a given log message.
135  virtual void WaitTillSent() {
136  // First, and very importantly, we save a copy of the message being
137  // processed before calling Log(), since Log() may indirectly call send()
138  // and WaitTillSent() in the same thread again.
139  MessageInfo message_info = message_info_;
140  Log(message_info.severity, message_info.file_path, message_info.message);
141  }
142 
143  // All relevant information about a logged message that needs to be passed
144  // from send() to WaitTillSent().
145  struct MessageInfo {
149  };
151 };
152 
153 } // namespace glog_testing
154 _END_GOOGLE_NAMESPACE_
155 
156 #endif // GLOG_SRC_MOCK_LOG_H_
glog_testing::ScopedMockLog::MessageInfo::message
std::string message
Definition: mock-log.h:148
testing::internal::Log
GTEST_API_ void Log(LogSeverity severity, const std::string &message, int stack_frames_to_skip)
Definition: gmock-internal-utils.cc:149
glog_testing::ScopedMockLog::~ScopedMockLog
~ScopedMockLog()
Definition: mock-log.h:75
LogSeverity
int LogSeverity
Definition: log_severity.h:51
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
RemoveLogSink
void RemoveLogSink(LogSink *destination)
Definition: logging.cc:2142
gmock.h
glog_testing::ScopedMockLog
Definition: mock-log.h:68
severity
GLenum GLuint GLenum severity
Definition: glcorearb.h:2695
glog_testing
Definition: mock-log.h:48
glog_testing::ScopedMockLog::MessageInfo::file_path
std::string file_path
Definition: mock-log.h:147
glog_testing::ScopedMockLog::send
virtual void send(GOOGLE_NAMESPACE::LogSeverity severity, const char *full_filename, const char *, int, const LogMessageTime &, const char *message, size_t message_len)
Definition: mock-log.h:116
glog_testing::ScopedMockLog::MOCK_METHOD3
MOCK_METHOD3(Log, void(GOOGLE_NAMESPACE::LogSeverity severity, const std::string &file_path, const std::string &message))
glog_testing::ScopedMockLog::MessageInfo::severity
GOOGLE_NAMESPACE::LogSeverity severity
Definition: mock-log.h:146
utilities.h
AddLogSink
void AddLogSink(LogSink *destination)
Definition: logging.cc:2138
glog_testing::ScopedMockLog::WaitTillSent
virtual void WaitTillSent()
Definition: mock-log.h:135
_START_GOOGLE_NAMESPACE_
Definition: signalhandler.cc:51
glog_testing::ScopedMockLog::message_info_
MessageInfo message_info_
Definition: mock-log.h:150
glog_testing::ScopedMockLog::ScopedMockLog
ScopedMockLog()
Definition: mock-log.h:72
glog_testing::ScopedMockLog::MessageInfo
Definition: mock-log.h:145
message
GLenum GLuint GLenum GLsizei const GLchar * message
Definition: glcorearb.h:2695


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:57