From 757231cc6e777b8f4717d1467ef7efa01c7fde15 Mon Sep 17 00:00:00 2001 From: peterbell10 Date: Wed, 3 Jan 2018 17:41:16 +0000 Subject: Add the fmt library (#4065) * Replaces AppendVPrintf with fmt::sprintf * fmt::ArgList now used as a type safe alternative to varargs. * Removed SIZE_T_FMT compatibility macros. fmt::sprintf is fully portable and supports %zu. * Adds FLOG functions to log with fmt's native formatting style. --- Tools/GrownBiomeGenVisualiser/CMakeLists.txt | 3 +++ Tools/GrownBiomeGenVisualiser/Globals.h | 31 +--------------------- .../GrownBiomeGenVisualiser.cpp | 14 +++++----- Tools/MCADefrag/CMakeLists.txt | 3 ++- Tools/MCADefrag/Globals.h | 16 +---------- Tools/MCADefrag/MCADefrag.cpp | 1 + Tools/NoiseSpeedTest/CMakeLists.txt | 3 +++ Tools/NoiseSpeedTest/Globals.h | 16 +---------- Tools/ProtoProxy/CMakeLists.txt | 3 ++- Tools/ProtoProxy/Connection.cpp | 28 +++++++++---------- Tools/ProtoProxy/Connection.h | 8 ++++-- Tools/ProtoProxy/Globals.h | 9 ++----- 12 files changed, 40 insertions(+), 95 deletions(-) (limited to 'Tools') diff --git a/Tools/GrownBiomeGenVisualiser/CMakeLists.txt b/Tools/GrownBiomeGenVisualiser/CMakeLists.txt index 0a8b7930e..4626ecf73 100644 --- a/Tools/GrownBiomeGenVisualiser/CMakeLists.txt +++ b/Tools/GrownBiomeGenVisualiser/CMakeLists.txt @@ -1,3 +1,4 @@ +cmake_minimum_required(VERSION 3.0.2) project (GrownBiomeGenVisualiser) # Without this, the MSVC variable isn't defined for MSVC builds ( https://www.cmake.org/pipermail/cmake/2011-November/047130.html ) @@ -89,4 +90,6 @@ add_executable(GrownBiomeGenVisualiser ${SHARED_OSS_HDR} ) +target_link_libraries(GrownBiomeGenVisualiser fmt::fmt) + set_target_properties(GrownBiomeGenVisualiser PROPERTIES FOLDER Tools) diff --git a/Tools/GrownBiomeGenVisualiser/Globals.h b/Tools/GrownBiomeGenVisualiser/Globals.h index bc2b6834f..11adc2f53 100644 --- a/Tools/GrownBiomeGenVisualiser/Globals.h +++ b/Tools/GrownBiomeGenVisualiser/Globals.h @@ -22,13 +22,6 @@ #define ALIGN_8 #define ALIGN_16 - #define FORMATSTRING(formatIndex, va_argsIndex) - - // MSVC has its own custom version of zu format - #define SIZE_T_FMT "%Iu" - #define SIZE_T_FMT_PRECISION(x) "%" #x "Iu" - #define SIZE_T_FMT_HEX "%Ix" - #define NORETURN __declspec(noreturn) #elif defined(__GNUC__) @@ -49,27 +42,6 @@ // Some portability macros :) #define stricmp strcasecmp - #define FORMATSTRING(formatIndex, va_argsIndex) __attribute__((format (printf, formatIndex, va_argsIndex))) - - #if defined(_WIN32) - // We're compiling on MinGW, which uses an old MSVCRT library that has no support for size_t printfing. - // We need direct size formats: - #if defined(_WIN64) - #define SIZE_T_FMT "%I64u" - #define SIZE_T_FMT_PRECISION(x) "%" #x "I64u" - #define SIZE_T_FMT_HEX "%I64x" - #else - #define SIZE_T_FMT "%u" - #define SIZE_T_FMT_PRECISION(x) "%" #x "u" - #define SIZE_T_FMT_HEX "%x" - #endif - #else - // We're compiling on Linux, so we can use libc's size_t printf format: - #define SIZE_T_FMT "%zu" - #define SIZE_T_FMT_PRECISION(x) "%" #x "zu" - #define SIZE_T_FMT_HEX "%zx" - #endif - #define NORETURN __attribute((__noreturn__)) #else @@ -92,8 +64,6 @@ #define ALIGN_16 */ - #define FORMATSTRING(formatIndex, va_argsIndex) __attribute__((format (printf, formatIndex, va_argsIndex))) - #endif @@ -213,6 +183,7 @@ typedef unsigned char Byte; // Common headers (without macros): +#include "fmt/format.h" #include "StringUtils.h" diff --git a/Tools/GrownBiomeGenVisualiser/GrownBiomeGenVisualiser.cpp b/Tools/GrownBiomeGenVisualiser/GrownBiomeGenVisualiser.cpp index 0efaf9a59..31418b008 100644 --- a/Tools/GrownBiomeGenVisualiser/GrownBiomeGenVisualiser.cpp +++ b/Tools/GrownBiomeGenVisualiser/GrownBiomeGenVisualiser.cpp @@ -10,6 +10,7 @@ #define PROT_INT_BUFFER_SIZE (130 * 130) #include "Generating/ProtIntGen.h" +#include "fmt/printf.h" @@ -20,7 +21,6 @@ typedef int Color[3]; // Color is an array of 3 ints // Forward declarations, needed for GCC and Clang: -void log(const char * a_Fmt, ...) FORMATSTRING(1, 2); void outputBitmapFile( const AString & a_FileName, unsigned a_ImageSizeX, unsigned a_ImageSizeY, @@ -155,14 +155,12 @@ biomeColorMap[] = -void log(const char * a_Fmt, ...) +template +void log(const char * a_Fmt, const Args & ... a_Args) { - AString buf; - va_list args; - va_start(args, a_Fmt); - AppendVPrintf(buf, a_Fmt, args); - va_end(args); - std::cout << buf << std::endl << std::flush; + fmt::printf(a_Fmt, a_Args...); + putchar('\n'); + fflush(stdout); } diff --git a/Tools/MCADefrag/CMakeLists.txt b/Tools/MCADefrag/CMakeLists.txt index 42d6f7381..3243bf1eb 100644 --- a/Tools/MCADefrag/CMakeLists.txt +++ b/Tools/MCADefrag/CMakeLists.txt @@ -1,3 +1,4 @@ +cmake_minimum_required(VERSION 3.0.2) project (MCADefrag) # Without this, the MSVC variable isn't defined for MSVC builds ( https://www.cmake.org/pipermail/cmake/2011-November/047130.html ) @@ -92,4 +93,4 @@ add_executable(MCADefrag ${SHARED_OSS_HDR} ) -target_link_libraries(MCADefrag zlib) +target_link_libraries(MCADefrag zlib fmt::fmt) diff --git a/Tools/MCADefrag/Globals.h b/Tools/MCADefrag/Globals.h index 4fe9db648..f8fd68b6a 100644 --- a/Tools/MCADefrag/Globals.h +++ b/Tools/MCADefrag/Globals.h @@ -22,13 +22,6 @@ #define ALIGN_8 #define ALIGN_16 - #define FORMATSTRING(formatIndex, va_argsIndex) - - // MSVC has its own custom version of zu format - #define SIZE_T_FMT "%Iu" - #define SIZE_T_FMT_PRECISION(x) "%" #x "Iu" - #define SIZE_T_FMT_HEX "%Ix" - #define NORETURN __declspec(noreturn) #elif defined(__GNUC__) @@ -44,12 +37,6 @@ // Some portability macros :) #define stricmp strcasecmp - #define FORMATSTRING(formatIndex, va_argsIndex) - - #define SIZE_T_FMT "%zu" - #define SIZE_T_FMT_PRECISION(x) "%" #x "zu" - #define SIZE_T_FMT_HEX "%zx" - #define NORETURN __attribute((__noreturn__)) #else @@ -69,8 +56,6 @@ #define ALIGN_16 */ - #define FORMATSTRING(formatIndex, va_argsIndex) __attribute__((format (printf, formatIndex, va_argsIndex))) - #endif @@ -190,6 +175,7 @@ typedef unsigned char Byte; // Common headers (without macros): +#include "fmt/format.h" #include "StringUtils.h" #include "OSSupport/CriticalSection.h" #include "OSSupport/Event.h" diff --git a/Tools/MCADefrag/MCADefrag.cpp b/Tools/MCADefrag/MCADefrag.cpp index 82f5ab807..c7886da35 100644 --- a/Tools/MCADefrag/MCADefrag.cpp +++ b/Tools/MCADefrag/MCADefrag.cpp @@ -6,6 +6,7 @@ #include "Globals.h" #include "MCADefrag.h" #include "Logger.h" +#include "LoggerSimple.h" #include "LoggerListeners.h" #include "zlib/zlib.h" diff --git a/Tools/NoiseSpeedTest/CMakeLists.txt b/Tools/NoiseSpeedTest/CMakeLists.txt index 1dca070da..86d153e2e 100644 --- a/Tools/NoiseSpeedTest/CMakeLists.txt +++ b/Tools/NoiseSpeedTest/CMakeLists.txt @@ -1,3 +1,4 @@ +cmake_minimum_required(VERSION 3.0.2) project (NoiseSpeedTest) include(../../SetFlags.cmake) @@ -60,6 +61,8 @@ add_executable(NoiseSpeedTest ${SHARED_HDR} ) +target_link_libraries(NoiseSpeedTest fmt::fmt) + set_target_properties( NoiseSpeedTest PROPERTIES FOLDER Tools diff --git a/Tools/NoiseSpeedTest/Globals.h b/Tools/NoiseSpeedTest/Globals.h index f332942ae..b6b7a4af3 100644 --- a/Tools/NoiseSpeedTest/Globals.h +++ b/Tools/NoiseSpeedTest/Globals.h @@ -22,13 +22,6 @@ #define ALIGN_8 #define ALIGN_16 - #define FORMATSTRING(formatIndex, va_argsIndex) - - // MSVC has its own custom version of zu format - #define SIZE_T_FMT "%Iu" - #define SIZE_T_FMT_PRECISION(x) "%" #x "Iu" - #define SIZE_T_FMT_HEX "%Ix" - #define NORETURN __declspec(noreturn) #elif defined(__GNUC__) @@ -44,12 +37,6 @@ // Some portability macros :) #define stricmp strcasecmp - #define FORMATSTRING(formatIndex, va_argsIndex) - - #define SIZE_T_FMT "%zu" - #define SIZE_T_FMT_PRECISION(x) "%" #x "zu" - #define SIZE_T_FMT_HEX "%zx" - #define NORETURN __attribute((__noreturn__)) #else @@ -69,8 +56,6 @@ #define ALIGN_16 */ - #define FORMATSTRING(formatIndex, va_argsIndex) __attribute__((format (printf, formatIndex, va_argsIndex))) - #endif @@ -191,6 +176,7 @@ typedef unsigned char Byte; // Common headers (without macros): +#include "fmt/format.h" #include "StringUtils.h" #include "OSSupport/CriticalSection.h" #include "OSSupport/Event.h" diff --git a/Tools/ProtoProxy/CMakeLists.txt b/Tools/ProtoProxy/CMakeLists.txt index c64a97428..b5b1365a2 100644 --- a/Tools/ProtoProxy/CMakeLists.txt +++ b/Tools/ProtoProxy/CMakeLists.txt @@ -1,3 +1,4 @@ +cmake_minimum_required(VERSION 3.0.2) project (ProtoProxy) include(../../SetFlags.cmake) @@ -100,5 +101,5 @@ add_executable(ProtoProxy ${SHARED_OSS_HDR} ) -target_link_libraries(ProtoProxy zlib mbedtls) +target_link_libraries(ProtoProxy zlib mbedtls fmt::fmt) diff --git a/Tools/ProtoProxy/Connection.cpp b/Tools/ProtoProxy/Connection.cpp index 940b9a7eb..9f602e873 100644 --- a/Tools/ProtoProxy/Connection.cpp +++ b/Tools/ProtoProxy/Connection.cpp @@ -10,6 +10,8 @@ #include "mbedTLS++/CryptoKey.h" #include "../../src/Logger.h" +#include "fmt/printf.h" + #ifdef _WIN32 #include // For _mkdir() #endif @@ -282,15 +284,12 @@ void cConnection::Run(void) -void cConnection::Log(const char * a_Format, ...) +void cConnection::Log(const char * a_Format, fmt::ArgList a_Args) { - va_list args; - va_start(args, a_Format); - AString msg; - AppendVPrintf(msg, a_Format, args); - va_end(args); - AString FullMsg; - Printf(FullMsg, "[%5.3f] %s\n", GetRelativeTime(), msg.c_str()); + fmt::MemoryWriter FullMsg; + fmt::printf(FullMsg, "[%5.3f] ", GetRelativeTime()); + fmt::printf(FullMsg, a_Format, a_Args); + fmt::printf(FullMsg, "\n"); // Log to file: cCSLock Lock(m_CSLog); @@ -307,16 +306,13 @@ void cConnection::Log(const char * a_Format, ...) -void cConnection::DataLog(const void * a_Data, size_t a_Size, const char * a_Format, ...) +void cConnection::DataLog(const void * a_Data, size_t a_Size, const char * a_Format, fmt::ArgList a_Args) { - va_list args; - va_start(args, a_Format); - AString msg; - AppendVPrintf(msg, a_Format, args); - va_end(args); - AString FullMsg; + fmt::MemoryWriter FullMsg; + fmt::printf(FullMsg, "[%5.3f] ", GetRelativeTime()); + fmt::printf(FullMsg, a_Format, a_Args); AString Hex; - Printf(FullMsg, "[%5.3f] %s\n%s\n", GetRelativeTime(), msg.c_str(), CreateHexDump(Hex, a_Data, a_Size, 16).c_str()); + fmt::printf(FullMsg, "\n%s\n", CreateHexDump(Hex, a_Data, a_Size, 16)); // Log to file: cCSLock Lock(m_CSLog); diff --git a/Tools/ProtoProxy/Connection.h b/Tools/ProtoProxy/Connection.h index 2402e9f95..3b9127530 100644 --- a/Tools/ProtoProxy/Connection.h +++ b/Tools/ProtoProxy/Connection.h @@ -59,8 +59,12 @@ public: void Run(void); - void Log(const char * a_Format, ...); - void DataLog(const void * a_Data, size_t a_Size, const char * a_Format, ...); + void Log(const char * a_Format, fmt::ArgList); + FMT_VARIADIC(void, Log, const char *) + + void DataLog(const void * a_Data, size_t a_Size, const char * a_Format, fmt::ArgList); + FMT_VARIADIC(void, DataLog, const void *, size_t, const char *) + void LogFlush(void); protected: diff --git a/Tools/ProtoProxy/Globals.h b/Tools/ProtoProxy/Globals.h index 0ce5ea878..a2d0664b0 100644 --- a/Tools/ProtoProxy/Globals.h +++ b/Tools/ProtoProxy/Globals.h @@ -22,8 +22,6 @@ #define ALIGN_8 #define ALIGN_16 - #define FORMATSTRING(formatIndex, va_argsIndex) - #elif defined(__GNUC__) // TODO: Can GCC explicitly mark classes as abstract (no instances can be created)? @@ -37,8 +35,6 @@ // Some portability macros :) #define stricmp strcasecmp - #define FORMATSTRING(formatIndex, va_argsIndex) - #else #error "You are using an unsupported compiler, you might need to #define some stuff here for your compiler" @@ -57,8 +53,6 @@ #define ALIGN_16 */ - #define FORMATSTRING(formatIndex, va_argsIndex) __attribute__((format (printf, formatIndex, va_argsIndex))) - #endif @@ -186,10 +180,11 @@ typedef unsigned char Byte; // Common headers (part 1, without macros): +#include "fmt/format.h" #include "StringUtils.h" #include "OSSupport/CriticalSection.h" - +#include "LoggerSimple.h" -- cgit v1.2.3