X-Git-Url: http://git.lttng.org./?a=blobdiff_plain;f=src%2Fcommon%2Flogging.h;h=5ebf17bf2900773f05b0e2e82d09bff9e3697969;hb=5fb2fe0679458e65ebdd52b69d6359c082664ce6;hp=4001c8852b90aabde67035d942506697ed321336;hpb=9d315d6d74aac2986b10d708c864d152a0febec7;p=lttng-ust.git diff --git a/src/common/logging.h b/src/common/logging.h index 4001c885..5ebf17bf 100644 --- a/src/common/logging.h +++ b/src/common/logging.h @@ -5,8 +5,8 @@ * Copyright (C) 2011 Mathieu Desnoyers */ -#ifndef _USTERR_SIGNAL_SAFE_H -#define _USTERR_SIGNAL_SAFE_H +#ifndef _UST_COMMON_LOGGING_H +#define _UST_COMMON_LOGGING_H #include #include @@ -14,33 +14,59 @@ #include #include #include + +#include +#include +#include + #include "common/patient.h" #include "common/compat/tid.h" #include "common/safe-snprintf.h" -enum ust_err_loglevel { - UST_ERR_LOGLEVEL_UNKNOWN = 0, - UST_ERR_LOGLEVEL_NORMAL, - UST_ERR_LOGLEVEL_DEBUG, +enum lttng_ust_log_level { + LTTNG_UST_LOG_LEVEL_UNKNOWN = 0, + LTTNG_UST_LOG_LEVEL_SILENT, + LTTNG_UST_LOG_LEVEL_DEBUG, }; -extern volatile enum ust_err_loglevel ust_err_loglevel +extern int lttng_ust_log_level /* enum lttng_ust_log_level */ __attribute__((visibility("hidden"))); -void ust_err_init(void) +/* + * Initialize the global log level from the "LTTNG_UST_DEBUG" environment + * variable. + * + * This could end up being called concurrently 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) __attribute__((visibility("hidden"))); #ifdef LTTNG_UST_DEBUG -static inline bool ust_err_debug_enabled(void) +static inline +bool lttng_ust_logging_debug_enabled(void) { return true; } #else /* #ifdef LTTNG_UST_DEBUG */ -static inline bool ust_err_debug_enabled(void) +static inline +bool lttng_ust_logging_debug_enabled(void) { - return ust_err_loglevel == UST_ERR_LOGLEVEL_DEBUG; + int current_log_level; + + current_log_level = CMM_LOAD_SHARED(lttng_ust_log_level); + + /* If the global log level is unknown, lazy-initialize it. */ + if (caa_unlikely(current_log_level == LTTNG_UST_LOG_LEVEL_UNKNOWN)) + current_log_level = lttng_ust_logging_init(); + + return current_log_level == LTTNG_UST_LOG_LEVEL_DEBUG; } -#endif /* #else #ifdef LTTNG_UST_DEBUG */ +#endif /* #ifdef LTTNG_UST_DEBUG */ /* * The default component for error messages. @@ -49,22 +75,18 @@ static inline bool ust_err_debug_enabled(void) #define UST_COMPONENT libust #endif -/* To stringify the expansion of a define */ -#define UST_XSTR(d) UST_STR(d) -#define UST_STR(s) #s - -#define UST_ERR_MAX_LEN 512 +#define LTTNG_UST_LOG_MAX_LEN 512 /* * We sometimes print in the tracing path, and tracing can occur in * signal handlers, so we must use a print method which is signal safe. */ -/* Can't use dynamic allocation. Limit ourselves to UST_ERR_MAX_LEN chars. */ +/* Can't use dynamic allocation. Limit ourselves to LTTNG_UST_LOG_MAX_LEN chars. */ /* Add end of string in case of buffer overflow. */ #define sigsafe_print_err(fmt, args...) \ do { \ - if (ust_err_debug_enabled()) { \ - char ____buf[UST_ERR_MAX_LEN]; \ + if (lttng_ust_logging_debug_enabled()) { \ + char ____buf[LTTNG_UST_LOG_MAX_LEN]; \ int ____saved_errno; \ \ ____saved_errno = errno; /* signal-safety */ \ @@ -76,11 +98,11 @@ do { \ } \ } while (0) -#define UST_STR_COMPONENT UST_XSTR(UST_COMPONENT) +#define LTTNG_UST_STR_COMPONENT lttng_ust_stringify(UST_COMPONENT) #define ERRMSG(fmt, args...) \ do { \ - sigsafe_print_err(UST_STR_COMPONENT "[%ld/%ld]: " fmt " (in %s() at " __FILE__ ":" UST_XSTR(__LINE__) ")\n", \ + sigsafe_print_err(LTTNG_UST_STR_COMPONENT "[%ld/%ld]: " fmt " (in %s() at " __FILE__ ":" lttng_ust_stringify(__LINE__) ")\n", \ (long) getpid(), \ (long) lttng_gettid(), \ ## args, __func__); \ @@ -99,7 +121,7 @@ do { \ */ #define PERROR(call, args...) \ do { \ - if (ust_err_debug_enabled()) { \ + if (lttng_ust_logging_debug_enabled()) { \ char perror_buf[200] = "Error in strerror_r()"; \ strerror_r(errno, perror_buf, \ sizeof(perror_buf)); \ @@ -113,7 +135,7 @@ do { \ */ #define PERROR(call, args...) \ do { \ - if (ust_err_debug_enabled()) { \ + if (lttng_ust_logging_debug_enabled()) { \ char *perror_buf; \ char perror_tmp[200]; \ perror_buf = strerror_r(errno, perror_tmp, \ @@ -136,4 +158,4 @@ do { \ } while(0) #define WARN_ON_ONCE(condition) WARN_ON(condition) -#endif /* _USTERR_SIGNAL_SAFE_H */ +#endif /* _UST_COMMON_LOGGING_H */