Add critical log level
[lttng-ust.git] / src / common / logging.c
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 int lttng_ust_log_critical_action = LTTNG_UST_LOG_CRITICAL_ACTION_UNKNOWN;
14
15 /*
16 * Initialize the global log level from the "LTTNG_UST_DEBUG" environment
17 * variable.
18 *
19 * This could end up being called concurrently by multiple threads but doesn't
20 * require a mutex since the input is invariant across threads and the result
21 * will be the same.
22 */
23 static
24 void lttng_ust_logging_log_level_init(void)
25 {
26 char *lttng_ust_debug;
27 int current_log_level;
28
29 current_log_level = CMM_LOAD_SHARED(lttng_ust_log_level);
30
31 /*
32 * Check early if we are initialized, this is unlikely as it's already tested
33 * in lttng_ust_logging_debug_enabled before performing lazy initialization.
34 */
35 if (caa_unlikely(current_log_level != LTTNG_UST_LOG_LEVEL_UNKNOWN))
36 return;
37
38 /*
39 * This getenv is not part of lttng_ust_getenv() because logging is
40 * used in the getenv initialization and thus logging must be
41 * initialized prior to getenv.
42 */
43 lttng_ust_debug = getenv("LTTNG_UST_DEBUG");
44
45 /*
46 * If the LTTNG_UST_DEBUG environment variable is defined, print all
47 * messages, otherwise print nothing.
48 */
49 if (lttng_ust_debug)
50 current_log_level = LTTNG_UST_LOG_LEVEL_DEBUG;
51 else
52 current_log_level = LTTNG_UST_LOG_LEVEL_SILENT;
53
54 /* Initialize the log level */
55 CMM_STORE_SHARED(lttng_ust_log_level, current_log_level);
56 }
57
58 /*
59 * Initialize the global log critical action from the "LTTNG_UST_ABORT_ON_CRITICAL"
60 * environment variable.
61 *
62 * This could end up being called concurrently by multiple threads but doesn't
63 * require a mutex since the input is invariant across threads and the result
64 * will be the same.
65 */
66 static
67 void lttng_ust_logging_log_critical_action_init(void)
68 {
69 char *lttng_ust_abort_on_critical;
70 int current_log_critical_action;
71
72 current_log_critical_action = CMM_LOAD_SHARED(lttng_ust_log_critical_action);
73
74 /*
75 * Check early if we are initialized, this is unlikely as it's already tested
76 * in lttng_ust_logging_abort_on_critical_enabled before performing lazy initialization.
77 */
78 if (caa_unlikely(current_log_critical_action != LTTNG_UST_LOG_CRITICAL_ACTION_UNKNOWN))
79 return;
80
81 /*
82 * This getenv is not part of lttng_ust_getenv() because logging is
83 * used in the getenv initialization and thus logging must be
84 * initialized prior to getenv.
85 */
86 lttng_ust_abort_on_critical = getenv("LTTNG_UST_ABORT_ON_CRITICAL");
87
88 /*
89 * If the LTTNG_UST_ABORT_ON_CRITICAL environment variable is defined,
90 * call abort() on CRIT(), otherwise take no action.
91 */
92 if (lttng_ust_abort_on_critical)
93 current_log_critical_action = LTTNG_UST_LOG_CRITICAL_ACTION_ABORT;
94 else
95 current_log_critical_action = LTTNG_UST_LOG_CRITICAL_ACTION_NONE;
96
97 /* Initialize the log critical action */
98 CMM_STORE_SHARED(lttng_ust_log_critical_action, current_log_critical_action);
99 }
100
101 /*
102 * Initialize the global log level from the "LTTNG_UST_DEBUG" environment
103 * variable and the global log critical action from "LTTNG_UST_ABORT_ON_CRITICAL".
104 *
105 * This could end up being called concurrently by multiple threads but doesn't
106 * require a mutex since the input is invariant across threads and the result
107 * will be the same.
108 */
109 void lttng_ust_logging_init(void)
110 {
111 lttng_ust_logging_log_level_init();
112 lttng_ust_logging_log_critical_action_init();
113 }
This page took 0.031371 seconds and 4 git commands to generate.