| 1 | /* |
| 2 | * SPDX-License-Identifier: LGPL-2.1-only |
| 3 | * |
| 4 | * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 5 | */ |
| 6 | |
| 7 | #include <urcu/compiler.h> |
| 8 | #include <urcu/system.h> |
| 9 | |
| 10 | #include "common/logging.h" |
| 11 | |
| 12 | int lttng_ust_log_level = LTTNG_UST_LOG_LEVEL_UNKNOWN; |
| 13 | |
| 14 | /* |
| 15 | * Initialize the global log level from the "LTTNG_UST_DEBUG" environment |
| 16 | * variable. |
| 17 | * |
| 18 | * This could end up being called concurently by multiple threads but doesn't |
| 19 | * require a mutex since the input is invariant across threads and the result |
| 20 | * will be the same. |
| 21 | * |
| 22 | * Return the current log level to save the caller a second read of the global |
| 23 | * log level. |
| 24 | */ |
| 25 | int lttng_ust_logging_init(void) |
| 26 | { |
| 27 | char *lttng_ust_debug; |
| 28 | int current_log_level; |
| 29 | |
| 30 | current_log_level = CMM_LOAD_SHARED(lttng_ust_log_level); |
| 31 | |
| 32 | /* |
| 33 | * Check early if we are initialized, this is unlikely as it's already tested |
| 34 | * in lttng_ust_debug_enabled before performing lazy initialization. |
| 35 | */ |
| 36 | if (caa_unlikely(current_log_level != LTTNG_UST_LOG_LEVEL_UNKNOWN)) |
| 37 | goto end; |
| 38 | |
| 39 | /* |
| 40 | * This getenv is not part of lttng_ust_getenv() because logging is |
| 41 | * used in the getenv initialization and thus logging must be |
| 42 | * initialized prior to getenv. |
| 43 | */ |
| 44 | lttng_ust_debug = getenv("LTTNG_UST_DEBUG"); |
| 45 | |
| 46 | /* |
| 47 | * If the LTTNG_UST_DEBUG environment variable is defined, print all |
| 48 | * messages, otherwise print nothing. |
| 49 | */ |
| 50 | if (lttng_ust_debug) |
| 51 | current_log_level = LTTNG_UST_LOG_LEVEL_DEBUG; |
| 52 | else |
| 53 | current_log_level = LTTNG_UST_LOG_LEVEL_SILENT; |
| 54 | |
| 55 | /* Initialize the log level */ |
| 56 | CMM_STORE_SHARED(lttng_ust_log_level, current_log_level); |
| 57 | |
| 58 | end: |
| 59 | return current_log_level; |
| 60 | } |