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:
		@@ -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;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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]]")
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -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" />
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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">
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user