dlogg icon indicating copy to clipboard operation
dlogg copied to clipboard

logger.reload() is not usable in a signal handler

Open burjui opened this issue 7 years ago • 0 comments

Tried to reload the log file on USR1 signal:

import core.stdc.signal;
enum SIGUSR1 = 10;
signal(SIGUSR1, function void() @system nothrow @nogc {
    logger.reload();
});

This code failed to compile:

source/log.d(56,16): Error: @nogc function 'log._sharedStaticCtor11.__funcliteral1' cannot call non-@nogc function 'dlogg.strict.StyledStrictLogger!(LogLevel, cast(LogLevel)0, "[%2$s]: \x1b[95mDebug:\x1b[0m %1$s", "[%2$s]: Debug: %1$s", cast(LogLevel)1, "[%2$s]: \x1b[92mInfo:\x1b[0m %1$s", "[%2$s]: Info: %1$s", cast(LogLevel)2, "[%2$s]: \x1b[93mWarning:\x1b[0m %1$s", "[%2$s]: Warning: %1$s", cast(LogLevel)3, "[%2$s]: \x1b[91mError:\x1b[0m %1$s", "[%2$s]: Error: %1$s", cast(LogLevel)4, "[%2$s]: \x1b[1m\x1b[91mFatal:\x1b[0m\x1b[0m %1$s", "[%2$s]: Fatal: %1$s", cast(LogLevel)5, "", "").StyledStrictLogger.reload'
source/log.d(56,16): Error: function `dlogg.strict.StyledStrictLogger!(LogLevel, cast(LogLevel)0, "[%2$s]: \x1b[95mDebug:\x1b[0m %1$s", "[%2$s]: Debug: %1$s", cast(LogLevel)1, "[%2$s]: \x1b[92mInfo:\x1b[0m %1$s", "[%2$s]: Info: %1$s", cast(LogLevel)2, "[%2$s]: \x1b[93mWarning:\x1b[0m %1$s", "[%2$s]: Warning: %1$s", cast(LogLevel)3, "[%2$s]: \x1b[91mError:\x1b[0m %1$s", "[%2$s]: Error: %1$s", cast(LogLevel)4, "[%2$s]: \x1b[1m\x1b[91mFatal:\x1b[0m\x1b[0m %1$s", "[%2$s]: Fatal: %1$s", cast(LogLevel)5, "", "").StyledStrictLogger.reload` is not nothrow
source/log.d(55,21): Error: nothrow function `log._sharedStaticCtor11.__funcliteral1` may throw

Indeed, it's neither nothrow nor @nogc, but it should be. To my suprise, even the function std.file.exists, which is used by reload is not @nogc, since it allocates temporary strings for paths. I could just declare the signal function myself and change the signature of the signal handler, but it would be asking for trouble. OS doesn't ask politely when a signal arrives, it just interrupts a random thread of the program to handle it. According to man 7 pthreads:

POSIX.1 distinguishes the notions of signals that are directed to the process as a whole and signals that are directed to individual threads. According to POSIX.1, a process-directed signal (sent using kill(2), for example) should be handled by a single, arbitrarily selected thread within the process.

burjui avatar Nov 09 '17 22:11 burjui