Skip to content

Instantly share code, notes, and snippets.

@Luctins
Last active December 6, 2024 14:28
Show Gist options
  • Save Luctins/f88d87dfced1292286f09a9d7984e9c4 to your computer and use it in GitHub Desktop.
Save Luctins/f88d87dfced1292286f09a9d7984e9c4 to your computer and use it in GitHub Desktop.
C Utilities
/**
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org>
@file util.h
@author Lucas Martins Mendes
@email [email protected]
@brief debug macros
@see [repository](https://gist.github.com/Luctins/f88d87dfced1292286f09a9d7984e9c4)
@date 2018
*/
#ifndef __UTIL_H__
#define __UTIL_H__
/*--- Macros ------------------------------------------------------------------*/
/*----- Token Manipulation -----*/
#define _TOKENPASTE(a,b,c) a ## b ## c
#define TOKENPASTE(a,b,c) _TOKENPASTE(a,b,c)
/*--- Stringify ---*/
#define _STR(x) #x
#define STR(x) _STR(x)
/*--- Environment specific macros ---------------------------------------------*/
/*--- ESP IDF utilities -----------------------------------*/
#if defined(ESP_IDF)
#define vTaskDelayMs(ms) vTaskDelay(pdMS_TO_TICKS(ms))
#define LOGE(...) ESP_LOGE(__FUNCTION__, __VA_ARGS__)
#define LOGW(...) ESP_LOGW(__FUNCTION__, __VA_ARGS__)
#define LOGI(...) ESP_LOGI(__FUNCTION__, __VA_ARGS__)
#define LOGV(...) ESP_LOGV(__FUNCTION__, __VA_ARGS__)
#define LOGD(...) ESP_LOGD(__FUNCTION__, __VA_ARGS__)
#define ASSERT(msg,cond) \
if(!(cond)) {} \
else \
{ \
LOGW(msg " : at " STR(__LINE__)); \
}
#define ASSERT_A(cond) \
if(!(cond)) {} \
else \
{ \
LOGW(STR(cond) " : at " STR(__LINE__)); \
}
#define ASSERT_ERROR(msg,cond,do_err) \
if(!(cond)) {} \
else \
{ \
LOGE(msg " : at " STR(__LINE__)); \
do_err; \
}
#define ASSERT_ERROR_A(cond,do_err) \
if(!(cond)) {} \
else \
{ \
LOGE(STR(cond) " : at " STR(__LINE__)); \
do_err; \
}
#else /*--- Generic Loggging util ---------------------------------------------*/
typedef enum log_level
{
LOG_LEVEL_NONE = 0, /*!< No output at all */
LOG_LEVEL_ERROR = 1, /*!< Errors only */
LOG_LEVEL_WARN = 2, /*!< all above + warnings */
LOG_LEVEL_INFO = 3, /*!< all above + general info */
LOG_LEVEL_VERBOSE = 4, /*!< all above + extra info */
LOG_LEVEL_DEBUG = 5 /*!< all above + debug info dumps */
} logLevel_t;
/* Handy defines to force a specific log level */
//#define LOG_LEVEL LOG_LEVEL_NONE
//#define LOG_LEVEL LOG_LEVEL_ERROR
//#define LOG_LEVEL LOG_LEVEL_WARN
//#define LOG_LEVEL LOG_LEVEL_INFO
//#define LOG_LEVEL LOG_LEVEL_VERBOSE
//#define LOG_LEVEL LOG_LEVEL_DEBUG
//#define LOG_LEVEL 5
#ifndef LOG_LEVEL
#warning Log level undefined
#define LOG_LEVEL LOG_LEVEL_NONE
#endif
#define LOG_COLOR_EN 1
#if !(defined DEBUG_OUTPUT) && !(defined DEBUG_OUTPUT_F)
#warning Output function not defined, using default
#define DEBUG_OUTPUT_F printf
#define DEBUG_OUTPUT printf
#endif
#if LOG_COLOR_EN
#define LOG_COLOR_BLACK "30"
#define LOG_COLOR_RED "31"
#define LOG_COLOR_GREEN "32"
#define LOG_COLOR_ORANGE "33"
#define LOG_COLOR_BLUE "34"
#define LOG_COLOR_PURPLE "35"
#define LOG_COLOR_CYAN "36"
#define LOG_COLOR_WHITE "37"
#define LOG_COLOR(COLOR) "\033[0;" COLOR "m"
#define LOG_BOLD(COLOR) "\033[1;" COLOR "m"
#define LOG_COLOR(COLOR) "\033[0;" COLOR "m"
#define LOG_BOLD(COLOR) "\033[1;" COLOR "m"
#define LOG_COLOR_E() DEBUG_OUTPUT(LOG_COLOR(LOG_COLOR_RED))
#define LOG_COLOR_W() DEBUG_OUTPUT(LOG_COLOR(LOG_COLOR_ORANGE))
#define LOG_COLOR_I() DEBUG_OUTPUT(LOG_COLOR(LOG_COLOR_GREEN))
#define LOG_COLOR_V() DEBUG_OUTPUT(LOG_COLOR(LOG_COLOR_WHITE))
#define LOG_COLOR_D() DEBUG_OUTPUT(LOG_COLOR(LOG_COLOR_BLUE))
#define LOG_COLOR_RESET() DEBUG_OUTPUT("\033[0m\n")
#else
#define LOG_COLOR_E()
#define LOG_COLOR_W()
#define LOG_COLOR_I()
#define LOG_COLOR_V()
#define LOG_COLOR_D()
#define LOG_COLOR_RESET()
#endif
#define LOGE(...) \
do { if (LOG_LEVEL >= LOG_LEVEL_ERROR) { \
LOG_COLOR_E(); \
DEBUG_OUTPUT_F("E:(" __FILE__ "):" STR(__LINE__)":"); \
DEBUG_OUTPUT_F(__VA_ARGS__); \
LOG_COLOR_RESET(); \
} } while (0)
#define LOGW(...) \
do { if (LOG_LEVEL >= LOG_LEVEL_WARN) { \
LOG_COLOR_W(); \
DEBUG_OUTPUT_F("W:(" __FILE__ "):" STR(__LINE__)":"); \
DEBUG_OUTPUT_F(__VA_ARGS__); \
LOG_COLOR_RESET(); \
} } while(0)
#define LOGI(...) \
do { if (LOG_LEVEL >= LOG_LEVEL_INFO) { \
LOG_COLOR_I(); \
DEBUG_OUTPUT_F("I:(" __FILE__ "):" STR(__LINE__)":"); \
DEBUG_OUTPUT_F(__VA_ARGS__); \
LOG_COLOR_RESET(); \
} } while(0)
#define LOGV(...) \
do { if (LOG_LEVEL >= LOG_LEVEL_VERBOSE) { \
LOG_COLOR_V(); \
DEBUG_OUTPUT_F("V:(" __FILE__ "):" STR(__LINE__)":"); \
DEBUG_OUTPUT_F(__VA_ARGS__); \
LOG_COLOR_RESET(); \
} } while(0)
#define LOGD(...) \
do { if (LOG_LEVEL >= LOG_LEVEL_DEBUG) { \
LOG_COLOR_D(); \
DEBUG_OUTPUT_F("D:(" __FILE__ "):" STR(__LINE__)":"); \
DEBUG_OUTPUT_F(__VA_ARGS__); \
LOG_COLOR_RESET(); \
} } while(0)
#endif /*--- Generic ----------------------------------------------------------*/
#define ASSERT(msg,cond) \
if(!(cond)) {} else { \
LOGW(msg " : at " STR(__LINE__)); \
}
#define ASSERT_A(cond) \
if(!(cond)) {} else { \
LOGW(STR(cond) " : at " STR(__LINE__)); \
}
#define ASSERT_ERROR(msg,cond,do_err) \
if(!(cond)) {} else { \
LOGE(msg " : at " STR(__LINE__)); \
do_err; \
}
#define ASSERT_ERROR_A(cond,do_err) \
if(!(cond)) {} else { \
LOGE(STR(cond) " : at " STR(__LINE__)); \
do_err; \
}
#define VAR_DUMP(var,fmt) LOGD(#var ": " fmt,var);
#endif /*--- __UTIL_H__ -------------------------------------------------------*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment