2013-07-14 01:57:26 +02:00
|
|
|
/*
|
|
|
|
* File: test_helper__restore_logger.h
|
|
|
|
* Author: kjell
|
|
|
|
*
|
|
|
|
* Created on July 13, 2013, 4:46 PM
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef TEST_HELPER__RESTORE_LOGGER_H
|
|
|
|
#define TEST_HELPER__RESTORE_LOGGER_H
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <iostream>
|
2013-07-14 03:33:00 +02:00
|
|
|
#include <mutex>
|
|
|
|
#include <algorithm>
|
2013-07-14 06:59:02 +02:00
|
|
|
#include "g2logworker.h"
|
2013-07-14 01:57:26 +02:00
|
|
|
|
|
|
|
// 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");
|
2013-07-14 03:33:00 +02:00
|
|
|
|
|
|
|
class ScopedCout {
|
|
|
|
std::streambuf* _old_cout;
|
|
|
|
public:
|
|
|
|
explicit ScopedCout(std::stringstream* buffer);
|
|
|
|
~ScopedCout();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
namespace testing_helper__cleaner {
|
|
|
|
bool removeFile(std::string path_to_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
class LogFileCleaner // RAII cluttering files cleanup
|
2013-07-14 01:57:26 +02:00
|
|
|
{
|
2013-07-14 03:33:00 +02:00
|
|
|
private:
|
|
|
|
std::vector<std::string> logs_to_clean_;
|
|
|
|
std::mutex g_mutex;
|
|
|
|
public:
|
|
|
|
size_t size();
|
|
|
|
|
|
|
|
LogFileCleaner() {
|
|
|
|
}
|
|
|
|
virtual ~LogFileCleaner();
|
|
|
|
void addLogToClean(std::string path_to_log);
|
2013-07-14 01:57:26 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// RAII temporarily replace of logger
|
|
|
|
// and restoration of original logger at scope end
|
|
|
|
|
2013-07-14 03:33:00 +02:00
|
|
|
struct RestoreLogger {
|
|
|
|
explicit RestoreLogger(std::string directory);
|
|
|
|
~RestoreLogger();
|
|
|
|
void reset();
|
2013-07-14 06:59:02 +02:00
|
|
|
|
2013-07-14 03:33:00 +02:00
|
|
|
std::unique_ptr<g2LogWorker> logger_;
|
|
|
|
|
2013-07-14 06:59:02 +02:00
|
|
|
template<typename Call, typename ... Args >
|
|
|
|
typename std::result_of<Call(Args...)>::type callToLogger(Call call, Args&&... args) {
|
|
|
|
auto func = std::bind(call, logger_.get(), std::forward<Args>(args)...);
|
|
|
|
return func();
|
2013-07-14 03:33:00 +02:00
|
|
|
}
|
2013-07-14 06:59:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
std::string logFile() { return log_file_; }
|
2013-07-14 01:57:26 +02:00
|
|
|
private:
|
2013-07-14 03:33:00 +02:00
|
|
|
std::string log_file_;
|
2013-07-14 01:57:26 +02:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2013-07-14 03:33:00 +02:00
|
|
|
|
|
|
|
|
2013-07-14 01:57:26 +02:00
|
|
|
#endif /* TEST_HELPER__RESTORE_LOGGER_H */
|
|
|
|
|