fixed bug in SPDLOG_TRACE_IF macro and added some related tests
This commit is contained in:
		
							parent
							
								
									4a159ad66d
								
							
						
					
					
						commit
						21ed31844c
					
				| @ -170,10 +170,10 @@ void drop_all(); | |||||||
| #define SPDLOG_STR_HELPER(x) SPDLOG_STR_H(x) | #define SPDLOG_STR_HELPER(x) SPDLOG_STR_H(x) | ||||||
| #ifdef _MSC_VER | #ifdef _MSC_VER | ||||||
|   #define SPDLOG_TRACE(logger, ...) logger->trace("[ " __FILE__ "(" SPDLOG_STR_HELPER(__LINE__) ") ] " __VA_ARGS__) |   #define SPDLOG_TRACE(logger, ...) logger->trace("[ " __FILE__ "(" SPDLOG_STR_HELPER(__LINE__) ") ] " __VA_ARGS__) | ||||||
|   #define SPDLOG_TRACE_IF(logger, flag, ...) logger.trace_if(flag, "[ " __FILE__ "(" SPDLOG_STR_HELPER(__LINE__) ") ] " __VA_ARGS__) |   #define SPDLOG_TRACE_IF(logger, flag, ...) logger->trace_if(flag, "[ " __FILE__ "(" SPDLOG_STR_HELPER(__LINE__) ") ] " __VA_ARGS__) | ||||||
| #else | #else | ||||||
|   #define SPDLOG_TRACE(logger, ...) logger->trace("[ " __FILE__ ":" SPDLOG_STR_HELPER(__LINE__) " ] " __VA_ARGS__) |   #define SPDLOG_TRACE(logger, ...) logger->trace("[ " __FILE__ ":" SPDLOG_STR_HELPER(__LINE__) " ] " __VA_ARGS__) | ||||||
|   #define SPDLOG_TRACE_IF(logger, flag, ...) logger.trace_if(flag, "[ " __FILE__ ":" SPDLOG_STR_HELPER(__LINE__) " ] " __VA_ARGS__) |   #define SPDLOG_TRACE_IF(logger, flag, ...) logger->trace_if(flag, "[ " __FILE__ ":" SPDLOG_STR_HELPER(__LINE__) " ] " __VA_ARGS__) | ||||||
| #endif | #endif | ||||||
| #else | #else | ||||||
| #define SPDLOG_TRACE(logger, ...) | #define SPDLOG_TRACE(logger, ...) | ||||||
|  | |||||||
| @ -10,6 +10,9 @@ | |||||||
| #include "catch.hpp" | #include "catch.hpp" | ||||||
| #include "utils.h" | #include "utils.h" | ||||||
| 
 | 
 | ||||||
|  | #define SPDLOG_TRACE_ON | ||||||
|  | #define SPDLOG_DEBUG_ON | ||||||
|  | 
 | ||||||
| #include "../include/spdlog/spdlog.h" | #include "../include/spdlog/spdlog.h" | ||||||
| #include "../include/spdlog/sinks/null_sink.h" | #include "../include/spdlog/sinks/null_sink.h" | ||||||
| #include "../include/spdlog/sinks/ostream_sink.h" | #include "../include/spdlog/sinks/ostream_sink.h" | ||||||
|  | |||||||
							
								
								
									
										43
									
								
								tests/test_macros.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								tests/test_macros.cpp
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,43 @@ | |||||||
|  | /*
 | ||||||
|  | * This content is released under the MIT License as specified in https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE
 | ||||||
|  | */ | ||||||
|  | 
 | ||||||
|  | #include "includes.h" | ||||||
|  | 
 | ||||||
