From a0a0a5eb6d06c13fa97724b0369211a2004d78b9 Mon Sep 17 00:00:00 2001 From: x86kernel Date: Tue, 22 Dec 2015 13:33:59 +0000 Subject: [PATCH] No chdir --- include/spdlog/details/file_helper.h | 66 ++++++++++++++++++---------- include/spdlog/details/os.h | 18 -------- 2 files changed, 44 insertions(+), 40 deletions(-) diff --git a/include/spdlog/details/file_helper.h b/include/spdlog/details/file_helper.h index 73a7b79e..d7dcdbc8 100644 --- a/include/spdlog/details/file_helper.h +++ b/include/spdlog/details/file_helper.h @@ -47,16 +47,18 @@ public: { close(); - std::list dirs; const char* mode = truncate ? "wb" : "ab"; _filename = fname; for (int tries = 0; tries < open_tries; ++tries) { - if (dir_check(fname)) + if(!_dir_created) { - get_dirlist(fname, dirs); - if(create_dirs(dirs)) - break; + if (dir_check(fname)) + { + get_dirlist(fname); + if(create_dirs()) + break; + } } if(!os::fopen_s(&_fd, fname, mode)) @@ -143,21 +145,26 @@ public: std::string::size_type index = 0; index = name.find("/", index); - if(index != std::string::npos) + if(index != std::string::npos) + { + if(index == 0) + _path_type = 1; + else + _path_type = 0; + return true; + } return false; } //Get list contain directories name - void get_dirlist(const std::string &name, std::list& dirs) + void get_dirlist(const std::string &name) { std::string directory; std::string::size_type index, previndex = 0; index = name.find("/", previndex); - if(!index) - dirs.push_back("/"); do { if(index - previndex == 0) @@ -174,21 +181,37 @@ public: } - //Create directories by referring to list - //Return 0 if success - bool create_dirs(std::list& dirs) + std::string make_dirstring(int depth) { - std::string origin_path, dirname; + std::string dirname; std::list::iterator li; - int r; + int i; + + if(_path_type) + dirname = "/"; - origin_path = os::getpwd(); + for(i = 0, li = dirs.begin(); i <= depth; i++, li++) + dirname = dirname + *li + "/"; + + return dirname; + } - for(li = dirs.begin(); li != dirs.end(); ++li) { - dirname = *li; + //Create directories by referring to list + //Return 0 if success + bool create_dirs() + { + std::string dirname; + std::list::iterator li; + + int r, depth = 0; + + for(li = dirs.begin(); li != dirs.end(); li++, depth++) + { + dirname = make_dirstring(depth); r = os::_mkdir(dirname); + if(r) { #ifdef _WIN32 @@ -198,13 +221,9 @@ public: return r; #endif } - - r = os::_chdir(dirname); - if(r) return r; } - r = os::_chdir(origin_path); - if(r) return r; + _dir_created = 1; return 0; } @@ -213,6 +232,9 @@ public: private: FILE* _fd; std::string _filename; + std::list dirs; + bool _dir_created; + bool _path_type; bool _force_flush; diff --git a/include/spdlog/details/os.h b/include/spdlog/details/os.h index 0b17ccb4..f9087862 100644 --- a/include/spdlog/details/os.h +++ b/include/spdlog/details/os.h @@ -139,15 +139,6 @@ inline int get_lasterror() #endif } -inline std::string getpwd() -{ -#ifdef _WIN32 - //not yet -#else - return get_current_dir_name(); -#endif -} - inline bool _mkdir(const std::string& dirname) { #ifdef _WIN32 @@ -157,15 +148,6 @@ inline bool _mkdir(const std::string& dirname) #endif } -inline bool _chdir(const std::string& dirname) -{ -#ifdef _WIN32 - //not yet -#else - return chdir(dirname.c_str()); -#endif -} - //fopen_s on non windows for writing inline int fopen_s(FILE** fp, const std::string& filename, const char* mode) {