mirror of
https://github.com/KjellKod/g3log.git
synced 2024-12-12 10:23:50 +01:00
Cleanup
This commit is contained in:
parent
f4c939dc8a
commit
bb778adf25
@ -1,5 +1,6 @@
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <iostream>
|
||||
#include "testing_helpers.h"
|
||||
#include "g2log.h"
|
||||
#include "g2logworker.h"
|
||||
@ -8,75 +9,66 @@
|
||||
|
||||
using namespace std;
|
||||
namespace {
|
||||
g2LogWorker* oldworker = nullptr;
|
||||
g2LogWorker* oldworker = nullptr;
|
||||
|
||||
|
||||
}
|
||||
|
||||
ScopedCout::ScopedCout(std::stringstream* buffer)
|
||||
: _old_cout(std::cout.rdbuf()) {
|
||||
cout.rdbuf(buffer->rdbuf());
|
||||
}
|
||||
namespace testing_helpers {
|
||||
|
||||
ScopedCout::~ScopedCout() {
|
||||
cout.rdbuf(_old_cout);
|
||||
}
|
||||
|
||||
RestoreLogger::RestoreLogger(std::string directory)
|
||||
: logger_(g2LogWorker::createWithNoSink()) {
|
||||
using namespace g2;
|
||||
auto filehandler = logger_->addSink(std2::make_unique<g2FileSink>("UNIT_TEST_LOGGER", directory), &g2FileSink::fileWrite);
|
||||
|
||||
oldworker = g2::shutDownLogging();
|
||||
initializeLogging(logger_.get());
|
||||
internal::changeFatalInitHandlerForUnitTesting();
|
||||
|
||||
auto filename = filehandler->call(&g2FileSink::fileName);
|
||||
if (!filename.valid()) ADD_FAILURE();
|
||||
log_file_ = filename.get();
|
||||
}
|
||||
|
||||
RestoreLogger::~RestoreLogger() {
|
||||
reset();
|
||||
g2::shutDownLogging();
|
||||
if (nullptr != oldworker) g2::initializeLogging(oldworker);
|
||||
if (0 != remove(log_file_.c_str()))
|
||||
ADD_FAILURE();
|
||||
}
|
||||
|
||||
void RestoreLogger::reset() {
|
||||
logger_.reset();
|
||||
}
|
||||
bool removeFile(std::string path_to_file) {
|
||||
return (0 == std::remove(path_to_file.c_str()));
|
||||
}
|
||||
|
||||
|
||||
RestoreLogger::RestoreLogger(std::string directory)
|
||||
: logger_(g2LogWorker::createWithNoSink()) {
|
||||
using namespace g2;
|
||||
auto filehandler = logger_->addSink(std2::make_unique<g2FileSink>("UNIT_TEST_LOGGER", directory), &g2FileSink::fileWrite);
|
||||
|
||||
oldworker = g2::shutDownLogging();
|
||||
initializeLogging(logger_.get());
|
||||
internal::changeFatalInitHandlerForUnitTesting();
|
||||
LOG(INFO) << "Restore logger test ";
|
||||
auto filename = filehandler->call(&g2FileSink::fileName);
|
||||
if (!filename.valid()) ADD_FAILURE();
|
||||
log_file_ = filename.get();
|
||||
}
|
||||
|
||||
RestoreLogger::~RestoreLogger() {
|
||||
reset();
|
||||
g2::shutDownLogging();
|
||||
if (nullptr != oldworker) g2::initializeLogging(oldworker);
|
||||
if (!removeFile(log_file_))
|
||||
ADD_FAILURE();
|
||||
}
|
||||
|
||||
namespace testing_helper__cleaner {
|
||||
void RestoreLogger::reset() {
|
||||
logger_.reset();
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
size_t LogFileCleaner::size() {
|
||||
return logs_to_clean_.size();
|
||||
}
|
||||
LogFileCleaner::~LogFileCleaner() {
|
||||
std::lock_guard<std::mutex> lock(g_mutex);
|
||||
{
|
||||
for (const auto& file : logs_to_clean_) {
|
||||
if (!removeFile(file)) {
|
||||
ADD_FAILURE() << "UNABLE to remove: " << file << std::endl;
|
||||
}
|
||||
}
|
||||
logs_to_clean_.clear();
|
||||
} // mutex
|
||||
}
|
||||
|
||||
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;
|
||||
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);
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
} // testing_helpers
|
||||
|
@ -5,36 +5,47 @@
|
||||
* Created on July 13, 2013, 4:46 PM
|
||||
*/
|
||||
|
||||
#ifndef TEST_HELPER__RESTORE_LOGGER_H
|
||||
#define TEST_HELPER__RESTORE_LOGGER_H
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <mutex>
|
||||
#include <algorithm>
|
||||
#include "g2logworker.h"
|
||||
|
||||
// After initializing ScopedCout all std::couts is redirected to the buffer
|
||||
// Example:
|
||||
// stringstream buffer;
|
||||
// ScopedCout guard(&buffer);
|
||||
// cout << "Hello World";
|
||||
// ASSERT_STREQ(buffer.str().c_str(), "Hello World");
|
||||
namespace testing_helpers {
|
||||
|
||||
class ScopedCout {
|
||||
bool removeFile(std::string path_to_file);
|
||||
|
||||
/** After initializing ScopedCout all std::couts is redirected to the buffer
|
||||
@verbatim
|
||||
Example:
|
||||
stringstream buffer;
|
||||
ScopedCout guard(&buffer);
|
||||
cout << "Hello World";
|
||||
ASSERT_STREQ(buffer.str().c_str(), "Hello World"); */
|
||||
class ScopedOut {
|
||||
std::ostream& _out_type;
|
||||
std::streambuf* _old_cout;
|
||||
public:
|
||||
explicit ScopedCout(std::stringstream* buffer);
|
||||
~ScopedCout();
|
||||
explicit ScopedOut(std::ostream& out_type, std::stringstream* buffer)
|
||||
: _out_type(out_type)
|
||||
, _old_cout(_out_type.rdbuf()) {
|
||||
_out_type.rdbuf(buffer->rdbuf());
|
||||
}
|
||||
|
||||
virtual ~ScopedOut() {
|
||||
_out_type.rdbuf(_old_cout);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
namespace testing_helper__cleaner {
|
||||
bool removeFile(std::string path_to_file);
|
||||
}
|
||||
|
||||
class LogFileCleaner // RAII cluttering files cleanup
|
||||
/// RAII cluttering files cleanup
|
||||
class LogFileCleaner
|
||||
{
|
||||
private:
|
||||
std::vector<std::string> logs_to_clean_;
|
||||
@ -50,14 +61,12 @@ public:
|
||||
|
||||
|
||||
|
||||
// RAII temporarily replace of logger
|
||||
// and restoration of original logger at scope end
|
||||
|
||||
/** RAII temporarily replace of logger
|
||||
* and restoration of original logger at scope end*/
|
||||
struct RestoreLogger {
|
||||
explicit RestoreLogger(std::string directory);
|
||||
~RestoreLogger();
|
||||
void reset();
|
||||
|
||||
void reset();
|
||||
std::unique_ptr<g2LogWorker> logger_;
|
||||
|
||||
template<typename Call, typename ... Args >
|
||||
@ -66,14 +75,12 @@ struct RestoreLogger {
|
||||
return func();
|
||||
}
|
||||
|
||||
|
||||
std::string logFile() { return log_file_; }
|
||||
private:
|
||||
std::string log_file_;
|
||||
|
||||
};
|
||||
} // testing_helpers
|
||||
|
||||
|
||||
|
||||
#endif /* TEST_HELPER__RESTORE_LOGGER_H */
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user