mirror of
https://github.com/KjellKod/g3log.git
synced 2024-12-12 10:23:50 +01:00
Allow parenthesis in path and filename
This commit is contained in:
parent
579579962c
commit
a0b961ee04
@ -60,10 +60,12 @@ namespace g3 {
|
||||
auto now_formatted = g3::localtime_formatted(now, {internal::date_formatted + " " + internal::time_formatted});
|
||||
|
||||
std::string file_name = createLogFileName(_log_prefix_backup);
|
||||
std::string prospect_log = directory + file_name;
|
||||
std::string prospect_log = directory + file_name;
|
||||
|
||||
std::unique_ptr<std::ofstream> log_stream = createLogFile(prospect_log);
|
||||
if (nullptr == log_stream) {
|
||||
filestream() << "\n" << now_formatted << " Unable to change log file. Illegal filename or busy? Unsuccessful log name was: " << prospect_log;
|
||||
filestream() << "\n" << now_formatted << " Unable to change log file."
|
||||
<< " Illegal filename or busy? Unsuccessful log name was: " << prospect_log;
|
||||
return {}; // no success
|
||||
}
|
||||
|
||||
|
@ -24,7 +24,7 @@ namespace g3 {
|
||||
|
||||
// check for filename validity - filename should not be part of PATH
|
||||
bool isValidFilename(const std::string &prefix_filename) {
|
||||
std::string illegal_characters("/,|<>:#$%{}()[]\'\"^!?+* ");
|
||||
std::string illegal_characters("/,|<>:#$%{}[]\'\"^!?+* ");
|
||||
size_t pos = prefix_filename.find_first_of(illegal_characters, 0);
|
||||
if (pos != std::string::npos) {
|
||||
std::cerr << "Illegal character [" << prefix_filename.at(pos) << "] in logname prefix: " << "[" << prefix_filename << "]" << std::endl;
|
||||
|
@ -2,7 +2,7 @@
|
||||
* 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
|
||||
* ============================================================================*/
|
||||
|
||||
@ -25,112 +25,124 @@ using namespace testing_helpers;
|
||||
|
||||
|
||||
namespace { // anonymous
|
||||
const char* name_path_1 = "./some_fake_DirectoryOrName_1_";
|
||||
g3::LogWorker* g_logger_ptr = nullptr;
|
||||
g3::SinkHandle<g3::FileSink>* g_filesink_handler = nullptr;
|
||||
LogFileCleaner* g_cleaner_ptr = nullptr;
|
||||
const char* name_path_1 = "./(some_fake_DirectoryOrName_1_)";
|
||||
g3::LogWorker* g_logger_ptr = nullptr;
|
||||
g3::SinkHandle<g3::FileSink>* g_filesink_handler = nullptr;
|
||||
LogFileCleaner* g_cleaner_ptr = nullptr;
|
||||
|
||||
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(&g3::FileSink::changeLogFile, new_file_to_create + add_count);
|
||||
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(&g3::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);
|
||||
} else {
|
||||
std::cout << "\nFailed to set filename: " << new_file_to_create << std::endl;
|
||||
}
|
||||
return new_log;
|
||||
}
|
||||
return add_count;
|
||||
}
|
||||
|
||||
std::string setLogName(std::string new_file_to_create) {
|
||||
auto future_new_log = g_filesink_handler->call(&g3::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;
|
||||
}
|
||||
return add_count;
|
||||
}
|
||||
}
|
||||
|
||||
std::string setLogName(std::string new_file_to_create) {
|
||||
auto future_new_log = g_filesink_handler->call(&g3::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(&g3::FileSink::fileName).get();
|
||||
}
|
||||
std::string getLogName() {
|
||||
return g_filesink_handler->call(&g3::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());
|
||||
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);
|
||||
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(&g3::FileSink::fileName).get();
|
||||
if (!old_log.empty()) g_cleaner_ptr->addLogToClean(old_log);
|
||||
auto old_log = g_filesink_handler->call(&g3::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();
|
||||
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());
|
||||
// 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());
|
||||
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 worker = g3::LogWorker::createLogWorker();
|
||||
auto handle= worker->addDefaultLogger("ReplaceLogFile", name_path_1);
|
||||
g_logger_ptr = worker.get();
|
||||
g_filesink_handler = handle.get();
|
||||
last_log_file = g_filesink_handler->call(&g3::FileSink::fileName).get();
|
||||
cleaner.addLogToClean(last_log_file);
|
||||
|
||||
|
||||
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;
|
||||
//g3::shutDownLogging();
|
||||
}
|
||||
std::cout << "FINISHED WITH THE TESTING" << std::endl;
|
||||
// cleaning up
|
||||
cleaner.addLogToClean(last_log_file);
|
||||
return return_value;
|
||||
TEST(TestOf_LegalLogFileNam, With_parenthesis) {
|
||||
std::string original = getLogName();
|
||||
auto perhaps_a_name = setLogName("(test)"); // does not exist
|
||||
std::string post_legal = getLogName();
|
||||
EXPECT_TRUE(std::string::npos != post_legal.find("(test)")) << "filename was: " << post_legal;
|
||||
}
|
||||
|
||||
|
||||
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();
|
||||
auto handle = worker->addDefaultLogger("(ReplaceLogFile)", name_path_1);
|
||||
g_logger_ptr = worker.get();
|
||||
g_filesink_handler = handle.get();
|
||||
last_log_file = g_filesink_handler->call(&g3::FileSink::fileName).get();
|
||||
cleaner.addLogToClean(last_log_file);
|
||||
|
||||
|
||||
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;
|
||||
g3::internal::shutDownLogging();
|
||||
}
|
||||
std::cout << "FINISHED WITH THE TESTING" << std::endl;
|
||||
// cleaning up
|
||||
//cleaner.addLogToClean(last_log_file);
|
||||
return return_value;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user