1+ /* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2+ /*
3+ * Copyright 2020 Couchbase, 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 " bucket_logger.h"
21+
22+ #include < folly/portability/GMock.h>
23+
24+ /* *
25+ * The MockBucket Logger is used to verify that the logger is called with
26+ * certain parameters / messages.
27+ *
28+ * The MockBucketLogger calls the log method as normal, and intercepts the
29+ * _sink_it call by overriding it to determine the correctness of the logging
30+ */
31+ class MockBucketLogger : public BucketLogger {
32+ public:
33+ MockBucketLogger (std::string name) : BucketLogger(name) {
34+ // Set the log level of the BucketLogger to trace to ensure messages
35+ // make it through to the sink it method. Does not alter the logging
36+ // level of the underlying spdlogger so we will not see console
37+ // output during the test.
38+ set_level (spdlog::level::level_enum::trace);
39+ using namespace testing ;
40+ ON_CALL (*this , mlog (_, _))
41+ .WillByDefault (Invoke ([](spdlog::level::level_enum sev,
42+ const std::string& msg) {}));
43+ }
44+
45+ // Mock a method taking a logging level and formatted message to test log
46+ // outputs.
47+ MOCK_CONST_METHOD2 (mlog,
48+ void (spdlog::level::level_enum severity,
49+ const std::string& message));
50+
51+ protected:
52+ // Override the sink_it_ method to redirect to the mocked method
53+ // Must call the mlog method to check the message details as they are
54+ // bundled in the log_msg object. Beware, msg.raw is not null terminated.
55+ // In these test cases however we just search for a substring within the log
56+ // message so this is okay.
57+ void sink_it_ (spdlog::details::log_msg& msg) override {
58+ mlog (msg.level , msg.raw .data ());
59+ }
60+ };
0 commit comments