Files
ternfs-XTXMarkets/cpp/core/Env.cpp
Francesco Mazzoli 38f3d54ecd Wait forever, rather than having timeouts
The goal here is to not have constant wakeups due to timeout. Do
not attempt to clean things up nicely before termination -- just
terminate instead. We can setup a proper termination system in
the future, I first want to see if this makes a difference.

Also, change xmon to use pipes for communication, so that it can
wait without timers as well.

Also, `write` directly for logging, so that we know the logs will
make it to the file after the logging call returns (since we now
do not have the chance to flush them afterwards).
2023-12-07 10:11:19 +00:00

80 lines
2.0 KiB
C++

#include <atomic>
#include <signal.h>
#include "Env.hpp"
#include "Assert.hpp"
#include "Exception.hpp"
std::ostream& operator<<(std::ostream& out, LogLevel ll) {
switch (ll) {
case LogLevel::LOG_TRACE:
out << "TRACE";
break;
case LogLevel::LOG_DEBUG:
out << "DEBUG";
break;
case LogLevel::LOG_INFO:
out << "INFO";
break;
case LogLevel::LOG_ERROR:
out << "ERROR";
break;
default:
throw EGGS_EXCEPTION("bad log level %s", (uint32_t)ll);
}
return out;
}
void Logger::toggleDebug() {
if (_logLevel == LogLevel::LOG_DEBUG) {
_logLevel = _savedLogLevel;
} else {
_logLevel = LogLevel::LOG_DEBUG;
}
}
static std::atomic<Logger*> debuggableLogger(nullptr);
extern "C" {
static void loggerSignalHandler(int signal_number) {
(*debuggableLogger).toggleDebug();
}
}
void installLoggerSignalHandler(void* logger) {
Logger* old = nullptr;
if (!debuggableLogger.compare_exchange_strong(old, (Logger*)logger)) {
throw EGGS_EXCEPTION("Could not install logger signal handler, some other logger is already here.");
}
struct sigaction act;
act.sa_handler = loggerSignalHandler;
if (sigemptyset(&act.sa_mask) != 0) {
SYSCALL_EXCEPTION("sigemptyset");
}
act.sa_flags = 0;
// USR1 is used by undertaker
if (sigaction(SIGUSR2, &act, NULL) < 0) {
throw SYSCALL_EXCEPTION("sigaction");
}
}
void tearDownLoggerSignalHandler(void* logger) {
Logger* old = (Logger*)logger;
if (!debuggableLogger.compare_exchange_strong(old, nullptr)) {
throw EGGS_EXCEPTION("Could not tear down logger signal handler, bad preexisting logger");
}
struct sigaction act;
act.sa_handler = SIG_DFL;
if (sigemptyset(&act.sa_mask) != 0) {
SYSCALL_EXCEPTION("sigemptyset");
}
act.sa_flags = 0;
if (sigaction(SIGUSR2, &act, NULL) < 0) {
throw SYSCALL_EXCEPTION("sigaction");
}
}