From 9b7812a0f22f4743ee1618c7d879c8f30004253d Mon Sep 17 00:00:00 2001 From: gabime Date: Sun, 20 Oct 2019 17:40:56 +0300 Subject: [PATCH 01/24] auto create log dir --- bench/CMakeLists.txt | 2 - bench/latency.cpp | 22 -------- bench/meson.build | 1 - example/CMakeLists.txt | 2 - example/meson.build | 1 - include/spdlog/details/file_helper-inl.h | 8 +++ include/spdlog/details/os-inl.h | 69 +++++++++++++++++++++++- include/spdlog/details/os.h | 11 ++++ tests/CMakeLists.txt | 4 +- tests/meson.build | 3 -- tests/test_async.cpp | 4 +- tests/test_create_dir.cpp | 27 ++++++++++ tests/test_daily_logger.cpp | 8 +-- tests/test_errors.cpp | 19 +++---- tests/test_file_helper.cpp | 2 +- tests/test_file_logging.cpp | 8 +-- tests/test_macros.cpp | 2 +- tests/utils.cpp | 12 ++--- 18 files changed, 141 insertions(+), 64 deletions(-) create mode 100644 tests/test_create_dir.cpp diff --git a/bench/CMakeLists.txt b/bench/CMakeLists.txt index d087cf66..0ea88423 100644 --- a/bench/CMakeLists.txt +++ b/bench/CMakeLists.txt @@ -24,5 +24,3 @@ target_link_libraries(latency PRIVATE benchmark::benchmark spdlog::spdlog) add_executable(formatter-bench formatter-bench.cpp) target_link_libraries(formatter-bench PRIVATE benchmark::benchmark spdlog::spdlog) - -file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/logs") diff --git a/bench/latency.cpp b/bench/latency.cpp index 730226ca..cd8717d9 100644 --- a/bench/latency.cpp +++ b/bench/latency.cpp @@ -16,26 +16,6 @@ #include "spdlog/sinks/null_sink.h" #include "spdlog/sinks/rotating_file_sink.h" -void prepare_logdir() -{ - spdlog::info("Preparing latency_logs directory.."); -#ifdef _WIN32 - system("if not exist logs mkdir latency_logs"); - system("del /F /Q logs\\*"); -#else - auto rv = system("mkdir -p latency_logs"); - if (rv != 0) - { - throw std::runtime_error("Failed to mkdir -p latency_logs"); - } - rv = system("rm -f latency_logs/*"); - if (rv != 0) - { - throw std::runtime_error("Failed to rm -f latency_logs/*"); - } -#endif -} - void bench_c_string(benchmark::State &state, std::shared_ptr logger) { const char *msg = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum pharetra metus cursus " @@ -83,8 +63,6 @@ int main(int argc, char *argv[]) size_t rotating_files = 5; int n_threads = benchmark::CPUInfo::Get().num_cpus; - prepare_logdir(); - // disabled loggers auto disabled_logger = std::make_shared("bench", std::make_shared()); disabled_logger->set_level(spdlog::level::off); diff --git a/bench/meson.build b/bench/meson.build index c2604271..c877b6ac 100644 --- a/bench/meson.build +++ b/bench/meson.build @@ -12,4 +12,3 @@ foreach i : bench_matrix benchmark('bench_' + i[0], bench_exe, args: i[2]) endforeach -run_command(find_program('mkdir'), meson.current_build_dir() + '/logs') diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index b5fc4060..458ca952 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -25,5 +25,3 @@ if(SPDLOG_BUILD_EXAMPLE_HO) target_link_libraries(example_header_only PRIVATE spdlog::spdlog_header_only) endif() -# Create logs directory -file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/logs") diff --git a/example/meson.build b/example/meson.build index 7e29abb2..c37c4c3c 100644 --- a/example/meson.build +++ b/example/meson.build @@ -1,5 +1,4 @@ executable('example', 'example.cpp', dependencies: spdlog_dep) executable('example_header_only', 'example.cpp', dependencies: spdlog_headeronly_dep) -run_command(find_program('mkdir'), meson.current_build_dir() + '/logs') diff --git a/include/spdlog/details/file_helper-inl.h b/include/spdlog/details/file_helper-inl.h index 440cb290..cd6b5480 100644 --- a/include/spdlog/details/file_helper-inl.h +++ b/include/spdlog/details/file_helper-inl.h @@ -29,9 +29,16 @@ SPDLOG_INLINE void file_helper::open(const filename_t &fname, bool truncate) { close(); auto *mode = truncate ? SPDLOG_FILENAME_T("wb") : SPDLOG_FILENAME_T("ab"); + auto folder_name = os::dir_name(fname); _filename = fname; + for (int tries = 0; tries < open_tries; ++tries) { + if (!folder_name.empty()) + { + os::create_dir(folder_name); + } + if (!os::fopen_s(&fd_, fname, mode)) { return; @@ -129,5 +136,6 @@ SPDLOG_INLINE std::tuple file_helper::split_by_extension // finally - return a valid base and extension tuple return std::make_tuple(fname.substr(0, ext_index), fname.substr(ext_index)); } + } // namespace details } // namespace spdlog diff --git a/include/spdlog/details/os-inl.h b/include/spdlog/details/os-inl.h index f436b0d1..98914eb4 100644 --- a/include/spdlog/details/os-inl.h +++ b/include/spdlog/details/os-inl.h @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -196,7 +197,7 @@ SPDLOG_INLINE bool file_exists(const filename_t &filename) SPDLOG_NOEXCEPT #else auto attribs = GetFileAttributesA(filename.c_str()); #endif - return (attribs != INVALID_FILE_ATTRIBUTES && !(attribs & FILE_ATTRIBUTE_DIRECTORY)); + return attribs != INVALID_FILE_ATTRIBUTES; #else // common linux/unix all have the stat system call struct stat buffer; return (::stat(filename.c_str(), &buffer) == 0); @@ -460,6 +461,72 @@ SPDLOG_INLINE void wstr_to_utf8buf(wstring_view_t wstr, memory_buf_t &target) } #endif // (defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)) && defined(_WIN32) +// return true on success +SPDLOG_INLINE bool mkdir_(const filename_t &path) +{ +#ifdef _WIN32 +#ifdef SPDLOG_WCHAR_FILENAMES + return ::_wmkdir(path.c_str()) == 0; +#else + return ::_mkdir(path.c_str()) == 0; +#endif +#else + return ::mkdir(path.c_str(), mode_t(0755)) == 0; +#endif +} + +// create the given directory - and all directories leading to it +// return true on success +SPDLOG_INLINE bool create_dir(filename_t path) +{ + if (file_exists(path)) + { + return true; + } + using char_type = filename_t::value_type; + std::basic_istringstream istream(path); + filename_t token; + filename_t cur_dir; + char_type sep = '/'; + +#ifdef _WIN32 + // support forward slash in windows + std::replace(path.begin(), path.end(), char_type('\\'), sep); +#endif + while (std::getline(istream, token, sep)) + { + if (!token.empty()) + { + cur_dir += token; + if (!file_exists(cur_dir) && !mkdir_(cur_dir)) + { + return false; + } + } + cur_dir += sep; + } + + return true; +} + +// Return directory name from given path or empty string +// "abc/file" => "abc" +// "abc/" => "abc" +// "abc" => "" +// "abc///" => "abc" +SPDLOG_INLINE filename_t dir_name(filename_t path) +{ + using char_type = filename_t::value_type; + char_type sep = '/'; + +#ifdef _WIN32 + // support forward slash in windows + std::replace(path.begin(), path.end(), char_type('\\'), sep); +#endif + auto pos = path.find_last_of(sep); + return pos != filename_t::npos ? path.substr(0, pos) : filename_t{}; +} + } // namespace os } // namespace details } // namespace spdlog diff --git a/include/spdlog/details/os.h b/include/spdlog/details/os.h index a4a57b7f..0d6abd2f 100644 --- a/include/spdlog/details/os.h +++ b/include/spdlog/details/os.h @@ -89,6 +89,17 @@ bool in_terminal(FILE *file) SPDLOG_NOEXCEPT; void wstr_to_utf8buf(wstring_view_t wstr, memory_buf_t &target); #endif +// Return directory name from given path or empty string +// "abc/file" => "abc" +// "abc/" => "abc" +// "abc" => "" +// "abc///" => "abc" +filename_t dir_name(filename_t path); + +// Create a dir from the given path. +// Return true if succeeded or if this dir already exists. +bool create_dir(filename_t path); + } // namespace os } // namespace details } // namespace spdlog diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 1cf8f805..fd90dd56 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -25,7 +25,8 @@ set(SPDLOG_UTESTS_SOURCES test_fmt_helper.cpp test_stdout_api.cpp test_dup_filter.cpp - test_backtrace.cpp) + test_backtrace.cpp + test_create_dir.cpp) if(NOT SPDLOG_NO_EXCEPTIONS) list(APPEND SPDLOG_UTESTS_SOURCES test_errors.cpp) @@ -35,7 +36,6 @@ if(systemd_FOUND) list(APPEND SPDLOG_UTESTS_SOURCES test_systemd.cpp) endif() -file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/logs") enable_testing() # The compiled library tests diff --git a/tests/meson.build b/tests/meson.build index 73d7b8f8..98b40797 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -35,7 +35,6 @@ if systemd_dep.found() global_test_deps += systemd_dep endif -run_command('mkdir', 'logs') # -------------------------------------- # --- Build the test executables --- # -------------------------------------- @@ -49,5 +48,3 @@ foreach i : test_matrix test_exe = executable(i[0], test_sources, dependencies: global_test_deps + [i[1]]) test('test_' + i[0], test_exe) endforeach - -run_command(find_program('mkdir'), meson.current_build_dir() + '/logs') \ No newline at end of file diff --git a/tests/test_async.cpp b/tests/test_async.cpp index 6f86cf6c..166ac21e 100644 --- a/tests/test_async.cpp +++ b/tests/test_async.cpp @@ -157,7 +157,7 @@ TEST_CASE("to_file", "[async]") prepare_logdir(); size_t messages = 1024; size_t tp_threads = 1; - std::string filename = "logs/async_test.log"; + std::string filename = "test_logs/async_test.log"; { auto file_sink = std::make_shared(filename, true); auto tp = std::make_shared(messages, tp_threads); @@ -179,7 +179,7 @@ TEST_CASE("to_file multi-workers", "[async]") prepare_logdir(); size_t messages = 1024 * 10; size_t tp_threads = 10; - std::string filename = "logs/async_test.log"; + std::string filename = "test_logs/async_test.log"; { auto file_sink = std::make_shared(filename, true); auto tp = std::make_shared(messages, tp_threads); diff --git a/tests/test_create_dir.cpp b/tests/test_create_dir.cpp new file mode 100644 index 00000000..5e1a2659 --- /dev/null +++ b/tests/test_create_dir.cpp @@ -0,0 +1,27 @@ +/* + * This content is released under the MIT License as specified in https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE + */ +#include "includes.h" + +using spdlog::details::os::create_dir; +using spdlog::details::os::file_exists; + +void test_create_dir(const char *path, const char *normalized_path) +{ + auto rv = create_dir(path); + REQUIRE(rv == true); + REQUIRE(file_exists(normalized_path)); +} + +#include "spdlog/sinks/stdout_color_sinks.h" + +TEST_CASE("create_dir", "[create_dir]") +{ + prepare_logdir(); + test_create_dir("test_logs/dir1/dir1", "test_logs/dir1/dir1"); + test_create_dir("test_logs/dir1///dir2", "test_logs/dir1/dir2"); + test_create_dir("./test_logs/dir1/dir3", "test_logs/dir1/dir3"); + test_create_dir("test_logs/../test_logs/dir1/dir4", "test_logs/dir1/dir4"); + test_create_dir("./test_logs/dir1/dir2/dir99/../dir23", "test_logs/dir1/dir2/dir23"); + spdlog::drop("test-create-dir"); +} diff --git a/tests/test_daily_logger.cpp b/tests/test_daily_logger.cpp index c89ef171..cf2002a3 100644 --- a/tests/test_daily_logger.cpp +++ b/tests/test_daily_logger.cpp @@ -10,7 +10,7 @@ TEST_CASE("daily_logger with dateonly calculator", "[daily_logger]") prepare_logdir(); // calculate filename (time based) - std::string basename = "logs/daily_dateonly"; + std::string basename = "test_logs/daily_dateonly"; std::tm tm = spdlog::details::os::localtime(); spdlog::memory_buf_t w; fmt::format_to(w, "{}_{:04d}-{:02d}-{:02d}", basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday); @@ -44,7 +44,7 @@ TEST_CASE("daily_logger with custom calculator", "[daily_logger]") prepare_logdir(); // calculate filename (time based) - std::string basename = "logs/daily_dateonly"; + std::string basename = "test_logs/daily_dateonly"; std::tm tm = spdlog::details::os::localtime(); spdlog::memory_buf_t w; fmt::format_to(w, "{}{:04d}{:02d}{:02d}", basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday); @@ -119,7 +119,7 @@ static void test_rotate(int days_to_run, uint16_t max_days, uint16_t expected_n_ prepare_logdir(); - std::string basename = "logs/daily_rotate.txt"; + std::string basename = "test_logs/daily_rotate.txt"; daily_file_sink_st sink{basename, 2, 30, true, max_days}; // simulate messages with 24 intervals @@ -130,7 +130,7 @@ static void test_rotate(int days_to_run, uint16_t max_days, uint16_t expected_n_ sink.log(create_msg(offset)); } - REQUIRE(count_files("logs") == static_cast(expected_n_files)); + REQUIRE(count_files("test_logs") == static_cast(expected_n_files)); } TEST_CASE("daily_logger rotate", "[daily_file_sink]") diff --git a/tests/test_errors.cpp b/tests/test_errors.cpp index 65185d3c..4fc40594 100644 --- a/tests/test_errors.cpp +++ b/tests/test_errors.cpp @@ -26,7 +26,7 @@ protected: TEST_CASE("default_error_handler", "[errors]]") { prepare_logdir(); - std::string filename = "logs/simple_log.txt"; + std::string filename = "test_logs/simple_log.txt"; auto logger = spdlog::create("test-error", filename, true); logger->set_pattern("%v"); @@ -43,7 +43,7 @@ struct custom_ex TEST_CASE("custom_error_handler", "[errors]]") { prepare_logdir(); - std::string filename = "logs/simple_log.txt"; + std::string filename = "test_logs/simple_log.txt"; auto logger = spdlog::create("logger", filename, true); logger->flush_on(spdlog::level::info); logger->set_error_handler([=](const std::string &) { throw custom_ex(); }); @@ -75,15 +75,15 @@ TEST_CASE("async_error_handler", "[errors]]") prepare_logdir(); std::string err_msg("log failed with some msg"); - std::string filename = "logs/simple_async_log.txt"; + std::string filename = "test_logs/simple_async_log.txt"; { spdlog::init_thread_pool(128, 1); auto logger = spdlog::create_async("logger", filename, true); logger->set_error_handler([=](const std::string &) { - std::ofstream ofs("logs/custom_err.txt"); + std::ofstream ofs("test_logs/custom_err.txt"); if (!ofs) { - throw std::runtime_error("Failed open logs/custom_err.txt"); + throw std::runtime_error("Failed open test_logs/custom_err.txt"); } ofs << err_msg; }); @@ -94,7 +94,7 @@ TEST_CASE("async_error_handler", "[errors]]") } spdlog::init_thread_pool(128, 1); REQUIRE(count_lines(filename) == 2); - REQUIRE(file_contents("logs/custom_err.txt") == err_msg); + REQUIRE(file_contents("test_logs/custom_err.txt") == err_msg); } // Make sure async error handler is executed @@ -103,12 +103,13 @@ TEST_CASE("async_error_handler2", "[errors]]") prepare_logdir(); std::string err_msg("This is async handler error message"); { + spdlog::details::os::create_dir("test_logs"); spdlog::init_thread_pool(128, 1); auto logger = spdlog::create_async("failed_logger"); logger->set_error_handler([=](const std::string &) { - std::ofstream ofs("logs/custom_err2.txt"); + std::ofstream ofs("test_logs/custom_err2.txt"); if (!ofs) - throw std::runtime_error("Failed open logs/custom_err2.txt"); + throw std::runtime_error("Failed open test_logs/custom_err2.txt"); ofs << err_msg; }); logger->info("Hello failure"); @@ -116,5 +117,5 @@ TEST_CASE("async_error_handler2", "[errors]]") } spdlog::init_thread_pool(128, 1); - REQUIRE(file_contents("logs/custom_err2.txt") == err_msg); + REQUIRE(file_contents("test_logs/custom_err2.txt") == err_msg); } diff --git a/tests/test_file_helper.cpp b/tests/test_file_helper.cpp index 74e3744e..85f62ae2 100644 --- a/tests/test_file_helper.cpp +++ b/tests/test_file_helper.cpp @@ -6,7 +6,7 @@ using spdlog::details::file_helper; using spdlog::details::log_msg; -static const std::string target_filename = "logs/file_helper_test.txt"; +static const std::string target_filename = "test_logs/file_helper_test.txt"; static void write_with_helper(file_helper &helper, size_t howmany) { diff --git a/tests/test_file_logging.cpp b/tests/test_file_logging.cpp index 7563cf65..a8f9b2bb 100644 --- a/tests/test_file_logging.cpp +++ b/tests/test_file_logging.cpp @@ -6,7 +6,7 @@ TEST_CASE("simple_file_logger", "[simple_logger]]") { prepare_logdir(); - std::string filename = "logs/simple_log"; + std::string filename = "test_logs/simple_log"; auto logger = spdlog::create("logger", filename); logger->set_pattern("%v"); @@ -22,7 +22,7 @@ TEST_CASE("simple_file_logger", "[simple_logger]]") TEST_CASE("flush_on", "[flush_on]]") { prepare_logdir(); - std::string filename = "logs/simple_log"; + std::string filename = "test_logs/simple_log"; auto logger = spdlog::create("logger", filename); logger->set_pattern("%v"); @@ -42,7 +42,7 @@ TEST_CASE("rotating_file_logger1", "[rotating_logger]]") { prepare_logdir(); size_t max_size = 1024 * 10; - std::string basename = "logs/rotating_log"; + std::string basename = "test_logs/rotating_log"; auto logger = spdlog::rotating_logger_mt("logger", basename, max_size, 0); for (int i = 0; i < 10; ++i) @@ -59,7 +59,7 @@ TEST_CASE("rotating_file_logger2", "[rotating_logger]]") { prepare_logdir(); size_t max_size = 1024 * 10; - std::string basename = "logs/rotating_log"; + std::string basename = "test_logs/rotating_log"; { // make an initial logger to create the first output file diff --git a/tests/test_macros.cpp b/tests/test_macros.cpp index 26b03cdf..75f53ab2 100644 --- a/tests/test_macros.cpp +++ b/tests/test_macros.cpp @@ -12,7 +12,7 @@ TEST_CASE("debug and trace w/o format string", "[macros]]") { prepare_logdir(); - std::string filename = "logs/simple_log"; + std::string filename = "test_logs/simple_log"; auto logger = spdlog::create("logger", filename); logger->set_pattern("%v"); diff --git a/tests/utils.cpp b/tests/utils.cpp index 6c7f3662..426acb30 100644 --- a/tests/utils.cpp +++ b/tests/utils.cpp @@ -9,18 +9,12 @@ void prepare_logdir() { spdlog::drop_all(); #ifdef _WIN32 - system("if not exist logs mkdir logs"); - system("del /F /Q logs\\*"); + system("rmdir /S /Q test_logs") #else - auto rv = system("mkdir -p logs"); + auto rv = system("rm -rf test_logs"); if (rv != 0) { - throw std::runtime_error("Failed to mkdir -p logs"); - } - rv = system("rm -f logs/*"); - if (rv != 0) - { - throw std::runtime_error("Failed to rm -f logs/*"); + throw std::runtime_error("Failed to rm -rf test_logs"); } #endif } From 9a68bd8cc8d721cc1c3a988c62229679df7b88be Mon Sep 17 00:00:00 2001 From: gabime Date: Sun, 20 Oct 2019 17:48:13 +0300 Subject: [PATCH 02/24] Fixed missing include --- include/spdlog/details/os-inl.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/spdlog/details/os-inl.h b/include/spdlog/details/os-inl.h index 98914eb4..a111844c 100644 --- a/include/spdlog/details/os-inl.h +++ b/include/spdlog/details/os-inl.h @@ -43,6 +43,8 @@ #include #endif +#include // for _mkdir/_wmkdir + #else // unix #include From 5f3521b3d4a38b01b2d7856d55ef0939df3c695e Mon Sep 17 00:00:00 2001 From: Gabi Melman Date: Sun, 20 Oct 2019 17:55:13 +0300 Subject: [PATCH 03/24] Update utils.cpp --- tests/utils.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/utils.cpp b/tests/utils.cpp index 426acb30..255a4fec 100644 --- a/tests/utils.cpp +++ b/tests/utils.cpp @@ -9,7 +9,7 @@ void prepare_logdir() { spdlog::drop_all(); #ifdef _WIN32 - system("rmdir /S /Q test_logs") + system("rmdir /S /Q test_logs"); #else auto rv = system("rm -rf test_logs"); if (rv != 0) From d9f726f2a5d43147b81642d3878e70c37d237268 Mon Sep 17 00:00:00 2001 From: Gabi Melman Date: Sun, 20 Oct 2019 18:25:09 +0300 Subject: [PATCH 04/24] Add global namespace qualifiers to global function calls in os-inl.h --- include/spdlog/details/os-inl.h | 40 ++++++++++++++++----------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/include/spdlog/details/os-inl.h b/include/spdlog/details/os-inl.h index a111844c..c539f5bf 100644 --- a/include/spdlog/details/os-inl.h +++ b/include/spdlog/details/os-inl.h @@ -94,17 +94,17 @@ SPDLOG_INLINE std::tm localtime(const std::time_t &time_tt) SPDLOG_NOEXCEPT #ifdef _WIN32 std::tm tm; - localtime_s(&tm, &time_tt); + ::localtime_s(&tm, &time_tt); #else std::tm tm; - localtime_r(&time_tt, &tm); + ::localtime_r(&time_tt, &tm); #endif return tm; } SPDLOG_INLINE std::tm localtime() SPDLOG_NOEXCEPT { - std::time_t now_t = time(nullptr); + std::time_t now_t = ::time(nullptr); return localtime(now_t); } @@ -123,7 +123,7 @@ SPDLOG_INLINE std::tm gmtime(const std::time_t &time_tt) SPDLOG_NOEXCEPT SPDLOG_INLINE std::tm gmtime() SPDLOG_NOEXCEPT { - std::time_t now_t = time(nullptr); + std::time_t now_t = ::time(nullptr); return gmtime(now_t); } @@ -132,13 +132,13 @@ SPDLOG_INLINE void prevent_child_fd(FILE *f) #ifdef _WIN32 #if !defined(__cplusplus_winrt) - auto file_handle = reinterpret_cast(_get_osfhandle(_fileno(f))); + auto file_handle = reinterpret_cast(_get_osfhandle(::_fileno(f))); if (!::SetHandleInformation(file_handle, HANDLE_FLAG_INHERIT, 0)) SPDLOG_THROW(spdlog_ex("SetHandleInformation failed", errno)); #endif #else - auto fd = fileno(f); - if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) + auto fd = ::fileno(f); + if (::fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) { SPDLOG_THROW(spdlog_ex("fcntl with FD_CLOEXEC failed", errno)); } @@ -150,12 +150,12 @@ SPDLOG_INLINE bool fopen_s(FILE **fp, const filename_t &filename, const filename { #ifdef _WIN32 #ifdef SPDLOG_WCHAR_FILENAMES - *fp = _wfsopen((filename.c_str()), mode.c_str(), _SH_DENYNO); + *fp = ::_wfsopen((filename.c_str()), mode.c_str(), _SH_DENYNO); #else - *fp = _fsopen((filename.c_str()), mode.c_str(), _SH_DENYNO); + *fp = ::_fsopen((filename.c_str()), mode.c_str(), _SH_DENYNO); #endif #else // unix - *fp = fopen((filename.c_str()), mode.c_str()); + *fp = ::fopen((filename.c_str()), mode.c_str()); #endif #ifdef SPDLOG_PREVENT_CHILD_FD @@ -170,7 +170,7 @@ SPDLOG_INLINE bool fopen_s(FILE **fp, const filename_t &filename, const filename SPDLOG_INLINE int remove(const filename_t &filename) SPDLOG_NOEXCEPT { #if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES) - return _wremove(filename.c_str()); + return ::_wremove(filename.c_str()); #else return std::remove(filename.c_str()); #endif @@ -184,7 +184,7 @@ SPDLOG_INLINE int remove_if_exists(const filename_t &filename) SPDLOG_NOEXCEPT SPDLOG_INLINE int rename(const filename_t &filename1, const filename_t &filename2) SPDLOG_NOEXCEPT { #if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES) - return _wrename(filename1.c_str(), filename2.c_str()); + return ::_wrename(filename1.c_str(), filename2.c_str()); #else return std::rename(filename1.c_str(), filename2.c_str()); #endif @@ -195,9 +195,9 @@ SPDLOG_INLINE bool file_exists(const filename_t &filename) SPDLOG_NOEXCEPT { #ifdef _WIN32 #ifdef SPDLOG_WCHAR_FILENAMES - auto attribs = GetFileAttributesW(filename.c_str()); + auto attribs = ::GetFileAttributesW(filename.c_str()); #else - auto attribs = GetFileAttributesA(filename.c_str()); + auto attribs = ::GetFileAttributesA(filename.c_str()); #endif return attribs != INVALID_FILE_ATTRIBUTES; #else // common linux/unix all have the stat system call @@ -214,16 +214,16 @@ SPDLOG_INLINE size_t filesize(FILE *f) SPDLOG_THROW(spdlog_ex("Failed getting file size. fd is null")); } #if defined(_WIN32) && !defined(__CYGWIN__) - int fd = _fileno(f); + int fd = ::_fileno(f); #if _WIN64 // 64 bits - __int64 ret = _filelengthi64(fd); + __int64 ret = ::_filelengthi64(fd); if (ret >= 0) { return static_cast(ret); } #else // windows 32 bits - long ret = _filelength(fd); + long ret = ::_filelength(fd); if (ret >= 0) { return static_cast(ret); @@ -231,7 +231,7 @@ SPDLOG_INLINE size_t filesize(FILE *f) #endif #else // unix - int fd = fileno(f); + int fd = ::fileno(f); // 64 bits(but not in osx or cygwin, where fstat64 is deprecated) #if (defined(__linux__) || defined(__sun) || defined(_AIX)) && (defined(__LP64__) || defined(_LP64)) struct stat64 st; @@ -258,10 +258,10 @@ SPDLOG_INLINE int utc_minutes_offset(const std::tm &tm) #ifdef _WIN32 #if _WIN32_WINNT < _WIN32_WINNT_WS08 TIME_ZONE_INFORMATION tzinfo; - auto rv = GetTimeZoneInformation(&tzinfo); + auto rv = ::GetTimeZoneInformation(&tzinfo); #else DYNAMIC_TIME_ZONE_INFORMATION tzinfo; - auto rv = GetDynamicTimeZoneInformation(&tzinfo); + auto rv = ::filnoGetDynamicTimeZoneInformation(&tzinfo); #endif if (rv == TIME_ZONE_ID_INVALID) SPDLOG_THROW(spdlog::spdlog_ex("Failed getting timezone info. ", errno)); From 8a638a95a076ee2fb244acf3d0b323509495e146 Mon Sep 17 00:00:00 2001 From: Gabi Melman Date: Sun, 20 Oct 2019 18:31:04 +0300 Subject: [PATCH 05/24] Update os-inl.h --- include/spdlog/details/os-inl.h | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/include/spdlog/details/os-inl.h b/include/spdlog/details/os-inl.h index c539f5bf..5d2db635 100644 --- a/include/spdlog/details/os-inl.h +++ b/include/spdlog/details/os-inl.h @@ -113,10 +113,10 @@ SPDLOG_INLINE std::tm gmtime(const std::time_t &time_tt) SPDLOG_NOEXCEPT #ifdef _WIN32 std::tm tm; - gmtime_s(&tm, &time_tt); + ::gmtime_s(&tm, &time_tt); #else std::tm tm; - gmtime_r(&time_tt, &tm); + ::gmtime_r(&time_tt, &tm); #endif return tm; } @@ -261,7 +261,7 @@ SPDLOG_INLINE int utc_minutes_offset(const std::tm &tm) auto rv = ::GetTimeZoneInformation(&tzinfo); #else DYNAMIC_TIME_ZONE_INFORMATION tzinfo; - auto rv = ::filnoGetDynamicTimeZoneInformation(&tzinfo); + auto rv = ::GetDynamicTimeZoneInformation(&tzinfo); #endif if (rv == TIME_ZONE_ID_INVALID) SPDLOG_THROW(spdlog::spdlog_ex("Failed getting timezone info. ", errno)); @@ -327,15 +327,15 @@ SPDLOG_INLINE size_t _thread_id() SPDLOG_NOEXCEPT #if defined(__ANDROID__) && defined(__ANDROID_API__) && (__ANDROID_API__ < 21) #define SYS_gettid __NR_gettid #endif - return static_cast(syscall(SYS_gettid)); + return static_cast(::syscall(SYS_gettid)); #elif defined(_AIX) || defined(__DragonFly__) || defined(__FreeBSD__) - return static_cast(pthread_getthreadid_np()); + return static_cast(::pthread_getthreadid_np()); #elif defined(__NetBSD__) - return static_cast(_lwp_self()); + return static_cast(::_lwp_self()); #elif defined(__OpenBSD__) - return static_cast(getthrid()); + return static_cast(::getthrid()); #elif defined(__sun) - return static_cast(thr_self()); + return static_cast(::thr_self()); #elif __APPLE__ uint64_t tid; pthread_threadid_np(nullptr, &tid); @@ -420,9 +420,9 @@ SPDLOG_INLINE bool in_terminal(FILE *file) SPDLOG_NOEXCEPT { #ifdef _WIN32 - return _isatty(_fileno(file)) != 0; + return ::_isatty(_fileno(file)) != 0; #else - return isatty(fileno(file)) != 0; + return ::isatty(fileno(file)) != 0; #endif } From 1271081865171443bb311df6a3fa6522c5352cd6 Mon Sep 17 00:00:00 2001 From: Gabi Melman Date: Sun, 20 Oct 2019 19:08:47 +0300 Subject: [PATCH 06/24] Update os-inl.h --- include/spdlog/details/os-inl.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/spdlog/details/os-inl.h b/include/spdlog/details/os-inl.h index 5d2db635..b237adeb 100644 --- a/include/spdlog/details/os-inl.h +++ b/include/spdlog/details/os-inl.h @@ -515,7 +515,7 @@ SPDLOG_INLINE bool create_dir(filename_t path) // "abc/file" => "abc" // "abc/" => "abc" // "abc" => "" -// "abc///" => "abc" +// "abc///" => "abc//" SPDLOG_INLINE filename_t dir_name(filename_t path) { using char_type = filename_t::value_type; From d3c6974e991cdcc645c19a1d4cc2db67be250ba9 Mon Sep 17 00:00:00 2001 From: Gabi Melman Date: Sun, 20 Oct 2019 19:09:37 +0300 Subject: [PATCH 07/24] Update os.h --- include/spdlog/details/os.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/spdlog/details/os.h b/include/spdlog/details/os.h index 0d6abd2f..352b063f 100644 --- a/include/spdlog/details/os.h +++ b/include/spdlog/details/os.h @@ -93,7 +93,7 @@ void wstr_to_utf8buf(wstring_view_t wstr, memory_buf_t &target); // "abc/file" => "abc" // "abc/" => "abc" // "abc" => "" -// "abc///" => "abc" +// "abc///" => "abc//" filename_t dir_name(filename_t path); // Create a dir from the given path. From 31ed1339325be8572cd74f08d379e4421332360b Mon Sep 17 00:00:00 2001 From: gabime Date: Mon, 21 Oct 2019 13:42:25 +0300 Subject: [PATCH 08/24] Added dir_name tests --- tests/test_create_dir.cpp | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/tests/test_create_dir.cpp b/tests/test_create_dir.cpp index 5e1a2659..3849e2fe 100644 --- a/tests/test_create_dir.cpp +++ b/tests/test_create_dir.cpp @@ -23,5 +23,36 @@ TEST_CASE("create_dir", "[create_dir]") test_create_dir("./test_logs/dir1/dir3", "test_logs/dir1/dir3"); test_create_dir("test_logs/../test_logs/dir1/dir4", "test_logs/dir1/dir4"); test_create_dir("./test_logs/dir1/dir2/dir99/../dir23", "test_logs/dir1/dir2/dir23"); - spdlog::drop("test-create-dir"); +} + +TEST_CASE("dir_name", "[create_dir]") +{ + using spdlog::details::os::dir_name; + REQUIRE(dir_name("").empty()); + REQUIRE(dir_name("dir").empty()); + REQUIRE(dir_name("dir/") == "dir"); + REQUIRE(dir_name("dir///") == "dir//"); + REQUIRE(dir_name("dir/file") == "dir"); + REQUIRE(dir_name("dir/file.txt") == "dir"); + REQUIRE(dir_name("dir/file.txt/") == "dir/file.txt"); + REQUIRE(dir_name("/dir/file.txt") == "/dir"); + REQUIRE(dir_name("//dir/file.txt") == "//dir"); + REQUIRE(dir_name("//dir/file.txt") == "//dir"); + REQUIRE(dir_name("../file.txt") == ".."); + REQUIRE(dir_name("./file.txt") == "."); +#ifdef WIN32 + REQUIRE(dir_name(R"(dir\)") == "dir"); + REQUIRE(dir_name(R"(dir\\\)") == "dir//"); + REQUIRE(dir_name(R"(dir\file)") == "dir"); + REQUIRE(dir_name(R"(dir\file.txt)") == "dir"); + REQUIRE(dir_name(R"(dir\file.txt\)") == "dir/file.txt"); + REQUIRE(dir_name(R"(\dir\file.txt)") == "/dir"); + REQUIRE(dir_name(R"(\\dir\file.txt)") == "//dir"); + REQUIRE(dir_name(R"(\\dir\file.txt)") == "//dir"); + REQUIRE(dir_name(R"(..\file.txt)") == ".."); + REQUIRE(dir_name(R"(.\file.txt)") == "."); + REQUIRE(dir_name(R"(c:\\a\b\c\d\file.txt)") == "c://a/b/c/d"); + //REQUIRE(dir_name(R"(c:\\a)") == "c://"); +#endif + } From e9d42e059f4381ce9402191293378adc5bdf2d2c Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 25 Oct 2019 12:52:39 +0300 Subject: [PATCH 09/24] // support forward slash in windows --- include/spdlog/details/os-inl.h | 18 +++++++----------- include/spdlog/details/os.h | 2 +- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/include/spdlog/details/os-inl.h b/include/spdlog/details/os-inl.h index b237adeb..590e35a1 100644 --- a/include/spdlog/details/os-inl.h +++ b/include/spdlog/details/os-inl.h @@ -488,14 +488,13 @@ SPDLOG_INLINE bool create_dir(filename_t path) using char_type = filename_t::value_type; std::basic_istringstream istream(path); filename_t token; - filename_t cur_dir; - char_type sep = '/'; + filename_t cur_dir; #ifdef _WIN32 // support forward slash in windows - std::replace(path.begin(), path.end(), char_type('\\'), sep); + std::replace(path.begin(), path.end(), '/', folder_sep); #endif - while (std::getline(istream, token, sep)) + while (std::getline(istream, token, folder_sep)) { if (!token.empty()) { @@ -505,7 +504,7 @@ SPDLOG_INLINE bool create_dir(filename_t path) return false; } } - cur_dir += sep; + cur_dir += folder_sep; } return true; @@ -517,15 +516,12 @@ SPDLOG_INLINE bool create_dir(filename_t path) // "abc" => "" // "abc///" => "abc//" SPDLOG_INLINE filename_t dir_name(filename_t path) -{ - using char_type = filename_t::value_type; - char_type sep = '/'; - +{ #ifdef _WIN32 // support forward slash in windows - std::replace(path.begin(), path.end(), char_type('\\'), sep); + std::replace(path.begin(), path.end(), '/', folder_sep); #endif - auto pos = path.find_last_of(sep); + auto pos = path.find_last_of(folder_sep); return pos != filename_t::npos ? path.substr(0, pos) : filename_t{}; } diff --git a/include/spdlog/details/os.h b/include/spdlog/details/os.h index 352b063f..2ab828ba 100644 --- a/include/spdlog/details/os.h +++ b/include/spdlog/details/os.h @@ -33,7 +33,7 @@ SPDLOG_CONSTEXPR static const char *default_eol = SPDLOG_EOL; // folder separator #ifdef _WIN32 -const char folder_sep = '\\'; +static const char folder_sep = '\\'; #else SPDLOG_CONSTEXPR static const char folder_sep = '/'; #endif From 066087b383d281a64c9e3fbf5294e480bf4ab41a Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 25 Oct 2019 14:14:50 +0300 Subject: [PATCH 10/24] Update create_dir --- include/spdlog/details/os-inl.h | 43 +++++++++++++------------ tests/test_create_dir.cpp | 56 ++++++++++++++++++++------------- 2 files changed, 55 insertions(+), 44 deletions(-) diff --git a/include/spdlog/details/os-inl.h b/include/spdlog/details/os-inl.h index 590e35a1..1764eeeb 100644 --- a/include/spdlog/details/os-inl.h +++ b/include/spdlog/details/os-inl.h @@ -485,27 +485,30 @@ SPDLOG_INLINE bool create_dir(filename_t path) { return true; } - using char_type = filename_t::value_type; - std::basic_istringstream istream(path); - filename_t token; - filename_t cur_dir; - + #ifdef _WIN32 // support forward slash in windows std::replace(path.begin(), path.end(), '/', folder_sep); -#endif - while (std::getline(istream, token, folder_sep)) - { - if (!token.empty()) - { - cur_dir += token; - if (!file_exists(cur_dir) && !mkdir_(cur_dir)) - { - return false; - } - } - cur_dir += folder_sep; - } +#endif + + size_t search_offset = 0; + do + { + auto token_pos = path.find(folder_sep, search_offset); + // treat the entire path as a folder if no folder separator not found + if (token_pos == filename_t::npos) + { + token_pos = path.size(); + } + + auto subdir = path.substr(0, token_pos); + + if (!subdir.empty() && !file_exists(subdir) && !mkdir_(subdir)) + { + return false; // return error if failed creating dir + } + search_offset = token_pos + 1; + } while (search_offset < path.size()); return true; } @@ -517,10 +520,6 @@ SPDLOG_INLINE bool create_dir(filename_t path) // "abc///" => "abc//" SPDLOG_INLINE filename_t dir_name(filename_t path) { -#ifdef _WIN32 - // support forward slash in windows - std::replace(path.begin(), path.end(), '/', folder_sep); -#endif auto pos = path.find_last_of(folder_sep); return pos != filename_t::npos ? path.substr(0, pos) : filename_t{}; } diff --git a/tests/test_create_dir.cpp b/tests/test_create_dir.cpp index 3849e2fe..1c20e5c1 100644 --- a/tests/test_create_dir.cpp +++ b/tests/test_create_dir.cpp @@ -8,6 +8,7 @@ using spdlog::details::os::file_exists; void test_create_dir(const char *path, const char *normalized_path) { + printf("Test Create dir %s\n", path); auto rv = create_dir(path); REQUIRE(rv == true); REQUIRE(file_exists(normalized_path)); @@ -18,11 +19,23 @@ void test_create_dir(const char *path, const char *normalized_path) TEST_CASE("create_dir", "[create_dir]") { prepare_logdir(); - test_create_dir("test_logs/dir1/dir1", "test_logs/dir1/dir1"); - test_create_dir("test_logs/dir1///dir2", "test_logs/dir1/dir2"); - test_create_dir("./test_logs/dir1/dir3", "test_logs/dir1/dir3"); - test_create_dir("test_logs/../test_logs/dir1/dir4", "test_logs/dir1/dir4"); - test_create_dir("./test_logs/dir1/dir2/dir99/../dir23", "test_logs/dir1/dir2/dir23"); +#ifdef WIN32 + test_create_dir("test_logs/dir1/dir1", "test_logs\\dir1\\dir1"); + test_create_dir("test_logs/dir1/dir1", "test_logs\\dir1\\dir1"); //test existing + test_create_dir("test_logs/dir1///dir2//", "test_logs\\dir1\\dir2"); + test_create_dir("./test_logs/dir1/dir3", "test_logs\\dir1\\dir3"); + test_create_dir("test_logs/../test_logs/dir1/dir4", "test_logs\\dir1\\dir4"); + // test backslash + test_create_dir("test_logs\\dir1\\dir222", "test_logs\\dir1\\dir222"); + test_create_dir("test_logs\\dir1\\dir223\\", "test_logs\\dir1\\dir223\\"); + test_create_dir(".\\test_logs\\dir1\\dir2\\dir99\\..\\dir23", "test_logs\\dir1\\dir2\\dir23"); +#else + test_create_dir("test_logs/dir1/dir1", "test_logs/dir1/dir1"); + test_create_dir("test_logs/dir1/dir1", "test_logs/dir1/dir1"); // test existing + test_create_dir("test_logs/dir1///dir2", "test_logs/dir1/dir2"); + test_create_dir("./test_logs/dir1/dir3", "test_logs/dir1/dir3"); + test_create_dir("test_logs/../test_logs/dir1/dir4", "test_logs/dir1/dir4"); +#endif } TEST_CASE("dir_name", "[create_dir]") @@ -30,29 +43,28 @@ TEST_CASE("dir_name", "[create_dir]") using spdlog::details::os::dir_name; REQUIRE(dir_name("").empty()); REQUIRE(dir_name("dir").empty()); - REQUIRE(dir_name("dir/") == "dir"); - REQUIRE(dir_name("dir///") == "dir//"); - REQUIRE(dir_name("dir/file") == "dir"); - REQUIRE(dir_name("dir/file.txt") == "dir"); - REQUIRE(dir_name("dir/file.txt/") == "dir/file.txt"); - REQUIRE(dir_name("/dir/file.txt") == "/dir"); - REQUIRE(dir_name("//dir/file.txt") == "//dir"); - REQUIRE(dir_name("//dir/file.txt") == "//dir"); - REQUIRE(dir_name("../file.txt") == ".."); - REQUIRE(dir_name("./file.txt") == "."); + #ifdef WIN32 REQUIRE(dir_name(R"(dir\)") == "dir"); - REQUIRE(dir_name(R"(dir\\\)") == "dir//"); + REQUIRE(dir_name(R"(dir\\\)") == R"(dir\\)"); REQUIRE(dir_name(R"(dir\file)") == "dir"); REQUIRE(dir_name(R"(dir\file.txt)") == "dir"); - REQUIRE(dir_name(R"(dir\file.txt\)") == "dir/file.txt"); - REQUIRE(dir_name(R"(\dir\file.txt)") == "/dir"); - REQUIRE(dir_name(R"(\\dir\file.txt)") == "//dir"); - REQUIRE(dir_name(R"(\\dir\file.txt)") == "//dir"); + REQUIRE(dir_name(R"(dir\file.txt\)") == R"(dir\file.txt)"); + REQUIRE(dir_name(R"(\dir\file.txt)") == R"(\dir)"); + REQUIRE(dir_name(R"(\\dir\file.txt)") == R"(\\dir)"); REQUIRE(dir_name(R"(..\file.txt)") == ".."); REQUIRE(dir_name(R"(.\file.txt)") == "."); - REQUIRE(dir_name(R"(c:\\a\b\c\d\file.txt)") == "c://a/b/c/d"); - //REQUIRE(dir_name(R"(c:\\a)") == "c://"); + REQUIRE(dir_name(R"(c:\\a\b\c\d\file.txt)") == R"(c:\\a\b\c\d)"); +#else + REQUIRE(dir_name("dir/") == "dir"); + REQUIRE(dir_name("dir///") == "dir//"); + REQUIRE(dir_name("dir/file") == "dir"); + REQUIRE(dir_name("dir/file.txt") == "dir"); + REQUIRE(dir_name("dir/file.txt/") == "dir/file.txt"); + REQUIRE(dir_name("/dir/file.txt") == "/dir"); + REQUIRE(dir_name("//dir/file.txt") == "//dir"); + REQUIRE(dir_name("../file.txt") == ".."); + REQUIRE(dir_name("./file.txt") == "."); #endif } From a1f283946ec3102e2ec493d10cfcabe6d91af023 Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 25 Oct 2019 14:29:57 +0300 Subject: [PATCH 11/24] updated os::dir_name and tests --- build-branch/example/logs2/basic-log.txt | 0 build-branch/tests/Debug/filename_2019-10-25 | 0 .../tests/Debug/test_logs/custom_err2.txt | 1 + include/spdlog/details/os-inl.h | 6 +++- tests/test_create_dir.cpp | 33 +++++++++---------- 5 files changed, 21 insertions(+), 19 deletions(-) create mode 100644 build-branch/example/logs2/basic-log.txt create mode 100644 build-branch/tests/Debug/filename_2019-10-25 create mode 100644 build-branch/tests/Debug/test_logs/custom_err2.txt diff --git a/build-branch/example/logs2/basic-log.txt b/build-branch/example/logs2/basic-log.txt new file mode 100644 index 00000000..e69de29b diff --git a/build-branch/tests/Debug/filename_2019-10-25 b/build-branch/tests/Debug/filename_2019-10-25 new file mode 100644 index 00000000..e69de29b diff --git a/build-branch/tests/Debug/test_logs/custom_err2.txt b/build-branch/tests/Debug/test_logs/custom_err2.txt new file mode 100644 index 00000000..58a61097 --- /dev/null +++ b/build-branch/tests/Debug/test_logs/custom_err2.txt @@ -0,0 +1 @@ +This is async handler error message \ No newline at end of file diff --git a/include/spdlog/details/os-inl.h b/include/spdlog/details/os-inl.h index 1764eeeb..a24b8005 100644 --- a/include/spdlog/details/os-inl.h +++ b/include/spdlog/details/os-inl.h @@ -519,7 +519,11 @@ SPDLOG_INLINE bool create_dir(filename_t path) // "abc" => "" // "abc///" => "abc//" SPDLOG_INLINE filename_t dir_name(filename_t path) -{ +{ +#ifdef _WIN32 + // support forward slash in windows + std::replace(path.begin(), path.end(), '/', folder_sep); +#endif auto pos = path.find_last_of(folder_sep); return pos != filename_t::npos ? path.substr(0, pos) : filename_t{}; } diff --git a/tests/test_create_dir.cpp b/tests/test_create_dir.cpp index 1c20e5c1..56f4282d 100644 --- a/tests/test_create_dir.cpp +++ b/tests/test_create_dir.cpp @@ -6,12 +6,11 @@ using spdlog::details::os::create_dir; using spdlog::details::os::file_exists; -void test_create_dir(const char *path, const char *normalized_path) -{ - printf("Test Create dir %s\n", path); +bool try_create_dir(const char *path, const char *normalized_path) +{ auto rv = create_dir(path); REQUIRE(rv == true); - REQUIRE(file_exists(normalized_path)); + return file_exists(normalized_path); } #include "spdlog/sinks/stdout_color_sinks.h" @@ -19,23 +18,21 @@ void test_create_dir(const char *path, const char *normalized_path) TEST_CASE("create_dir", "[create_dir]") { prepare_logdir(); + + REQUIRE(try_create_dir("test_logs/dir1/dir1", "test_logs/dir1/dir1")); + REQUIRE(try_create_dir("test_logs/dir1/dir1", "test_logs/dir1/dir1")); //test existing + REQUIRE(try_create_dir("test_logs/dir1///dir2//", "test_logs/dir1/dir2")); + REQUIRE(try_create_dir("./test_logs/dir1/dir3", "test_logs/dir1/dir3")); + REQUIRE(try_create_dir("test_logs/../test_logs/dir1/dir4", "test_logs/dir1/dir4")); + #ifdef WIN32 - test_create_dir("test_logs/dir1/dir1", "test_logs\\dir1\\dir1"); - test_create_dir("test_logs/dir1/dir1", "test_logs\\dir1\\dir1"); //test existing - test_create_dir("test_logs/dir1///dir2//", "test_logs\\dir1\\dir2"); - test_create_dir("./test_logs/dir1/dir3", "test_logs\\dir1\\dir3"); - test_create_dir("test_logs/../test_logs/dir1/dir4", "test_logs\\dir1\\dir4"); // test backslash - test_create_dir("test_logs\\dir1\\dir222", "test_logs\\dir1\\dir222"); - test_create_dir("test_logs\\dir1\\dir223\\", "test_logs\\dir1\\dir223\\"); - test_create_dir(".\\test_logs\\dir1\\dir2\\dir99\\..\\dir23", "test_logs\\dir1\\dir2\\dir23"); -#else - test_create_dir("test_logs/dir1/dir1", "test_logs/dir1/dir1"); - test_create_dir("test_logs/dir1/dir1", "test_logs/dir1/dir1"); // test existing - test_create_dir("test_logs/dir1///dir2", "test_logs/dir1/dir2"); - test_create_dir("./test_logs/dir1/dir3", "test_logs/dir1/dir3"); - test_create_dir("test_logs/../test_logs/dir1/dir4", "test_logs/dir1/dir4"); + REQUIRE(try_create_dir("test_logs\\dir1\\dir222", "test_logs\\dir1\\dir222")); + REQUIRE(try_create_dir("test_logs\\dir1\\dir223\\", "test_logs\\dir1\\dir223\\")); + REQUIRE(try_create_dir(".\\test_logs\\dir1\\dir2\\dir99\\..\\dir23", "test_logs\\dir1\\dir2\\dir23")); + REQUIRE(try_create_dir("test_logs\\..\\test_logs\\dir1\\dir5", "test_logs\\dir1\\dir5")); #endif + } TEST_CASE("dir_name", "[create_dir]") From bfc76278a9ab5ff1e5530d32ed24f263e55aa187 Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 25 Oct 2019 15:24:54 +0300 Subject: [PATCH 12/24] update tests --- build-branch/example/Debug/logs/mylog.txt | 2 ++ build-branch/example/Debug/mylog.txt | 1 + build-branch/example/logs2/basic-log.txt | 0 build-branch/example/mylog.txt | 2 ++ build-branch/tests/Debug/test_logs/custom_err2.txt | 1 - tests/test_create_dir.cpp | 11 +++++++++-- 6 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 build-branch/example/Debug/logs/mylog.txt create mode 100644 build-branch/example/Debug/mylog.txt delete mode 100644 build-branch/example/logs2/basic-log.txt create mode 100644 build-branch/example/mylog.txt delete mode 100644 build-branch/tests/Debug/test_logs/custom_err2.txt diff --git a/build-branch/example/Debug/logs/mylog.txt b/build-branch/example/Debug/logs/mylog.txt new file mode 100644 index 00000000..040f5132 --- /dev/null +++ b/build-branch/example/Debug/logs/mylog.txt @@ -0,0 +1,2 @@ +[2019-10-25 15:16:33.502] [LOGGER] [info] Hello +[2019-10-25 15:16:41.712] [LOGGER] [info] Hello diff --git a/build-branch/example/Debug/mylog.txt b/build-branch/example/Debug/mylog.txt new file mode 100644 index 00000000..f4e0ab68 --- /dev/null +++ b/build-branch/example/Debug/mylog.txt @@ -0,0 +1 @@ +[2019-10-25 15:16:58.845] [LOGGER] [info] Hello diff --git a/build-branch/example/logs2/basic-log.txt b/build-branch/example/logs2/basic-log.txt deleted file mode 100644 index e69de29b..00000000 diff --git a/build-branch/example/mylog.txt b/build-branch/example/mylog.txt new file mode 100644 index 00000000..a7126955 --- /dev/null +++ b/build-branch/example/mylog.txt @@ -0,0 +1,2 @@ +[2019-10-25 15:16:53.945] [LOGGER] [info] Hello +[2019-10-25 15:18:50.127] [LOGGER] [info] Hello diff --git a/build-branch/tests/Debug/test_logs/custom_err2.txt b/build-branch/tests/Debug/test_logs/custom_err2.txt deleted file mode 100644 index 58a61097..00000000 --- a/build-branch/tests/Debug/test_logs/custom_err2.txt +++ /dev/null @@ -1 +0,0 @@ -This is async handler error message \ No newline at end of file diff --git a/tests/test_create_dir.cpp b/tests/test_create_dir.cpp index 56f4282d..9ce13675 100644 --- a/tests/test_create_dir.cpp +++ b/tests/test_create_dir.cpp @@ -13,7 +13,6 @@ bool try_create_dir(const char *path, const char *normalized_path) return file_exists(normalized_path); } -#include "spdlog/sinks/stdout_color_sinks.h" TEST_CASE("create_dir", "[create_dir]") { @@ -26,7 +25,7 @@ TEST_CASE("create_dir", "[create_dir]") REQUIRE(try_create_dir("test_logs/../test_logs/dir1/dir4", "test_logs/dir1/dir4")); #ifdef WIN32 - // test backslash + // test backslash folder separator REQUIRE(try_create_dir("test_logs\\dir1\\dir222", "test_logs\\dir1\\dir222")); REQUIRE(try_create_dir("test_logs\\dir1\\dir223\\", "test_logs\\dir1\\dir223\\")); REQUIRE(try_create_dir(".\\test_logs\\dir1\\dir2\\dir99\\..\\dir23", "test_logs\\dir1\\dir2\\dir23")); @@ -45,13 +44,21 @@ TEST_CASE("dir_name", "[create_dir]") REQUIRE(dir_name(R"(dir\)") == "dir"); REQUIRE(dir_name(R"(dir\\\)") == R"(dir\\)"); REQUIRE(dir_name(R"(dir\file)") == "dir"); + REQUIRE(dir_name(R"(dir/file)") == "dir"); REQUIRE(dir_name(R"(dir\file.txt)") == "dir"); + REQUIRE(dir_name(R"(dir/file)") == "dir"); REQUIRE(dir_name(R"(dir\file.txt\)") == R"(dir\file.txt)"); + REQUIRE(dir_name(R"(dir/file.txt/)") == R"(dir\file.txt)"); REQUIRE(dir_name(R"(\dir\file.txt)") == R"(\dir)"); + REQUIRE(dir_name(R"(/dir/file.txt)") == R"(\dir)"); REQUIRE(dir_name(R"(\\dir\file.txt)") == R"(\\dir)"); + REQUIRE(dir_name(R"(//dir/file.txt)") == R"(\\dir)"); REQUIRE(dir_name(R"(..\file.txt)") == ".."); + REQUIRE(dir_name(R"(../file.txt)") == ".."); REQUIRE(dir_name(R"(.\file.txt)") == "."); + REQUIRE(dir_name(R"(./file.txt)") == "."); REQUIRE(dir_name(R"(c:\\a\b\c\d\file.txt)") == R"(c:\\a\b\c\d)"); + REQUIRE(dir_name(R"(c://a/b/c/d/file.txt)") == R"(c:\\a\b\c\d)"); #else REQUIRE(dir_name("dir/") == "dir"); REQUIRE(dir_name("dir///") == "dir//"); From c40555c0ac2c2f067fcd954a89fd1b2d2fa9f304 Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 25 Oct 2019 15:44:53 +0300 Subject: [PATCH 13/24] clang-format --- include/spdlog/details/os-inl.h | 46 ++++++++++++------------ tests/test_create_dir.cpp | 63 ++++++++++++++++----------------- 2 files changed, 53 insertions(+), 56 deletions(-) diff --git a/include/spdlog/details/os-inl.h b/include/spdlog/details/os-inl.h index a24b8005..31b20cea 100644 --- a/include/spdlog/details/os-inl.h +++ b/include/spdlog/details/os-inl.h @@ -485,30 +485,30 @@ SPDLOG_INLINE bool create_dir(filename_t path) { return true; } - + #ifdef _WIN32 // support forward slash in windows - std::replace(path.begin(), path.end(), '/', folder_sep); -#endif + std::replace(path.begin(), path.end(), '/', folder_sep); +#endif - size_t search_offset = 0; - do - { - auto token_pos = path.find(folder_sep, search_offset); - // treat the entire path as a folder if no folder separator not found - if (token_pos == filename_t::npos) - { - token_pos = path.size(); - } + size_t search_offset = 0; + do + { + auto token_pos = path.find(folder_sep, search_offset); + // treat the entire path as a folder if no folder separator not found + if (token_pos == filename_t::npos) + { + token_pos = path.size(); + } - auto subdir = path.substr(0, token_pos); + auto subdir = path.substr(0, token_pos); - if (!subdir.empty() && !file_exists(subdir) && !mkdir_(subdir)) - { - return false; // return error if failed creating dir - } - search_offset = token_pos + 1; - } while (search_offset < path.size()); + if (!subdir.empty() && !file_exists(subdir) && !mkdir_(subdir)) + { + return false; // return error if failed creating dir + } + search_offset = token_pos + 1; + } while (search_offset < path.size()); return true; } @@ -519,11 +519,11 @@ SPDLOG_INLINE bool create_dir(filename_t path) // "abc" => "" // "abc///" => "abc//" SPDLOG_INLINE filename_t dir_name(filename_t path) -{ +{ #ifdef _WIN32 - // support forward slash in windows - std::replace(path.begin(), path.end(), '/', folder_sep); -#endif + // support forward slash in windows + std::replace(path.begin(), path.end(), '/', folder_sep); +#endif auto pos = path.find_last_of(folder_sep); return pos != filename_t::npos ? path.substr(0, pos) : filename_t{}; } diff --git a/tests/test_create_dir.cpp b/tests/test_create_dir.cpp index 9ce13675..3e428d74 100644 --- a/tests/test_create_dir.cpp +++ b/tests/test_create_dir.cpp @@ -7,31 +7,29 @@ using spdlog::details::os::create_dir; using spdlog::details::os::file_exists; bool try_create_dir(const char *path, const char *normalized_path) -{ +{ auto rv = create_dir(path); REQUIRE(rv == true); return file_exists(normalized_path); } - TEST_CASE("create_dir", "[create_dir]") { prepare_logdir(); - REQUIRE(try_create_dir("test_logs/dir1/dir1", "test_logs/dir1/dir1")); - REQUIRE(try_create_dir("test_logs/dir1/dir1", "test_logs/dir1/dir1")); //test existing - REQUIRE(try_create_dir("test_logs/dir1///dir2//", "test_logs/dir1/dir2")); - REQUIRE(try_create_dir("./test_logs/dir1/dir3", "test_logs/dir1/dir3")); - REQUIRE(try_create_dir("test_logs/../test_logs/dir1/dir4", "test_logs/dir1/dir4")); + REQUIRE(try_create_dir("test_logs/dir1/dir1", "test_logs/dir1/dir1")); + REQUIRE(try_create_dir("test_logs/dir1/dir1", "test_logs/dir1/dir1")); // test existing + REQUIRE(try_create_dir("test_logs/dir1///dir2//", "test_logs/dir1/dir2")); + REQUIRE(try_create_dir("./test_logs/dir1/dir3", "test_logs/dir1/dir3")); + REQUIRE(try_create_dir("test_logs/../test_logs/dir1/dir4", "test_logs/dir1/dir4")); #ifdef WIN32 - // test backslash folder separator - REQUIRE(try_create_dir("test_logs\\dir1\\dir222", "test_logs\\dir1\\dir222")); - REQUIRE(try_create_dir("test_logs\\dir1\\dir223\\", "test_logs\\dir1\\dir223\\")); - REQUIRE(try_create_dir(".\\test_logs\\dir1\\dir2\\dir99\\..\\dir23", "test_logs\\dir1\\dir2\\dir23")); - REQUIRE(try_create_dir("test_logs\\..\\test_logs\\dir1\\dir5", "test_logs\\dir1\\dir5")); + // test backslash folder separator + REQUIRE(try_create_dir("test_logs\\dir1\\dir222", "test_logs\\dir1\\dir222")); + REQUIRE(try_create_dir("test_logs\\dir1\\dir223\\", "test_logs\\dir1\\dir223\\")); + REQUIRE(try_create_dir(".\\test_logs\\dir1\\dir2\\dir99\\..\\dir23", "test_logs\\dir1\\dir2\\dir23")); + REQUIRE(try_create_dir("test_logs\\..\\test_logs\\dir1\\dir5", "test_logs\\dir1\\dir5")); #endif - } TEST_CASE("dir_name", "[create_dir]") @@ -39,36 +37,35 @@ TEST_CASE("dir_name", "[create_dir]") using spdlog::details::os::dir_name; REQUIRE(dir_name("").empty()); REQUIRE(dir_name("dir").empty()); - + #ifdef WIN32 REQUIRE(dir_name(R"(dir\)") == "dir"); REQUIRE(dir_name(R"(dir\\\)") == R"(dir\\)"); REQUIRE(dir_name(R"(dir\file)") == "dir"); - REQUIRE(dir_name(R"(dir/file)") == "dir"); + REQUIRE(dir_name(R"(dir/file)") == "dir"); REQUIRE(dir_name(R"(dir\file.txt)") == "dir"); - REQUIRE(dir_name(R"(dir/file)") == "dir"); + REQUIRE(dir_name(R"(dir/file)") == "dir"); REQUIRE(dir_name(R"(dir\file.txt\)") == R"(dir\file.txt)"); - REQUIRE(dir_name(R"(dir/file.txt/)") == R"(dir\file.txt)"); + REQUIRE(dir_name(R"(dir/file.txt/)") == R"(dir\file.txt)"); REQUIRE(dir_name(R"(\dir\file.txt)") == R"(\dir)"); - REQUIRE(dir_name(R"(/dir/file.txt)") == R"(\dir)"); - REQUIRE(dir_name(R"(\\dir\file.txt)") == R"(\\dir)"); - REQUIRE(dir_name(R"(//dir/file.txt)") == R"(\\dir)"); + REQUIRE(dir_name(R"(/dir/file.txt)") == R"(\dir)"); + REQUIRE(dir_name(R"(\\dir\file.txt)") == R"(\\dir)"); + REQUIRE(dir_name(R"(//dir/file.txt)") == R"(\\dir)"); REQUIRE(dir_name(R"(..\file.txt)") == ".."); - REQUIRE(dir_name(R"(../file.txt)") == ".."); + REQUIRE(dir_name(R"(../file.txt)") == ".."); REQUIRE(dir_name(R"(.\file.txt)") == "."); - REQUIRE(dir_name(R"(./file.txt)") == "."); + REQUIRE(dir_name(R"(./file.txt)") == "."); REQUIRE(dir_name(R"(c:\\a\b\c\d\file.txt)") == R"(c:\\a\b\c\d)"); - REQUIRE(dir_name(R"(c://a/b/c/d/file.txt)") == R"(c:\\a\b\c\d)"); + REQUIRE(dir_name(R"(c://a/b/c/d/file.txt)") == R"(c:\\a\b\c\d)"); #else - REQUIRE(dir_name("dir/") == "dir"); - REQUIRE(dir_name("dir///") == "dir//"); - REQUIRE(dir_name("dir/file") == "dir"); - REQUIRE(dir_name("dir/file.txt") == "dir"); - REQUIRE(dir_name("dir/file.txt/") == "dir/file.txt"); - REQUIRE(dir_name("/dir/file.txt") == "/dir"); - REQUIRE(dir_name("//dir/file.txt") == "//dir"); - REQUIRE(dir_name("../file.txt") == ".."); - REQUIRE(dir_name("./file.txt") == "."); + REQUIRE(dir_name("dir/") == "dir"); + REQUIRE(dir_name("dir///") == "dir//"); + REQUIRE(dir_name("dir/file") == "dir"); + REQUIRE(dir_name("dir/file.txt") == "dir"); + REQUIRE(dir_name("dir/file.txt/") == "dir/file.txt"); + REQUIRE(dir_name("/dir/file.txt") == "/dir"); + REQUIRE(dir_name("//dir/file.txt") == "//dir"); + REQUIRE(dir_name("../file.txt") == ".."); + REQUIRE(dir_name("./file.txt") == "."); #endif - } From dbe5c17a9610cecb2dcab311a4b9844bfe94cf83 Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 25 Oct 2019 15:56:23 +0300 Subject: [PATCH 14/24] Renamed file_exists()->path_exists() --- include/spdlog/details/file_helper-inl.h | 5 ----- include/spdlog/details/file_helper.h | 1 - include/spdlog/details/os-inl.h | 10 +++++----- include/spdlog/details/os.h | 2 +- include/spdlog/sinks/rotating_file_sink-inl.h | 3 ++- tests/test_create_dir.cpp | 4 ++-- tests/test_file_helper.cpp | 8 -------- 7 files changed, 10 insertions(+), 23 deletions(-) diff --git a/include/spdlog/details/file_helper-inl.h b/include/spdlog/details/file_helper-inl.h index cd6b5480..98ef82fa 100644 --- a/include/spdlog/details/file_helper-inl.h +++ b/include/spdlog/details/file_helper-inl.h @@ -97,11 +97,6 @@ SPDLOG_INLINE const filename_t &file_helper::filename() const return _filename; } -SPDLOG_INLINE bool file_helper::file_exists(const filename_t &fname) -{ - return os::file_exists(fname); -} - // // return file path and its extension: // diff --git a/include/spdlog/details/file_helper.h b/include/spdlog/details/file_helper.h index 990d99e3..89d6b244 100644 --- a/include/spdlog/details/file_helper.h +++ b/include/spdlog/details/file_helper.h @@ -29,7 +29,6 @@ public: void write(const memory_buf_t &buf); size_t size() const; const filename_t &filename() const; - static bool file_exists(const filename_t &fname); // // return file path and its extension: diff --git a/include/spdlog/details/os-inl.h b/include/spdlog/details/os-inl.h index 31b20cea..b5669ac7 100644 --- a/include/spdlog/details/os-inl.h +++ b/include/spdlog/details/os-inl.h @@ -178,7 +178,7 @@ SPDLOG_INLINE int remove(const filename_t &filename) SPDLOG_NOEXCEPT SPDLOG_INLINE int remove_if_exists(const filename_t &filename) SPDLOG_NOEXCEPT { - return file_exists(filename) ? remove(filename) : 0; + return path_exists(filename) ? remove(filename) : 0; } SPDLOG_INLINE int rename(const filename_t &filename1, const filename_t &filename2) SPDLOG_NOEXCEPT @@ -190,8 +190,8 @@ SPDLOG_INLINE int rename(const filename_t &filename1, const filename_t &filename #endif } -// Return true if file exists -SPDLOG_INLINE bool file_exists(const filename_t &filename) SPDLOG_NOEXCEPT +// Return true if path exists (file or directory) +SPDLOG_INLINE bool path_exists(const filename_t &filename) SPDLOG_NOEXCEPT { #ifdef _WIN32 #ifdef SPDLOG_WCHAR_FILENAMES @@ -481,7 +481,7 @@ SPDLOG_INLINE bool mkdir_(const filename_t &path) // return true on success SPDLOG_INLINE bool create_dir(filename_t path) { - if (file_exists(path)) + if (path_exists(path)) { return true; } @@ -503,7 +503,7 @@ SPDLOG_INLINE bool create_dir(filename_t path) auto subdir = path.substr(0, token_pos); - if (!subdir.empty() && !file_exists(subdir) && !mkdir_(subdir)) + if (!subdir.empty() && !path_exists(subdir) && !mkdir_(subdir)) { return false; // return error if failed creating dir } diff --git a/include/spdlog/details/os.h b/include/spdlog/details/os.h index 2ab828ba..e275a40f 100644 --- a/include/spdlog/details/os.h +++ b/include/spdlog/details/os.h @@ -53,7 +53,7 @@ int remove_if_exists(const filename_t &filename) SPDLOG_NOEXCEPT; int rename(const filename_t &filename1, const filename_t &filename2) SPDLOG_NOEXCEPT; // Return if file exists. -bool file_exists(const filename_t &filename) SPDLOG_NOEXCEPT; +bool path_exists(const filename_t &filename) SPDLOG_NOEXCEPT; // Return file size according to open FILE* object size_t filesize(FILE *f); diff --git a/include/spdlog/sinks/rotating_file_sink-inl.h b/include/spdlog/sinks/rotating_file_sink-inl.h index 15443c5a..82347001 100644 --- a/include/spdlog/sinks/rotating_file_sink-inl.h +++ b/include/spdlog/sinks/rotating_file_sink-inl.h @@ -88,11 +88,12 @@ template SPDLOG_INLINE void rotating_file_sink::rotate_() { using details::os::filename_to_str; + using details::os::path_exists; file_helper_.close(); for (auto i = max_files_; i > 0; --i) { filename_t src = calc_filename(base_filename_, i - 1); - if (!details::file_helper::file_exists(src)) + if (!path_exists(src)) { continue; } diff --git a/tests/test_create_dir.cpp b/tests/test_create_dir.cpp index 3e428d74..a41c63a5 100644 --- a/tests/test_create_dir.cpp +++ b/tests/test_create_dir.cpp @@ -4,13 +4,13 @@ #include "includes.h" using spdlog::details::os::create_dir; -using spdlog::details::os::file_exists; +using spdlog::details::os::path_exists; bool try_create_dir(const char *path, const char *normalized_path) { auto rv = create_dir(path); REQUIRE(rv == true); - return file_exists(normalized_path); + return path_exists(normalized_path); } TEST_CASE("create_dir", "[create_dir]") diff --git a/tests/test_file_helper.cpp b/tests/test_file_helper.cpp index 85f62ae2..0aaa4ce6 100644 --- a/tests/test_file_helper.cpp +++ b/tests/test_file_helper.cpp @@ -38,14 +38,6 @@ TEST_CASE("file_helper_size", "[file_helper::size()]]") REQUIRE(get_filesize(target_filename) == expected_size); } -TEST_CASE("file_helper_exists", "[file_helper::file_exists()]]") -{ - prepare_logdir(); - REQUIRE(!file_helper::file_exists(target_filename)); - file_helper helper; - helper.open(target_filename); - REQUIRE(file_helper::file_exists(target_filename)); -} TEST_CASE("file_helper_reopen", "[file_helper::reopen()]]") { From a4602021d86a394df6b0e790e3ce0cbc0b34258c Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 25 Oct 2019 16:04:07 +0300 Subject: [PATCH 15/24] Renamed private members of file_helper --- include/spdlog/details/file_helper-inl.h | 18 +++++++++--------- include/spdlog/details/file_helper.h | 6 +++--- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/include/spdlog/details/file_helper-inl.h b/include/spdlog/details/file_helper-inl.h index 98ef82fa..e7161132 100644 --- a/include/spdlog/details/file_helper-inl.h +++ b/include/spdlog/details/file_helper-inl.h @@ -30,9 +30,9 @@ SPDLOG_INLINE void file_helper::open(const filename_t &fname, bool truncate) close(); auto *mode = truncate ? SPDLOG_FILENAME_T("wb") : SPDLOG_FILENAME_T("ab"); auto folder_name = os::dir_name(fname); - _filename = fname; + filename_ = fname; - for (int tries = 0; tries < open_tries; ++tries) + for (int tries = 0; tries < open_tries_; ++tries) { if (!folder_name.empty()) { @@ -44,19 +44,19 @@ SPDLOG_INLINE void file_helper::open(const filename_t &fname, bool truncate) return; } - details::os::sleep_for_millis(open_interval); + details::os::sleep_for_millis(open_interval_); } - SPDLOG_THROW(spdlog_ex("Failed opening file " + os::filename_to_str(_filename) + " for writing", errno)); + SPDLOG_THROW(spdlog_ex("Failed opening file " + os::filename_to_str(filename_) + " for writing", errno)); } SPDLOG_INLINE void file_helper::reopen(bool truncate) { - if (_filename.empty()) + if (filename_.empty()) { SPDLOG_THROW(spdlog_ex("Failed re opening file - was not opened before")); } - open(_filename, truncate); + this->open(filename_, truncate); } SPDLOG_INLINE void file_helper::flush() @@ -79,7 +79,7 @@ SPDLOG_INLINE void file_helper::write(const memory_buf_t &buf) auto data = buf.data(); if (std::fwrite(data, 1, msg_size, fd_) != msg_size) { - SPDLOG_THROW(spdlog_ex("Failed writing to file " + os::filename_to_str(_filename), errno)); + SPDLOG_THROW(spdlog_ex("Failed writing to file " + os::filename_to_str(filename_), errno)); } } @@ -87,14 +87,14 @@ SPDLOG_INLINE size_t file_helper::size() const { if (fd_ == nullptr) { - SPDLOG_THROW(spdlog_ex("Cannot use size() on closed file " + os::filename_to_str(_filename))); + SPDLOG_THROW(spdlog_ex("Cannot use size() on closed file " + os::filename_to_str(filename_))); } return os::filesize(fd_); } SPDLOG_INLINE const filename_t &file_helper::filename() const { - return _filename; + return filename_; } // diff --git a/include/spdlog/details/file_helper.h b/include/spdlog/details/file_helper.h index 89d6b244..5ac4c27b 100644 --- a/include/spdlog/details/file_helper.h +++ b/include/spdlog/details/file_helper.h @@ -46,10 +46,10 @@ public: static std::tuple split_by_extension(const filename_t &fname); private: - const int open_tries = 5; - const int open_interval = 10; + const int open_tries_ = 5; + const int open_interval_ = 10; std::FILE *fd_{nullptr}; - filename_t _filename; + filename_t filename_; }; } // namespace details } // namespace spdlog From 88335bd92ecb8c9714be738e88032a7210f01055 Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 25 Oct 2019 16:05:12 +0300 Subject: [PATCH 16/24] clang-format --- tests/test_file_helper.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_file_helper.cpp b/tests/test_file_helper.cpp index 0aaa4ce6..3a3545fe 100644 --- a/tests/test_file_helper.cpp +++ b/tests/test_file_helper.cpp @@ -38,7 +38,6 @@ TEST_CASE("file_helper_size", "[file_helper::size()]]") REQUIRE(get_filesize(target_filename) == expected_size); } - TEST_CASE("file_helper_reopen", "[file_helper::reopen()]]") { prepare_logdir(); From bd92c23addf3b15390f7703f84d8259fc4698b7d Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 25 Oct 2019 16:07:10 +0300 Subject: [PATCH 17/24] comment --- include/spdlog/details/file_helper-inl.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/spdlog/details/file_helper-inl.h b/include/spdlog/details/file_helper-inl.h index e7161132..eabc9aaa 100644 --- a/include/spdlog/details/file_helper-inl.h +++ b/include/spdlog/details/file_helper-inl.h @@ -36,7 +36,7 @@ SPDLOG_INLINE void file_helper::open(const filename_t &fname, bool truncate) { if (!folder_name.empty()) { - os::create_dir(folder_name); + os::create_dir(folder_name); // will not created if already exists } if (!os::fopen_s(&fd_, fname, mode)) From c19e325b83fcd42e40bd99b9325d7c60bf482c90 Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 25 Oct 2019 16:17:02 +0300 Subject: [PATCH 18/24] Added some tests for create_dir --- include/spdlog/details/file_helper-inl.h | 6 ++---- include/spdlog/details/os-inl.h | 7 ++++++- tests/test_create_dir.cpp | 8 ++++++++ 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/include/spdlog/details/file_helper-inl.h b/include/spdlog/details/file_helper-inl.h index eabc9aaa..be670347 100644 --- a/include/spdlog/details/file_helper-inl.h +++ b/include/spdlog/details/file_helper-inl.h @@ -34,10 +34,8 @@ SPDLOG_INLINE void file_helper::open(const filename_t &fname, bool truncate) for (int tries = 0; tries < open_tries_; ++tries) { - if (!folder_name.empty()) - { - os::create_dir(folder_name); // will not created if already exists - } + // will not created if already exists or empty. + os::create_dir(folder_name); if (!os::fopen_s(&fd_, fname, mode)) { diff --git a/include/spdlog/details/os-inl.h b/include/spdlog/details/os-inl.h index b5669ac7..95a67917 100644 --- a/include/spdlog/details/os-inl.h +++ b/include/spdlog/details/os-inl.h @@ -478,7 +478,7 @@ SPDLOG_INLINE bool mkdir_(const filename_t &path) } // create the given directory - and all directories leading to it -// return true on success +// return true on success or if the directory already exists SPDLOG_INLINE bool create_dir(filename_t path) { if (path_exists(path)) @@ -486,6 +486,11 @@ SPDLOG_INLINE bool create_dir(filename_t path) return true; } + if(path.empty()) + { + return false; + } + #ifdef _WIN32 // support forward slash in windows std::replace(path.begin(), path.end(), '/', folder_sep); diff --git a/tests/test_create_dir.cpp b/tests/test_create_dir.cpp index a41c63a5..41f91ef3 100644 --- a/tests/test_create_dir.cpp +++ b/tests/test_create_dir.cpp @@ -32,6 +32,14 @@ TEST_CASE("create_dir", "[create_dir]") #endif } +TEST_CASE("create_invalid_dir", "[create_dir]") +{ + REQUIRE(create_dir("") == false); +#ifdef __linux__ + REQUIRE(create_dir("/proc/spdlog-utest") == false); +#endif +} + TEST_CASE("dir_name", "[create_dir]") { using spdlog::details::os::dir_name; From aac7dccf458625312a44b202905f4154c47a884d Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 25 Oct 2019 16:17:55 +0300 Subject: [PATCH 19/24] comment --- include/spdlog/details/file_helper-inl.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/spdlog/details/file_helper-inl.h b/include/spdlog/details/file_helper-inl.h index be670347..1935ddf2 100644 --- a/include/spdlog/details/file_helper-inl.h +++ b/include/spdlog/details/file_helper-inl.h @@ -34,7 +34,7 @@ SPDLOG_INLINE void file_helper::open(const filename_t &fname, bool truncate) for (int tries = 0; tries < open_tries_; ++tries) { - // will not created if already exists or empty. + // create if not empty and not exists already os::create_dir(folder_name); if (!os::fopen_s(&fd_, fname, mode)) From 594d226056edb6d099cf65c14e477038226542fe Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 25 Oct 2019 16:20:24 +0300 Subject: [PATCH 20/24] update tests --- tests/test_create_dir.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_create_dir.cpp b/tests/test_create_dir.cpp index 41f91ef3..b538e80e 100644 --- a/tests/test_create_dir.cpp +++ b/tests/test_create_dir.cpp @@ -35,6 +35,7 @@ TEST_CASE("create_dir", "[create_dir]") TEST_CASE("create_invalid_dir", "[create_dir]") { REQUIRE(create_dir("") == false); + REQUIRE(create_dir(spdlog::filename_t{}) == false); #ifdef __linux__ REQUIRE(create_dir("/proc/spdlog-utest") == false); #endif From 49eb9cbdd8ed5869f2210159d06252519bc6dc0b Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 25 Oct 2019 16:26:21 +0300 Subject: [PATCH 21/24] Removed junk folder --- build-branch/example/Debug/logs/mylog.txt | 2 -- build-branch/example/Debug/mylog.txt | 1 - build-branch/example/mylog.txt | 2 -- build-branch/tests/Debug/filename_2019-10-25 | 0 4 files changed, 5 deletions(-) delete mode 100644 build-branch/example/Debug/logs/mylog.txt delete mode 100644 build-branch/example/Debug/mylog.txt delete mode 100644 build-branch/example/mylog.txt delete mode 100644 build-branch/tests/Debug/filename_2019-10-25 diff --git a/build-branch/example/Debug/logs/mylog.txt b/build-branch/example/Debug/logs/mylog.txt deleted file mode 100644 index 040f5132..00000000 --- a/build-branch/example/Debug/logs/mylog.txt +++ /dev/null @@ -1,2 +0,0 @@ -[2019-10-25 15:16:33.502] [LOGGER] [info] Hello -[2019-10-25 15:16:41.712] [LOGGER] [info] Hello diff --git a/build-branch/example/Debug/mylog.txt b/build-branch/example/Debug/mylog.txt deleted file mode 100644 index f4e0ab68..00000000 --- a/build-branch/example/Debug/mylog.txt +++ /dev/null @@ -1 +0,0 @@ -[2019-10-25 15:16:58.845] [LOGGER] [info] Hello diff --git a/build-branch/example/mylog.txt b/build-branch/example/mylog.txt deleted file mode 100644 index a7126955..00000000 --- a/build-branch/example/mylog.txt +++ /dev/null @@ -1,2 +0,0 @@ -[2019-10-25 15:16:53.945] [LOGGER] [info] Hello -[2019-10-25 15:18:50.127] [LOGGER] [info] Hello diff --git a/build-branch/tests/Debug/filename_2019-10-25 b/build-branch/tests/Debug/filename_2019-10-25 deleted file mode 100644 index e69de29b..00000000 From 0c60107e62222f0052a408269fda0a6744826b38 Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 25 Oct 2019 16:50:06 +0300 Subject: [PATCH 22/24] refactoed file_helper --- include/spdlog/details/file_helper-inl.h | 9 ++++----- include/spdlog/details/os-inl.h | 1 + 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/include/spdlog/details/file_helper-inl.h b/include/spdlog/details/file_helper-inl.h index 1935ddf2..cc21075c 100644 --- a/include/spdlog/details/file_helper-inl.h +++ b/include/spdlog/details/file_helper-inl.h @@ -28,15 +28,14 @@ SPDLOG_INLINE file_helper::~file_helper() SPDLOG_INLINE void file_helper::open(const filename_t &fname, bool truncate) { close(); - auto *mode = truncate ? SPDLOG_FILENAME_T("wb") : SPDLOG_FILENAME_T("ab"); - auto folder_name = os::dir_name(fname); filename_ = fname; + // create containing folder if not empty string and not exists already + os::create_dir(os::dir_name(fname)); + + auto *mode = truncate ? SPDLOG_FILENAME_T("wb") : SPDLOG_FILENAME_T("ab"); for (int tries = 0; tries < open_tries_; ++tries) { - // create if not empty and not exists already - os::create_dir(folder_name); - if (!os::fopen_s(&fd_, fname, mode)) { return; diff --git a/include/spdlog/details/os-inl.h b/include/spdlog/details/os-inl.h index 95a67917..35a8934a 100644 --- a/include/spdlog/details/os-inl.h +++ b/include/spdlog/details/os-inl.h @@ -499,6 +499,7 @@ SPDLOG_INLINE bool create_dir(filename_t path) size_t search_offset = 0; do { + fmt::string_view t; auto token_pos = path.find(folder_sep, search_offset); // treat the entire path as a folder if no folder separator not found if (token_pos == filename_t::npos) From 9f96545fa7ee3370047eac5e00937ea894944323 Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 25 Oct 2019 16:52:12 +0300 Subject: [PATCH 23/24] refactoed file_helper --- include/spdlog/details/file_helper-inl.h | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/include/spdlog/details/file_helper-inl.h b/include/spdlog/details/file_helper-inl.h index cc21075c..3c1d8056 100644 --- a/include/spdlog/details/file_helper-inl.h +++ b/include/spdlog/details/file_helper-inl.h @@ -29,13 +29,12 @@ SPDLOG_INLINE void file_helper::open(const filename_t &fname, bool truncate) { close(); filename_ = fname; - - // create containing folder if not empty string and not exists already - os::create_dir(os::dir_name(fname)); - auto *mode = truncate ? SPDLOG_FILENAME_T("wb") : SPDLOG_FILENAME_T("ab"); + for (int tries = 0; tries < open_tries_; ++tries) { + // create containing folder if not exists already. + os::create_dir(os::dir_name(fname)); if (!os::fopen_s(&fd_, fname, mode)) { return; From 05105155f8839a02879932aebcff2c7a48d9c346 Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 25 Oct 2019 16:55:24 +0300 Subject: [PATCH 24/24] refactoed file_helper --- include/spdlog/details/file_helper-inl.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/spdlog/details/file_helper-inl.h b/include/spdlog/details/file_helper-inl.h index 3c1d8056..669999b4 100644 --- a/include/spdlog/details/file_helper-inl.h +++ b/include/spdlog/details/file_helper-inl.h @@ -30,7 +30,7 @@ SPDLOG_INLINE void file_helper::open(const filename_t &fname, bool truncate) close(); filename_ = fname; auto *mode = truncate ? SPDLOG_FILENAME_T("wb") : SPDLOG_FILENAME_T("ab"); - + for (int tries = 0; tries < open_tries_; ++tries) { // create containing folder if not exists already.