libcryfs/test/cpp-utils/logging/testutils/LoggingTest.h

53 lines
1.3 KiB
C
Raw Normal View History

2015-10-17 15:49:54 +02:00
#pragma once
#ifndef MESSMER_CPPUTILS_TEST_LOGGING_TESTUTILS_LOGGINGTEST_H
#define MESSMER_CPPUTILS_TEST_LOGGING_TESTUTILS_LOGGINGTEST_H
#include <gtest/gtest.h>
#include <gmock/gmock.h>
2016-02-11 12:53:42 +01:00
#include "cpp-utils/logging/logging.h"
2017-02-04 20:03:20 +01:00
#include <spdlog/sinks/ostream_sink.h>
2015-10-17 15:49:54 +02:00
class MockLogger final {
public:
MockLogger():
_capturedLogData(),
_sink(std::make_shared<spdlog::sinks::ostream_sink<std::mutex>>(_capturedLogData, true)),
2015-10-17 16:42:56 +02:00
_logger(spdlog::create("MockLogger", {_sink})) {
2015-10-17 15:49:54 +02:00
}
~MockLogger() {
spdlog::drop("MockLogger");
};
std::shared_ptr<spdlog::logger> get() {
return _logger;
}
std::string capturedLog() const {
return _capturedLogData.str();
}
private:
std::ostringstream _capturedLogData;
std::shared_ptr<spdlog::sinks::ostream_sink<std::mutex>> _sink;
std::shared_ptr<spdlog::logger> _logger;
};
class LoggingTest: public ::testing::Test {
public:
2015-10-17 20:15:20 +02:00
LoggingTest(): mockLogger() {}
std::string captureStderr(std::function<void()> func) {
testing::internal::CaptureStderr();
2015-10-17 15:49:54 +02:00
func();
return testing::internal::GetCapturedStderr();
2015-10-17 15:49:54 +02:00
}
~LoggingTest() {
cpputils::logging::reset();
}
MockLogger mockLogger;
};
#endif