Skip to content

Instantly share code, notes, and snippets.

Created November 23, 2015 21:58
Show Gist options
  • Save anonymous/52b02b84a914a29036bb to your computer and use it in GitHub Desktop.
Save anonymous/52b02b84a914a29036bb to your computer and use it in GitHub Desktop.
#pragma once
// http://stackoverflow.com/questions/11761703/overloading-macro-on-number-of-arguments/11763277
// http://codecraft.co/2014/11/25/variadic-macros-tricks/
#define _BB_GET_NTH_ARG(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...) N
#define BB_COUNT_VARARGS(...) _BB_GET_NTH_ARG("ignored", ##__VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
#define _BB_ENUM_10(x, ...) idx_ ##x, _BB_ENUM_9(__VA_ARGS__)\
#define _BB_ENUM_9(x, ...) idx_ ##x, _BB_ENUM_8(__VA_ARGS__)\
#define _BB_ENUM_8(x, ...) idx_ ##x, _BB_ENUM_7(__VA_ARGS__)\
#define _BB_ENUM_7(x, ...) idx_ ##x, _BB_ENUM_6(__VA_ARGS__)\
#define _BB_ENUM_6(x, ...) idx_ ##x, _BB_ENUM_5(__VA_ARGS__)\
#define _BB_ENUM_5(x, ...) idx_ ##x, _BB_ENUM_4(__VA_ARGS__)\
#define _BB_ENUM_4(x, ...) idx_ ##x, _BB_ENUM_3(__VA_ARGS__)\
#define _BB_ENUM_3(x, ...) idx_ ##x, _BB_ENUM_2(__VA_ARGS__)\
#define _BB_ENUM_2(x, ...) idx_ ##x, _BB_ENUM_1(__VA_ARGS__)\
#define _BB_ENUM_1(x) idx_ ##x\
#define BB_MACRO_STR(x) BB_STRINGIFY(x)
#define BB_STRINGIFY(x) #x
#define _BB_STR_10(x, ...) BB_STRINGIFY(x), _BB_STR_9(__VA_ARGS__)\
#define _BB_STR_9(x, ...) BB_STRINGIFY(x), _BB_STR_8(__VA_ARGS__)\
#define _BB_STR_8(x, ...) BB_STRINGIFY(x), _BB_STR_7(__VA_ARGS__)\
#define _BB_STR_7(x, ...) BB_STRINGIFY(x), _BB_STR_6(__VA_ARGS__)\
#define _BB_STR_6(x, ...) BB_STRINGIFY(x), _BB_STR_5(__VA_ARGS__)\
#define _BB_STR_5(x, ...) BB_STRINGIFY(x), _BB_STR_4(__VA_ARGS__)\
#define _BB_STR_4(x, ...) BB_STRINGIFY(x), _BB_STR_3(__VA_ARGS__)\
#define _BB_STR_3(x, ...) BB_STRINGIFY(x), _BB_STR_2(__VA_ARGS__)\
#define _BB_STR_2(x, ...) BB_STRINGIFY(x), _BB_STR_1(__VA_ARGS__)\
#define _BB_STR_1(x) BB_STRINGIFY(x)\
#define _BB_ENUM_ITEMS(...) _BB_GET_NTH_ARG(__VA_ARGS__, _BB_ENUM_10, _BB_ENUM_9, _BB_ENUM_8, _BB_ENUM_7, _BB_ENUM_6, _BB_ENUM_5, _BB_ENUM_4, _BB_ENUM_3, _BB_ENUM_2, _BB_ENUM_1)(__VA_ARGS__)
#define _BB_STR_ARRAY(...) _BB_GET_NTH_ARG(__VA_ARGS__, _BB_STR_10, _BB_STR_9, _BB_STR_8, _BB_STR_7, _BB_STR_6, _BB_STR_5, _BB_STR_4, _BB_STR_3, _BB_STR_2, _BB_STR_1)(__VA_ARGS__)
#define BB_ENUM(NAME, ...) enum NAME\
{\
_BB_ENUM_ITEMS(__VA_ARGS__)\
, idx_ ##NAME ##_count\
};\
const char* NAME ## _strings[] = {\
_BB_STR_ARRAY(__VA_ARGS__)\
}
#pragma once
#include <stdio.h>
#if defined(BB_NO_LOG)
#define BB_LOG(format, ...)
#define BB_LOG_DEBUG(format, ...)
#define BB_LOG_ERROR(format, ...)
#else
/****************
* from http://ascii-table.com/ansi-escape-sequences.php :
* Text Attributes:
* 0 All attributes off
* 1 Bold on
* 4 Underscore (on monochrome display adapter only)
* 5 Blink on
* 7 Reverse video on
* 8 Concealed on
*
* Foreground colors
* 30 Black
* 31 Red
* 32 Green
* 33 Yellow
* 34 Blue
* 35 Magenta
* 36 Cyan
* 37 White
*
* Background colors
* 40 Black
* 41 Red
* 42 Green
* 43 Yellow
* 44 Blue
* 45 Magenta
* 46 Cyan
* 47 White
****************/
// "default"
#define BB_COL_DEFAULT "\x1B[0m"
#define BB_COL_RED "\x1B[31m"
#define BB_COL_GREEN "\x1B[32m"
#define BB_COL_YELLOW "\x1B[33m"
#define BB_COL_BLUE "\x1B[34m"
#define BB_COL_MAGENTA "\x1B[35m"
#define BB_COL_CYAN "\x1B[36m"
#define BB_COL_WHITE "\x1B[37m"
// bold
#define BB_COL_BRED "\x1B[31;1m"
#define BB_COL_BGREEN "\x1B[32;1m"
#define BB_COL_BYELLOW "\x1B[33;1m"
#define BB_COL_BBLUE "\x1B[34;1m"
#define BB_COL_BMAGENTA "\x1B[35;1m"
#define BB_COL_BCYAN "\x1B[36;1m"
#define BB_COL_BWHITE "\x1B[37;1m"
// special colour for asserts: black text on red background
#define BB_COL_ASSERT "\x1B[31;1;7m"
#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
#define BB_FUNCTION_LEN_STR "%-23s"
#define BB_LOG(format, ...) fprintf(stdout, format, ##__VA_ARGS__)
#define BB_LOG_DEBUG(format, ...) fprintf(stdout, "[" BB_COL_BBLUE "DEBUG " BB_COL_DEFAULT "]%s:%d [" BB_COL_CYAN BB_FUNCTION_LEN_STR BB_COL_DEFAULT "] "format, __FILENAME__, __LINE__, __FUNCTION__, ##__VA_ARGS__)
#define BB_LOG_WARNING(format, ...) fprintf(stderr, "[" BB_COL_BYELLOW "WARNING" BB_COL_DEFAULT "]%s:%d [" BB_COL_CYAN BB_FUNCTION_LEN_STR BB_COL_DEFAULT "] "format, __FILENAME__, __LINE__, __FUNCTION__, ##__VA_ARGS__)
#define BB_LOG_ERROR(format, ...) fprintf(stderr, "[" BB_COL_BRED "ERROR " BB_COL_DEFAULT "]%s:%d [" BB_COL_CYAN BB_FUNCTION_LEN_STR BB_COL_DEFAULT "] "format, __FILENAME__, __LINE__, __FUNCTION__, ##__VA_ARGS__)
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment