Created
June 4, 2011 06:42
-
-
Save OrangeTide/1007672 to your computer and use it in GitHub Desktop.
some portable vararg debug macros based on an old example I saw
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* a complicated set of macros to support empty __VA_ARGS__ macros. | |
* example: | |
* pr_dbg("Check\n"); | |
* pr_dbg("Check %d\n", 12); | |
* pr_dbg("Check %d %s %d\n", 42, "Hello", 99); | |
* | |
* pr_info() and pr_err() macros don't use __func__ and __LINE__ | |
*/ | |
#ifndef DEBUG_H | |
#define DEBUG_H | |
#include <stdio.h> | |
#define CONCAT(x, y) CONCAT_(x, y) | |
#define CONCAT_(x, y) x##y | |
#define ID(...) __VA_ARGS__ | |
#define IFMULTIARG(if,then,else) \ | |
CONCAT(IFMULTIARG_, IFMULTIARG_(if, \ | |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \ | |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \ | |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \ | |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \ | |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \ | |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \ | |
1, 1, 0, ))(then,else) | |
#define IFMULTIARG_(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, \ | |
_10, _11, _12, _13, _14, _15, _16, _17, _18, _19, \ | |
_20, _21, _22, _23, _24, _25, _26, _27, _28, _29, \ | |
_30, _31, _32, _33, _34, _35, _36, _37, _38, _39, \ | |
_40, _41, _42, _43, _44, _45, _46, _47, _48, _49, \ | |
_50, _51, _52, _53, _54, _55, _56, _57, _58, _59, \ | |
_60, _61, _62, _63, ...) _63 | |
#define IFMULTIARG_0(then, else) else | |
#define IFMULTIARG_1(then, else) then | |
#define PROVIDE_SECOND_ARGUMENT(x, y, ...) \ | |
CONCAT(IFMULTIARG(ID(__VA_ARGS__), INSERT_, ADD_), \ | |
SECOND_ARGUMENT) (x, y, __VA_ARGS__) | |
#define ADD_SECOND_ARGUMENT(x, y, z) z, x, y | |
#define INSERT_SECOND_ARGUMENT(x, y, z, ...) z, x, y, __VA_ARGS__ | |
#define pr_err(...) fprintf(stderr, "Error:" __VA_ARGS__) | |
#define pr_info(...) fprintf(stderr, "Info:" __VA_ARGS__) | |
#define pr_dbg(...) fprintf(stderr, "Debug:%s():%d:" PROVIDE_SECOND_ARGUMENT(__func__, __LINE__, __VA_ARGS__)) | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment