mirror of
https://github.com/KjellKod/g3log.git
synced 2024-12-12 10:23:50 +01:00
Merge pull request #82 from KjellKod/repace-g3log-in-path
Repace g3log in path
This commit is contained in:
commit
56cef7959c
18
API.markdown
18
API.markdown
@ -89,6 +89,24 @@ A logging sink is not required to be a subclass of a specific type. The only req
|
||||
|
||||
### Using the default sink
|
||||
Sink creation is defined in [logworker.hpp](src/g3log/logworker.hpp) and used in [logworker.cpp](src/logworker.cpp). For in-depth knowlege regarding sink implementation details you can look at [sinkhandle.hpp](src/g3log/sinkhandle.hpp) and [sinkwrapper.hpp](src/g3log/sinkwrapper.hpp)
|
||||
```
|
||||
std::unique_ptr<FileSinkHandle> addDefaultLogger(
|
||||
const std::string& log_prefix
|
||||
, const std::string& log_directory
|
||||
, const std::string& default_id = "g3log");
|
||||
```
|
||||
|
||||
With the default id left as is (i.e. "g3log") a creation of the logger in the unit test "test_filechange" would look like this
|
||||
```
|
||||
const std::string directory = "./";
|
||||
const std::string name = "(ReplaceLogFile)";
|
||||
auto worker = g3::LogWorker::createLogWorker();
|
||||
auto handle = worker->addDefaultLogger(name, directory);
|
||||
```
|
||||
The resulting filename would be something like:
|
||||
```
|
||||
./(ReplaceLogFile).g3log.20160217-001406.log
|
||||
```
|
||||
|
||||
|
||||
## LOG <a name="log_flushing">flushing</a>
|
||||
|
@ -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,18 +54,16 @@ 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 prospect_log = directory + file_name;
|
||||
|
||||
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) {
|
||||
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
|
||||
}
|
||||
|
||||
|
@ -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();
|
||||
|
@ -7,6 +7,7 @@
|
||||
*
|
||||
* For more information see g3log/LICENSE or refer refer to http://unlicense.org
|
||||
* ============================================================================*/
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include "g3log/loglevels.hpp"
|
||||
|
@ -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);
|
||||
std::string fileName();
|
||||
|
||||
|
||||
|
@ -12,7 +12,7 @@
|
||||
* PUBLIC DOMAIN and Not copywrited since it was built on public-domain software and influenced
|
||||
* at least in "spirit" from the following sources
|
||||
* 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/
|
||||
* 4. Google 'glog': http://google-glog.googlecode.com/svn/trunk/doc/glog.html
|
||||
* 5. Various Q&A at StackOverflow
|
||||
|
@ -22,7 +22,8 @@
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#include <string>
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
|
||||
|
||||
// Levels for logging, made so that it would be easy to change, remove, add levels -- KjellKod
|
||||
|
@ -84,7 +84,7 @@ namespace g3 {
|
||||
std::cout << "The filename is: " << log_file_name.get() << std::endl;
|
||||
// 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");
|
||||
|
||||
|
||||
|
||||
|
@ -15,7 +15,6 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
|
||||
namespace g3 {
|
||||
|
||||
LogWorkerImpl::LogWorkerImpl() : _bg(kjellkod::Active::createActive()) { }
|
||||
@ -121,8 +120,8 @@ namespace g3 {
|
||||
return std::unique_ptr<LogWorker>(new LogWorker);
|
||||
}
|
||||
|
||||
std::unique_ptr<FileSinkHandle>LogWorker::addDefaultLogger(const std::string& log_prefix, const std::string& log_directory) {
|
||||
return addSink(std2::make_unique<g3::FileSink>(log_prefix, log_directory), &FileSink::fileWrite);
|
||||
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, default_id), &FileSink::fileWrite);
|
||||
}
|
||||
|
||||
|
||||
|
@ -26,31 +26,32 @@ using namespace testing_helpers;
|
||||
|
||||
namespace { // anonymous
|
||||
const char* name_path_1 = "./(some_fake_DirectoryOrName_1_)";
|
||||
const std::string kReplaceFileName = "(ReplaceLogFile)";
|
||||
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) {
|
||||
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);
|
||||
} else {
|
||||
std::cout << "\nFailed to set filename: " << new_file_to_create << std::endl;
|
||||
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);
|
||||
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;
|
||||
@ -76,6 +77,28 @@ TEST(TestOf_ChangingLogFile, Expecting_NewLogFileUsed) {
|
||||
ASSERT_NE(old_log, new_log);
|
||||
}
|
||||
|
||||
TEST(TestOf_ChangingLogFile_Id, Expecting_NewLogFileUsed1) {
|
||||
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();
|
||||
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();
|
||||
std::string name = setLogNameAndAddCount(name_path_1);
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
@ -86,7 +109,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();
|
||||
@ -103,6 +127,12 @@ 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);
|
||||
}
|
||||
|
||||
TEST(TestOf_LegalLogFileNam, With_parenthesis) {
|
||||
std::string original = getLogName();
|
||||
@ -124,10 +154,11 @@ int main(int argc, char* argv[]) {
|
||||
testing_helpers::ScopedOut scopedCerr(std::cerr, &cerrDump);
|
||||
|
||||
auto worker = g3::LogWorker::createLogWorker();
|
||||
auto handle = worker->addDefaultLogger("(ReplaceLogFile)", name_path_1);
|
||||
auto handle = worker->addDefaultLogger(kReplaceFileName, name_path_1);
|
||||
g_logger_ptr = worker.get();
|
||||
g_filesink_handler = handle.get();
|
||||
last_log_file = g_filesink_handler->call(&g3::FileSink::fileName).get();
|
||||
std::cout << "log file at: " << last_log_file << std::endl;
|
||||
cleaner.addLogToClean(last_log_file);
|
||||
|
||||
|
||||
@ -145,4 +176,4 @@ int main(int argc, char* argv[]) {
|
||||
// cleaning up
|
||||
cleaner.addLogToClean(last_log_file);
|
||||
return return_value;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user