X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=include%2Flttngerr.h;h=b14f23218526e4843dc2131bfc1aee76fc8232ed;hp=5c3d8c4fb824a8e877d7e445e6fac389cd5bf772;hb=1fea938d0d3adc783bb49d7ff3d0686ba87b0520;hpb=fac6795d6e2c60e79bdc7dab42da94d2025a57d3;ds=sidebyside diff --git a/include/lttngerr.h b/include/lttngerr.h index 5c3d8c4fb..b14f23218 100644 --- a/include/lttngerr.h +++ b/include/lttngerr.h @@ -1,9 +1,10 @@ -/* Copyright (C) 2011 - David Goulet +/* + * Copyright (C) 2011 - David Goulet * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. + * as published by the Free Software Foundation; only version 2 + * of the License. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -13,52 +14,75 @@ * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * */ #ifndef _LTTNGERR_H #define _LTTNGERR_H #include -#include +#include + +/* Stringify the expansion of a define */ +#define XSTR(d) STR(d) +#define STR(s) #s extern int opt_quiet; extern int opt_verbose; -enum __lttng_print_type { - PRINT_ERR, - PRINT_WARN, - PRINT_BUG, - PRINT_DBG, - PRINT_MSG, -}; +#define PRINT_ERR 0x1 +#define PRINT_WARN 0x2 +#define PRINT_BUG 0x3 +#define PRINT_MSG 0x4 +#define PRINT_DBG 0x10 +#define PRINT_DBG2 0x20 +#define PRINT_DBG3 0x30 /* - * __lttng_print - * - * Internal function for printing message - * depending on command line option and verbosity. + * Macro for printing message depending on command line option and verbosity. */ -void __lttng_print(enum __lttng_print_type type, const char *fmt, ...) -{ - va_list ap; - va_start(ap, fmt); +#define __lttng_print(type, fmt, args...) \ + do { \ + if (opt_quiet == 0) { \ + if (type == PRINT_MSG) { \ + fprintf(stdout, fmt, ## args); \ + } else if (((type & PRINT_DBG) && opt_verbose == 1) || \ + ((type & (PRINT_DBG | PRINT_DBG2)) && \ + opt_verbose == 2) || \ + ((type & (PRINT_DBG | PRINT_DBG2 | PRINT_DBG3)) && \ + opt_verbose == 3)) { \ + fprintf(stderr, fmt, ## args); \ + } else if (type & (PRINT_ERR | PRINT_WARN | PRINT_BUG)) { \ + fprintf(stderr, fmt, ## args); \ + } \ + } \ + } while (0); + +#define MSG(fmt, args...) \ + __lttng_print(PRINT_MSG, fmt "\n", ## args) +#define ERR(fmt, args...) \ + __lttng_print(PRINT_ERR, "Error: " fmt "\n", ## args) +#define WARN(fmt, args...) \ + __lttng_print(PRINT_WARN, "Warning: " fmt "\n", ## args) +#define BUG(fmt, args...) \ + __lttng_print(PRINT_BUG, "BUG: " fmt "\n", ## args) - if (opt_quiet == 0) { - if (type == PRINT_MSG || (opt_verbose && type == PRINT_DBG)) { - vfprintf(stdout, fmt, ap); - } else if (type != PRINT_MSG && type != PRINT_DBG) { - vfprintf(stderr, fmt, ap); - } - } +/* Three level of debug. Use -v, -vv or -vvv for the levels */ +#define DBG(fmt, args...) __lttng_print(PRINT_DBG, "DEBUG1: " fmt \ + " [in %s() at " __FILE__ ":" XSTR(__LINE__) "]\n", ## args, __func__) +#define DBG2(fmt, args...) __lttng_print(PRINT_DBG2, "DEBUG2: " fmt \ + " [in %s() at " __FILE__ ":" XSTR(__LINE__) "]\n", ## args, __func__) +#define DBG3(fmt, args...) __lttng_print(PRINT_DBG3, "DEBUG3: " fmt \ + " [in %s() at " __FILE__ ":" XSTR(__LINE__) "]\n", ## args, __func__) - va_end(ap); -} +#define _PERROR(fmt, args...) \ + __lttng_print(PRINT_ERR, "perror " fmt "\n", ## args) -#define MSG(fmt, args...) __lttng_print(PRINT_MSG, fmt "\n", ## args) -#define ERR(fmt, args...) __lttng_print(PRINT_ERR, "Error: " fmt "\n", ## args) -#define WARN(fmt, args...) __lttng_print(PRINT_WARN, "Warning: " fmt "\n", ## args) -#define BUG(fmt, args...) __lttng_print(PRINT_BUG, "BUG: " fmt "\n", ## args) -#define DBG(fmt, args...) __lttng_print(PRINT_DBG, "DEBUG: " fmt "\n", ## args) +#define PERROR(call, args...) \ + do { \ + char *buf; \ + char tmp[200]; \ + buf = strerror_r(errno, tmp, sizeof(tmp)); \ + _PERROR(call ": %s", ## args, buf); \ + } while(0); #endif /* _LTTNGERR_H */