mirror of
https://github.com/KjellKod/g3log.git
synced 2024-12-12 10:23:50 +01:00
fixed merge conflict
This commit is contained in:
commit
b7704b1ed7
@ -24,7 +24,7 @@ namespace g3 {
|
|||||||
|
|
||||||
// check for filename validity - filename should not be part of PATH
|
// check for filename validity - filename should not be part of PATH
|
||||||
bool isValidFilename(const std::string &prefix_filename) {
|
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);
|
size_t pos = prefix_filename.find_first_of(illegal_characters, 0);
|
||||||
if (pos != std::string::npos) {
|
if (pos != std::string::npos) {
|
||||||
std::cerr << "Illegal character [" << prefix_filename.at(pos) << "] in logname prefix: " << "[" << prefix_filename << "]" << std::endl;
|
std::cerr << "Illegal character [" << prefix_filename.at(pos) << "] in logname prefix: " << "[" << prefix_filename << "]" << std::endl;
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
* PUBLIC DOMAIN and Not copywrited since it was built on public-domain software and influenced
|
* PUBLIC DOMAIN and Not copywrited since it was built on public-domain software and influenced
|
||||||
* at least in "spirit" from the following sources
|
* at least in "spirit" from the following sources
|
||||||
* 1. kjellkod.cc ;)
|
* 1. kjellkod.cc ;)
|
||||||
* 2. Dr.Dobbs, Petru Marginean: http://drdobbs.com/article/printableArticle.jhtml?articleId=201804215&dept_url=/cpp/
|
* 2. Dr.Dobbs, Petru Marginean: http://drdobbs.com/article/printableArticle.jhtml?articleId=201804215&dept_url=/caddpp/
|
||||||
* 3. Dr.Dobbs, Michael Schulze: http://drdobbs.com/article/printableArticle.jhtml?articleId=225700666&dept_url=/cpp/
|
* 3. Dr.Dobbs, Michael Schulze: http://drdobbs.com/article/printableArticle.jhtml?articleId=225700666&dept_url=/cpp/
|
||||||
* 4. Google 'glog': http://google-glog.googlecode.com/svn/trunk/doc/glog.html
|
* 4. Google 'glog': http://google-glog.googlecode.com/svn/trunk/doc/glog.html
|
||||||
* 5. Various Q&A at StackOverflow
|
* 5. Various Q&A at StackOverflow
|
||||||
|
@ -84,7 +84,7 @@ namespace g3 {
|
|||||||
std::cout << "The filename is: " << log_file_name.get() << std::endl;
|
std::cout << "The filename is: " << log_file_name.get() << std::endl;
|
||||||
// something like: /tmp/my_test_log.g3log.20150819-100300.log
|
// something like: /tmp/my_test_log.g3log.20150819-100300.log
|
||||||
*/
|
*/
|
||||||
std::unique_ptr<FileSinkHandle> addDefaultLogger(const std::string& log_prefix, const std::string& log_directory);
|
std::unique_ptr<FileSinkHandle> addDefaultLogger(const std::string& log_prefix, const std::string& log_directory, const std::string& default_id = "g3log");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -120,8 +120,8 @@ namespace g3 {
|
|||||||
return std::unique_ptr<LogWorker>(new LogWorker);
|
return std::unique_ptr<LogWorker>(new LogWorker);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<FileSinkHandle>LogWorker::addDefaultLogger(const std::string& log_prefix, const std::string& log_directory) {
|
std::unique_ptr<FileSinkHandle>LogWorker::addDefaultLogger(const std::string& log_prefix, const std::string& log_directory, const std::string& default_id) {
|
||||||
return addSink(std2::make_unique<g3::FileSink>(log_prefix, log_directory), &FileSink::fileWrite);
|
return addSink(std2::make_unique<g3::FileSink>(log_prefix, log_directory, default_id), &FileSink::fileWrite);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,162 +1,178 @@
|
|||||||
/** ==========================================================================
|
/** ==========================================================================
|
||||||
* 2012 by KjellKod.cc. This is PUBLIC DOMAIN to use at your own risk and comes
|
* 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
|
* with no warranties. This code is yours to share, use and modify with no
|
||||||
* strings attached and no restrictions or obligations.
|
* strings attached and no restrictions or obligations.
|
||||||
*
|
*
|
||||||
* For more information see g3log/LICENSE or refer refer to http://unlicense.org
|
* For more information see g3log/LICENSE or refer refer to http://unlicense.org
|
||||||
* ============================================================================*/
|
* ============================================================================*/
|
||||||
|
|
||||||
|
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <future>
|
#include <future>
|
||||||
#include <queue>
|
#include <queue>
|
||||||
|
|
||||||
|
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include "g3log/g3log.hpp"
|
#include "g3log/g3log.hpp"
|
||||||
#include "g3log/logworker.hpp"
|
#include "g3log/logworker.hpp"
|
||||||
#include "testing_helpers.h"
|
#include "testing_helpers.h"
|
||||||
|
|
||||||
using namespace testing_helpers;
|
using namespace testing_helpers;
|
||||||
|
|
||||||
|
|
||||||
namespace { // anonymous
|
namespace { // anonymous
|
||||||
const char* name_path_1 = "./some_fake_DirectoryOrName_1_";
|
const char* name_path_1 = "./(some_fake_DirectoryOrName_1_)";
|
||||||
g3::LogWorker* g_logger_ptr = nullptr;
|
const std::string kReplaceFileName = "(ReplaceLogFile)";
|
||||||
g3::SinkHandle<g3::FileSink>* g_filesink_handler = nullptr;
|
g3::LogWorker* g_logger_ptr = nullptr;
|
||||||
LogFileCleaner* g_cleaner_ptr = nullptr;
|
g3::SinkHandle<g3::FileSink>* g_filesink_handler = nullptr;
|
||||||
|
LogFileCleaner* g_cleaner_ptr = nullptr;
|
||||||
std::string setLogNameAndAddCount(std::string new_file_to_create, std::string logger_id = "g3log") {
|
|
||||||
static std::mutex m;
|
std::string setLogNameAndAddCount(std::string new_file_to_create, std::string logger_id = "g3log") {
|
||||||
static int count;
|
static std::mutex m;
|
||||||
std::string add_count;
|
static int count;
|
||||||
std::lock_guard<std::mutex> lock(m);
|
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, logger_id);
|
add_count = std::to_string(++count) + "_";
|
||||||
auto new_log = future_new_log.get();
|
auto future_new_log = g_filesink_handler->call(&g3::FileSink::changeLogFile, new_file_to_create + add_count, logger_id);
|
||||||
if (!new_log.empty()) g_cleaner_ptr->addLogToClean(new_log);
|
auto new_log = future_new_log.get();
|
||||||
return new_log;
|
if (!new_log.empty()) {
|
||||||
}
|
g_cleaner_ptr->addLogToClean(new_log);
|
||||||
return add_count;
|
} else {
|
||||||
}
|
std::cout << "\nFailed to set filename: " << new_file_to_create << std::endl;
|
||||||
|
}
|
||||||
std::string setLogName(std::string new_file_to_create, std::string logger_id = "g3log") {
|
return new_log;
|
||||||
auto future_new_log = g_filesink_handler->call(&g3::FileSink::changeLogFile, new_file_to_create, logger_id);
|
}
|
||||||
auto new_log = future_new_log.get();
|
return add_count;
|
||||||
if (!new_log.empty()) g_cleaner_ptr->addLogToClean(new_log);
|
}
|
||||||
return new_log;
|
|
||||||
}
|
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);
|
||||||
std::string getLogName() {
|
auto new_log = future_new_log.get();
|
||||||
return g_filesink_handler->call(&g3::FileSink::fileName).get();
|
if (!new_log.empty()) g_cleaner_ptr->addLogToClean(new_log);
|
||||||
}
|
return new_log;
|
||||||
|
}
|
||||||
} // anonymous
|
|
||||||
|
std::string getLogName() {
|
||||||
TEST(TestOf_GetFileName, Expecting_ValidLogFile) {
|
return g_filesink_handler->call(&g3::FileSink::fileName).get();
|
||||||
|
}
|
||||||
LOG(INFO) << "test_filechange, Retrieving file name: ";
|
|
||||||
ASSERT_NE(g_logger_ptr, nullptr);
|
} // anonymous
|
||||||
ASSERT_FALSE(getLogName().empty());
|
|
||||||
}
|
TEST(TestOf_GetFileName, Expecting_ValidLogFile) {
|
||||||
|
|
||||||
TEST(TestOf_ChangingLogFile, Expecting_NewLogFileUsed) {
|
LOG(INFO) << "test_filechange, Retrieving file name: ";
|
||||||
auto old_log = getLogName();
|
ASSERT_NE(g_logger_ptr, nullptr);
|
||||||
std::string name = setLogNameAndAddCount(name_path_1);
|
ASSERT_FALSE(getLogName().empty());
|
||||||
auto new_log = setLogName(name);
|
}
|
||||||
ASSERT_NE(old_log, new_log);
|
|
||||||
}
|
TEST(TestOf_ChangingLogFile, Expecting_NewLogFileUsed) {
|
||||||
|
auto old_log = getLogName();
|
||||||
TEST(TestOf_ChangingLogFile_Id, Expecting_NewLogFileUsed) {
|
std::string name = setLogNameAndAddCount(name_path_1);
|
||||||
auto old_log = getLogName();
|
auto new_log = setLogName(name);
|
||||||
std::string name = setLogNameAndAddCount(name_path_1);
|
ASSERT_NE(old_log, new_log);
|
||||||
auto new_log = setLogName("foo","new_logger_id");
|
}
|
||||||
ASSERT_NE(old_log, new_log);
|
|
||||||
std::string new_name = getLogName();
|
TEST(TestOf_ChangingLogFile_Id, Expecting_NewLogFileUsed1) {
|
||||||
ASSERT_STREQ(new_name.substr(0,31).c_str(),"fooReplaceLogFile.new_logger_id");
|
auto old_log = getLogName();
|
||||||
}
|
std::string name = setLogNameAndAddCount(name_path_1);
|
||||||
|
auto new_log = setLogName("foo", "new_logger_id");
|
||||||
TEST(TestOf_ChangingLogFile_NoId, Expecting_NewLogFileUsed) {
|
ASSERT_NE(old_log, new_log);
|
||||||
auto old_log = getLogName();
|
std::string new_name = getLogName();
|
||||||
std::string name = setLogNameAndAddCount(name_path_1);
|
auto expected_part_of__new_name = std::string("foo") + kReplaceFileName + ".new_logger_id";
|
||||||
auto new_log = setLogName("foo","");
|
auto extracted_name = new_name.substr(0, expected_part_of__new_name.size());
|
||||||
ASSERT_NE(old_log, new_log);
|
ASSERT_EQ(extracted_name.c_str(), expected_part_of__new_name);
|
||||||
std::string new_name = getLogName();
|
}
|
||||||
ASSERT_STREQ(new_name.substr(0,17).c_str(),"fooReplaceLogFile");
|
|
||||||
}
|
TEST(TestOf_ChangingLogFile_NoId, Expecting_NewLogFileUsed2) {
|
||||||
|
auto old_log = getLogName();
|
||||||
TEST(TestOf_ManyThreadsChangingLogFileName, Expecting_EqualNumberLogsCreated) {
|
std::string name = setLogNameAndAddCount(name_path_1);
|
||||||
auto old_log = g_filesink_handler->call(&g3::FileSink::fileName).get();
|
auto new_log = setLogName("foo", "");
|
||||||
if (!old_log.empty()) g_cleaner_ptr->addLogToClean(old_log);
|
ASSERT_NE(old_log, new_log);
|
||||||
|
std::string new_name = getLogName();
|
||||||
LOG(INFO) << "SoManyThreadsAllDoingChangeFileName";
|
auto expected_part_of__new_name = std::string("foo") + kReplaceFileName;
|
||||||
std::vector<std::thread> threads;
|
auto extracted_name = new_name.substr(0, expected_part_of__new_name.size());
|
||||||
auto max = 2;
|
ASSERT_EQ(extracted_name.c_str(), expected_part_of__new_name);
|
||||||
auto size = g_cleaner_ptr->size();
|
}
|
||||||
for (auto count = 0; count < max; ++count) {
|
|
||||||
std::string drive = ((count % 2) == 0) ? "./_threadEven_" : "./_threaOdd_";
|
TEST(TestOf_ManyThreadsChangingLogFileName, Expecting_EqualNumberLogsCreated) {
|
||||||
std::string logger_id = std::to_string(count);
|
auto old_log = g_filesink_handler->call(&g3::FileSink::fileName).get();
|
||||||
threads.push_back(std::thread(setLogNameAndAddCount, drive, logger_id));
|
if (!old_log.empty()) g_cleaner_ptr->addLogToClean(old_log);
|
||||||
}
|
|
||||||
for (auto& thread : threads)
|
LOG(INFO) << "SoManyThreadsAllDoingChangeFileName";
|
||||||
thread.join();
|
std::vector<std::thread> threads;
|
||||||
|
auto max = 2;
|
||||||
// check that all logs were created
|
auto size = g_cleaner_ptr->size();
|
||||||
ASSERT_EQ(size + max, g_cleaner_ptr->size());
|
for (auto count = 0; count < max; ++count) {
|
||||||
}
|
std::string drive = ((count % 2) == 0) ? "./_threadEven_" : "./_threaOdd_";
|
||||||
|
std::string logger_id = std::to_string(count);
|
||||||
TEST(TestOf_IllegalLogFileName, Expecting_NoChangeToOriginalFileName) {
|
threads.push_back(std::thread(setLogNameAndAddCount, drive, logger_id));
|
||||||
std::string original = getLogName();
|
}
|
||||||
auto perhaps_a_name = setLogName("XY:/"); // does not exist
|
for (auto& thread : threads)
|
||||||
ASSERT_TRUE(perhaps_a_name.empty());
|
thread.join();
|
||||||
std::string post_illegal = getLogName();
|
|
||||||
ASSERT_STREQ(original.c_str(), post_illegal.c_str());
|
// check that all logs were created
|
||||||
}
|
ASSERT_EQ(size + max, g_cleaner_ptr->size());
|
||||||
|
}
|
||||||
TEST(TestOf_SinkHandleDifferentId, Expecting_DifferentId)
|
|
||||||
{
|
TEST(TestOf_IllegalLogFileName, Expecting_NoChangeToOriginalFileName) {
|
||||||
auto sink = std2::make_unique<g3::FileSink>("AnotherLogFile", name_path_1, "logger_id");
|
std::string original = getLogName();
|
||||||
auto name = sink->fileName();
|
auto perhaps_a_name = setLogName("XY:/"); // does not exist
|
||||||
ASSERT_STREQ( name.substr(0,26).c_str(), "./AnotherLogFile.logger_id");
|
ASSERT_TRUE(perhaps_a_name.empty());
|
||||||
g_cleaner_ptr->addLogToClean(name);
|
std::string post_illegal = getLogName();
|
||||||
}
|
ASSERT_STREQ(original.c_str(), post_illegal.c_str());
|
||||||
|
}
|
||||||
int main(int argc, char *argv[]) {
|
|
||||||
LogFileCleaner cleaner;
|
TEST(TestOf_SinkHandleDifferentId, Expecting_DifferentId) {
|
||||||
g_cleaner_ptr = &cleaner;
|
auto sink = std2::make_unique<g3::FileSink>("AnotherLogFile", name_path_1, "logger_id");
|
||||||
int return_value = 1;
|
auto name = sink->fileName();
|
||||||
std::stringstream cerrDump;
|
ASSERT_STREQ( name.substr(0, 26).c_str(), "./AnotherLogFile.logger_id");
|
||||||
|
g_cleaner_ptr->addLogToClean(name);
|
||||||
std::string last_log_file;
|
}
|
||||||
{
|
|
||||||
|
TEST(TestOf_LegalLogFileNam, With_parenthesis) {
|
||||||
testing_helpers::ScopedOut scopedCerr(std::cerr, &cerrDump);
|
std::string original = getLogName();
|
||||||
|
auto perhaps_a_name = setLogName("(test)"); // does not exist
|
||||||
auto worker = g3::LogWorker::createLogWorker();
|
std::string post_legal = getLogName();
|
||||||
auto handle= worker->addDefaultLogger("ReplaceLogFile", name_path_1);
|
EXPECT_TRUE(std::string::npos != post_legal.find("(test)")) << "filename was: " << post_legal;
|
||||||
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);
|
int main(int argc, char* argv[]) {
|
||||||
|
LogFileCleaner cleaner;
|
||||||
|
g_cleaner_ptr = &cleaner;
|
||||||
g3::initializeLogging(g_logger_ptr);
|
int return_value = 1;
|
||||||
LOG(INFO) << "test_filechange demo*" << std::endl;
|
std::stringstream cerrDump;
|
||||||
|
|
||||||
testing::InitGoogleTest(&argc, argv);
|
std::string last_log_file;
|
||||||
return_value = RUN_ALL_TESTS();
|
{
|
||||||
|
|
||||||
last_log_file = g_filesink_handler->call(&g3::FileSink::fileName).get();
|
testing_helpers::ScopedOut scopedCerr(std::cerr, &cerrDump);
|
||||||
std::cout << "log file at: " << last_log_file << std::endl;
|
|
||||||
//g3::shutDownLogging();
|
auto worker = g3::LogWorker::createLogWorker();
|
||||||
}
|
auto handle = worker->addDefaultLogger(kReplaceFileName, name_path_1, "kjell");
|
||||||
std::cout << "FINISHED WITH THE TESTING" << std::endl;
|
g_logger_ptr = worker.get();
|
||||||
// cleaning up
|
g_filesink_handler = handle.get();
|
||||||
cleaner.addLogToClean(last_log_file);
|
last_log_file = g_filesink_handler->call(&g3::FileSink::fileName).get();
|
||||||
return return_value;
|
//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