mirror of
https://github.com/KjellKod/g3log.git
synced 2024-12-13 10:42:56 +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
154 lines
4.9 KiB
C++
154 lines
4.9 KiB
C++
/** ==========================================================================
|
|
* 2012 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 <memory>
|
|
#include <fstream>
|
|
#include <string>
|
|
#include <memory>
|
|
#include <future>
|
|
#include <queue>
|
|
|
|
|
|
#include <thread>
|
|
#include "g2log.hpp"
|
|
#include "g2logworker.hpp"
|
|
#include "testing_helpers.h"
|
|
|
|
using namespace testing_helpers;
|
|
|
|
|
|
namespace { // anonymous
|
|
const char* name_path_1 = "./some_fake_DirectoryOrName_1_";
|
|
g2::LogWorker* g_logger_ptr = nullptr;
|
|
g2::SinkHandle<g2::FileSink>* g_filesink_handler = nullptr;
|
|
LogFileCleaner* g_cleaner_ptr = nullptr;
|
|
|
|
bool isTextAvailableInContent(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();
|
|
std::string content(oss.str());
|
|
return content;
|
|
}
|
|
|
|
std::string setLogNameAndAddCount(std::string new_file_to_create) {
|
|
static std::mutex m;
|
|
static int count;
|
|
std::string add_count;
|
|
std::lock_guard<std::mutex> lock(m);
|
|
{
|
|
add_count = std::to_string(++count) + "_";
|
|
auto future_new_log = g_filesink_handler->call(&g2::FileSink::changeLogFile, new_file_to_create + add_count);
|
|
auto new_log = future_new_log.get();
|
|
if (!new_log.empty()) g_cleaner_ptr->addLogToClean(new_log);
|
|
return new_log;
|
|
}
|
|
return add_count;
|
|
}
|
|
|
|
std::string setLogName(std::string new_file_to_create) {
|
|
auto future_new_log = g_filesink_handler->call(&g2::FileSink::changeLogFile, new_file_to_create);
|
|
auto new_log = future_new_log.get();
|
|
if (!new_log.empty()) g_cleaner_ptr->addLogToClean(new_log);
|
|
return new_log;
|
|
}
|
|
|
|
std::string getLogName() {
|
|
return g_filesink_handler->call(&g2::FileSink::fileName).get();
|
|
}
|
|
|
|
} // anonymous
|
|
|
|
TEST(TestOf_GetFileName, Expecting_ValidLogFile) {
|
|
|
|
LOG(INFO) << "test_filechange, Retrieving file name: ";
|
|
ASSERT_NE(g_logger_ptr, nullptr);
|
|
ASSERT_FALSE(getLogName().empty());
|
|
}
|
|
|
|
TEST(TestOf_ChangingLogFile, Expecting_NewLogFileUsed) {
|
|
auto old_log = getLogName();
|
|
std::string name = setLogNameAndAddCount(name_path_1);
|
|
auto new_log = setLogName(name);
|
|
ASSERT_NE(old_log, new_log);
|
|
}
|
|
|
|
TEST(TestOf_ManyThreadsChangingLogFileName, Expecting_EqualNumberLogsCreated) {
|
|
auto old_log = g_filesink_handler->call(&g2::FileSink::fileName).get();
|
|
if (!old_log.empty()) g_cleaner_ptr->addLogToClean(old_log);
|
|
|
|
LOG(INFO) << "SoManyThreadsAllDoingChangeFileName";
|
|
std::vector<std::thread> threads;
|
|
auto max = 2;
|
|
auto size = g_cleaner_ptr->size();
|
|
for (auto count = 0; count < max; ++count) {
|
|
std::string drive = ((count % 2) == 0) ? "./_threadEven_" : "./_threaOdd_";
|
|
threads.push_back(std::thread(setLogNameAndAddCount, drive));
|
|
}
|
|
for (auto& thread : threads)
|
|
thread.join();
|
|
|
|
// check that all logs were created
|
|
ASSERT_EQ(size + max, g_cleaner_ptr->size());
|
|
}
|
|
|
|
TEST(TestOf_IllegalLogFileName, Expecting_NoChangeToOriginalFileName) {
|
|
std::string original = getLogName();
|
|
auto perhaps_a_name = setLogName("XY:/"); // does not exist
|
|
ASSERT_TRUE(perhaps_a_name.empty());
|
|
std::string post_illegal = getLogName();
|
|
ASSERT_STREQ(original.c_str(), post_illegal.c_str());
|
|
}
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
LogFileCleaner cleaner;
|
|
g_cleaner_ptr = &cleaner;
|
|
int return_value = 1;
|
|
std::stringstream cerrDump;
|
|
|
|
std::string last_log_file;
|
|
{
|
|
|
|
testing_helpers::ScopedOut scopedCerr(std::cerr, &cerrDump);
|
|
|
|
auto logger = g2::LogWorker::createWithDefaultLogger("ReplaceLogFile", name_path_1);
|
|
g_logger_ptr = logger.worker.get();
|
|
g_filesink_handler = logger.sink.get();
|
|
last_log_file = g_filesink_handler->call(&g2::FileSink::fileName).get();
|
|
cleaner.addLogToClean(last_log_file);
|
|
|
|
|
|
g2::initializeLogging(g_logger_ptr);
|
|
LOG(INFO) << "test_filechange demo*" << std::endl;
|
|
|
|
testing::InitGoogleTest(&argc, argv);
|
|
return_value = RUN_ALL_TESTS();
|
|
|
|
last_log_file = g_filesink_handler->call(&g2::FileSink::fileName).get();
|
|
std::cout << "log file at: " << last_log_file << std::endl;
|
|
//g2::shutDownLogging();
|
|
}
|
|
std::cout << "FINISHED WITH THE TESTING" << std::endl;
|
|
// cleaning up
|
|
cleaner.addLogToClean(last_log_file);
|
|
return return_value;
|
|
}
|