diff --git a/include/spdlog/details/file_helper.h b/include/spdlog/details/file_helper.h index a694e27a..0a544ca1 100644 --- a/include/spdlog/details/file_helper.h +++ b/include/spdlog/details/file_helper.h @@ -14,7 +14,7 @@ #include #include #include "os.h" - +#include "log_msg.h" @@ -125,16 +125,8 @@ public: static bool file_exists(const std::string& name) { - FILE* file; - if (!os::fopen_s(&file, name.c_str(), "r")) - { - fclose(file); - return true; - } - else - { - return false; - } + + return os::file_exists(name); } diff --git a/include/spdlog/details/os.h b/include/spdlog/details/os.h index 91512013..94f17af8 100644 --- a/include/spdlog/details/os.h +++ b/include/spdlog/details/os.h @@ -2,8 +2,8 @@ // Copyright(c) 2015 Gabi Melman. // Distributed under the MIT License (http://opensource.org/licenses/MIT) // - #pragma once + #include #include #include @@ -20,6 +20,7 @@ #elif __linux__ #include //Use gettid() syscall under linux to get thread id +#include #include #else #include @@ -138,6 +139,19 @@ inline int fopen_s(FILE** fp, const std::string& filename, const char* mode) return *fp == nullptr; #endif +} + + +//Return if file exists +inline bool file_exists(const std::string& filename) +{ +#ifdef _WIN32 + auto attribs = GetFileAttributesA(filename.c_str()); + return (attribs != INVALID_FILE_ATTRIBUTES && !(attribs & FILE_ATTRIBUTE_DIRECTORY)); +#else + struct stat buffer; + return (stat (name.c_str(), &buffer) == 0); +#endif } diff --git a/tests/file_log.cpp b/tests/file_log.cpp index 52903944..bbca8e4d 100644 --- a/tests/file_log.cpp +++ b/tests/file_log.cpp @@ -35,16 +35,6 @@ std::ifstream::pos_type filesize(const std::string& filename) return ifs.tellg(); } -static void prepare_logdir() -{ - spdlog::drop_all(); -#ifdef _WIN32 - auto rv = system("del /F /Q logs\\*"); -#else - auto rv = system("rm -f logs/*"); -#endif -} - TEST_CASE("simple_file_logger", "[simple_logger]]") diff --git a/tests/includes.h b/tests/includes.h index 7cda161d..d19e2100 100644 --- a/tests/includes.h +++ b/tests/includes.h @@ -10,3 +10,14 @@ #include "catch.hpp" #include "../include/spdlog/spdlog.h" #include "../include/spdlog/sinks/null_sink.h" + + +static void prepare_logdir() +{ + spdlog::drop_all(); +#ifdef _WIN32 + auto rv = system("del /F /Q logs\\*"); +#else + auto rv = system("rm -f logs/*"); +#endif +} \ No newline at end of file diff --git a/tests/tests.vcxproj b/tests/tests.vcxproj index 58cfd522..7272c212 100644 --- a/tests/tests.vcxproj +++ b/tests/tests.vcxproj @@ -82,6 +82,7 @@ Level3 Disabled true + _MBCS;%(PreprocessorDefinitions) true @@ -110,6 +111,7 @@ true true true + _MBCS;%(PreprocessorDefinitions) true @@ -119,6 +121,7 @@ + diff --git a/tests/tests.vcxproj.filters b/tests/tests.vcxproj.filters index 36fe0a87..bdadb0bc 100644 --- a/tests/tests.vcxproj.filters +++ b/tests/tests.vcxproj.filters @@ -27,6 +27,9 @@ Source Files + + Source Files +