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