| 1 | /* |
| 2 | * SPDX-License-Identifier: LGPL-2.1-only |
| 3 | * |
| 4 | * Copyright (C) 2009 Pierre-Marc Fournier |
| 5 | * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 6 | */ |
| 7 | |
| 8 | #ifndef _UST_COMMON_LOGGING_H |
| 9 | #define _UST_COMMON_LOGGING_H |
| 10 | |
| 11 | #include <string.h> |
| 12 | #include <sys/types.h> |
| 13 | #include <errno.h> |
| 14 | #include <stdarg.h> |
| 15 | #include <stdbool.h> |
| 16 | #include <stdio.h> |
| 17 | |
| 18 | #include <lttng/ust-utils.h> |
| 19 | |
| 20 | #include "common/patient.h" |
| 21 | #include "common/compat/tid.h" |
| 22 | #include "common/safe-snprintf.h" |
| 23 | |
| 24 | enum lttng_ust_log_level { |
| 25 | LTTNG_UST_LOG_LEVEL_UNKNOWN = 0, |
| 26 | LTTNG_UST_LOG_LEVEL_NORMAL, |
| 27 | LTTNG_UST_LOG_LEVEL_DEBUG, |
| 28 | }; |
| 29 | |
| 30 | extern volatile enum lttng_ust_log_level lttng_ust_log_level |
| 31 | __attribute__((visibility("hidden"))); |
| 32 | |
| 33 | void lttng_ust_logging_init(void) |
| 34 | __attribute__((visibility("hidden"))); |
| 35 | |
| 36 | #ifdef LTTNG_UST_DEBUG |
| 37 | static inline bool lttng_ust_logging_debug_enabled(void) |
| 38 | { |
| 39 | return true; |
| 40 | } |
| 41 | #else /* #ifdef LTTNG_UST_DEBUG */ |
| 42 | static inline bool lttng_ust_logging_debug_enabled(void) |
| 43 | { |
| 44 | return lttng_ust_log_level == LTTNG_UST_LOG_LEVEL_DEBUG; |
| 45 | } |
| 46 | #endif /* #else #ifdef LTTNG_UST_DEBUG */ |
| 47 | |
| 48 | /* |
| 49 | * The default component for error messages. |
| 50 | */ |
| 51 | #ifndef UST_COMPONENT |
| 52 | #define UST_COMPONENT libust |
| 53 | #endif |
| 54 | |
| 55 | #define LTTNG_UST_LOG_MAX_LEN 512 |
| 56 | |
| 57 | /* |
| 58 | * We sometimes print in the tracing path, and tracing can occur in |
| 59 | * signal handlers, so we must use a print method which is signal safe. |
| 60 | */ |
| 61 | /* Can't use dynamic allocation. Limit ourselves to LTTNG_UST_LOG_MAX_LEN chars. */ |
| 62 | /* Add end of string in case of buffer overflow. */ |
| 63 | #define sigsafe_print_err(fmt, args...) \ |
| 64 | do { \ |
| 65 | if (lttng_ust_logging_debug_enabled()) { \ |
| 66 | char ____buf[LTTNG_UST_LOG_MAX_LEN]; \ |
| 67 | int ____saved_errno; \ |
| 68 | \ |
| 69 | ____saved_errno = errno; /* signal-safety */ \ |
| 70 | ust_safe_snprintf(____buf, sizeof(____buf), fmt, ## args); \ |
| 71 | ____buf[sizeof(____buf) - 1] = 0; \ |
| 72 | ust_patient_write(STDERR_FILENO, ____buf, strlen(____buf)); \ |
| 73 | errno = ____saved_errno; /* signal-safety */ \ |
| 74 | fflush(stderr); \ |
| 75 | } \ |
| 76 | } while (0) |
| 77 | |
| 78 | #define LTTNG_UST_STR_COMPONENT lttng_ust_stringify(UST_COMPONENT) |
| 79 | |
| 80 | #define ERRMSG(fmt, args...) \ |
| 81 | do { \ |
| 82 | sigsafe_print_err(LTTNG_UST_STR_COMPONENT "[%ld/%ld]: " fmt " (in %s() at " __FILE__ ":" lttng_ust_stringify(__LINE__) ")\n", \ |
| 83 | (long) getpid(), \ |
| 84 | (long) lttng_gettid(), \ |
| 85 | ## args, __func__); \ |
| 86 | } while(0) |
| 87 | |
| 88 | |
| 89 | #define DBG(fmt, args...) ERRMSG(fmt, ## args) |
| 90 | #define DBG_raw(fmt, args...) sigsafe_print_err(fmt, ## args) |
| 91 | #define WARN(fmt, args...) ERRMSG("Warning: " fmt, ## args) |
| 92 | #define ERR(fmt, args...) ERRMSG("Error: " fmt, ## args) |
| 93 | #define BUG(fmt, args...) ERRMSG("BUG: " fmt, ## args) |
| 94 | |
| 95 | #if !defined(__GLIBC__) || ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE)) |
| 96 | /* |
| 97 | * Version using XSI strerror_r. |
| 98 | */ |
| 99 | #define PERROR(call, args...) \ |
| 100 | do { \ |
| 101 | if (lttng_ust_logging_debug_enabled()) { \ |
| 102 | char perror_buf[200] = "Error in strerror_r()"; \ |
| 103 | strerror_r(errno, perror_buf, \ |
| 104 | sizeof(perror_buf)); \ |
| 105 | ERRMSG("Error: " call ": %s", ## args, \ |
| 106 | perror_buf); \ |
| 107 | } \ |
| 108 | } while(0) |
| 109 | #else |
| 110 | /* |
| 111 | * Version using GNU strerror_r, for linux with appropriate defines. |
| 112 | */ |
| 113 | #define PERROR(call, args...) \ |
| 114 | do { \ |
| 115 | if (lttng_ust_logging_debug_enabled()) { \ |
| 116 | char *perror_buf; \ |
| 117 | char perror_tmp[200]; \ |
| 118 | perror_buf = strerror_r(errno, perror_tmp, \ |
| 119 | sizeof(perror_tmp)); \ |
| 120 | ERRMSG("Error: " call ": %s", ## args, \ |
| 121 | perror_buf); \ |
| 122 | } \ |
| 123 | } while(0) |
| 124 | #endif |
| 125 | |
| 126 | #define BUG_ON(condition) \ |
| 127 | do { \ |
| 128 | if (caa_unlikely(condition)) \ |
| 129 | ERR("condition not respected (BUG) on line %s:%d", __FILE__, __LINE__); \ |
| 130 | } while(0) |
| 131 | #define WARN_ON(condition) \ |
| 132 | do { \ |
| 133 | if (caa_unlikely(condition)) \ |
| 134 | WARN("condition not respected on line %s:%d", __FILE__, __LINE__); \ |
| 135 | } while(0) |
| 136 | #define WARN_ON_ONCE(condition) WARN_ON(condition) |
| 137 | |
| 138 | #endif /* _UST_COMMON_LOGGING_H */ |