|  | TEST_CASE("debug and trace w/o format string", "[macros]]") | ||||||
|  | { | ||||||
|  | 	prepare_logdir(); | ||||||
|  | 	std::string filename = "logs/simple_log"; | ||||||
|  | 
 | ||||||
|  | 	auto logger = spdlog::create<spdlog::sinks::simple_file_sink_mt>("logger", filename); | ||||||
|  | 	logger->set_pattern("%v"); | ||||||
|  | 	logger->set_level(spdlog::level::trace);		 | ||||||
|  | 	 | ||||||
|  | 	SPDLOG_TRACE(logger, "Test message 1"); | ||||||
|  | 	//SPDLOG_DEBUG(logger, "Test message 2");	
 | ||||||
|  | 	SPDLOG_DEBUG(logger, "Test message 2"); | ||||||
|  | 	logger->flush(); | ||||||
|  | 	 | ||||||
|  | 	REQUIRE(ends_with(file_contents(filename), "Test message 2\n")); | ||||||
|  | 	REQUIRE(count_lines(filename) == 2); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | TEST_CASE("debug and trace with format strings", "[macros]]") | ||||||
|  | { | ||||||
|  | 	prepare_logdir(); | ||||||
|  | 	std::string filename = "logs/simple_log"; | ||||||
|  | 
 | ||||||
|  | 	auto logger = spdlog::create<spdlog::sinks::simple_file_sink_mt>("logger", filename); | ||||||
|  | 	logger->set_pattern("%v"); | ||||||
|  | 	logger->set_level(spdlog::level::trace); | ||||||
|  | 
 | ||||||
|  | 	SPDLOG_TRACE(logger, "Test message 1"); | ||||||
|  | 	//SPDLOG_DEBUG(logger, "Test message 2");	
 | ||||||
|  | 	SPDLOG_DEBUG(logger, "Test message {}", 222); | ||||||
|  | 	logger->flush(); | ||||||
|  | 
 | ||||||
|  | 	REQUIRE(ends_with(file_contents(filename), "Test message 222\n")); | ||||||
|  | 	REQUIRE(count_lines(filename) == 2); | ||||||
|  | } | ||||||
|  | 
 | ||||||
| @ -132,6 +132,7 @@ | |||||||
|     <ClCompile Include="format.cpp" /> |     <ClCompile Include="format.cpp" /> | ||||||
|     <ClCompile Include="main.cpp" /> |     <ClCompile Include="main.cpp" /> | ||||||
|     <ClCompile Include="registry.cpp" /> |     <ClCompile Include="registry.cpp" /> | ||||||
|  |     <ClCompile Include="test_macros.cpp" /> | ||||||
|     <ClCompile Include="utils.cpp" /> |     <ClCompile Include="utils.cpp" /> | ||||||
|   </ItemGroup> |   </ItemGroup> | ||||||
|   <ItemGroup> |   <ItemGroup> | ||||||
|  | |||||||
| @ -39,6 +39,9 @@ | |||||||
|     <ClCompile Include="cond_logging.cpp"> |     <ClCompile Include="cond_logging.cpp"> | ||||||
|       <Filter>Source Files</Filter> |       <Filter>Source Files</Filter> | ||||||
|     </ClCompile> |     </ClCompile> | ||||||
|  |     <ClCompile Include="test_macros.cpp"> | ||||||
|  |       <Filter>Source Files</Filter> | ||||||
|  |     </ClCompile> | ||||||
|   </ItemGroup> |   </ItemGroup> | ||||||
|   <ItemGroup> |   <ItemGroup> | ||||||
|     <ClInclude Include="includes.h"> |     <ClInclude Include="includes.h"> | ||||||
|  | |||||||
| @ -46,3 +46,11 @@ std::size_t get_filesize(const std::string& filename) | |||||||
| 
 | 
 | ||||||
|     return static_cast<std::size_t>(ifs.tellg()); |     return static_cast<std::size_t>(ifs.tellg()); | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | // source: https://stackoverflow.com/a/2072890/192001
 | ||||||
|  | bool ends_with(std::string const & value, std::string const & ending) | ||||||
|  | { | ||||||
|  | 	if (ending.size() > value.size()) return false; | ||||||
|  | 	return std::equal(ending.rbegin(), ending.rend(), value.rbegin()); | ||||||
|  | } | ||||||
|  | |||||||
| @ -13,3 +13,4 @@ std::size_t count_lines(const std::string& filename); | |||||||
| 
 | 
 | ||||||
| std::size_t get_filesize(const std::string& filename); | std::size_t get_filesize(const std::string& filename); | ||||||
| 
 | 
 | ||||||
|  | bool ends_with(std::string const & value, std::string const & ending); | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user