This commit is contained in:
KjellKod 2013-08-18 23:17:04 -06:00
parent f4c939dc8a
commit bb778adf25
2 changed files with 82 additions and 83 deletions

View File

@ -1,5 +1,6 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <iostream>
#include "testing_helpers.h" #include "testing_helpers.h"
#include "g2log.h" #include "g2log.h"
#include "g2logworker.h" #include "g2logworker.h"
@ -9,74 +10,65 @@
using namespace std; using namespace std;
namespace { namespace {
g2LogWorker* oldworker = nullptr; g2LogWorker* oldworker = nullptr;
} }
ScopedCout::ScopedCout(std::stringstream* buffer) namespace testing_helpers {
: _old_cout(std::cout.rdbuf()) {
cout.rdbuf(buffer->rdbuf());
}
ScopedCout::~ScopedCout() { bool removeFile(std::string path_to_file) {
cout.rdbuf(_old_cout); return (0 == std::remove(path_to_file.c_str()));
} }
RestoreLogger::RestoreLogger(std::string directory)
: logger_(g2LogWorker::createWithNoSink()) { RestoreLogger::RestoreLogger(std::string directory)
: logger_(g2LogWorker::createWithNoSink()) {
using namespace g2; using namespace g2;
auto filehandler = logger_->addSink(std2::make_unique<g2FileSink>("UNIT_TEST_LOGGER", directory), &g2FileSink::fileWrite); auto filehandler = logger_->addSink(std2::make_unique<g2FileSink>("UNIT_TEST_LOGGER", directory), &g2FileSink::fileWrite);
oldworker = g2::shutDownLogging(); oldworker = g2::shutDownLogging();
initializeLogging(logger_.get()); initializeLogging(logger_.get());
internal::changeFatalInitHandlerForUnitTesting(); internal::changeFatalInitHandlerForUnitTesting();
LOG(INFO) << "Restore logger test ";
auto filename = filehandler->call(&g2FileSink::fileName); auto filename = filehandler->call(&g2FileSink::fileName);
if (!filename.valid()) ADD_FAILURE(); if (!filename.valid()) ADD_FAILURE();
log_file_ = filename.get(); log_file_ = filename.get();
} }
RestoreLogger::~RestoreLogger() { RestoreLogger::~RestoreLogger() {
reset(); reset();
g2::shutDownLogging(); g2::shutDownLogging();
if (nullptr != oldworker) g2::initializeLogging(oldworker); if (nullptr != oldworker) g2::initializeLogging(oldworker);
if (0 != remove(log_file_.c_str())) if (!removeFile(log_file_))
ADD_FAILURE(); ADD_FAILURE();
}
void RestoreLogger::reset() {
logger_.reset();
}
namespace testing_helper__cleaner {
bool removeFile(std::string path_to_file) {
return (0 == std::remove(path_to_file.c_str()));
} }
}
size_t LogFileCleaner::size() { void RestoreLogger::reset() {
logger_.reset();
}
size_t LogFileCleaner::size() {
return logs_to_clean_.size(); return logs_to_clean_.size();
} }
LogFileCleaner::~LogFileCleaner() { LogFileCleaner::~LogFileCleaner() {
std::lock_guard<std::mutex> lock(g_mutex); std::lock_guard<std::mutex> lock(g_mutex);
{ {
for (std::string p : logs_to_clean_) { for (const auto& file : logs_to_clean_) {
if (false == testing_helper__cleaner::removeFile(p)) { if (!removeFile(file)) {
ADD_FAILURE() << "UNABLE to remove: " << p.c_str() << std::endl; ADD_FAILURE() << "UNABLE to remove: " << file << std::endl;
} }
} }
logs_to_clean_.clear(); logs_to_clean_.clear();
} // mutex } // mutex
} }
void LogFileCleaner::addLogToClean(std::string path_to_log) { void LogFileCleaner::addLogToClean(std::string path_to_log) {
std::lock_guard<std::mutex> lock(g_mutex); 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()) 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_.push_back(path_to_log);
} }
} }
} // testing_helpers

View File

@ -5,36 +5,47 @@
* Created on July 13, 2013, 4:46 PM * Created on July 13, 2013, 4:46 PM
*/ */
#ifndef TEST_HELPER__RESTORE_LOGGER_H #pragma once
#define TEST_HELPER__RESTORE_LOGGER_H
#include <memory> #include <memory>
#include <string> #include <string>
#include <iostream>
#include <mutex> #include <mutex>
#include <algorithm> #include <algorithm>
#include "g2logworker.h" #include "g2logworker.h"
// After initializing ScopedCout all std::couts is redirected to the buffer namespace testing_helpers {
// Example:
// stringstream buffer;
// ScopedCout guard(&buffer);
// cout << "Hello World";
// ASSERT_STREQ(buffer.str().c_str(), "Hello World");
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; std::streambuf* _old_cout;
public: public:
explicit ScopedCout(std::stringstream* buffer); explicit ScopedOut(std::ostream& out_type, std::stringstream* buffer)
~ScopedCout(); : _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: private:
std::vector<std::string> logs_to_clean_; std::vector<std::string> logs_to_clean_;
@ -50,14 +61,12 @@ public:
// RAII temporarily replace of logger /** RAII temporarily replace of logger
// and restoration of original logger at scope end * and restoration of original logger at scope end*/
struct RestoreLogger { struct RestoreLogger {
explicit RestoreLogger(std::string directory); explicit RestoreLogger(std::string directory);
~RestoreLogger(); ~RestoreLogger();
void reset(); void reset();
std::unique_ptr<g2LogWorker> logger_; std::unique_ptr<g2LogWorker> logger_;
template<typename Call, typename ... Args > template<typename Call, typename ... Args >
@ -66,14 +75,12 @@ struct RestoreLogger {
return func(); return func();
} }
std::string logFile() { return log_file_; } std::string logFile() { return log_file_; }
private: private:
std::string log_file_; std::string log_file_;
}; };
} // testing_helpers
#endif /* TEST_HELPER__RESTORE_LOGGER_H */