add missing licence headers
[ust.git] / include / usterr.h
CommitLineData
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
59b161cd
PMF
18#ifndef USTERR_H
19#define USTERR_H
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
PMF
27
28#include "share.h"
d61bfa72 29
2028e7fd
PMF
30#ifndef UST_COMPONENT
31//#error UST_COMPONENT is undefined
32#define UST_COMPONENT libust
33#endif
34
35/* To stringify the expansion of a define */
36#define XSTR(d) STR(d)
37#define STR(s) #s
38
ff1fedb9
PMF
39/* We sometimes print in the tracing path, and tracing can occur in
40 * signal handlers, so we must use a print method which is signal safe.
41 */
42
bf0d695d
PMF
43extern int ust_safe_snprintf(char *str, size_t n, const char *fmt, ...);
44
ff1fedb9
PMF
45#define sigsafe_print_err(fmt, args...) \
46{ \
47 /* Can't use dynamic allocation. Limit ourselves to 250 chars. */ \
48 char ____buf[250]; \
49 int ____saved_errno; \
50\
51 /* Save the errno. */ \
52 ____saved_errno = errno; \
53\
bf0d695d 54 ust_safe_snprintf(____buf, sizeof(____buf), fmt, ## args); \
ff1fedb9
PMF
55\
56 /* Add end of string in case of buffer overflow. */ \
57 ____buf[sizeof(____buf)-1] = 0; \
58\
59 patient_write(STDERR_FILENO, ____buf, strlen(____buf)); \
60 /* Can't print errors because we are in the error printing code path. */ \
61\
62 /* Restore errno, in order to be async-signal safe. */ \
63 errno = ____saved_errno; \
64}
65
2028e7fd
PMF
66#define UST_STR_COMPONENT XSTR(UST_COMPONENT)
67
38f015df 68#define ERRMSG(fmt, args...) do { sigsafe_print_err(UST_STR_COMPONENT "[%ld/%ld]: " fmt " (in %s() at " __FILE__ ":" XSTR(__LINE__) ")\n", (long) getpid(), (long) syscall(SYS_gettid), ## args, __func__); fflush(stderr); } while(0)
dd80aafe 69
02549014 70#ifdef UST_DEBUG
dd80aafe 71# define DBG(fmt, args...) ERRMSG(fmt, ## args)
106e716d 72# define DBG_raw(fmt, args...) do { sigsafe_print_err(fmt, ## args); fflush(stderr); } while(0)
17674885
PMF
73#else
74# define DBG(fmt, args...) do {} while(0)
106e716d 75# define DBG_raw(fmt, args...) do {} while(0)
17674885 76#endif
dd80aafe
PMF
77#define WARN(fmt, args...) ERRMSG("Warning: " fmt, ## args)
78#define ERR(fmt, args...) ERRMSG("Error: " fmt, ## args)
79#define BUG(fmt, args...) ERRMSG("BUG: " fmt, ## args)
d61bfa72
PMF
80
81#if (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE)
82#define PERROR(call, args...)\
83 do { \
84 char buf[200] = "Error in strerror_r()"; \
85 strerror_r(errno, buf, sizeof(buf)); \
dd80aafe 86 ERRMSG("Error: " call ": %s", ## args, buf); \
d61bfa72
PMF
87 } while(0);
88#else
89#define PERROR(call, args...)\
90 do { \
91 char *buf; \
92 char tmp[200]; \
93 buf = strerror_r(errno, tmp, sizeof(tmp)); \
dd80aafe 94 ERRMSG("Error: " call ": %s", ## args, buf); \
d61bfa72
PMF
95 } while(0);
96#endif
c9b64079
PMF
97
98#define BUG_ON(condition) do { if (unlikely(condition)) ERR("condition not respected (BUG)"); } while(0)
5f54827b 99#define WARN_ON(condition) do { if (unlikely(condition)) WARN("condition not respected on line %s:%d", __FILE__, __LINE__); } while(0)
d6322067 100#define WARN_ON_ONCE(condition) WARN_ON(condition)
c9b64079 101
59b161cd 102#endif /* USTERR_H */
This page took 0.030176 seconds and 4 git commands to generate.