| 1 | #ifndef _USTERR_H |
| 2 | #define _USTERR_H |
| 3 | |
| 4 | /* |
| 5 | * Copyright (C) 2009 Pierre-Marc Fournier |
| 6 | * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 7 | * |
| 8 | * This library is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU Lesser General Public |
| 10 | * License as published by the Free Software Foundation; version 2.1 of |
| 11 | * the License. |
| 12 | * |
| 13 | * This library is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * Lesser General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU Lesser General Public |
| 19 | * License along with this library; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 | */ |
| 22 | |
| 23 | #include <string.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/syscall.h> |
| 26 | #include <errno.h> |
| 27 | #include <stdarg.h> |
| 28 | #include <stdio.h> |
| 29 | |
| 30 | #include <ust/core.h> |
| 31 | |
| 32 | #include "share.h" |
| 33 | |
| 34 | enum ust_loglevel { |
| 35 | UST_LOGLEVEL_UNKNOWN = 0, |
| 36 | UST_LOGLEVEL_NORMAL, |
| 37 | UST_LOGLEVEL_DEBUG, |
| 38 | }; |
| 39 | |
| 40 | extern volatile enum ust_loglevel ust_loglevel; |
| 41 | void init_usterr(void); |
| 42 | |
| 43 | static inline int ust_debug(void) |
| 44 | { |
| 45 | return ust_loglevel == UST_LOGLEVEL_DEBUG; |
| 46 | } |
| 47 | |
| 48 | #ifndef UST_COMPONENT |
| 49 | #define UST_COMPONENT libust |
| 50 | #endif |
| 51 | |
| 52 | /* To stringify the expansion of a define */ |
| 53 | #define XSTR(d) STR(d) |
| 54 | #define STR(s) #s |
| 55 | |
| 56 | #define UST_STR_COMPONENT XSTR(UST_COMPONENT) |
| 57 | |
| 58 | #define ERRMSG(fmt, args...) \ |
| 59 | do { \ |
| 60 | fprintf(stderr, UST_STR_COMPONENT "[%ld/%ld]: " fmt " (in %s() at " __FILE__ ":" XSTR(__LINE__) ")\n", \ |
| 61 | (long) getpid(), \ |
| 62 | (long) syscall(SYS_gettid), \ |
| 63 | ## args, \ |
| 64 | __func__); \ |
| 65 | } while(0) |
| 66 | |
| 67 | #ifdef UST_DEBUG |
| 68 | # define DBG(fmt, args...) ERRMSG(fmt, ## args) |
| 69 | # define DBG_raw(fmt, args...) \ |
| 70 | do { \ |
| 71 | fprintf(stderr, fmt, ## args); \ |
| 72 | } while(0) |
| 73 | #else |
| 74 | # define DBG(fmt, args...) \ |
| 75 | do { \ |
| 76 | if (ust_debug()) \ |
| 77 | ERRMSG(fmt, ## args); \ |
| 78 | } while (0) |
| 79 | # define DBG_raw(fmt, args...) \ |
| 80 | do { \ |
| 81 | if (ust_debug()) { \ |
| 82 | fprintf(stderr, fmt, ## args); \ |
| 83 | } \ |
| 84 | } while(0) |
| 85 | #endif |
| 86 | |
| 87 | #define WARN(fmt, args...) ERRMSG("Warning: " fmt, ## args) |
| 88 | #define ERR(fmt, args...) ERRMSG("Error: " fmt, ## args) |
| 89 | #define BUG(fmt, args...) ERRMSG("BUG: " fmt, ## args) |
| 90 | |
| 91 | #if (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE) |
| 92 | #define PERROR(call, args...)\ |
| 93 | do { \ |
| 94 | char buf[200] = "Error in strerror_r()"; \ |
| 95 | strerror_r(errno, buf, sizeof(buf)); \ |
| 96 | ERRMSG("Error: " call ": %s", ## args, buf); \ |
| 97 | } while(0); |
| 98 | #else |
| 99 | #define PERROR(call, args...)\ |
| 100 | do { \ |
| 101 | char *buf; \ |
| 102 | char tmp[200]; \ |
| 103 | buf = strerror_r(errno, tmp, sizeof(tmp)); \ |
| 104 | ERRMSG("Error: " call ": %s", ## args, buf); \ |
| 105 | } while(0); |
| 106 | #endif |
| 107 | |
| 108 | #define BUG_ON(condition) \ |
| 109 | do { \ |
| 110 | if (caa_unlikely(condition)) \ |
| 111 | ERR("condition not respected (BUG) on line %s:%d", __FILE__, __LINE__); \ |
| 112 | } while(0) |
| 113 | #define WARN_ON(condition) \ |
| 114 | do { \ |
| 115 | if (caa_unlikely(condition)) \ |
| 116 | WARN("condition not respected on line %s:%d", __FILE__, __LINE__); \ |
| 117 | } while(0) |
| 118 | #define WARN_ON_ONCE(condition) WARN_ON(condition) |
| 119 | |
| 120 | #endif /* _USTERR_H */ |