X-Git-Url: http://git.lttng.org/?a=blobdiff_plain;f=src%2Fcommon%2Flogging.c;h=fb3f119fcbba3c34e32e6741c6686c17f33ed6a3;hb=662d05907560345f06a19463ce5bf24ca72a442c;hp=eb610833676d46519cd7e7437e36d00df3f84654;hpb=c6e6343c93e1b6ec02de1f9dc825faae0d6cc7ab;p=lttng-ust.git diff --git a/src/common/logging.c b/src/common/logging.c index eb610833..fb3f119f 100644 --- a/src/common/logging.c +++ b/src/common/logging.c @@ -4,24 +4,57 @@ * Copyright (C) 2011 Mathieu Desnoyers */ +#include +#include + #include "common/logging.h" -volatile enum lttng_ust_log_level lttng_ust_log_level; +int lttng_ust_log_level = LTTNG_UST_LOG_LEVEL_UNKNOWN; -void lttng_ust_logging_init(void) +/* + * Initialize the global log level from the "LTTNG_UST_DEBUG" environment + * variable. + * + * This could end up being called concurently by multiple threads but doesn't + * require a mutex since the input is invariant across threads and the result + * will be the same. + * + * Return the current log level to save the caller a second read of the global + * log level. + */ +int lttng_ust_logging_init(void) { char *lttng_ust_debug; + int current_log_level; + + current_log_level = CMM_LOAD_SHARED(lttng_ust_log_level); + + /* + * Check early if we are initialized, this is unlikely as it's already tested + * in lttng_ust_debug_enabled before performing lazy initialization. + */ + if (caa_unlikely(current_log_level != LTTNG_UST_LOG_LEVEL_UNKNOWN)) + goto end; + + /* + * This getenv is not part of lttng_ust_getenv() because logging is + * used in the getenv initialization and thus logging must be + * initialized prior to getenv. + */ + lttng_ust_debug = getenv("LTTNG_UST_DEBUG"); + + /* + * If the LTTNG_UST_DEBUG environment variable is defined, print all + * messages, otherwise print nothing. + */ + if (lttng_ust_debug) + current_log_level = LTTNG_UST_LOG_LEVEL_DEBUG; + else + current_log_level = LTTNG_UST_LOG_LEVEL_SILENT; + + /* Initialize the log level */ + CMM_STORE_SHARED(lttng_ust_log_level, current_log_level); - if (lttng_ust_log_level == LTTNG_UST_LOG_LEVEL_UNKNOWN) { - /* - * This getenv is not part of lttng_ust_getenv() because it - * is required to print ERR() performed during getenv - * initialization. - */ - lttng_ust_debug = getenv("LTTNG_UST_DEBUG"); - if (lttng_ust_debug) - lttng_ust_log_level = LTTNG_UST_LOG_LEVEL_DEBUG; - else - lttng_ust_log_level = LTTNG_UST_LOG_LEVEL_NORMAL; - } +end: + return current_log_level; }