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 2d8ab87
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 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,34 @@
// 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, IceoryxPlatformLogLevel::IOX_PLATFORM_LOG_LEVEL_ERROR, buffer);
}
return lastError;
}

0 comments on commit 2d8ab87

Please sign in to comment.