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 #pragma once 18 19 #include "logger.h" 20 21 #include <platform/dirutils.h> 22 23 #include <boost/optional.hpp> 24 #include <gtest/gtest.h> 25 26 class SpdloggerTest : virtual public ::testing::Test { 27 protected: 28 /* 29 * Unset a few environment variables which affect how the logger works. 30 * unsetenv() is not supported on Windows. 31 */ 32 #ifndef WIN32 33 34 static void SetUpTestCase() { 35 unsetenv("CB_MAXIMIZE_LOGGER_CYCLE_SIZE"); 36 unsetenv("CB_MAXIMIZE_LOGGER_BUFFER_SIZE"); 37 } 38 39 #endif 40 41 virtual void SetUp(); 42 virtual void TearDown(); 43 44 /** 45 * Helper function - initializes a cb logger object using the given 46 * parameters 47 * 48 * @param level the minimum logging level 49 * @param cyclesize the size to use before switching file (default 100MB) 50 */ 51 virtual void setUpLogger(const spdlog::level::level_enum level, 52 const size_t cyclesize = 100 * 1024 * 1024); 53 54 /** 55 * Helper function - removes the files in the test working directory that 56 * are prefixed with the given filename 57 */ 58 void RemoveFiles(); 59 60 /** 61 * Helper function - counts how many times a string appears in a file. 62 * 63 * @param file the name of the file 64 * @param msg the message to search for 65 * @return the number of times we found the message in the file 66 */ 67 static int countInFile(const std::string& file, const std::string& msg); 68 69 std::string getLogContents(); 70 71 std::vector<std::string> files; 72 std::string filename{"spdlogger_test"}; 73 const std::string openingHook = "---------- Opening logfile: "; 74 const std::string closingHook = "---------- Closing logfile"; 75 }; 76