C Utilities
#ifndef __UTIL_H__ | |
#define __UTIL_H__ | |
/** | |
Copyright (c) 2021 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 | |
@brief Utility macros | |
@date 2018 | |
*/ | |
#include <stdio.h> | |
#include <stdarg.h> | |
/*--------- Macros ---------*/ | |
/* 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 based on the RELEASE parameter */ | |
#ifndef LOG_LEVEL | |
#if RELEASE | |
#define LOG_LEVEL LOG_LEVEL_NONE | |
#else | |
#define LOG_LEVEL LOG_LEVEL_DEBUG | |
#endif | |
#endif | |
#define LOG_COLOR_EN 1 /*!< Enable Log Color */ | |
/*----- Token Manipulation macros -----*/ | |
#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) | |
/*--- Output function ---*/ | |
#define DEBUG_OUTPUT printf | |
#define DEBUG_OUTPUT_F printf | |
/*--- Output Macros ---*/ | |
#if defined(ESP_IDF) /* For Espressif's ESP-IDF */ | |
#define LOGE(...) ESP_LOGE(TAG, __VA_ARGS__) | |
#define LOGW(...) ESP_LOGW(TAG, __VA_ARGS__) | |
#define LOGI(...) ESP_LOGI(TAG, __VA_ARGS__) | |
#define LOGV(...) ESP_LOGV(TAG, __VA_ARGS__) | |
#define LOGD(...) ESP_LOGD(TAG, __VA_ARGS__) | |
#else /* Generic */ | |
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; | |
#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_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