g3log/test_unit/testing_helpers.h
KjellKod 87a095e384 Renamed directories for a cleaner project structure
--HG--
rename : g2log/Build.cmake => Build.cmake
rename : g2log/CMakeLists.txt => CMakeLists.txt
rename : g2log/CPackLists.txt => CPackLists.txt
rename : g2log/Dynamic.cmake => Dynamic.cmake
rename : g2log/example/Example.cmake => example/Example.cmake
rename : g2log/example/main_contract.cpp => example/main_contract.cpp
rename : g2log/example/main_sigsegv.cpp => example/main_sigsegv.cpp
rename : g2log/src/active.hpp => src/active.hpp
rename : g2log/src/crashhandler.hpp => src/crashhandler.hpp
rename : g2log/src/crashhandler_unix.cpp => src/crashhandler_unix.cpp
rename : g2log/src/crashhandler_win.cpp => src/crashhandler_win.cpp
rename : g2log/src/g2filesink.cpp => src/g2filesink.cpp
rename : g2log/src/g2filesink.hpp => src/g2filesink.hpp
rename : g2log/src/g2filesinkhelper.ipp => src/g2filesinkhelper.ipp
rename : g2log/src/g2future.hpp => src/g2future.hpp
rename : g2log/src/g2log.cpp => src/g2log.cpp
rename : g2log/src/g2log.hpp => src/g2log.hpp
rename : g2log/src/g2loglevels.cpp => src/g2loglevels.cpp
rename : g2log/src/g2loglevels.hpp => src/g2loglevels.hpp
rename : g2log/src/g2logmessage.cpp => src/g2logmessage.cpp
rename : g2log/src/g2logmessage.hpp => src/g2logmessage.hpp
rename : g2log/src/g2logmessagecapture.cpp => src/g2logmessagecapture.cpp
rename : g2log/src/g2logmessagecapture.hpp => src/g2logmessagecapture.hpp
rename : g2log/src/g2logworker.cpp => src/g2logworker.cpp
rename : g2log/src/g2logworker.hpp => src/g2logworker.hpp
rename : g2log/src/g2moveoncopy.hpp => src/g2moveoncopy.hpp
rename : g2log/src/g2sink.hpp => src/g2sink.hpp
rename : g2log/src/g2sinkhandle.hpp => src/g2sinkhandle.hpp
rename : g2log/src/g2sinkwrapper.hpp => src/g2sinkwrapper.hpp
rename : g2log/src/g2time.cpp => src/g2time.cpp
rename : g2log/src/g2time.hpp => src/g2time.hpp
rename : g2log/src/shared_queue.hpp => src/shared_queue.hpp
rename : g2log/src/std2_make_unique.hpp => src/std2_make_unique.hpp
rename : g2log/src/stlpatch_future.hpp => src/stlpatch_future.hpp
rename : g2log/test_performance/Performance.cmake => test_performance/Performance.cmake
rename : g2log/test_performance/main_threaded_mean.cpp => test_performance/main_threaded_mean.cpp
rename : g2log/test_performance/main_threaded_worst.cpp => test_performance/main_threaded_worst.cpp
rename : g2log/test_performance/performance.h => test_performance/performance.h
rename : g2log/test_unit/Test.cmake => test_unit/Test.cmake
rename : g2log/test_unit/test_concept_sink.cpp => test_unit/test_concept_sink.cpp
rename : g2log/test_unit/test_configuration.cpp => test_unit/test_configuration.cpp
rename : g2log/test_unit/test_filechange.cpp => test_unit/test_filechange.cpp
rename : g2log/test_unit/test_io.cpp => test_unit/test_io.cpp
rename : g2log/test_unit/test_linux_dynamic_loaded_sharedlib.cpp => test_unit/test_linux_dynamic_loaded_sharedlib.cpp
rename : g2log/test_unit/test_sink.cpp => test_unit/test_sink.cpp
rename : g2log/test_unit/tester_sharedlib.cpp => test_unit/tester_sharedlib.cpp
rename : g2log/test_unit/tester_sharedlib.h => test_unit/tester_sharedlib.h
rename : g2log/test_unit/testing_helpers.cpp => test_unit/testing_helpers.cpp
rename : g2log/test_unit/testing_helpers.h => test_unit/testing_helpers.h
2014-10-03 01:20:33 -06:00

141 lines
3.2 KiB
C++

/** ==========================================================================
* 2013 by KjellKod.cc. This is PUBLIC DOMAIN to use at your own risk and comes
* with no warranties. This code is yours to share, use and modify with no
* strings attached and no restrictions or obligations.
*
* For more information see g3log/LICENSE or refer refer to http://unlicense.org
* ============================================================================*/
#pragma once
#include <memory>
#include <string>
#include <atomic>
#include <chrono>
#include <thread>
#include <algorithm>
#include "g2logworker.hpp"
#include "g2logmessage.hpp"
#include "g2filesink.hpp"
namespace testing_helpers {
std::string mockFatalMessage();
int mockFatalSignal();
bool mockFatalWasCalled();
void mockFatalCall(g2::FatalMessagePtr fatal_message);
void clearMockFatal();
bool removeFile(std::string path_to_file);
bool verifyContent(const std::string &total_text, std::string msg_to_find);
std::string readFileToText(std::string filename);
/** After initializing ScopedCout all std::couts is redirected to the buffer
@verbatim
Example:
stringstream buffer;
ScopedCout guard(std::cout, &buffer); // std::cerr is also fine
cout << "Hello World";
ASSERT_STREQ(buffer.str().c_str(), "Hello World"); */
class ScopedOut {
std::ostream& _out_type;
std::streambuf* _old_cout;
public:
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);
}
};
/// RAII cluttering files cleanup
class LogFileCleaner
{
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);
};
struct ScopedLogger {
ScopedLogger();
virtual ~ScopedLogger();
g2::LogWorker* get();
std::unique_ptr<g2::LogWorker> _currentWorker;
};
/** RAII temporarily replace of logger
* and restoration of original logger at scope end*/
struct RestoreFileLogger {
explicit RestoreFileLogger(std::string directory);
~RestoreFileLogger();
std::unique_ptr<ScopedLogger> _scope;
void reset(){ _scope.reset();}
template<typename Call, typename ... Args >
typename std::result_of<Call(Args...)>::type callToLogger(Call call, Args&&... args) {
auto func = std::bind(call, _scope->get(), std::forward<Args>(args)...);
return func();
}
std::string logFile();
std::string resetAndRetrieveContent();
private:
std::unique_ptr<g2::SinkHandle<g2::FileSink>> _handle;
std::string _log_file;
};
typedef std::shared_ptr<std::atomic<bool>> AtomicBoolPtr;
typedef std::shared_ptr<std::atomic<int>> AtomicIntPtr;
struct ScopedSetTrue {
AtomicBoolPtr _flag;
AtomicIntPtr _count;
explicit ScopedSetTrue(AtomicBoolPtr flag, AtomicIntPtr count)
: _flag(flag), _count(count) {
}
void ReceiveMsg(std::string message) {
std::chrono::milliseconds wait{100};
std::this_thread::sleep_for(wait);
++(*_count);
}
~ScopedSetTrue() {
(*_flag) = true;
}
};
} // testing_helpers