Skip to content

Commit

Permalink
iox-eclipse-iceoryx#1755 Flush TestingLogger on SIGSEGV
Browse files Browse the repository at this point in the history
  • Loading branch information
elBoberido committed Feb 6, 2023
1 parent 5cf0bdc commit 29d60be
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions iceoryx_hoofs/testing/testing_logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@

#include <iostream>

#include <csetjmp>
#include <csignal>
#include <cstdio>
#include <cstring>

namespace iox
{
namespace testing
Expand Down Expand Up @@ -112,6 +117,16 @@ std::vector<std::string> TestingLogger::getLogMessages() noexcept
return logger.m_loggerData->buffer;
}

std::jmp_buf sigsevJmpPoint;

static void sigsegvHandler(int /*sig*/, siginfo_t*, void*)
{
std::cout << "SIGSEGV\n" << std::flush;
dynamic_cast<TestingLogger&>(log::Logger::get()).printLogBuffer();

std::longjmp(&sigsevJmpPoint[0], 1);
}

void LogPrinter::OnTestStart(const ::testing::TestInfo&)
{
dynamic_cast<TestingLogger&>(log::Logger::get()).clearLogBuffer();
Expand All @@ -123,10 +138,16 @@ void LogPrinter::OnTestStart(const ::testing::TestInfo&)
std::abort();
});

/// @todo iox-#1755 register signal handler for sigterm to flush to logger;
/// there might be tests to register a handler itself and when this is
/// done at each start of the test only the tests who use their
/// own signal handler are affected and don't get an log output on termination
struct sigaction action
{
};
memset(&action, 0, sizeof(struct sigaction));
sigemptyset(&action.sa_mask);

action.sa_flags = SA_NODEFER;
action.sa_sigaction = sigsegvHandler;

sigaction(SIGSEGV, &action, nullptr);
}

void LogPrinter::OnTestPartResult(const ::testing::TestPartResult& result)
Expand Down

0 comments on commit 29d60be

Please sign in to comment.