mirror of
https://github.com/KjellKod/g3log.git
synced 2024-12-13 18:45:12 +01:00
87a095e384
--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
161 lines
5.0 KiB
C++
161 lines
5.0 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
|
|
* ============================================================================*/
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
#include <iostream>
|
|
#include "testing_helpers.h"
|
|
#include "g2log.hpp"
|
|
#include "g2logworker.hpp"
|
|
#include "std2_make_unique.hpp"
|
|
#include "g2logmessage.hpp"
|
|
#include <fstream>
|
|
|
|
using namespace std;
|
|
using namespace g2;
|
|
|
|
namespace testing_helpers {
|
|
|
|
std::string g_mockFatal_message = {};
|
|
int g_mockFatal_signal = -1;
|
|
bool g_mockFatalWasCalled = false;
|
|
|
|
std::string mockFatalMessage() {
|
|
return g_mockFatal_message;
|
|
}
|
|
|
|
int mockFatalSignal() {
|
|
return g_mockFatal_signal;
|
|
}
|
|
|
|
bool mockFatalWasCalled() {
|
|
return g_mockFatalWasCalled;
|
|
}
|
|
|
|
void mockFatalCall(FatalMessagePtr fatal_message) {
|
|
g_mockFatal_message = fatal_message.get()->toString();
|
|
g_mockFatal_signal = fatal_message.get()->_signal_id;
|
|
g_mockFatalWasCalled = true;
|
|
LogMessagePtr message{fatal_message.release()};
|
|
g2::internal::pushMessageToLogger(message); //fatal_message.copyToLogMessage());
|
|
}
|
|
|
|
void clearMockFatal() {
|
|
g_mockFatal_message.clear();
|
|
g_mockFatal_signal = -1;
|
|
g_mockFatalWasCalled = false;
|
|
}
|
|
|
|
bool removeFile(std::string path_to_file) {
|
|
return (0 == std::remove(path_to_file.c_str()));
|
|
}
|
|
|
|
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();
|
|
}
|
|
|
|
size_t LogFileCleaner::size() {
|
|
return logs_to_clean_.size();
|
|
}
|
|
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
ScopedLogger::ScopedLogger() : _currentWorker(g2::LogWorker::createWithNoSink()) {}
|
|
ScopedLogger::~ScopedLogger() {}
|
|
|
|
g2::LogWorker* ScopedLogger::get() {
|
|
return _currentWorker.get();
|
|
}
|
|
|
|
RestoreFileLogger::RestoreFileLogger(std::string directory)
|
|
: _scope(new ScopedLogger), _handle(_scope->get()->addSink(std2::make_unique<g2::FileSink>("UNIT_TEST_LOGGER", directory), &g2::FileSink::fileWrite)) {
|
|
using namespace g2;
|
|
g2::initializeLogging(_scope->_currentWorker.get());
|
|
clearMockFatal();
|
|
internal::changeFatalInitHandlerForUnitTesting(&mockFatalCall);
|
|
|
|
auto filename = _handle->call(&FileSink::fileName);
|
|
if (!filename.valid()) ADD_FAILURE();
|
|
_log_file = filename.get();
|
|
|
|
#ifdef G2_DYNAMIC_LOGGING
|
|
g2::setLogLevel(INFO, true);
|
|
g2::setLogLevel(DEBUG, true);
|
|
g2::setLogLevel(WARNING, true);
|
|
g2::setLogLevel(FATAL, true);
|
|
#endif
|
|
}
|
|
|
|
RestoreFileLogger::~RestoreFileLogger() {
|
|
g2::internal::shutDownLogging(); // is done at reset. Added for test clarity
|
|
reset();
|
|
|
|
if (!removeFile(_log_file))
|
|
ADD_FAILURE();
|
|
}
|
|
|
|
std::string RestoreFileLogger::logFile() {
|
|
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)
|
|
std::future<std::string> filename = _handle->call(&g2::FileSink::fileName);
|
|
_log_file = filename.get();
|
|
}
|
|
return _log_file;
|
|
}
|
|
|
|
// 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() {
|
|
std::future<std::string> filename = _handle->call(&g2::FileSink::fileName);
|
|
reset(); // flush all queues to sinks
|
|
EXPECT_TRUE(filename.valid());
|
|
auto file = filename.get();
|
|
return readFileToText(file);
|
|
}
|
|
} // testing_helpers
|