1
0
mirror of https://github.com/gabime/spdlog.git synced 2025-04-30 04:23:51 +00:00

1. Fixed file_helper::exists() bug under windows which returned false in some circumstances

2. Improved file_helper::exists() performance under linux to use stat sys call
3. Added unit tests
This commit is contained in:
gabime 2015-12-12 20:53:00 +02:00
parent 87da9aab26
commit a9fb96e088
6 changed files with 35 additions and 22 deletions

View File

@ -14,7 +14,7 @@
#include <thread> #include <thread>
#include <chrono> #include <chrono>
#include "os.h" #include "os.h"
#include "log_msg.h"
@ -125,16 +125,8 @@ public:
static bool file_exists(const std::string& name) static bool file_exists(const std::string& name)
{ {
FILE* file;
if (!os::fopen_s(&file, name.c_str(), "r")) return os::file_exists(name);
{
fclose(file);
return true;
}
else
{
return false;
}
} }

View File

@ -2,8 +2,8 @@
// Copyright(c) 2015 Gabi Melman. // Copyright(c) 2015 Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT) // Distributed under the MIT License (http://opensource.org/licenses/MIT)
// //
#pragma once #pragma once
#include<string> #include<string>
#include<cstdio> #include<cstdio>
#include<ctime> #include<ctime>
@ -20,6 +20,7 @@
#elif __linux__ #elif __linux__
#include <sys/syscall.h> //Use gettid() syscall under linux to get thread id #include <sys/syscall.h> //Use gettid() syscall under linux to get thread id
#include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#else #else
#include <thread> #include <thread>
@ -138,6 +139,19 @@ inline int fopen_s(FILE** fp, const std::string& filename, const char* mode)
return *fp == nullptr; return *fp == nullptr;
#endif #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
} }

View File

@ -35,16 +35,6 @@ std::ifstream::pos_type filesize(const std::string& filename)
return ifs.tellg(); 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]]") TEST_CASE("simple_file_logger", "[simple_logger]]")

View File

@ -10,3 +10,14 @@
#include "catch.hpp" #include "catch.hpp"
#include "../include/spdlog/spdlog.h" #include "../include/spdlog/spdlog.h"
#include "../include/spdlog/sinks/null_sink.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
}

View File

@ -82,6 +82,7 @@
<WarningLevel>Level3</WarningLevel> <WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization> <Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck> <SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_MBCS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile> </ClCompile>
<Link> <Link>
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
@ -110,6 +111,7 @@
<FunctionLevelLinking>true</FunctionLevelLinking> <FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions> <IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck> <SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_MBCS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile> </ClCompile>
<Link> <Link>
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
@ -119,6 +121,7 @@
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="file_helper.cpp" />
<ClCompile Include="file_log.cpp" /> <ClCompile Include="file_log.cpp" />
<ClCompile Include="format.cpp" /> <ClCompile Include="format.cpp" />
<ClCompile Include="main.cpp" /> <ClCompile Include="main.cpp" />

View File

@ -27,6 +27,9 @@
<ClCompile Include="registry.cpp"> <ClCompile Include="registry.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="file_helper.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="includes.h"> <ClInclude Include="includes.h">