From 5f19966ee0f7052ea4dba369e251449a3d12840c Mon Sep 17 00:00:00 2001 From: Mathias Kraus Date: Mon, 11 Mar 2024 22:08:18 +0100 Subject: [PATCH] iox-#1755 Use platform log frontend in Windows error handling --- .../include/iceoryx_platform/logging.hpp | 3 ++- .../win/source/win32_errorHandling.cpp | 18 +++++++++++------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/iceoryx_platform/generic/include/iceoryx_platform/logging.hpp b/iceoryx_platform/generic/include/iceoryx_platform/logging.hpp index 25cabefb04..a4500654cc 100644 --- a/iceoryx_platform/generic/include/iceoryx_platform/logging.hpp +++ b/iceoryx_platform/generic/include/iceoryx_platform/logging.hpp @@ -52,7 +52,8 @@ void iox_platform_detail_log( /// @param[in] log_level is the log level to be used for the log message /// @param[in] msg is the message to be logged #define IOX_PLATFORM_LOG(log_level, msg) \ - iox_platform_detail_log(__FILE__, __LINE__, static_cast(__FUNCTION__), log_level, msg) + iox_platform_detail_log( \ + __FILE__, __LINE__, static_cast(__FUNCTION__), IceoryxPlatformLogLevel::log_level, msg) // NOLINTEND(cppcoreguidelines-macro-usage) #endif // IOX_PLATFORM_LOGGING_HPP diff --git a/iceoryx_platform/win/source/win32_errorHandling.cpp b/iceoryx_platform/win/source/win32_errorHandling.cpp index 954699af6c..0ea5ac9340 100644 --- a/iceoryx_platform/win/source/win32_errorHandling.cpp +++ b/iceoryx_platform/win/source/win32_errorHandling.cpp @@ -16,29 +16,33 @@ // SPDX-License-Identifier: Apache-2.0 #include "iceoryx_platform/win32_errorHandling.hpp" +#include "iceoryx_platform/logging.hpp" +#include #include #include int __PrintLastErrorToConsole(const char* functionName, const char* file, const int line) noexcept { - static std::mutex coutMutex; constexpr uint64_t BUFFER_SIZE{2048u}; int lastError = GetLastError(); if (lastError != 0) { char buffer[BUFFER_SIZE]; - FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, + sprintf(buffer, "< Win32API Error > [%d] ::: ", lastError); + size_t used_buffer_size = strlen(buffer); + constexpr size_t NULL_TERMINATOR_SIZE{1}; + size_t remaining_buffer_size = BUFFER_SIZE - used_buffer_size - NULL_TERMINATOR_SIZE; + + FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_MAX_WIDTH_MASK, NULL, lastError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - buffer, - BUFFER_SIZE - 1, + &buffer[used_buffer_size], + remaining_buffer_size, NULL); - coutMutex.lock(); - fprintf(stderr, "< Win32API Error > %s:%d { %s } [ %d ] ::: %s", file, line, functionName, lastError, buffer); - coutMutex.unlock(); + iox_platform_detail_log(file, line, functionName, IOX_PLATFORM_LOG_LEVEL_ERROR, buffer); } return lastError; }