| 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 | |
| 26 | #ifndef _GNU_SOURCE |
| 27 | #error "lttng-tools error.h needs _GNU_SOURCE" |
| 28 | #endif |
| 29 | |
| 30 | #include <lttng/lttng-error.h> |
| 31 | |
| 32 | /* Stringify the expansion of a define */ |
| 33 | #define XSTR(d) STR(d) |
| 34 | #define STR(s) #s |
| 35 | |
| 36 | extern int lttng_opt_quiet; |
| 37 | extern int lttng_opt_verbose; |
| 38 | |
| 39 | #define PRINT_ERR 0x1 |
| 40 | #define PRINT_WARN 0x2 |
| 41 | #define PRINT_BUG 0x3 |
| 42 | #define PRINT_MSG 0x4 |
| 43 | #define PRINT_DBG 0x10 |
| 44 | #define PRINT_DBG2 0x20 |
| 45 | #define PRINT_DBG3 0x30 |
| 46 | |
| 47 | /* |
| 48 | * Macro for printing message depending on command line option and verbosity. |
| 49 | */ |
| 50 | #define __lttng_print(type, fmt, args...) \ |
| 51 | do { \ |
| 52 | if (lttng_opt_quiet == 0 && type == PRINT_MSG) { \ |
| 53 | fprintf(stdout, fmt, ## args); \ |
| 54 | } else if (lttng_opt_quiet == 0 && \ |
| 55 | (((type & PRINT_DBG) && lttng_opt_verbose == 1) || \ |
| 56 | ((type & (PRINT_DBG | PRINT_DBG2)) && \ |
| 57 | lttng_opt_verbose == 2) || \ |
| 58 | ((type & (PRINT_DBG | PRINT_DBG2 | PRINT_DBG3)) && \ |
| 59 | lttng_opt_verbose == 3))) { \ |
| 60 | fprintf(stderr, fmt, ## args); \ |
| 61 | } else if (lttng_opt_quiet == 0 && (type & (PRINT_WARN))) { \ |
| 62 | fprintf(stderr, fmt, ## args); \ |
| 63 | } else if (type & (PRINT_ERR | PRINT_BUG)) { \ |
| 64 | fprintf(stderr, fmt, ## args); \ |
| 65 | } \ |
| 66 | } while (0); |
| 67 | |
| 68 | #define MSG(fmt, args...) \ |
| 69 | __lttng_print(PRINT_MSG, fmt "\n", ## args) |
| 70 | #define _MSG(fmt, args...) \ |
| 71 | __lttng_print(PRINT_MSG, fmt, ## args) |
| 72 | #define ERR(fmt, args...) \ |
| 73 | __lttng_print(PRINT_ERR, "Error: " fmt "\n", ## args) |
| 74 | #define WARN(fmt, args...) \ |
| 75 | __lttng_print(PRINT_WARN, "Warning: " fmt "\n", ## args) |
| 76 | #define BUG(fmt, args...) \ |
| 77 | __lttng_print(PRINT_BUG, "BUG: " fmt "\n", ## args) |
| 78 | |
| 79 | /* Three level of debug. Use -v, -vv or -vvv for the levels */ |
| 80 | #define DBG(fmt, args...) __lttng_print(PRINT_DBG, "DEBUG1: " fmt \ |
| 81 | " [in %s() at " __FILE__ ":" XSTR(__LINE__) "]\n", ## args, __func__) |
| 82 | #define DBG2(fmt, args...) __lttng_print(PRINT_DBG2, "DEBUG2: " fmt \ |
| 83 | " [in %s() at " __FILE__ ":" XSTR(__LINE__) "]\n", ## args, __func__) |
| 84 | #define DBG3(fmt, args...) __lttng_print(PRINT_DBG3, "DEBUG3: " fmt \ |
| 85 | " [in %s() at " __FILE__ ":" XSTR(__LINE__) "]\n", ## args, __func__) |
| 86 | |
| 87 | #define _PERROR(fmt, args...) \ |
| 88 | __lttng_print(PRINT_ERR, "PERROR: " fmt \ |
| 89 | " [in %s() at " __FILE__ ":" XSTR(__LINE__) "]\n", ## args, __func__) |
| 90 | |
| 91 | #if !defined(__linux__) || ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE)) |
| 92 | |
| 93 | /* |
| 94 | * Version using XSI strerror_r. |
| 95 | */ |
| 96 | #define PERROR(call, args...) \ |
| 97 | do { \ |
| 98 | char buf[200]; \ |
| 99 | strerror_r(errno, buf, sizeof(buf)); \ |
| 100 | _PERROR(call ": %s", ## args, buf); \ |
| 101 | } while(0); |
| 102 | #else |
| 103 | /* |
| 104 | * Version using GNU strerror_r, for linux with appropriate defines. |
| 105 | */ |
| 106 | #define PERROR(call, args...) \ |
| 107 | do { \ |
| 108 | char *buf; \ |
| 109 | char tmp[200]; \ |
| 110 | buf = strerror_r(errno, tmp, sizeof(tmp)); \ |
| 111 | _PERROR(call ": %s", ## args, buf); \ |
| 112 | } while(0); |
| 113 | #endif |
| 114 | |
| 115 | const char *error_get_str(int32_t code); |
| 116 | |
| 117 | #endif /* _ERROR_H */ |