2013-07-14 01:57:26 +02:00
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include "testing_helpers.h"
|
|
|
|
#include "g2log.h"
|
|
|
|
#include "g2logworker.h"
|
2013-07-30 06:43:33 +02:00
|
|
|
#include "g2filesink.hpp"
|
2013-07-14 03:33:00 +02:00
|
|
|
#include "std2_make_unique.hpp"
|
2013-07-14 01:57:26 +02:00
|
|
|
|
|
|
|
using namespace std;
|
2013-07-14 03:33:00 +02:00
|
|
|
namespace {
|
|
|
|
g2LogWorker* oldworker = nullptr;
|
|
|
|
}
|
2013-07-14 01:57:26 +02:00
|
|
|
|
2013-07-14 03:33:00 +02:00
|
|
|
ScopedCout::ScopedCout(std::stringstream* buffer)
|
2013-07-14 01:57:26 +02:00
|
|
|
: _old_cout(std::cout.rdbuf()) {
|
|
|
|
cout.rdbuf(buffer->rdbuf());
|
|
|
|
}
|
|
|
|
|
2013-07-14 03:33:00 +02:00
|
|
|
ScopedCout::~ScopedCout() {
|
|
|
|
cout.rdbuf(_old_cout);
|
|
|
|
}
|
2013-07-14 01:57:26 +02:00
|
|
|
|
|
|
|
RestoreLogger::RestoreLogger(std::string directory)
|
2013-07-30 06:43:33 +02:00
|
|
|
: logger_(g2LogWorker::createWithNoSink()) {
|
|
|
|
using namespace g2;
|
|
|
|
auto filehandler = logger_->addSink(std2::make_unique<g2FileSink>("UNIT_TEST_LOGGER", directory), &g2FileSink::fileWrite);
|
2013-07-14 03:33:00 +02:00
|
|
|
|
|
|
|
oldworker = g2::shutDownLogging();
|
2013-07-30 06:43:33 +02:00
|
|
|
initializeLogging(logger_.get());
|
|
|
|
internal::changeFatalInitHandlerForUnitTesting();
|
2013-07-24 06:01:12 +02:00
|
|
|
|
2013-07-30 06:43:33 +02:00
|
|
|
auto filename = filehandler->call(&g2FileSink::fileName);
|
2013-07-14 01:57:26 +02:00
|
|
|
if (!filename.valid()) ADD_FAILURE();
|
|
|
|
log_file_ = filename.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
RestoreLogger::~RestoreLogger() {
|
|
|
|
reset();
|
|
|
|
g2::shutDownLogging();
|
2013-07-14 03:33:00 +02:00
|
|
|
if (nullptr != oldworker) g2::initializeLogging(oldworker);
|
2013-07-14 01:57:26 +02:00
|
|
|
if (0 != remove(log_file_.c_str()))
|
|
|
|
ADD_FAILURE();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RestoreLogger::reset() {
|
|
|
|
logger_.reset();
|
2013-07-14 03:33:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace testing_helper__cleaner {
|
|
|
|
|
|
|
|
bool removeFile(std::string path_to_file) {
|
|
|
|
return (0 == std::remove(path_to_file.c_str()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t LogFileCleaner::size() {
|
|
|
|
return logs_to_clean_.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
LogFileCleaner::~LogFileCleaner() {
|
|
|
|
std::lock_guard<std::mutex> lock(g_mutex);
|
|
|
|
{
|
|
|
|
for (std::string p : logs_to_clean_) {
|
|
|
|
if (false == testing_helper__cleaner::removeFile(p)) {
|
|
|
|
ADD_FAILURE() << "UNABLE to remove: " << p.c_str() << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
logs_to_clean_.clear();
|
|
|
|
} // mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
void LogFileCleaner::addLogToClean(std::string path_to_log) {
|
|
|
|
std::lock_guard<std::mutex> lock(g_mutex);
|
|
|
|
{
|
|
|
|
if (std::find(logs_to_clean_.begin(), logs_to_clean_.end(), path_to_log.c_str()) == logs_to_clean_.end())
|
|
|
|
logs_to_clean_.push_back(path_to_log);
|
|
|
|
}
|
|
|
|
}
|