Skip to content

Commit

Permalink
iox-eclipse-iceoryx#1755 Use platform log frontend in Windows error h…
Browse files Browse the repository at this point in the history
…andling
  • Loading branch information
elBoberido committed Mar 11, 2024
1 parent d13ab20 commit 5f19966
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<const char*>(__FUNCTION__), log_level, msg)
iox_platform_detail_log( \
__FILE__, __LINE__, static_cast<const char*>(__FUNCTION__), IceoryxPlatformLogLevel::log_level, msg)
// NOLINTEND(cppcoreguidelines-macro-usage)

#endif // IOX_PLATFORM_LOGGING_HPP
18 changes: 11 additions & 7 deletions iceoryx_platform/win/source/win32_errorHandling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,33 @@
// SPDX-License-Identifier: Apache-2.0

#include "iceoryx_platform/win32_errorHandling.hpp"
#include "iceoryx_platform/logging.hpp"

#include <cstdio>
#include <iostream>
#include <mutex>

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;
}

0 comments on commit 5f19966

Please sign in to comment.