1/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3 *     Copyright 2018 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#include "logger_test_fixture.h"
19
20#include <gsl/gsl>
21
22SpdloggerTest::SpdloggerTest() {
23    // Use default values from cb::logger::Config, apart from:
24    config.log_level = spdlog::level::level_enum::debug;
25    config.filename = "spdlogger_test";
26    config.unit_test = true; // Enable unit test mode (synchronous logging)
27    config.console = false; // Don't print to stderr
28}
29
30void SpdloggerTest::SetUp() {
31    setUpLogger();
32}
33
34void SpdloggerTest::TearDown() {
35    cb::logger::shutdown();
36    RemoveFiles();
37}
38
39void SpdloggerTest::RemoveFiles() {
40    Expects(!config.filename.empty());
41    files = cb::io::findFilesWithPrefix(config.filename);
42    for (const auto& file : files) {
43        cb::io::rmrf(file);
44    }
45}
46
47void SpdloggerTest::setUpLogger() {
48    RemoveFiles();
49
50    const auto ret = cb::logger::initialize(config);
51    EXPECT_FALSE(ret) << ret.get();
52
53    cb::logger::get()->set_level(config.log_level);
54}
55
56int SpdloggerTest::countInFile(const std::string& file,
57                               const std::string& msg) {
58    const auto content = cb::io::loadFile(file);
59
60    const auto* begin = content.data();
61    const auto* end = begin + content.size();
62
63    int count = 0;
64    while ((begin = std::search(begin, end, msg.begin(), msg.end())) != end) {
65        ++count;
66        begin += msg.size();
67    }
68    return count;
69}
70
71std::string SpdloggerTest::getLogContents() {
72    files = cb::io::findFilesWithPrefix(config.filename);
73    std::string ret;
74
75    for (const auto& file : files) {
76        ret.append(cb::io::loadFile(file));
77    }
78
79    return ret;
80}
81