| 1 | /* |
| 2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License, version 2 only, |
| 6 | * as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 11 | * more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along |
| 14 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 16 | */ |
| 17 | |
| 18 | #ifndef _ERROR_H |
| 19 | #define _ERROR_H |
| 20 | |
| 21 | #include <errno.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdint.h> |
| 24 | #include <string.h> |
| 25 | #include <urcu/tls-compat.h> |
| 26 | #include <time.h> |
| 27 | |
| 28 | #ifndef _GNU_SOURCE |
| 29 | #error "lttng-tools error.h needs _GNU_SOURCE" |
| 30 | #endif |
| 31 | |
| 32 | #include <lttng/lttng-error.h> |
| 33 | #include <common/compat/tid.h> |
| 34 | |
| 35 | /* Avoid conflict with Solaris <sys/regset.h> */ |
| 36 | #if defined(ERR) && defined(__sun__) |
| 37 | #undef ERR |
| 38 | #endif |
| 39 | |
| 40 | /* Stringify the expansion of a define */ |
| 41 | #define XSTR(d) STR(d) |
| 42 | #define STR(s) #s |
| 43 | |
| 44 | /* |
| 45 | * Contains the string of the log entry time. This is used as a thread local |
| 46 | * storage so we don't race between thread and also avoid memory allocation |
| 47 | * every time a log is fired. |
| 48 | */ |
| 49 | struct log_time { |
| 50 | /* Format: 00:00:00.000000 plus NULL byte. */ |
| 51 | char str[16]; |
| 52 | }; |
| 53 | extern DECLARE_URCU_TLS(struct log_time, error_log_time); |
| 54 | |
| 55 | extern int lttng_opt_quiet; |
| 56 | extern int lttng_opt_verbose; |
| 57 | extern int lttng_opt_mi; |
| 58 | |
| 59 | /* Error type. */ |
| 60 | #define PRINT_ERR (1 << 0) |
| 61 | #define PRINT_WARN (1 << 1) |
| 62 | #define PRINT_BUG (1 << 2) |
| 63 | #define PRINT_MSG (1 << 3) |
| 64 | #define PRINT_DBG (1 << 4) |
| 65 | #define PRINT_DBG2 (1 << 5) |
| 66 | #define PRINT_DBG3 (1 << 6) |
| 67 | |
| 68 | /* |
| 69 | * Macro for printing message depending on command line option and verbosity. |
| 70 | * |
| 71 | * Machine interface: |
| 72 | * We use lttng_opt_mi to suppress all normal msg to stdout. We don't |
| 73 | * want any nested msg to show up when printing mi to stdout(if it's the case). |
| 74 | * All warnings and errors should be printed to stderr as normal. |
| 75 | */ |
| 76 | #define __lttng_print(type, fmt, args...) \ |
| 77 | do { \ |
| 78 | if (!lttng_opt_quiet && !lttng_opt_mi && \ |
| 79 | (type) == PRINT_MSG) { \ |
| 80 | fprintf(stdout, fmt, ## args); \ |
| 81 | } else if (!lttng_opt_quiet && !lttng_opt_mi && \ |
| 82 | ((((type) & PRINT_DBG) && lttng_opt_verbose == 1) || \ |
| 83 | (((type) & (PRINT_DBG | PRINT_DBG2)) && \ |
| 84 | lttng_opt_verbose == 2) || \ |
| 85 | (((type) & (PRINT_DBG | PRINT_DBG2 | PRINT_DBG3)) && \ |
| 86 | lttng_opt_verbose == 3))) { \ |
| 87 | fprintf(stderr, fmt, ## args); \ |
| 88 | } else if (!lttng_opt_quiet && \ |
| 89 | ((type) & (PRINT_WARN | PRINT_ERR | PRINT_BUG))) { \ |
| 90 | fprintf(stderr, fmt, ## args); \ |
| 91 | } \ |
| 92 | if ((type) & (PRINT_ERR | PRINT_BUG)) { \ |
| 93 | lttng_abort_on_error(); \ |
| 94 | } \ |
| 95 | } while (0); |
| 96 | |
| 97 | /* Three level of debug. Use -v, -vv or -vvv for the levels */ |
| 98 | #define _ERRMSG(msg, type, fmt, args...) __lttng_print(type, msg \ |
| 99 | " - %s [%ld/%ld]: " fmt " (in %s() at " __FILE__ ":" XSTR(__LINE__) ")\n", \ |
| 100 | log_add_time(), (long) getpid(), (long) gettid(), ## args, __func__) |
| 101 | |
| 102 | #define MSG(fmt, args...) \ |
| 103 | __lttng_print(PRINT_MSG, fmt "\n", ## args) |
| 104 | #define _MSG(fmt, args...) \ |
| 105 | __lttng_print(PRINT_MSG, fmt, ## args) |
| 106 | #define ERR(fmt, args...) \ |
| 107 | __lttng_print(PRINT_ERR, "Error: " fmt "\n", ## args) |
| 108 | #define WARN(fmt, args...) \ |
| 109 | __lttng_print(PRINT_WARN, "Warning: " fmt "\n", ## args) |
| 110 | |
| 111 | #define BUG(fmt, args...) _ERRMSG("BUG", PRINT_BUG, fmt, ## args) |
| 112 | |
| 113 | #define DBG(fmt, args...) _ERRMSG("DEBUG1", PRINT_DBG, fmt, ## args) |
| 114 | #define DBG2(fmt, args...) _ERRMSG("DEBUG2", PRINT_DBG2, fmt, ## args) |
| 115 | #define DBG3(fmt, args...) _ERRMSG("DEBUG3", PRINT_DBG3, fmt, ## args) |
| 116 | #define LOG(type, fmt, args...) \ |
| 117 | do { \ |
| 118 | switch (type) { \ |
| 119 | case PRINT_ERR: \ |
| 120 | ERR(fmt, ## args); \ |
| 121 | break; \ |
| 122 | case PRINT_WARN: \ |
| 123 | WARN(fmt, ## args); \ |
| 124 | break; \ |
| 125 | case PRINT_BUG: \ |
| 126 | BUG(fmt, ## args); \ |
| 127 | break; \ |
| 128 | case PRINT_MSG: \ |
| 129 | MSG(fmt, ## args); \ |
| 130 | break; \ |
| 131 | case PRINT_DBG: \ |
| 132 | DBG(fmt, ## args); \ |
| 133 | break; \ |
| 134 | case PRINT_DBG2: \ |
| 135 | DBG2(fmt, ## args); \ |
| 136 | break; \ |
| 137 | case PRINT_DBG3: \ |
| 138 | DBG3(fmt, ## args); \ |
| 139 | break; \ |
| 140 | default: \ |
| 141 | assert(0); \ |
| 142 | } \ |
| 143 | } while(0); |
| 144 | |
| 145 | #define _PERROR(fmt, args...) _ERRMSG("PERROR", PRINT_ERR, fmt, ## args) |
| 146 | |
| 147 | #if !defined(__linux__) || ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE)) |
| 148 | |
| 149 | /* |
| 150 | * Version using XSI strerror_r. |
| 151 | */ |
| 152 | #define PERROR(call, args...) \ |
| 153 | do { \ |
| 154 | char buf[200]; \ |
| 155 | strerror_r(errno, buf, sizeof(buf)); \ |
| 156 | _PERROR(call ": %s", ## args, buf); \ |
| 157 | } while(0); |
| 158 | #else |
| 159 | /* |
| 160 | * Version using GNU strerror_r, for linux with appropriate defines. |
| 161 | */ |
| 162 | #define PERROR(call, args...) \ |
| 163 | do { \ |
| 164 | char *buf; \ |
| 165 | char tmp[200]; \ |
| 166 | buf = strerror_r(errno, tmp, sizeof(tmp)); \ |
| 167 | _PERROR(call ": %s", ## args, buf); \ |
| 168 | } while(0); |
| 169 | #endif |
| 170 | |
| 171 | const char *error_get_str(int32_t code); |
| 172 | |
| 173 | /* |
| 174 | * Function that format the time and return the reference of log_time.str to |
| 175 | * the caller. On error, an empty string is returned thus no time will be |
| 176 | * printed in the log. |
| 177 | */ |
| 178 | const char *log_add_time(); |
| 179 | |
| 180 | void lttng_abort_on_error(void); |
| 181 | |
| 182 | #endif /* _ERROR_H */ |