Commit | Line | Data |
---|---|---|
a09dac63 PMF |
1 | /* Copyright (C) 2009 Pierre-Marc Fournier |
2 | * | |
3 | * This library is free software; you can redistribute it and/or | |
4 | * modify it under the terms of the GNU Lesser General Public | |
5 | * License as published by the Free Software Foundation; either | |
6 | * version 2.1 of the License, or (at your option) any later version. | |
7 | * | |
8 | * This library is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
11 | * Lesser General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU Lesser General Public | |
14 | * License along with this library; if not, write to the Free Software | |
15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
16 | */ | |
17 | ||
30ffe279 NC |
18 | #ifndef _USTERR_H |
19 | #define _USTERR_H | |
59b161cd | 20 | |
d61bfa72 | 21 | #include <string.h> |
dd80aafe PMF |
22 | #include <sys/types.h> |
23 | #include <sys/syscall.h> | |
24 | #include <errno.h> | |
ff1fedb9 | 25 | #include <stdarg.h> |
c1f20530 | 26 | #include <stdio.h> |
ff1fedb9 | 27 | |
518d7abb PMF |
28 | #include <ust/core.h> |
29 | ||
ff1fedb9 | 30 | #include "share.h" |
d61bfa72 | 31 | |
2028e7fd | 32 | #ifndef UST_COMPONENT |
2028e7fd PMF |
33 | #define UST_COMPONENT libust |
34 | #endif | |
35 | ||
36 | /* To stringify the expansion of a define */ | |
37 | #define XSTR(d) STR(d) | |
38 | #define STR(s) #s | |
39 | ||
30ffe279 | 40 | /* A dummy function to force format checking */ |
d9d1c081 MD |
41 | static inline void __attribute__ ((format (printf, 1, 2))) |
42 | __check_ust_safe_fmt(const char *fmt, ...) | |
43 | { | |
44 | } | |
45 | ||
2028e7fd PMF |
46 | #define UST_STR_COMPONENT XSTR(UST_COMPONENT) |
47 | ||
30ffe279 | 48 | #define ERRMSG(fmt, args...) do { fprintf(stderr, UST_STR_COMPONENT "[%ld/%ld]: " fmt " (in %s() at " __FILE__ ":" XSTR(__LINE__) ")\n", (long) getpid(), (long) syscall(SYS_gettid), ## args, __func__); } while(0) |
dd80aafe | 49 | |
02549014 | 50 | #ifdef UST_DEBUG |
dd80aafe | 51 | # define DBG(fmt, args...) ERRMSG(fmt, ## args) |
30ffe279 | 52 | # define DBG_raw(fmt, args...) do { fprintf(stderr, fmt, ## args); } while(0) |
17674885 | 53 | #else |
d9d1c081 MD |
54 | # define DBG(fmt, args...) __check_ust_safe_fmt(fmt, ## args) |
55 | # define DBG_raw(fmt, args...) __check_ust_safe_fmt(fmt, ## args) | |
17674885 | 56 | #endif |
dd80aafe PMF |
57 | #define WARN(fmt, args...) ERRMSG("Warning: " fmt, ## args) |
58 | #define ERR(fmt, args...) ERRMSG("Error: " fmt, ## args) | |
59 | #define BUG(fmt, args...) ERRMSG("BUG: " fmt, ## args) | |
d61bfa72 PMF |
60 | |
61 | #if (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE) | |
62 | #define PERROR(call, args...)\ | |
63 | do { \ | |
64 | char buf[200] = "Error in strerror_r()"; \ | |
65 | strerror_r(errno, buf, sizeof(buf)); \ | |
dd80aafe | 66 | ERRMSG("Error: " call ": %s", ## args, buf); \ |
d61bfa72 PMF |
67 | } while(0); |
68 | #else | |
69 | #define PERROR(call, args...)\ | |
70 | do { \ | |
71 | char *buf; \ | |
72 | char tmp[200]; \ | |
73 | buf = strerror_r(errno, tmp, sizeof(tmp)); \ | |
dd80aafe | 74 | ERRMSG("Error: " call ": %s", ## args, buf); \ |
d61bfa72 PMF |
75 | } while(0); |
76 | #endif | |
c9b64079 PMF |
77 | |
78 | #define BUG_ON(condition) do { if (unlikely(condition)) ERR("condition not respected (BUG)"); } while(0) | |
5f54827b | 79 | #define WARN_ON(condition) do { if (unlikely(condition)) WARN("condition not respected on line %s:%d", __FILE__, __LINE__); } while(0) |
d6322067 | 80 | #define WARN_ON_ONCE(condition) WARN_ON(condition) |
c9b64079 | 81 | |
30ffe279 | 82 | #endif /* _USTERR_H */ |