2013-08-02 07:06:12 +02:00
|
|
|
/** ==========================================================================
|
|
|
|
* 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.
|
2016-02-17 06:52:38 +01:00
|
|
|
*
|
2014-07-03 23:42:19 +02:00
|
|
|
* For more information see g3log/LICENSE or refer refer to http://unlicense.org
|
2013-08-02 07:06:12 +02:00
|
|
|
* ============================================================================*/
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <fstream>
|
|
|
|
#include <future>
|
2023-12-01 00:17:45 +01:00
|
|
|
#include <memory>
|
2013-08-02 07:06:12 +02:00
|
|
|
#include <queue>
|
2023-12-01 00:17:45 +01:00
|
|
|
#include <string>
|
2013-08-02 07:06:12 +02:00
|
|
|
|
|
|
|
#include <thread>
|
2015-07-16 09:55:23 +02:00
|
|
|
#include "g3log/g3log.hpp"
|
|
|
|
#include "g3log/logworker.hpp"
|
2013-08-02 07:06:12 +02:00
|
|
|
#include "testing_helpers.h"
|
|
|
|
|
2013-08-19 07:18:18 +02:00
|
|
|
using namespace testing_helpers;
|
2013-08-02 07:06:12 +02:00
|
|
|
|
2023-12-01 00:17:45 +01:00
|
|
|
namespace { // anonymous
|
2016-02-17 06:52:38 +01:00
|
|
|
const char* name_path_1 = "./(some_fake_DirectoryOrName_1_)";
|
2016-02-17 08:06:45 +01:00
|
|
|
const std::string kReplaceFileName = "(ReplaceLogFile)";
|
2016-02-17 06:52:38 +01:00
|
|
|
g3::LogWorker* g_logger_ptr = nullptr;
|
|
|
|
g3::SinkHandle<g3::FileSink>* g_filesink_handler = nullptr;
|
|
|
|
LogFileCleaner* g_cleaner_ptr = nullptr;
|
|
|
|
|
2016-02-17 08:06:45 +01:00
|
|
|
std::string setLogNameAndAddCount(std::string new_file_to_create, std::string logger_id = "g3log") {
|
2016-02-17 06:52:38 +01:00
|
|
|
static std::mutex m;
|
|
|
|
static int count;
|
|
|
|
std::string add_count;
|
|
|
|
std::lock_guard<std::mutex> lock(m);
|
|
|
|
{
|
|
|
|
add_count = std::to_string(++count) + "_";
|
2016-02-17 08:06:45 +01:00
|
|
|
auto future_new_log = g_filesink_handler->call(&g3::FileSink::changeLogFile, new_file_to_create + add_count, logger_id);
|
2016-02-17 06:52:38 +01:00
|
|
|
auto new_log = future_new_log.get();
|
|
|
|
if (!new_log.empty()) {
|
|
|
|
g_cleaner_ptr->addLogToClean(new_log);
|
|
|
|
} else {
|
2016-02-17 08:06:45 +01:00
|
|
|
std::cout << "\nFailed to set filename: " << new_file_to_create << std::endl;
|
2016-02-17 06:52:38 +01:00
|
|
|
}
|
|
|
|
return new_log;
|
|
|
|
}
|
|
|
|
return add_count;
|
|
|
|
}
|
|
|
|
|
2016-02-17 08:06:45 +01:00
|
|
|
std::string setLogName(std::string new_file_to_create, std::string logger_id = "g3log") {
|
|
|
|
auto future_new_log = g_filesink_handler->call(&g3::FileSink::changeLogFile, new_file_to_create, logger_id);
|
2013-08-02 07:06:12 +02:00
|
|
|
auto new_log = future_new_log.get();
|
2023-12-01 00:17:45 +01:00
|
|
|
if (!new_log.empty())
|
|
|
|
g_cleaner_ptr->addLogToClean(new_log);
|
2013-08-02 07:06:12 +02:00
|
|
|
return new_log;
|
2016-02-17 06:52:38 +01:00
|
|
|
}
|
2013-08-02 07:06:12 +02:00
|
|
|
|
2016-02-17 06:52:38 +01:00
|
|
|
std::string getLogName() {
|
|
|
|
return g_filesink_handler->call(&g3::FileSink::fileName).get();
|
|
|
|
}
|
2013-08-02 07:06:12 +02:00
|
|
|
|
2023-12-01 00:17:45 +01:00
|
|
|
} // namespace
|
2013-08-02 07:06:12 +02:00
|
|
|
|
|
|
|
TEST(TestOf_GetFileName, Expecting_ValidLogFile) {
|
|
|
|
|
2016-02-17 06:52:38 +01:00
|
|
|
LOG(INFO) << "test_filechange, Retrieving file name: ";
|
|
|
|
ASSERT_NE(g_logger_ptr, nullptr);
|
|
|
|
ASSERT_FALSE(getLogName().empty());
|
2013-08-02 07:06:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TestOf_ChangingLogFile, Expecting_NewLogFileUsed) {
|
2016-02-17 06:52:38 +01:00
|
|
|
auto old_log = getLogName();
|
|
|
|
std::string name = setLogNameAndAddCount(name_path_1);
|
|
|
|
auto new_log = setLogName(name);
|
|
|
|
ASSERT_NE(old_log, new_log);
|
2013-08-02 07:06:12 +02:00
|
|
|
}
|
|
|
|
|
2016-02-17 08:06:45 +01:00
|
|
|
TEST(TestOf_ChangingLogFile_Id, Expecting_NewLogFileUsed1) {
|
|
|
|
auto old_log = getLogName();
|
2016-03-18 17:50:59 +01:00
|
|
|
setLogNameAndAddCount(name_path_1);
|
2016-02-17 08:06:45 +01:00
|
|
|
auto new_log = setLogName("foo", "new_logger_id");
|
|
|
|
ASSERT_NE(old_log, new_log);
|
|
|
|
std::string new_name = getLogName();
|
|
|
|
auto expected_part_of__new_name = std::string("foo") + kReplaceFileName + ".new_logger_id";
|
|
|
|
auto extracted_name = new_name.substr(0, expected_part_of__new_name.size());
|
|
|
|
ASSERT_EQ(extracted_name.c_str(), expected_part_of__new_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TestOf_ChangingLogFile_NoId, Expecting_NewLogFileUsed2) {
|
|
|
|
auto old_log = getLogName();
|
2016-03-18 17:50:59 +01:00
|
|
|
setLogNameAndAddCount(name_path_1);
|
2016-02-17 08:06:45 +01:00
|
|
|
auto new_log = setLogName("foo", "");
|
|
|
|
ASSERT_NE(old_log, new_log);
|
|
|
|
std::string new_name = getLogName();
|
|
|
|
auto expected_part_of__new_name = std::string("foo") + kReplaceFileName;
|
|
|
|
auto extracted_name = new_name.substr(0, expected_part_of__new_name.size());
|
|
|
|
ASSERT_EQ(extracted_name.c_str(), expected_part_of__new_name);
|
|
|
|
}
|
|
|
|
|
2013-08-02 07:06:12 +02:00
|
|
|
TEST(TestOf_ManyThreadsChangingLogFileName, Expecting_EqualNumberLogsCreated) {
|
2016-02-17 06:52:38 +01:00
|
|
|
auto old_log = g_filesink_handler->call(&g3::FileSink::fileName).get();
|
2023-12-01 00:17:45 +01:00
|
|
|
if (!old_log.empty())
|
|
|
|
g_cleaner_ptr->addLogToClean(old_log);
|
2016-02-17 06:52:38 +01:00
|
|
|
|
|
|
|
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_";
|
2016-02-17 08:06:45 +01:00
|
|
|
std::string logger_id = std::to_string(count);
|
|
|
|
threads.push_back(std::thread(setLogNameAndAddCount, drive, logger_id));
|
2016-02-17 06:52:38 +01:00
|
|
|
}
|
|
|
|
for (auto& thread : threads)
|
|
|
|
thread.join();
|
|
|
|
|
|
|
|
// check that all logs were created
|
|
|
|
ASSERT_EQ(size + max, g_cleaner_ptr->size());
|
2013-08-02 07:06:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TestOf_IllegalLogFileName, Expecting_NoChangeToOriginalFileName) {
|
2016-02-17 06:52:38 +01:00
|
|
|
std::string original = getLogName();
|
2023-12-01 00:17:45 +01:00
|
|
|
auto perhaps_a_name = setLogName("XY:/"); // does not exist
|
2016-02-17 06:52:38 +01:00
|
|
|
ASSERT_TRUE(perhaps_a_name.empty());
|
|
|
|
std::string post_illegal = getLogName();
|
|
|
|
ASSERT_STREQ(original.c_str(), post_illegal.c_str());
|
|
|
|
}
|
|
|
|
|
2016-02-17 08:06:45 +01:00
|
|
|
TEST(TestOf_SinkHandleDifferentId, Expecting_DifferentId) {
|
2018-02-21 06:02:19 +01:00
|
|
|
auto sink = std::make_unique<g3::FileSink>("AnotherLogFile", name_path_1, "logger_id");
|
2016-02-17 08:06:45 +01:00
|
|
|
auto name = sink->fileName();
|
2023-12-01 00:17:45 +01:00
|
|
|
ASSERT_STREQ(name.substr(0, 26).c_str(), "./AnotherLogFile.logger_id");
|
2016-02-17 08:06:45 +01:00
|
|
|
g_cleaner_ptr->addLogToClean(name);
|
|
|
|
}
|
2016-02-17 06:52:38 +01:00
|
|
|
|
2023-12-01 00:17:45 +01:00
|
|
|
TEST(TestOf_LegalLogFileNam, With_parenthesis) {
|
2016-02-17 06:52:38 +01:00
|
|
|
std::string original = getLogName();
|
2023-12-01 00:17:45 +01:00
|
|
|
auto perhaps_a_name = setLogName("(test)"); // does not exist
|
2016-03-18 17:50:59 +01:00
|
|
|
EXPECT_NE(original, perhaps_a_name);
|
2016-02-17 06:52:38 +01:00
|
|
|
std::string post_legal = getLogName();
|
|
|
|
EXPECT_TRUE(std::string::npos != post_legal.find("(test)")) << "filename was: " << post_legal;
|
2013-08-02 07:06:12 +02:00
|
|
|
}
|
|
|
|
|
2016-02-17 06:52:38 +01:00
|
|
|
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 worker = g3::LogWorker::createLogWorker();
|
2016-02-17 08:25:55 +01:00
|
|
|
auto handle = worker->addDefaultLogger(kReplaceFileName, name_path_1);
|
2016-02-17 06:52:38 +01:00
|
|
|
g_logger_ptr = worker.get();
|
|
|
|
g_filesink_handler = handle.get();
|
|
|
|
last_log_file = g_filesink_handler->call(&g3::FileSink::fileName).get();
|
2016-02-17 08:25:55 +01:00
|
|
|
std::cout << "log file at: " << last_log_file << std::endl;
|
|
|
|
cleaner.addLogToClean(last_log_file);
|
2016-02-17 06:52:38 +01:00
|
|
|
|
|
|
|
g3::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(&g3::FileSink::fileName).get();
|
|
|
|
std::cout << "log file at: " << last_log_file << std::endl;
|
2016-02-17 06:56:21 +01:00
|
|
|
//g3::internal::shutDownLogging();
|
2016-02-17 06:52:38 +01:00
|
|
|
}
|
|
|
|
std::cout << "FINISHED WITH THE TESTING" << std::endl;
|
|
|
|
// cleaning up
|
2016-02-17 08:25:55 +01:00
|
|
|
cleaner.addLogToClean(last_log_file);
|
2016-02-17 06:52:38 +01:00
|
|
|
return return_value;
|
2016-02-17 08:06:45 +01:00
|
|
|
}
|