Skip to content

Commit

Permalink
Add the fmt library (cuberite#4065)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
peterbell10 authored Jan 3, 2018
1 parent 68fc288 commit 757231c
Show file tree
Hide file tree
Showing 83 changed files with 407 additions and 490 deletions.
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,7 @@
path = lib/zlib
url = https://github.com/cuberite/zlib.git
ignore = dirty
[submodule "lib/fmt"]
path = lib/fmt
url = https://github.com/fmtlib/fmt.git
ignore = dirty
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,9 @@ endif()
if (NOT EXISTS ${PROJECT_SOURCE_DIR}/lib/expat/CMakeLists.txt)
message(FATAL_ERROR "expat is missing in folder lib/expat. Have you initialized and updated the submodules / downloaded the extra libraries?")
endif()
if (NOT EXISTS ${PROJECT_SOURCE_DIR}/lib/fmt/CMakeLists.txt)
message(FATAL_ERROR "fmt is missing in folder lib/fmt. Have you initialized and updated the submodules / downloaded the extra libraries?")
endif()
if (NOT EXISTS ${PROJECT_SOURCE_DIR}/lib/lua/CMakeLists.txt)
message(FATAL_ERROR "lua is missing in folder lib/lua. Have you initialized and updated the submodules / downloaded the extra libraries?")
endif()
Expand Down Expand Up @@ -256,6 +259,8 @@ add_subdirectory(lib/SQLiteCpp/)
add_subdirectory(lib/expat/)
add_subdirectory(lib/luaexpat/)
add_subdirectory(lib/libevent/ EXCLUDE_FROM_ALL)
add_subdirectory(lib/fmt)


# Add proper include directories so that SQLiteCpp can find SQLite3:
get_property(SQLITECPP_INCLUDES DIRECTORY "lib/SQLiteCpp/" PROPERTY INCLUDE_DIRECTORIES)
Expand Down Expand Up @@ -299,6 +304,7 @@ if (MSVC)
event_core
event_extra
expat
fmt
jsoncpp_lib_static
lua
luaexpat
Expand Down
23 changes: 23 additions & 0 deletions Install/ThirdPartyLicenses/fmt-LICENSE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Copyright (c) 2012 - 2016, Victor Zverovich

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2 changes: 1 addition & 1 deletion SetFlags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ macro(set_exe_flags)
string(REPLACE "-w" "" CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}")
string(REPLACE "-w" "" CMAKE_CXX_FLAGS_COVERAGE "${CMAKE_CXX_FLAGS_COVERAGE}")
string(REPLACE "-w" "" CMAKE_C_FLAGS_COVERAGE "${CMAKE_C_FLAGS_COVERAGE}")
add_flags_cxx("-Wall -Wextra -Wno-unused-parameter")
add_flags_cxx("-Wall -Wextra -Wno-unused-parameter -Wno-missing-noreturn")

# we support non-IEEE 754 fpus so can make no guarentees about error
add_flags_cxx("-ffast-math")
Expand Down
3 changes: 3 additions & 0 deletions Tools/GrownBiomeGenVisualiser/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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 )
Expand Down Expand Up @@ -89,4 +90,6 @@ add_executable(GrownBiomeGenVisualiser
${SHARED_OSS_HDR}
)

target_link_libraries(GrownBiomeGenVisualiser fmt::fmt)

set_target_properties(GrownBiomeGenVisualiser PROPERTIES FOLDER Tools)
31 changes: 1 addition & 30 deletions Tools/GrownBiomeGenVisualiser/Globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand All @@ -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

Expand All @@ -92,8 +64,6 @@
#define ALIGN_16
*/

#define FORMATSTRING(formatIndex, va_argsIndex) __attribute__((format (printf, formatIndex, va_argsIndex)))

#endif


Expand Down Expand Up @@ -213,6 +183,7 @@ typedef unsigned char Byte;


// Common headers (without macros):
#include "fmt/format.h"
#include "StringUtils.h"


Expand Down
14 changes: 6 additions & 8 deletions Tools/GrownBiomeGenVisualiser/GrownBiomeGenVisualiser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define PROT_INT_BUFFER_SIZE (130 * 130)
#include "Generating/ProtIntGen.h"

#include "fmt/printf.h"



Expand All @@ -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,
Expand Down Expand Up @@ -155,14 +155,12 @@ biomeColorMap[] =



void log(const char * a_Fmt, ...)
template <typename ... Args>
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);
}


Expand Down
3 changes: 2 additions & 1 deletion Tools/MCADefrag/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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 )
Expand Down Expand Up @@ -92,4 +93,4 @@ add_executable(MCADefrag
${SHARED_OSS_HDR}
)

target_link_libraries(MCADefrag zlib)
target_link_libraries(MCADefrag zlib fmt::fmt)
16 changes: 1 addition & 15 deletions Tools/MCADefrag/Globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand All @@ -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

Expand All @@ -69,8 +56,6 @@
#define ALIGN_16
*/

#define FORMATSTRING(formatIndex, va_argsIndex) __attribute__((format (printf, formatIndex, va_argsIndex)))

#endif


Expand Down Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions Tools/MCADefrag/MCADefrag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "Globals.h"
#include "MCADefrag.h"
#include "Logger.h"
#include "LoggerSimple.h"
#include "LoggerListeners.h"
#include "zlib/zlib.h"

Expand Down
3 changes: 3 additions & 0 deletions Tools/NoiseSpeedTest/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
cmake_minimum_required(VERSION 3.0.2)
project (NoiseSpeedTest)

include(../../SetFlags.cmake)
Expand Down Expand Up @@ -60,6 +61,8 @@ add_executable(NoiseSpeedTest
${SHARED_HDR}
)

target_link_libraries(NoiseSpeedTest fmt::fmt)

set_target_properties(
NoiseSpeedTest
PROPERTIES FOLDER Tools
Expand Down
16 changes: 1 addition & 15 deletions Tools/NoiseSpeedTest/Globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand All @@ -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

Expand All @@ -69,8 +56,6 @@
#define ALIGN_16
*/

#define FORMATSTRING(formatIndex, va_argsIndex) __attribute__((format (printf, formatIndex, va_argsIndex)))

#endif


Expand Down Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion Tools/ProtoProxy/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
cmake_minimum_required(VERSION 3.0.2)
project (ProtoProxy)

include(../../SetFlags.cmake)
Expand Down Expand Up @@ -100,5 +101,5 @@ add_executable(ProtoProxy
${SHARED_OSS_HDR}
)

target_link_libraries(ProtoProxy zlib mbedtls)
target_link_libraries(ProtoProxy zlib mbedtls fmt::fmt)

28 changes: 12 additions & 16 deletions Tools/ProtoProxy/Connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include "mbedTLS++/CryptoKey.h"
#include "../../src/Logger.h"

#include "fmt/printf.h"

#ifdef _WIN32
#include <direct.h> // For _mkdir()
#endif
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
8 changes: 6 additions & 2 deletions Tools/ProtoProxy/Connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,12 @@ class cConnection

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:
Expand Down
Loading

0 comments on commit 757231c

Please sign in to comment.