From c9522fb740200ccef6230cec452c48efb31e5394 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Thu, 11 May 2023 22:05:17 +0200 Subject: Removed all Printf-family functions from StringUtils. Replaced them with fmt::format calls, including changes to the format strings. Also changed the format strings to use FMT_STRING, so that the format is checked compile-time against the arguments. Also fixed code-style violations already present in the code. --- src/OSSupport/File.cpp | 17 +++-------------- src/OSSupport/File.h | 13 +++---------- src/OSSupport/ServerHandleImpl.cpp | 26 +++++++++++++++----------- src/OSSupport/UDPEndpointImpl.cpp | 16 ++++++++++++---- 4 files changed, 33 insertions(+), 39 deletions(-) (limited to 'src/OSSupport') diff --git a/src/OSSupport/File.cpp b/src/OSSupport/File.cpp index 618463bd6..ba90f4038 100644 --- a/src/OSSupport/File.cpp +++ b/src/OSSupport/File.cpp @@ -666,7 +666,7 @@ unsigned cFile::GetLastModificationTime(const AString & a_FileName) -AString cFile::GetPathSeparator(void) +AString cFile::GetPathSeparator() { #ifdef _WIN32 return "\\"; @@ -679,7 +679,7 @@ AString cFile::GetPathSeparator(void) -AString cFile::GetExecutableExt(void) +AString cFile::GetExecutableExt() { #ifdef _WIN32 return ".exe"; @@ -692,18 +692,7 @@ AString cFile::GetExecutableExt(void) -int cFile::vPrintf(const char * a_Format, fmt::printf_args a_ArgList) -{ - fmt::memory_buffer Buffer; - fmt::vprintf(Buffer, fmt::to_string_view(a_Format), a_ArgList); - return Write(Buffer.data(), Buffer.size()); -} - - - - - -void cFile::Flush(void) +void cFile::Flush() { fflush(m_File); } diff --git a/src/OSSupport/File.h b/src/OSSupport/File.h index 7a3333483..975b78cd7 100644 --- a/src/OSSupport/File.h +++ b/src/OSSupport/File.h @@ -158,25 +158,18 @@ public: /** Returns the path separator used by the current platform. Note that the platform / CRT may support additional path separators (such as slashes on Windows), these don't get reported. */ - static AString GetPathSeparator(void); + static AString GetPathSeparator(); /** Returns the customary executable extension used by the current platform. */ - static AString GetExecutableExt(void); + static AString GetExecutableExt(); // tolua_end /** Returns the list of all items in the specified folder (files, folders, nix pipes, whatever's there). */ static AStringVector GetFolderContents(const AString & a_Folder); // Exported in ManualBindings.cpp - int vPrintf(const char * a_Format, fmt::printf_args a_ArgList); - template - int Printf(const char * a_Format, const Args & ... a_Args) - { - return vPrintf(a_Format, fmt::make_printf_args(a_Args...)); - } - /** Flushes all the bufferef output into the file (only when writing) */ - void Flush(void); + void Flush(); private: FILE * m_File; diff --git a/src/OSSupport/ServerHandleImpl.cpp b/src/OSSupport/ServerHandleImpl.cpp index 1550e38f6..e68f82757 100644 --- a/src/OSSupport/ServerHandleImpl.cpp +++ b/src/OSSupport/ServerHandleImpl.cpp @@ -141,7 +141,9 @@ bool cServerHandleImpl::Listen(UInt16 a_Port) if (!ServerHandleImplHelper::IsValidSocket(MainSock)) { m_ErrorCode = EVUTIL_SOCKET_ERROR(); - Printf(m_ErrorMsg, "Cannot create socket for port %d: %s", a_Port, evutil_socket_error_to_string(m_ErrorCode)); + m_ErrorMsg = fmt::format(FMT_STRING("Cannot create a server socket for port {}: {} ({})"), + a_Port, m_ErrorCode, evutil_socket_error_to_string(m_ErrorCode) + ); return false; } @@ -149,10 +151,10 @@ bool cServerHandleImpl::Listen(UInt16 a_Port) if (evutil_make_listen_socket_reuseable(MainSock) != 0) { m_ErrorCode = EVUTIL_SOCKET_ERROR(); - Printf(m_ErrorMsg, "Port %d cannot be made reusable: %d (%s). Restarting the server might not work.", + m_ErrorMsg = fmt::format(FMT_STRING("Port {} cannot be made reusable: {} ({}). Restarting the server might not work."), a_Port, m_ErrorCode, evutil_socket_error_to_string(m_ErrorCode) ); - LOG("%s", m_ErrorMsg.c_str()); + LOG("%s", m_ErrorMsg); } // Bind to all interfaces: @@ -163,7 +165,9 @@ bool cServerHandleImpl::Listen(UInt16 a_Port) if (bind(MainSock, reinterpret_cast(&name), sizeof(name)) != 0) { m_ErrorCode = EVUTIL_SOCKET_ERROR(); - Printf(m_ErrorMsg, "Cannot bind IPv4 socket to port %d: %s", a_Port, evutil_socket_error_to_string(m_ErrorCode)); + m_ErrorMsg = fmt::format(FMT_STRING("Cannot bind IPv4 socket to port {}: {} ({})"), + a_Port, m_ErrorCode, evutil_socket_error_to_string(m_ErrorCode) + ); evutil_closesocket(MainSock); return false; } @@ -185,10 +189,10 @@ bool cServerHandleImpl::Listen(UInt16 a_Port) if (evutil_make_listen_socket_reuseable(MainSock) != 0) { m_ErrorCode = EVUTIL_SOCKET_ERROR(); - Printf(m_ErrorMsg, "Port %d cannot be made reusable: %d (%s). Restarting the server might not work.", + m_ErrorMsg = fmt::format(FMT_STRING("Port {} cannot be made reusable: {} ({}). Restarting the server might not work."), a_Port, m_ErrorCode, evutil_socket_error_to_string(m_ErrorCode) ); - LOG("%s", m_ErrorMsg.c_str()); + LOG("%s", m_ErrorMsg); } // Bind to all interfaces: @@ -199,7 +203,7 @@ bool cServerHandleImpl::Listen(UInt16 a_Port) if (bind(MainSock, reinterpret_cast(&name), sizeof(name)) != 0) { m_ErrorCode = EVUTIL_SOCKET_ERROR(); - Printf(m_ErrorMsg, "Cannot bind IPv6 socket to port %d: %d (%s)", a_Port, m_ErrorCode, evutil_socket_error_to_string(m_ErrorCode)); + m_ErrorMsg = fmt::format(FMT_STRING("Cannot bind IPv6 socket to port {}: {} ({})"), a_Port, m_ErrorCode, evutil_socket_error_to_string(m_ErrorCode)); evutil_closesocket(MainSock); return false; } @@ -207,14 +211,14 @@ bool cServerHandleImpl::Listen(UInt16 a_Port) if (evutil_make_socket_nonblocking(MainSock) != 0) { m_ErrorCode = EVUTIL_SOCKET_ERROR(); - Printf(m_ErrorMsg, "Cannot make socket on port %d non-blocking: %d (%s)", a_Port, m_ErrorCode, evutil_socket_error_to_string(m_ErrorCode)); + m_ErrorMsg = fmt::format(FMT_STRING("Cannot make socket on port {} non-blocking: {} ({})"), a_Port, m_ErrorCode, evutil_socket_error_to_string(m_ErrorCode)); evutil_closesocket(MainSock); return false; } if (listen(MainSock, SOMAXCONN) != 0) { m_ErrorCode = EVUTIL_SOCKET_ERROR(); - Printf(m_ErrorMsg, "Cannot listen on port %d: %d (%s)", a_Port, m_ErrorCode, evutil_socket_error_to_string(m_ErrorCode)); + m_ErrorMsg = fmt::format(FMT_STRING("Cannot listen on port {}: {} ({})"), a_Port, m_ErrorCode, evutil_socket_error_to_string(m_ErrorCode)); evutil_closesocket(MainSock); return false; } @@ -241,10 +245,10 @@ bool cServerHandleImpl::Listen(UInt16 a_Port) if (evutil_make_listen_socket_reuseable(SecondSock) != 0) { m_ErrorCode = EVUTIL_SOCKET_ERROR(); - Printf(m_ErrorMsg, "Port %d cannot be made reusable (second socket): %d (%s). Restarting the server might not work.", + m_ErrorMsg = fmt::format(FMT_STRING("Port {} cannot be made reusable (second socket): {} ({}). Restarting the server might not work."), a_Port, m_ErrorCode, evutil_socket_error_to_string(m_ErrorCode) ); - LOG("%s", m_ErrorMsg.c_str()); + LOG("%s", m_ErrorMsg); } // Make the secondary socket nonblocking: diff --git a/src/OSSupport/UDPEndpointImpl.cpp b/src/OSSupport/UDPEndpointImpl.cpp index 3f04dd4f5..2550c5378 100644 --- a/src/OSSupport/UDPEndpointImpl.cpp +++ b/src/OSSupport/UDPEndpointImpl.cpp @@ -392,7 +392,9 @@ void cUDPEndpointImpl::Open(UInt16 a_Port) if (!UDPEndpointImplHelper::IsValidSocket(m_MainSock)) { err = EVUTIL_SOCKET_ERROR(); - m_Callbacks.OnError(err, Printf("Cannot create UDP socket for port %d: %s", a_Port, evutil_socket_error_to_string(err))); + m_Callbacks.OnError(err, fmt::format(FMT_STRING("Cannot create UDP socket for port {}: {} ({})"), + a_Port, err, evutil_socket_error_to_string(err)) + ); return; } @@ -413,7 +415,9 @@ void cUDPEndpointImpl::Open(UInt16 a_Port) if (bind(m_MainSock, reinterpret_cast(&name), sizeof(name)) != 0) { err = EVUTIL_SOCKET_ERROR(); - m_Callbacks.OnError(err, Printf("Cannot bind UDP port %d: %s", a_Port, evutil_socket_error_to_string(err))); + m_Callbacks.OnError(err, fmt::format(FMT_STRING("Cannot bind UDP port {}: {} ({})"), + a_Port, err, evutil_socket_error_to_string(err)) + ); evutil_closesocket(m_MainSock); return; } @@ -448,7 +452,9 @@ void cUDPEndpointImpl::Open(UInt16 a_Port) if (bind(m_MainSock, reinterpret_cast(&name), sizeof(name)) != 0) { err = EVUTIL_SOCKET_ERROR(); - m_Callbacks.OnError(err, Printf("Cannot bind to UDP port %d: %s", a_Port, evutil_socket_error_to_string(err))); + m_Callbacks.OnError(err, fmt::format(FMT_STRING("Cannot bind to UDP port {}: {} ({})"), + a_Port, err, evutil_socket_error_to_string(err)) + ); evutil_closesocket(m_MainSock); return; } @@ -456,7 +462,9 @@ void cUDPEndpointImpl::Open(UInt16 a_Port) if (evutil_make_socket_nonblocking(m_MainSock) != 0) { err = EVUTIL_SOCKET_ERROR(); - m_Callbacks.OnError(err, Printf("Cannot make socket on UDP port %d nonblocking: %s", a_Port, evutil_socket_error_to_string(err))); + m_Callbacks.OnError(err, fmt::format(FMT_STRING("Cannot make socket on UDP port {} nonblocking: {} ({})"), + a_Port, err, evutil_socket_error_to_string(err)) + ); evutil_closesocket(m_MainSock); return; } -- cgit v1.2.3