mirror of
https://github.com/KjellKod/g3log.git
synced 2024-12-12 10:23:50 +01:00
Add ability for user to override 'g3log' monniker in log file
This commit is contained in:
parent
69e0cafee8
commit
31c01b168c
@ -14,7 +14,7 @@ namespace g3 {
|
||||
using namespace internal;
|
||||
|
||||
|
||||
FileSink::FileSink(const std::string &log_prefix, const std::string &log_directory)
|
||||
FileSink::FileSink(const std::string &log_prefix, const std::string &log_directory, const std::string& logger_id)
|
||||
: _log_file_with_path(log_directory)
|
||||
, _log_prefix_backup(log_prefix)
|
||||
, _outptr(new std::ofstream)
|
||||
@ -25,7 +25,7 @@ namespace g3 {
|
||||
abort();
|
||||
}
|
||||
|
||||
std::string file_name = createLogFileName(_log_prefix_backup);
|
||||
std::string file_name = createLogFileName(_log_prefix_backup, logger_id);
|
||||
_log_file_with_path = pathSanityFix(_log_file_with_path, file_name);
|
||||
_outptr = createLogFile(_log_file_with_path);
|
||||
|
||||
@ -54,12 +54,12 @@ namespace g3 {
|
||||
out << message.get().toString() << std::flush;
|
||||
}
|
||||
|
||||
std::string FileSink::changeLogFile(const std::string &directory) {
|
||||
std::string FileSink::changeLogFile(const std::string &directory, const std::string &logger_id) {
|
||||
|
||||
auto now = g3::systemtime_now();
|
||||
auto now_formatted = g3::localtime_formatted(now, {internal::date_formatted + " " + internal::time_formatted});
|
||||
|
||||
std::string file_name = createLogFileName(_log_prefix_backup);
|
||||
std::string file_name = createLogFileName(_log_prefix_backup, logger_id);
|
||||
std::string prospect_log = directory + file_name;
|
||||
std::unique_ptr<std::ofstream> log_stream = createLogFile(prospect_log);
|
||||
if (nullptr == log_stream) {
|
||||
|
@ -85,9 +85,10 @@ namespace g3 {
|
||||
return ss_entry.str();
|
||||
}
|
||||
|
||||
std::string createLogFileName(const std::string &verified_prefix) {
|
||||
std::string createLogFileName(const std::string &verified_prefix, const std::string &logger_id) {
|
||||
std::stringstream oss_name;
|
||||
oss_name << verified_prefix << ".g3log.";
|
||||
oss_name << verified_prefix << ".";
|
||||
if( logger_id != "" ) oss_name << logger_id << ".";
|
||||
oss_name << g3::localtime_formatted(g3::systemtime_now(), file_name_time_formatted);
|
||||
oss_name << ".log";
|
||||
return oss_name.str();
|
||||
|
@ -15,11 +15,11 @@ namespace g3 {
|
||||
|
||||
class FileSink {
|
||||
public:
|
||||
FileSink(const std::string &log_prefix, const std::string &log_directory);
|
||||
FileSink(const std::string &log_prefix, const std::string &log_directory, const std::string &logger_id="g3log");
|
||||
virtual ~FileSink();
|
||||
|
||||
void fileWrite(LogMessageMover message);
|
||||
std::string changeLogFile(const std::string &directory);
|
||||
std::string changeLogFile(const std::string &directory, const std::string &logger_id="g3log");
|
||||
std::string fileName();
|
||||
|
||||
|
||||
|
@ -30,14 +30,14 @@ namespace { // anonymous
|
||||
g3::SinkHandle<g3::FileSink>* g_filesink_handler = nullptr;
|
||||
LogFileCleaner* g_cleaner_ptr = nullptr;
|
||||
|
||||
std::string setLogNameAndAddCount(std::string new_file_to_create) {
|
||||
std::string setLogNameAndAddCount(std::string new_file_to_create, std::string logger_id = "g3log") {
|
||||
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 future_new_log = g_filesink_handler->call(&g3::FileSink::changeLogFile, new_file_to_create + add_count, logger_id);
|
||||
auto new_log = future_new_log.get();
|
||||
if (!new_log.empty()) g_cleaner_ptr->addLogToClean(new_log);
|
||||
return new_log;
|
||||
@ -45,8 +45,8 @@ namespace { // anonymous
|
||||
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);
|
||||
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);
|
||||
auto new_log = future_new_log.get();
|
||||
if (!new_log.empty()) g_cleaner_ptr->addLogToClean(new_log);
|
||||
return new_log;
|
||||
@ -72,6 +72,24 @@ TEST(TestOf_ChangingLogFile, Expecting_NewLogFileUsed) {
|
||||
ASSERT_NE(old_log, new_log);
|
||||
}
|
||||
|
||||
TEST(TestOf_ChangingLogFile_Id, Expecting_NewLogFileUsed) {
|
||||
auto old_log = getLogName();
|
||||
std::string name = setLogNameAndAddCount(name_path_1);
|
||||
auto new_log = setLogName("foo","new_logger_id");
|
||||
ASSERT_NE(old_log, new_log);
|
||||
std::string new_name = getLogName();
|
||||
ASSERT_STREQ(new_name.substr(0,31).c_str(),"fooReplaceLogFile.new_logger_id");
|
||||
}
|
||||
|
||||
TEST(TestOf_ChangingLogFile_NoId, Expecting_NewLogFileUsed) {
|
||||
auto old_log = getLogName();
|
||||
std::string name = setLogNameAndAddCount(name_path_1);
|
||||
auto new_log = setLogName("foo","");
|
||||
ASSERT_NE(old_log, new_log);
|
||||
std::string new_name = getLogName();
|
||||
ASSERT_STREQ(new_name.substr(0,17).c_str(),"fooReplaceLogFile");
|
||||
}
|
||||
|
||||
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);
|
||||
@ -82,7 +100,8 @@ TEST(TestOf_ManyThreadsChangingLogFileName, Expecting_EqualNumberLogsCreated) {
|
||||
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));
|
||||
std::string logger_id = std::to_string(count);
|
||||
threads.push_back(std::thread(setLogNameAndAddCount, drive, logger_id));
|
||||
}
|
||||
for (auto& thread : threads)
|
||||
thread.join();
|
||||
@ -99,6 +118,13 @@ TEST(TestOf_IllegalLogFileName, Expecting_NoChangeToOriginalFileName) {
|
||||
ASSERT_STREQ(original.c_str(), post_illegal.c_str());
|
||||
}
|
||||
|
||||
TEST(TestOf_SinkHandleDifferentId, Expecting_DifferentId)
|
||||
{
|
||||
auto sink = std2::make_unique<g3::FileSink>("AnotherLogFile", name_path_1, "logger_id");
|
||||
auto name = sink->fileName();
|
||||
ASSERT_STREQ( name.substr(0,26).c_str(), "./AnotherLogFile.logger_id");
|
||||
g_cleaner_ptr->addLogToClean(name);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
LogFileCleaner cleaner;
|
||||
|
Loading…
Reference in New Issue
Block a user