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:
		
							parent
							
								
									87da9aab26
								
							
						
					
					
						commit
						a9fb96e088
					
				| @ -14,7 +14,7 @@ | ||||
| #include <thread> | ||||
| #include <chrono> | ||||
| #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); | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
|  | ||||
| @ -2,8 +2,8 @@ | ||||
| // Copyright(c) 2015 Gabi Melman.
 | ||||
| // Distributed under the MIT License (http://opensource.org/licenses/MIT)
 | ||||
| //
 | ||||
| 
 | ||||
| #pragma once | ||||
| 
 | ||||
| #include<string> | ||||
| #include<cstdio> | ||||
| #include<ctime> | ||||
| @ -20,6 +20,7 @@ | ||||
| 
 | ||||
| #elif __linux__ | ||||
| #include <sys/syscall.h> //Use gettid() syscall under linux to get thread id | ||||
| #include <sys/stat.h> | ||||
| #include <unistd.h> | ||||
| #else | ||||
| #include <thread> | ||||
| @ -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 | ||||
| 
 | ||||
| } | ||||
| 
 | ||||
|  | ||||
| @ -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]]") | ||||
|  | ||||
| @ -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 | ||||
| } | ||||
| @ -82,6 +82,7 @@ | ||||
|       <WarningLevel>Level3</WarningLevel> | ||||
|       <Optimization>Disabled</Optimization> | ||||
|       <SDLCheck>true</SDLCheck> | ||||
|       <PreprocessorDefinitions>_MBCS;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||||
|     </ClCompile> | ||||
|     <Link> | ||||
|       <GenerateDebugInformation>true</GenerateDebugInformation> | ||||
| @ -110,6 +111,7 @@ | ||||
|       <FunctionLevelLinking>true</FunctionLevelLinking> | ||||
|       <IntrinsicFunctions>true</IntrinsicFunctions> | ||||
|       <SDLCheck>true</SDLCheck> | ||||
|       <PreprocessorDefinitions>_MBCS;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||||
|     </ClCompile> | ||||
|     <Link> | ||||
|       <GenerateDebugInformation>true</GenerateDebugInformation> | ||||
| @ -119,6 +121,7 @@ | ||||
|     </Link> | ||||
|   </ItemDefinitionGroup> | ||||
|   <ItemGroup> | ||||
|     <ClCompile Include="file_helper.cpp" /> | ||||
|     <ClCompile Include="file_log.cpp" /> | ||||
|     <ClCompile Include="format.cpp" /> | ||||
|     <ClCompile Include="main.cpp" /> | ||||
|  | ||||
| @ -27,6 +27,9 @@ | ||||
|     <ClCompile Include="registry.cpp"> | ||||
|       <Filter>Source Files</Filter> | ||||
|     </ClCompile> | ||||
|     <ClCompile Include="file_helper.cpp"> | ||||
|       <Filter>Source Files</Filter> | ||||
|     </ClCompile> | ||||
|   </ItemGroup> | ||||
|   <ItemGroup> | ||||
|     <ClInclude Include="includes.h"> | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user