Skip to content

Instantly share code, notes, and snippets.

@Luctins
Last active September 8, 2021 17:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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
/**
Copyright (c) 2020 Lucas Martins Mendes.
All rights reserved.
Redistribution and use in source and binary forms are permitted
provided that the above copyright notice and this paragraph are
duplicated in all such forms and that any documentation,
advertising materials, and other materials related to such
distribution and use acknowledge that the software was developed
by the Lucas M. The name of the
Lucas M. may not be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
@file util.h
@author Lucas Martins Mendes
@email contact@luctins.me
@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