2013-12-16 06:29:26 +01:00
|
|
|
/** ==========================================================================
|
|
|
|
* 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.
|
2020-06-19 21:49:30 +02:00
|
|
|
*
|
2014-07-03 23:42:19 +02:00
|
|
|
* For more information see g3log/LICENSE or refer refer to http://unlicense.org
|
2013-12-16 06:29:26 +01:00
|
|
|
* ============================================================================*/
|
2013-07-14 01:57:26 +02:00
|
|
|
|
2013-08-19 07:17:04 +02:00
|
|
|
#pragma once
|
2013-07-14 01:57:26 +02:00
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
2013-08-30 08:01:07 +02:00
|
|
|
#include <atomic>
|
|
|
|
#include <chrono>
|
|
|
|
#include <thread>
|
2013-07-14 03:33:00 +02:00
|
|
|
#include <algorithm>
|
2015-07-16 09:55:23 +02:00
|
|
|
#include "g3log/logworker.hpp"
|
|
|
|
#include "g3log/logmessage.hpp"
|
|
|
|
#include "g3log/filesink.hpp"
|
2013-07-14 01:57:26 +02:00
|
|
|
|
2013-08-19 07:17:04 +02:00
|
|
|
namespace testing_helpers {
|
|
|
|
|
2013-11-07 09:56:48 +01:00
|
|
|
std::string mockFatalMessage();
|
|
|
|
int mockFatalSignal();
|
|
|
|
bool mockFatalWasCalled();
|
2015-07-20 07:10:56 +02:00
|
|
|
void mockFatalCall(g3::FatalMessagePtr fatal_message);
|
2013-11-07 09:56:48 +01:00
|
|
|
void clearMockFatal();
|
|
|
|
|
2013-08-19 07:17:04 +02:00
|
|
|
bool removeFile(std::string path_to_file);
|
2020-06-19 21:49:30 +02:00
|
|
|
bool verifyContent(const std::string& total_text, std::string msg_to_find);
|
2013-11-07 09:56:48 +01:00
|
|
|
std::string readFileToText(std::string filename);
|
2020-06-19 21:49:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** 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();
|
|
|
|
|
|
|
|
g3::LogWorker* get();
|
|
|
|
std::unique_ptr<g3::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 >
|
2020-12-16 14:51:26 +01:00
|
|
|
std::invoke_result_t<Call(Args...)> callToLogger(Call call, Args&& ... args) {
|
2020-06-19 21:49:30 +02:00
|
|
|
auto func = std::bind(call, _scope->get(), std::forward<Args>(args)...);
|
|
|
|
return func();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string logFile();
|
|
|
|
std::string resetAndRetrieveContent();
|
|
|
|
std::unique_ptr<g3::SinkHandle<g3::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;
|
|
|
|
}
|
|
|
|
};
|
2013-08-19 07:17:04 +02:00
|
|
|
} // testing_helpers
|
2013-07-14 01:57:26 +02:00
|
|
|
|
2016-10-03 16:00:08 +02:00
|
|
|
#ifdef CHANGE_G3LOG_DEBUG_TO_DBUG
|
|
|
|
#undef DEBUG
|
|
|
|
#define DEBUG DBUG
|
|
|
|
#endif
|
2013-07-14 03:33:00 +02:00
|
|
|
|
2013-07-14 01:57:26 +02:00
|
|
|
|