Bugfix: Thanks to Alexander Ignatyev. Now handles illegal, empty directory names.

Bugfix: If directory path cannot be resolved it will as a last resort write the log to the current directory
This commit is contained in:
KjellKod 2013-07-11 23:22:40 -06:00
parent 68d1c12858
commit 12dfc91458
3 changed files with 23 additions and 17 deletions

View File

@ -73,23 +73,24 @@ std::string prefixSanityFix(std::string prefix)
std::string pathSanityFix(std::string path, std::string file_name) {
// Unify the delimeters,. maybe sketchy solution but it seems to work
// on at least win7 + ubuntu. All bets are off for older windows
std::replace(path.begin(), path.end(), '\\', '/');
// Unify the delimeters,. maybe sketchy solution but it seems to work
// on at least win7 + ubuntu. All bets are off for older windows
std::replace(path.begin(), path.end(), '\\', '/');
// clean up in case of multiples
auto contains_end = [&](std::string& in) -> bool {
size_t size = in.size();
if(!size) return false;
char end = in[size-1];
return (end == '/' || end == ' ');
};
// clean up in case of multiples
auto contains_end = [&](std::string& in) -> bool {
size_t size = in.size();
if(!size) return false;
char end = in[size-1];
return (end == '/' || end == ' ');
};
while(contains_end(path))
path.erase(path.size()-1);
path.insert(path.end(), '/'); // works on both unix and windows (win7, ubuntu)
path.insert(path.size(), file_name);
return path;
while(contains_end(path)) { path.erase(path.size()-1); }
if(!path.empty()) {
path.insert(path.end(), '/');
}
path.insert(path.size(), file_name);
return path;
}
@ -189,7 +190,12 @@ g2LogWorkerImpl::g2LogWorkerImpl(const std::string& log_prefix, const std::strin
std::string file_name = createLogFileName(log_prefix_backup_);
log_file_with_path_ = pathSanityFix(log_file_with_path_, file_name);
outptr_ = createLogFile(log_file_with_path_);
assert((nullptr != outptr_) && "cannot open log file at startup");
if(!outptr_) {
std::cerr << "Cannot write logfile to location, attempting current directory" << std::endl;
log_file_with_path_ = file_name;
outptr_ = createLogFile(log_file_with_path_);
}
assert((outptr_) && "cannot open log file at startup");
}

View File

@ -109,6 +109,7 @@ std::string changeDirectoryOrName(std::string new_file_to_create)
TEST(TestOf_GetFileName, Expecting_ValidLogFile)
{
LOG(INFO) << "test_filechange, Retrieving file name: ";
ASSERT_NE(g_logger_ptr, nullptr);
std::future<std::string> f_get_old_name = g_logger_ptr->logFileName();

View File

@ -6,7 +6,6 @@
#include <gtest/gtest.h>
#include <iostream>
#include <chrono>
int main(int argc, char *argv[])
{