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 Dec 19, 2022
1 parent d471fe2 commit 79e3665
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 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 <setjmp.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>

namespace iox
{
namespace testing
Expand Down Expand Up @@ -102,6 +107,16 @@ void TestingLogger::flush() noexcept
Base::assumeFlushed();
}

sigjmp_buf point;

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

longjmp(point, 1);
}

void LogPrinter::OnTestStart(const ::testing::TestInfo&)
{
dynamic_cast<TestingLogger&>(log::Logger::get()).clearLogBuffer();
Expand All @@ -113,10 +128,14 @@ 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 79e3665

Please sign in to comment.