Clean up logger

This commit is contained in:
2022-12-10 21:53:21 +01:00
parent 2efe65fc74
commit 9ac67dbfd3
2 changed files with 28 additions and 32 deletions

View File

@ -54,6 +54,12 @@ static const char *level_keywords[] = {
"DEBUG" "DEBUG"
}; };
static void err(const char *restrict msg) {
char err_buf[64];
strerror_r(errno, err_buf, sizeof(err_buf));
fprintf(stderr, ERR_STR "[%6s][logger] %s: %s" CLR_STR "\n", level_keywords[LOG_CRITICAL], msg, err_buf);
}
void logmsgf(log_lvl_t level, const char *restrict format, ...) { void logmsgf(log_lvl_t level, const char *restrict format, ...) {
char buf[256]; char buf[256];
char err_buf[64]; char err_buf[64];
@ -75,11 +81,10 @@ void logmsgf(log_lvl_t level, const char *restrict format, ...) {
if (!logger_alive) { if (!logger_alive) {
// no logger thread running // no logger thread running
// simply write to stdout/stderr without synchronization // simply write to stdout without synchronization
FILE *out = (level <= LOG_CRITICAL) ? stderr : stdout; printf("%s[%-6s][%-6s]%s%s ", color, level_keywords[level], (name != NULL) ? (char *) name : "", CLR_STR, (prefix != NULL) ? (char *) prefix : "");
fprintf(out, "%s[%-6s][%-6s]%s%s ", color, level_keywords[level], (name != NULL) ? (char *) name : "", CLR_STR, (prefix != NULL) ? (char *) prefix : ""); vprintf(buf, args);
vfprintf(out, buf, args); printf("\n");
fprintf(out, "\n");
} else { } else {
// wait for free slot in buffer // wait for free slot in buffer
try_again_free: try_again_free:
@ -87,8 +92,7 @@ void logmsgf(log_lvl_t level, const char *restrict format, ...) {
if (errno == EINTR) { if (errno == EINTR) {
goto try_again_free; goto try_again_free;
} else { } else {
strerror_r(errno, err_buf, sizeof(err_buf)); err("Unable to lock semaphore");
fprintf(stderr, "[logger] " ERR_STR "Unable to lock semaphore: %s" CLR_STR "\n", err_buf);
} }
// cleanup // cleanup
va_end(args); va_end(args);
@ -101,8 +105,7 @@ void logmsgf(log_lvl_t level, const char *restrict format, ...) {
if (errno == EINTR) { if (errno == EINTR) {
goto try_again_buf; goto try_again_buf;
} else { } else {
strerror_r(errno, err_buf, sizeof(err_buf)); err("Unable to lock semaphore");
fprintf(stderr, "[logger] " ERR_STR "Unable to lock semaphore: %s" CLR_STR "\n", err_buf);
} }
// cleanup // cleanup
sem_post(&sem_buf_free); sem_post(&sem_buf_free);
@ -147,13 +150,11 @@ static void logger_destroy(void) {
} }
static int logger_init(void) { static int logger_init(void) {
char err_buf[64];
int ret; int ret;
// try to initialize all three semaphores // try to initialize all three semaphores
if (sem_init(&sem_buf, 0, 1) != 0 || sem_init(&sem_buf_free, 0, 1) != 0 || sem_init(&sem_buf_used, 0, 0) != 0) { if (sem_init(&sem_buf, 0, 1) != 0 || sem_init(&sem_buf_free, 0, 1) != 0 || sem_init(&sem_buf_used, 0, 0) != 0) {
strerror_r(errno, err_buf, sizeof(err_buf)); err("Unable to initialize semaphore");
fprintf(stderr, "[logger] " ERR_STR "Unable to initialize semaphore: %s" CLR_STR "\n", err_buf);
logger_destroy(); logger_destroy();
return -1; return -1;
} }
@ -164,8 +165,8 @@ static int logger_init(void) {
// initialize thread specific values (keys) // initialize thread specific values (keys)
if ((ret = pthread_key_create(&key_name, free)) != 0 || (ret = pthread_key_create(&key_prefix, free)) != 0) { if ((ret = pthread_key_create(&key_name, free)) != 0 || (ret = pthread_key_create(&key_prefix, free)) != 0) {
strerror_r(ret, err_buf, sizeof(err_buf)); errno = ret;
fprintf(stderr, "[logger] " ERR_STR "Unable to initialize thread specific values: %s" CLR_STR "\n", err_buf); err("Unable to initialize thread specific values");
logger_destroy(); logger_destroy();
return -1; return -1;
} }
@ -184,14 +185,13 @@ void logger_set_name(const char *restrict name) {
// not initialized // not initialized
strncpy(global_name, name, sizeof(global_name)); strncpy(global_name, name, sizeof(global_name));
} else { } else {
char err_buf[64];
int ret; int ret;
void *ptr = pthread_getspecific(key_name); void *ptr = pthread_getspecific(key_name);
if (!ptr) { if (!ptr) {
ptr = malloc(LOG_NAME_LEN); ptr = malloc(LOG_NAME_LEN);
if ((ret = pthread_setspecific(key_name, ptr)) != 0) { if ((ret = pthread_setspecific(key_name, ptr)) != 0) {
strerror_r(ret, err_buf, sizeof(err_buf)); errno = ret;
fprintf(stderr, "[logger] " ERR_STR "Unable to set thread specific values: %s" CLR_STR "\n", err_buf); err("Unable to set thread specific values");
return; return;
} }
} }
@ -203,15 +203,14 @@ void logger_set_prefix(const char *restrict prefix) {
if (key_name == -1) { if (key_name == -1) {
strncpy(global_prefix, prefix, sizeof(global_prefix)); strncpy(global_prefix, prefix, sizeof(global_prefix));
} else { } else {
char err_buf[64];
int ret; int ret;
void *ptr = pthread_getspecific(key_name); void *ptr = pthread_getspecific(key_name);
if (!ptr) { if (!ptr) {
ptr = malloc(LOG_PREFIX_LEN); ptr = malloc(LOG_PREFIX_LEN);
pthread_setspecific(key_prefix, ptr); pthread_setspecific(key_prefix, ptr);
if ((ret = pthread_setspecific(key_prefix, ptr)) != 0) { if ((ret = pthread_setspecific(key_prefix, ptr)) != 0) {
strerror_r(ret, err_buf, sizeof(err_buf)); errno = ret;
fprintf(stderr, "[logger] " ERR_STR "Unable to set thread specific values: %s" CLR_STR "\n", err_buf); err("Unable to set thread specific values");
return; return;
} }
} }
@ -224,8 +223,6 @@ void logger_stop(void) {
} }
void logger(void) { void logger(void) {
char err_buf[64];
if (logger_init() != 0) if (logger_init() != 0)
return; return;
@ -238,8 +235,7 @@ void logger(void) {
if (errno == EINTR) { if (errno == EINTR) {
continue; continue;
} else { } else {
strerror_r(errno, err_buf, sizeof(err_buf)); err("Unable to lock semaphore");
fprintf(stderr, "[logger] " ERR_STR "Unable to lock semaphore: %s" CLR_STR "\n", err_buf);
break; break;
} }
} }
@ -247,7 +243,7 @@ void logger(void) {
log_msg_t *msg = &buffer.msgs[buffer.wr]; log_msg_t *msg = &buffer.msgs[buffer.wr];
buffer.wr = (buffer.wr + 1) % LOG_BUF_SIZE; buffer.wr = (buffer.wr + 1) % LOG_BUF_SIZE;
fprintf((msg->lvl <= LOG_CRITICAL) ? stderr : stdout, "[%s]%s %s\n", msg->name, (msg->prefix[0] != 0) ? msg->prefix : "", msg->txt); printf("[%s]%s %s\n", msg->name, (msg->prefix[0] != 0) ? msg->prefix : "", msg->txt);
// unlock slot in buffer // unlock slot in buffer
sem_post(&sem_buf_free); sem_post(&sem_buf_free);

View File

@ -51,8 +51,8 @@ static int ssl_servername_cb(SSL *ssl, int *ad, void *arg) {
return SSL_TLSEXT_ERR_OK; return SSL_TLSEXT_ERR_OK;
} }
void destroy(int _) { void destroy(int sig) {
error("Terminating forcefully!"); critical("Terminating forcefully!");
int status = 0; int status = 0;
int ret; int ret;
int kills = 0; int kills = 0;
@ -60,11 +60,11 @@ void destroy(int _) {
if (children[i] != 0) { if (children[i] != 0) {
ret = waitpid(children[i], &status, WNOHANG); ret = waitpid(children[i], &status, WNOHANG);
if (ret < 0) { if (ret < 0) {
critical("Unable to wait for child process (PID %i)", children[i]); error("Unable to wait for child process (PID %i)", children[i]);
} else if (ret == children[i]) { } else if (ret == children[i]) {
children[i] = 0; children[i] = 0;
if (status != 0) { if (status != 0) {
critical("Child process with PID %i terminated with exit code %i", ret, status); error("Child process with PID %i terminated with exit code %i", ret, status);
} }
} else { } else {
kill(children[i], SIGKILL); kill(children[i], SIGKILL);
@ -80,7 +80,7 @@ void destroy(int _) {
exit(2); exit(2);
} }
void terminate(int _) { void terminate(int sig) {
fprintf(stderr, "\n"); fprintf(stderr, "\n");
notice("Terminating gracefully..."); notice("Terminating gracefully...");
active = 0; active = 0;
@ -169,8 +169,8 @@ int main(int argc, const char *argv[]) {
logger_set_name("server"); logger_set_name("server");
if (setvbuf(stdout, NULL, _IOLBF, 0) != 0) { if (setvbuf(stdout, NULL, _IOLBF, 0) != 0 || setvbuf(stderr, NULL, _IOLBF, 0) != 0) {
critical("Unable to set stdout to line-buffered mode"); critical("Unable to set stdout/stderr to line-buffered mode");
return 1; return 1;
} }
printf("Sesimos web server " SERVER_VERSION "\n"); printf("Sesimos web server " SERVER_VERSION "\n");