diff --git a/maestro/logging.c b/maestro/logging.c index 78c8e2158a898a45214378e4b4cec9e54cf62d1c..5eb5759781e0d791ec1ad683eddb518b42d87ef5 100644 --- a/maestro/logging.c +++ b/maestro/logging.c @@ -5,6 +5,7 @@ **/ /* * Copyright (C) 2019 Cray Computer GmbH + * Copyright (C) 2020 HPE, HP Schweiz GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are @@ -38,6 +39,7 @@ #include "maestro/i_globals.h" #include "maestro/i_statistics.h" #include <errno.h> +#include <ctype.h> #include <pthread.h> #ifdef HAVE_PTHREAD_NP_H @@ -259,7 +261,6 @@ mstro__ensure_threadid(void) static _Atomic(bool) g_queried_env = false; /* log destination choice */ static int g_log_dst = MSTRO_LOG_DST_STDERR; -static char *g_env_debug_flag = NULL; static uint64_t g_log_module_mask = MSTRO_LOG_MODULE_ALL; @@ -332,15 +333,39 @@ const char *mstro_log_module_name(uint64_t module) return "???"; } +static inline +int mstro_log__parse_debug_level(const char *debug_level) +{ + /* FIXME: improve when merging with libERL */ + if(strncasecmp(debug_level, "noise", 1)==0) { + return MSTRO_LOG_NOISE; + } else if(strncasecmp(debug_level, "debug", 1)==0) { + return MSTRO_LOG_DEBUG; + } else if(strncasecmp(debug_level, "info", 1)==0) { + return MSTRO_LOG_INFO; + } else if(strncasecmp(debug_level, "warn", 1)==0) { + return MSTRO_LOG_WARN; + } else if(strncasecmp(debug_level, "err", 1)==0) { + return MSTRO_LOG_ERR; + } else { + return MSTRO_MAX_LOG_LEVEL; /* default */ + } +} /* one-time init function for log infrastructure */ static inline void mstro_log__init() { - _Atomic(bool) already_initialized = - atomic_exchange(&g_queried_env, true); - if(!already_initialized) { + pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; + + int err = pthread_mutex_lock(&mtx); + if(err!=0) + abort(); + + if(!g_queried_env) { + /* we're the first ones, do init */ + g_queried_env = true; g_pid=getpid(); const char *env_log_dst = getenv(MSTRO_ENV_LOG_DST); @@ -361,21 +386,25 @@ mstro_log__init() } } - g_env_debug_flag = getenv(MSTRO_ENV_LOG_LEVEL); - if(g_env_debug_flag!=NULL) { - int env_level = atoi(g_env_debug_flag); - if(env_level<0 || env_level>=MSTRO_log__MAX) { + char * env_debug_level = getenv(MSTRO_ENV_LOG_LEVEL); + if(env_debug_level!=NULL) { + if(isalpha(env_debug_level[0])) { + g_debug_level = mstro_log__parse_debug_level(env_debug_level); + } else { + g_debug_level = atoi(env_debug_level); + } + if(g_debug_level<0 || g_debug_level>=MSTRO_log__MAX) { LOG_WARN(MSTRO_LOG_MODULE_CORE, "Attempt to set debug level to %s in environment variable %s, ignored\n", - g_env_debug_flag, MSTRO_ENV_LOG_LEVEL); + g_debug_level, MSTRO_ENV_LOG_LEVEL); + g_debug_level = MSTRO_MAX_LOG_LEVEL; } else { - g_debug_level = MIN(MSTRO_MAX_LOG_LEVEL,env_level); + g_debug_level = MIN(MSTRO_MAX_LOG_LEVEL,g_debug_level); } LOG_INFO(MSTRO_LOG_MODULE_CORE, "Environment variable %s sets log level to %d\n", MSTRO_ENV_LOG_LEVEL, g_debug_level); - } - + } char *env_log_color = getenv(MSTRO_ENV_LOG_COLOR); if(env_log_color!=NULL) { @@ -412,7 +441,14 @@ mstro_log__init() /* thread ID can't be set here, every thread sets it at first call */ + } else { + /* we're not the first */ + LOG_DEBUG(MSTRO_LOG_MODULE_CORE, + "Competing init, lost to other thread\n"); } + err=pthread_mutex_unlock(&mtx); + if(err!=0) + abort(); }