2013-12-16 06:29:26 +01:00
|
|
|
/** ==========================================================================
|
2014-07-03 23:42:19 +02: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.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
2015-07-20 07:10:56 +02:00
|
|
|
#include <g3log/g3log.hpp>
|
|
|
|
#include <g3log/logworker.hpp>
|
|
|
|
#include <g3log/std2_make_unique.hpp>
|
|
|
|
#include <g3log/logmessage.hpp>
|
|
|
|
|
|
|
|
#include "testing_helpers.h"
|
|
|
|
|
|
|
|
|
2013-07-14 01:57:26 +02:00
|
|
|
#include <gtest/gtest.h>
|
2013-08-19 07:17:04 +02:00
|
|
|
#include <iostream>
|
2013-11-07 09:56:48 +01:00
|
|
|
#include <fstream>
|
2013-07-14 01:57:26 +02:00
|
|
|
|
|
|
|
using namespace std;
|
2015-07-20 07:10:56 +02:00
|
|
|
using namespace g3;
|
2013-11-08 09:45:10 +01:00
|
|
|
|
2013-08-19 07:17:04 +02:00
|
|
|
namespace testing_helpers {
|
|
|
|
|
2013-11-07 09:56:48 +01:00
|
|
|
std::string g_mockFatal_message = {};
|
|
|
|
int g_mockFatal_signal = -1;
|
|
|
|
bool g_mockFatalWasCalled = false;
|
|
|
|
|
2013-11-08 09:45:10 +01:00
|
|
|
std::string mockFatalMessage() {
|
|
|
|
return g_mockFatal_message;
|
|
|
|
}
|
|
|
|
|
|
|
|
int mockFatalSignal() {
|
|
|
|
return g_mockFatal_signal;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool mockFatalWasCalled() {
|
|
|
|
return g_mockFatalWasCalled;
|
|
|
|
}
|
2013-11-07 09:56:48 +01:00
|
|
|
|
2013-11-12 10:21:40 +01:00
|
|
|
void mockFatalCall(FatalMessagePtr fatal_message) {
|
|
|
|
g_mockFatal_message = fatal_message.get()->toString();
|
|
|
|
g_mockFatal_signal = fatal_message.get()->_signal_id;
|
2013-11-07 09:56:48 +01:00
|
|
|
g_mockFatalWasCalled = true;
|
2013-11-12 10:21:40 +01:00
|
|
|
LogMessagePtr message{fatal_message.release()};
|
2015-07-20 07:10:56 +02:00
|
|
|
g3::internal::pushMessageToLogger(message); //fatal_message.copyToLogMessage());
|
2013-11-07 09:56:48 +01:00
|
|
|
}
|
2013-11-08 09:45:10 +01:00
|
|
|
|
2013-11-07 09:56:48 +01:00
|
|
|
void clearMockFatal() {
|
2013-11-23 09:12:00 +01:00
|
|
|
g_mockFatal_message.clear();
|
2013-11-07 09:56:48 +01:00
|
|
|
g_mockFatal_signal = -1;
|
|
|
|
g_mockFatalWasCalled = false;
|
|
|
|
}
|
|
|
|
|
2013-08-19 07:17:04 +02:00
|
|
|
bool removeFile(std::string path_to_file) {
|
|
|
|
return (0 == std::remove(path_to_file.c_str()));
|
|
|
|
}
|
|
|
|
|
2013-11-07 09:56:48 +01:00
|
|
|
bool verifyContent(const std::string &total_text, std::string msg_to_find) {
|
|
|
|
std::string content(total_text);
|
|
|
|
size_t location = content.find(msg_to_find);
|
|
|
|
return (location != std::string::npos);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string readFileToText(std::string filename) {
|
|
|
|
std::ifstream in;
|
|
|
|
in.open(filename.c_str(), std::ios_base::in);
|
|
|
|
if (!in.is_open()) {
|
|
|
|
return
|
|
|
|
{
|
|
|
|
}; // error just return empty string - test will 'fault'
|
|
|
|
}
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << in.rdbuf();
|
|
|
|
return oss.str();
|
|
|
|
}
|
2013-11-08 09:45:10 +01:00
|
|
|
|
|
|
|
size_t LogFileCleaner::size() {
|
2013-08-19 07:17:04 +02:00
|
|
|
return logs_to_clean_.size();
|
|
|
|
}
|
|
|
|
|
2014-03-09 07:33:49 +01:00
|
|
|
|
2013-08-19 07:17:04 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2013-07-14 03:33:00 +02:00
|
|
|
}
|
2013-08-19 07:17:04 +02:00
|
|
|
}
|
2013-10-05 06:14:35 +02:00
|
|
|
|
2015-07-20 07:10:56 +02:00
|
|
|
ScopedLogger::ScopedLogger() : _currentWorker(g3::LogWorker::createWithNoSink()) {}
|
2014-03-09 07:33:49 +01:00
|
|
|
ScopedLogger::~ScopedLogger() {}
|
2013-11-08 09:45:10 +01:00
|
|
|
|
2015-07-20 07:10:56 +02:00
|
|
|
g3::LogWorker* ScopedLogger::get() {
|
2014-03-09 07:33:49 +01:00
|
|
|
return _currentWorker.get();
|
|
|
|
}
|
2013-11-08 09:45:10 +01:00
|
|
|
|
2013-10-05 06:14:35 +02:00
|
|
|
RestoreFileLogger::RestoreFileLogger(std::string directory)
|
2015-07-20 07:10:56 +02:00
|
|
|
: _scope(new ScopedLogger), _handle(_scope->get()->addSink(std2::make_unique<g3::FileSink>("UNIT_TEST_LOGGER", directory), &g3::FileSink::fileWrite)) {
|
|
|
|
using namespace g3;
|
|
|
|
g3::initializeLogging(_scope->_currentWorker.get());
|
2013-11-08 09:45:10 +01:00
|
|
|
clearMockFatal();
|
2015-02-24 07:29:02 +01:00
|
|
|
setFatalExitHandler(&mockFatalCall);
|
2013-10-05 06:14:35 +02:00
|
|
|
|
2013-11-07 09:56:48 +01:00
|
|
|
auto filename = _handle->call(&FileSink::fileName);
|
2013-10-05 06:14:35 +02:00
|
|
|
if (!filename.valid()) ADD_FAILURE();
|
2013-11-07 09:56:48 +01:00
|
|
|
_log_file = filename.get();
|
2014-03-09 07:33:49 +01:00
|
|
|
|
2015-07-20 07:10:56 +02:00
|
|
|
#ifdef G3_DYNAMIC_LOGGING
|
2015-07-20 16:00:33 +02:00
|
|
|
g3::only_change_at_initialization::setLogLevel(INFO, true);
|
|
|
|
g3::only_change_at_initialization::setLogLevel(DEBUG, true);
|
|
|
|
g3::only_change_at_initialization::setLogLevel(WARNING, true);
|
|
|
|
g3::only_change_at_initialization::setLogLevel(FATAL, true);
|
2014-01-09 06:48:18 +01:00
|
|
|
#endif
|
2013-08-21 07:40:39 +02:00
|
|
|
}
|
|
|
|
|
2013-10-05 06:14:35 +02:00
|
|
|
RestoreFileLogger::~RestoreFileLogger() {
|
2015-07-20 07:10:56 +02:00
|
|
|
g3::internal::shutDownLogging(); // is done at reset. Added for test clarity
|
2013-12-16 06:16:32 +01:00
|
|
|
reset();
|
2013-11-08 09:45:10 +01:00
|
|
|
|
2013-11-08 10:31:32 +01:00
|
|
|
if (!removeFile(_log_file))
|
|
|
|
ADD_FAILURE();
|
2013-10-05 06:14:35 +02:00
|
|
|
}
|
|
|
|
|
2013-12-10 22:57:04 +01:00
|
|
|
std::string RestoreFileLogger::logFile() {
|
2014-03-09 07:33:49 +01:00
|
|
|
if (_scope) {
|
|
|
|
// beware for race condition
|
|
|
|
// example:
|
|
|
|
// LOG(INFO) << ...
|
|
|
|
// auto file = logger.logFile()
|
|
|
|
// auto content = ReadContentFromFile(file)
|
|
|
|
// ... it is not guaranteed that the content will contain (yet) the LOG(INFO)
|
2015-07-20 07:10:56 +02:00
|
|
|
std::future<std::string> filename = _handle->call(&g3::FileSink::fileName);
|
2014-03-09 07:33:49 +01:00
|
|
|
_log_file = filename.get();
|
|
|
|
}
|
|
|
|
return _log_file;
|
|
|
|
}
|
|
|
|
|
2013-11-08 09:45:10 +01:00
|
|
|
// Beware of race between LOG(...) and this function.
|
|
|
|
// since LOG(...) passes two queues but the handle::call only passes one queue
|
|
|
|
// the handle::call can happen faster
|
|
|
|
std::string RestoreFileLogger::resetAndRetrieveContent() {
|
2015-07-20 07:10:56 +02:00
|
|
|
std::future<std::string> filename = _handle->call(&g3::FileSink::fileName);
|
2013-11-08 09:45:10 +01:00
|
|
|
reset(); // flush all queues to sinks
|
2013-11-07 09:56:48 +01:00
|
|
|
EXPECT_TRUE(filename.valid());
|
|
|
|
auto file = filename.get();
|
|
|
|
return readFileToText(file);
|
|
|
|
}
|
2013-08-19 07:17:04 +02:00
|
|
|
} // testing_helpers
|