From 2460f97a2044d86d2afbc458c3926e88645626c8 Mon Sep 17 00:00:00 2001 From: x86kernel Date: Fri, 18 Dec 2015 13:53:20 +0000 Subject: [PATCH 1/9] New feature about dir --- include/spdlog/details/os.h | 72 ++++++++++++++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-) diff --git a/include/spdlog/details/os.h b/include/spdlog/details/os.h index ab569caa..fdbcfd4c 100644 --- a/include/spdlog/details/os.h +++ b/include/spdlog/details/os.h @@ -7,6 +7,7 @@ #include #include #include +#include #ifdef _WIN32 # ifndef WIN32_LEAN_AND_MEAN @@ -128,6 +129,65 @@ constexpr inline unsigned short eol_size() } #endif +//Return true and list if filename contains relative +inline bool dir_check(const std::string& filename, std::list& dirs) +{ +#ifdef __linux__ + std::string::size_type index, previndex = 0, size; + + index = filename.find("/", previndex); + if(!index) + return false; + + do { + dirs.push_back(filename.substr(previndex, index)); + previndex = index + 1; + index = filename.find("/", previndex); + }while(index != std::string::npos); + + size = filename.length() - 1; + if(previndex < size) + dirs.push_back(filename.substr(previndex, size)); + + return true; + +#endif + +} + +//Create directories by referring to list +//Return 0 if success +inline bool create_dirs(std::list& dirs) +{ +#ifdef __linux__ + std::string origin_path; + std::list::iterator li; + + int r; + + origin_path = get_current_dir_name(); + for(li = dirs.begin(); li != dirs.end(); li++) { + r = mkdir((*li).c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); + if(r) + { + if(errno == EEXIST) + return 0; + } + else return r; + + r = chdir((*li).c_str()); + if(r) return r; + } + + r = chdir(origin_path.c_str()); + if(r) return r; + + return 0; + +#endif + +} + //fopen_s on non windows for writing inline int fopen_s(FILE** fp, const std::string& filename, const char* mode) { @@ -135,7 +195,16 @@ inline int fopen_s(FILE** fp, const std::string& filename, const char* mode) *fp = _fsopen((filename.c_str()), mode, _SH_DENYWR); return *fp == nullptr; #else - *fp = fopen((filename.c_str()), mode); + std::list dirs; + + if(dir_check(filename, dirs)) + { + dirs.pop_back(); + if(create_dirs(dirs)) + return true; + } + + *fp = fopen((filename.c_str()), mode); return *fp == nullptr; #endif @@ -164,6 +233,7 @@ inline bool file_exists(const std::string& filename) } + //Return utc offset in minutes or -1 on failure inline int utc_minutes_offset(const std::tm& tm = details::os::localtime()) { From 105d3a7c92b57cd55f42f91feef0829dd78d24e7 Mon Sep 17 00:00:00 2001 From: x86kernel Date: Fri, 18 Dec 2015 14:00:20 +0000 Subject: [PATCH 2/9] New feature about dir --- include/spdlog/details/os.h | 89 +++++++++++++++++++------------------ 1 file changed, 45 insertions(+), 44 deletions(-) diff --git a/include/spdlog/details/os.h b/include/spdlog/details/os.h index fdbcfd4c..1344ba7a 100644 --- a/include/spdlog/details/os.h +++ b/include/spdlog/details/os.h @@ -133,23 +133,23 @@ constexpr inline unsigned short eol_size() inline bool dir_check(const std::string& filename, std::list& dirs) { #ifdef __linux__ - std::string::size_type index, previndex = 0, size; + std::string::size_type index, previndex = 0, size; - index = filename.find("/", previndex); - if(!index) - return false; + index = filename.find("/", previndex); + if(!index) + return false; - do { - dirs.push_back(filename.substr(previndex, index)); - previndex = index + 1; - index = filename.find("/", previndex); - }while(index != std::string::npos); + do { + dirs.push_back(filename.substr(previndex, index)); + previndex = index + 1; + index = filename.find("/", previndex); + }while(index != std::string::npos); - size = filename.length() - 1; - if(previndex < size) - dirs.push_back(filename.substr(previndex, size)); + size = filename.length() - 1; + if(previndex < size) + dirs.push_back(filename.substr(previndex, size)); - return true; + return true; #endif @@ -160,29 +160,29 @@ inline bool dir_check(const std::string& filename, std::list& dirs) inline bool create_dirs(std::list& dirs) { #ifdef __linux__ - std::string origin_path; - std::list::iterator li; - - int r; - - origin_path = get_current_dir_name(); - for(li = dirs.begin(); li != dirs.end(); li++) { - r = mkdir((*li).c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); - if(r) - { - if(errno == EEXIST) - return 0; - } - else return r; - - r = chdir((*li).c_str()); - if(r) return r; - } + std::string origin_path; + std::list::iterator li; + + int r; + + origin_path = get_current_dir_name(); + for(li = dirs.begin(); li != dirs.end(); li++) { + r = mkdir((*li).c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); + if(r) + { + if(errno == EEXIST) + return 0; + } + else return r; + + r = chdir((*li).c_str()); + if(r) return r; + } - r = chdir(origin_path.c_str()); - if(r) return r; + r = chdir(origin_path.c_str()); + if(r) return r; - return 0; + return 0; #endif @@ -195,17 +195,18 @@ inline int fopen_s(FILE** fp, const std::string& filename, const char* mode) *fp = _fsopen((filename.c_str()), mode, _SH_DENYWR); return *fp == nullptr; #else - std::list dirs; - - if(dir_check(filename, dirs)) - { - dirs.pop_back(); - if(create_dirs(dirs)) - return true; - } - - *fp = fopen((filename.c_str()), mode); + std::list dirs; + + if(dir_check(filename, dirs)) + { + dirs.pop_back(); + if(create_dirs(dirs)) + return true; + } + + *fp = fopen((filename.c_str()), mode); return *fp == nullptr; + #endif } From fc35c2f94bc2e975ba98b772996e0f07ba308b30 Mon Sep 17 00:00:00 2001 From: x86kernel Date: Sat, 19 Dec 2015 00:56:48 +0000 Subject: [PATCH 3/9] New feature for directory --- include/spdlog/details/os.h | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/include/spdlog/details/os.h b/include/spdlog/details/os.h index 1344ba7a..340fdef8 100644 --- a/include/spdlog/details/os.h +++ b/include/spdlog/details/os.h @@ -133,22 +133,24 @@ constexpr inline unsigned short eol_size() inline bool dir_check(const std::string& filename, std::list& dirs) { #ifdef __linux__ + std::string directory; std::string::size_type index, previndex = 0, size; index = filename.find("/", previndex); if(!index) - return false; + dirs.push_back("/"); do { - dirs.push_back(filename.substr(previndex, index)); + if(index - previndex == 0) + directory = filename.substr(previndex, index - previndex + 1); + else + directory = filename.substr(previndex, index - previndex); + if(directory != "/") + dirs.push_back(directory); previndex = index + 1; index = filename.find("/", previndex); }while(index != std::string::npos); - size = filename.length() - 1; - if(previndex < size) - dirs.push_back(filename.substr(previndex, size)); - return true; #endif @@ -166,14 +168,10 @@ inline bool create_dirs(std::list& dirs) int r; origin_path = get_current_dir_name(); - for(li = dirs.begin(); li != dirs.end(); li++) { + for(li = dirs.begin(); li != dirs.end(); ++li) { r = mkdir((*li).c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); - if(r) - { - if(errno == EEXIST) - return 0; - } - else return r; + if(r && errno != EEXIST) + return r; r = chdir((*li).c_str()); if(r) return r; @@ -199,7 +197,6 @@ inline int fopen_s(FILE** fp, const std::string& filename, const char* mode) if(dir_check(filename, dirs)) { - dirs.pop_back(); if(create_dirs(dirs)) return true; } From 99e79dd4f8c621b96aa1323fdd266d06f5cb87b0 Mon Sep 17 00:00:00 2001 From: x86kernel Date: Sat, 19 Dec 2015 01:10:46 +0000 Subject: [PATCH 4/9] some modification --- include/spdlog/details/os.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/spdlog/details/os.h b/include/spdlog/details/os.h index 340fdef8..8c45a854 100644 --- a/include/spdlog/details/os.h +++ b/include/spdlog/details/os.h @@ -145,8 +145,10 @@ inline bool dir_check(const std::string& filename, std::list& dirs) directory = filename.substr(previndex, index - previndex + 1); else directory = filename.substr(previndex, index - previndex); + if(directory != "/") dirs.push_back(directory); + previndex = index + 1; index = filename.find("/", previndex); }while(index != std::string::npos); From 7cf172d282d1b4b5d1f9159d04e1327ffbf42875 Mon Sep 17 00:00:00 2001 From: x86kernel Date: Sat, 19 Dec 2015 23:38:41 +0000 Subject: [PATCH 5/9] some modification about directory feature --- include/spdlog/details/file_helper.h | 7 ++++++- include/spdlog/details/os.h | 8 -------- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/include/spdlog/details/file_helper.h b/include/spdlog/details/file_helper.h index 0a544ca1..6a7b31ff 100644 --- a/include/spdlog/details/file_helper.h +++ b/include/spdlog/details/file_helper.h @@ -51,8 +51,12 @@ public: _filename = fname; for (int tries = 0; tries < open_tries; ++tries) { + if (os::dir_check(fname, dirs)) + if(os::create_dirs(dirs)) + return; + if (!os::fopen_s(&_fd, fname, mode)) - return; + return ; std::this_thread::sleep_for(std::chrono::milliseconds(open_interval)); } @@ -133,6 +137,7 @@ public: private: FILE* _fd; + std::list dirs; std::string _filename; bool _force_flush; diff --git a/include/spdlog/details/os.h b/include/spdlog/details/os.h index 8c45a854..8f1beadf 100644 --- a/include/spdlog/details/os.h +++ b/include/spdlog/details/os.h @@ -195,14 +195,6 @@ inline int fopen_s(FILE** fp, const std::string& filename, const char* mode) *fp = _fsopen((filename.c_str()), mode, _SH_DENYWR); return *fp == nullptr; #else - std::list dirs; - - if(dir_check(filename, dirs)) - { - if(create_dirs(dirs)) - return true; - } - *fp = fopen((filename.c_str()), mode); return *fp == nullptr; From a541dd35f862ed2fa71fc7c47b0eda96d597feb7 Mon Sep 17 00:00:00 2001 From: x86kernel Date: Sat, 19 Dec 2015 23:57:24 +0000 Subject: [PATCH 6/9] some modification about directory feature --- include/spdlog/details/file_helper.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/spdlog/details/file_helper.h b/include/spdlog/details/file_helper.h index 6a7b31ff..3a234897 100644 --- a/include/spdlog/details/file_helper.h +++ b/include/spdlog/details/file_helper.h @@ -51,12 +51,12 @@ public: _filename = fname; for (int tries = 0; tries < open_tries; ++tries) { - if (os::dir_check(fname, dirs)) + if (os::dir_check(fname, dirs)) if(os::create_dirs(dirs)) - return; + break; - if (!os::fopen_s(&_fd, fname, mode)) - return ; + if(!os::fopen_s(&_fd, fname, mode)) + return; std::this_thread::sleep_for(std::chrono::milliseconds(open_interval)); } From 21214f2860e1ba979081842d530ccf2f3d15d9fd Mon Sep 17 00:00:00 2001 From: x86kernel Date: Sun, 20 Dec 2015 02:19:53 +0000 Subject: [PATCH 7/9] some modifications about directory feature --- include/spdlog/details/file_helper.h | 80 ++++++++++++++++++++++++++-- include/spdlog/details/os.h | 76 ++++++++++---------------- 2 files changed, 104 insertions(+), 52 deletions(-) diff --git a/include/spdlog/details/file_helper.h b/include/spdlog/details/file_helper.h index 3a234897..73a7b79e 100644 --- a/include/spdlog/details/file_helper.h +++ b/include/spdlog/details/file_helper.h @@ -47,13 +47,17 @@ public: { close(); + std::list dirs; const char* mode = truncate ? "wb" : "ab"; _filename = fname; for (int tries = 0; tries < open_tries; ++tries) { - if (os::dir_check(fname, dirs)) - if(os::create_dirs(dirs)) + if (dir_check(fname)) + { + get_dirlist(fname, dirs); + if(create_dirs(dirs)) break; + } if(!os::fopen_s(&_fd, fname, mode)) return; @@ -133,11 +137,81 @@ public: return os::file_exists(name); } + //Return true if filenamd contains relative path + bool dir_check(const std::string &name) + { + std::string::size_type index = 0; + + index = name.find("/", index); + if(index != std::string::npos) + return true; + + return false; + } + + //Get list contain directories name + void get_dirlist(const std::string &name, std::list& dirs) + { + std::string directory; + std::string::size_type index, previndex = 0; + + index = name.find("/", previndex); + if(!index) + dirs.push_back("/"); + + do { + if(index - previndex == 0) + directory = name.substr(previndex, index - previndex + 1); + else + directory = name.substr(previndex, index - previndex); + + if(directory != "/") + dirs.push_back(directory); + + previndex = index + 1; + index = name.find("/", previndex); + }while(index != std::string::npos); + } + + + //Create directories by referring to list + //Return 0 if success + bool create_dirs(std::list& dirs) + { + std::string origin_path, dirname; + std::list::iterator li; + + int r; + + origin_path = os::getpwd(); + + for(li = dirs.begin(); li != dirs.end(); ++li) { + dirname = *li; + + r = os::_mkdir(dirname); + if(r) + { +#ifdef _WIN32 + //not yet +#else + if(os::get_lasterror() != EEXIST) + return r; +#endif + } + + r = os::_chdir(dirname); + if(r) return r; + } + + r = os::_chdir(origin_path); + if(r) return r; + + return 0; + } private: FILE* _fd; - std::list dirs; std::string _filename; bool _force_flush; diff --git a/include/spdlog/details/os.h b/include/spdlog/details/os.h index 8f1beadf..0b17ccb4 100644 --- a/include/spdlog/details/os.h +++ b/include/spdlog/details/os.h @@ -129,63 +129,41 @@ constexpr inline unsigned short eol_size() } #endif -//Return true and list if filename contains relative -inline bool dir_check(const std::string& filename, std::list& dirs) -{ -#ifdef __linux__ - std::string directory; - std::string::size_type index, previndex = 0, size; - - index = filename.find("/", previndex); - if(!index) - dirs.push_back("/"); - - do { - if(index - previndex == 0) - directory = filename.substr(previndex, index - previndex + 1); - else - directory = filename.substr(previndex, index - previndex); - - if(directory != "/") - dirs.push_back(directory); - - previndex = index + 1; - index = filename.find("/", previndex); - }while(index != std::string::npos); - - return true; +inline int get_lasterror() +{ +#ifdef _WIN32 + //not yet +#else + return errno; #endif - } -//Create directories by referring to list -//Return 0 if success -inline bool create_dirs(std::list& dirs) +inline std::string getpwd() { -#ifdef __linux__ - std::string origin_path; - std::list::iterator li; - - int r; - - origin_path = get_current_dir_name(); - for(li = dirs.begin(); li != dirs.end(); ++li) { - r = mkdir((*li).c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); - if(r && errno != EEXIST) - return r; - - r = chdir((*li).c_str()); - if(r) return r; - } - - r = chdir(origin_path.c_str()); - if(r) return r; - - return 0; +#ifdef _WIN32 + //not yet +#else + return get_current_dir_name(); +#endif +} +inline bool _mkdir(const std::string& dirname) +{ +#ifdef _WIN32 + //not yet +#else + return mkdir(dirname.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); #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 From a0a0a5eb6d06c13fa97724b0369211a2004d78b9 Mon Sep 17 00:00:00 2001 From: x86kernel Date: Tue, 22 Dec 2015 13:33:59 +0000 Subject: [PATCH 8/9] 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) { From 7fb7259b3b300bb261c007716983cccc57316bf6 Mon Sep 17 00:00:00 2001 From: x86kernel Date: Thu, 31 Dec 2015 11:23:26 +0000 Subject: [PATCH 9/9] no chdir2 --- include/spdlog/details/file_helper.h | 3 ++- include/spdlog/details/os.h | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/include/spdlog/details/file_helper.h b/include/spdlog/details/file_helper.h index d7dcdbc8..88a46e16 100644 --- a/include/spdlog/details/file_helper.h +++ b/include/spdlog/details/file_helper.h @@ -215,7 +215,8 @@ public: if(r) { #ifdef _WIN32 - //not yet + if(os::get_lasterror() != ERROR_ALREADY_EXISTS) + return r; #else if(os::get_lasterror() != EEXIST) return r; diff --git a/include/spdlog/details/os.h b/include/spdlog/details/os.h index f9087862..8cc8d88e 100644 --- a/include/spdlog/details/os.h +++ b/include/spdlog/details/os.h @@ -133,7 +133,7 @@ constexpr inline unsigned short eol_size() inline int get_lasterror() { #ifdef _WIN32 - //not yet + return GetLastError(); #else return errno; #endif @@ -142,7 +142,7 @@ inline int get_lasterror() inline bool _mkdir(const std::string& dirname) { #ifdef _WIN32 - //not yet + return CreateDirectoryA(dirname.c_str(), NULL) == 0; #else return mkdir(dirname.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); #endif