Last active
March 16, 2023 03:54
-
-
Save duruyao/a52c6eaa2cb4b2102364bfaebcbd4c1d to your computer and use it in GitHub Desktop.
An example of printing logs of different levels in different colors on the console (implemented in C/C++).
This file contains 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
/* | |
* @date 2022-06-19 | |
* @author duruyao@gmail.com | |
* @desc print different levels of logs in the console | |
*/ | |
#ifndef CONSOLE_PRINT_H | |
#define CONSOLE_PRINT_H | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdint.h> | |
#define CONSOLE_COLOR_NONE "\033[m" | |
#define CONSOLE_COLOR_RED "\033[0;32;31m" | |
#define CONSOLE_COLOR_RED_BOLD "\033[1;32;31m" | |
#define CONSOLE_COLOR_GREEN "\033[0;32;32m" | |
#define CONSOLE_COLOR_YELLOW "\033[0;32;33m" | |
#define CONSOLE_COLOR_MAGENTA "\033[0;32;35m" | |
#define CONSOLE_COLOR_CYAN_BLUE "\033[0;32;36m" | |
#define CONSOLE_COLOR_LIGHT_BLUE "\033[0;32;34m" | |
#define CONSOLE_COLOR_DEBUG CONSOLE_COLOR_LIGHT_BLUE | |
#define CONSOLE_COLOR_INFO CONSOLE_COLOR_GREEN | |
#define CONSOLE_COLOR_TRACE CONSOLE_COLOR_CYAN_BLUE | |
#define CONSOLE_COLOR_WARN CONSOLE_COLOR_YELLOW | |
#define CONSOLE_COLOR_ERROR CONSOLE_COLOR_RED | |
#define CONSOLE_COLOR_FATAL CONSOLE_COLOR_RED_BOLD | |
#define CONSOLE_LOG_LEVEL_DEBUG 1 | |
#define CONSOLE_LOG_LEVEL_INFO 2 | |
#define CONSOLE_LOG_LEVEL_TRACE 3 | |
#define CONSOLE_LOG_LEVEL_WARN 4 | |
#define CONSOLE_LOG_LEVEL_ERROR 5 | |
#define CONSOLE_LOG_LEVEL_FATAL 6 | |
#define CONSOLE_LOG_LEVEL_NONE 7 | |
uint8_t CONSOLE_LOG_LEVEL = CONSOLE_LOG_LEVEL_DEBUG; | |
#ifndef DEBUG | |
#define DEBUG(FMT, ARGS...) ({ \ | |
if (CONSOLE_LOG_LEVEL_DEBUG >= CONSOLE_LOG_LEVEL) { \ | |
fprintf(stdout, "%s" "[%s][%s:%d:%s() %s %s] " FMT CONSOLE_COLOR_NONE "\n", \ | |
CONSOLE_COLOR_DEBUG, "DEBUG", __FILE__, __LINE__, __func__, __DATE__, __TIME__, ##ARGS); \ | |
} \ | |
}) | |
#endif//DEBUG | |
#ifndef INFO | |
#define INFO(FMT, ARGS...) ({ \ | |
if (CONSOLE_LOG_LEVEL_INFO >= CONSOLE_LOG_LEVEL) { \ | |
fprintf(stdout, "%s" "[%s][%s:%d:%s() %s %s] " FMT CONSOLE_COLOR_NONE "\n", \ | |
CONSOLE_COLOR_INFO, "INFO ", __FILE__, __LINE__, __func__, __DATE__, __TIME__, ##ARGS); \ | |
} \ | |
}) | |
#endif//INFO | |
#ifndef TRACE | |
#define TRACE(FMT, ARGS...) ({ \ | |
if (CONSOLE_LOG_LEVEL_TRACE >= CONSOLE_LOG_LEVEL) { \ | |
fprintf(stdout, "%s" "[%s][%s:%d:%s() %s %s] " FMT CONSOLE_COLOR_NONE "\n", \ | |
CONSOLE_COLOR_TRACE, "TRACE", __FILE__, __LINE__, __func__, __DATE__, __TIME__, ##ARGS); \ | |
} \ | |
}) | |
#endif//TRACE | |
#ifndef WARN | |
#define WARN(FMT, ARGS...) ({ \ | |
if (CONSOLE_LOG_LEVEL_WARN >= CONSOLE_LOG_LEVEL) { \ | |
fprintf(stdout, "%s" "[%s][%s:%d:%s() %s %s] " FMT CONSOLE_COLOR_NONE "\n", \ | |
CONSOLE_COLOR_WARN, "WARN ", __FILE__, __LINE__, __func__, __DATE__, __TIME__, ##ARGS); \ | |
} \ | |
}) | |
#endif//WARN | |
#ifndef WARNING | |
#define WARNING(FMT, ARGS...) WARN(FMT, ARGS) | |
#endif//WARNING | |
#ifndef ERROR | |
#define ERROR(FMT, ARGS...) ({ \ | |
if (CONSOLE_LOG_LEVEL_ERROR >= CONSOLE_LOG_LEVEL) { \ | |
fprintf(stderr, "%s" "[%s][%s:%d:%s() %s %s] " FMT CONSOLE_COLOR_NONE "\n", \ | |
CONSOLE_COLOR_ERROR, "ERROR", __FILE__, __LINE__, __func__, __DATE__, __TIME__, ##ARGS); \ | |
} \ | |
}) | |
#endif//ERROR | |
#ifndef FATAL | |
#define FATAL(FMT, ARGS...) ({ \ | |
if (CONSOLE_LOG_LEVEL_FATAL >= CONSOLE_LOG_LEVEL) { \ | |
fprintf(stderr, "%s" "[%s][%s:%d:%s() %s %s] " FMT CONSOLE_COLOR_NONE "\n", \ | |
CONSOLE_COLOR_FATAL, "FATAL", __FILE__, __LINE__, __func__, __DATE__, __TIME__, ##ARGS); \ | |
exit(1); \ | |
} \ | |
}) | |
#endif//FATAL | |
#endif//CONSOLE_PRINT_H |
This file contains 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
/* | |
* @date 2021-12-29 | |
* @author duruyao@gmail.com | |
* @desc print logs of different levels on the console | |
* @remark c++ version >= 14 | |
*/ | |
#ifndef CONSOLE_PRINT_HPP | |
#define CONSOLE_PRINT_HPP | |
#include <mutex> | |
#include <cstdio> | |
#include <atomic> | |
#define CONSOLE_COLOR_NONE "\033[m" | |
#define CONSOLE_COLOR_RED "\033[0;32;31m" | |
#define CONSOLE_COLOR_RED_BOLD "\033[1;32;31m" | |
#define CONSOLE_COLOR_GREEN "\033[0;32;32m" | |
#define CONSOLE_COLOR_YELLOW "\033[0;32;33m" | |
#define CONSOLE_COLOR_MAGENTA "\033[0;32;35m" | |
#define CONSOLE_COLOR_CYAN_BLUE "\033[0;32;36m" | |
#define CONSOLE_COLOR_LIGHT_BLUE "\033[0;32;34m" | |
enum ConsoleLogLevel { | |
ConsoleLogLevelDebug = 0, | |
ConsoleLogLevelInfo = 1, | |
ConsoleLogLevelTrace = 2, | |
ConsoleLogLevelWarn = 3, | |
ConsoleLogLevelError = 4, | |
ConsoleLogLevelFatal = 5, | |
ConsoleLogLevelNone = 6, | |
}; | |
std::atomic<enum ConsoleLogLevel> consoleLogLevel(ConsoleLogLevelDebug); | |
std::mutex consolePrintMutex; | |
#ifndef DEBUG | |
#define DEBUG(FMT, ARGS...) ({ \ | |
if (ConsoleLogLevelDebug >= consoleLogLevel) { \ | |
std::lock_guard<std::mutex> guard(consolePrintMutex); \ | |
fprintf(stdout, "%s" "%s [%s:%d:%s() %s %s] " FMT CONSOLE_COLOR_NONE "\n", \ | |
CONSOLE_COLOR_LIGHT_BLUE, "DEBUG", __FILE__, __LINE__, __func__, __DATE__, __TIME__, ##ARGS); \ | |
} \ | |
}) | |
#endif//DEBUG | |
#ifndef INFO | |
#define INFO(FMT, ARGS...) ({ \ | |
if (ConsoleLogLevelInfo >= consoleLogLevel) { \ | |
std::lock_guard<std::mutex> guard(consolePrintMutex); \ | |
fprintf(stdout, "%s" "%s [%s:%d:%s() %s %s] " FMT CONSOLE_COLOR_NONE "\n", \ | |
CONSOLE_COLOR_GREEN, "INFO ", __FILE__, __LINE__, __func__, __DATE__, __TIME__, ##ARGS); \ | |
} \ | |
}) | |
#endif//INFO | |
#ifndef TRACE | |
#define TRACE(FMT, ARGS...) ({ \ | |
if (ConsoleLogLevelTrace >= consoleLogLevel) { \ | |
std::lock_guard<std::mutex> guard(consolePrintMutex); \ | |
fprintf(stdout, "%s" "%s [%s:%d:%s() %s %s] " FMT CONSOLE_COLOR_NONE "\n", \ | |
CONSOLE_COLOR_CYAN_BLUE, "TRACE", __FILE__, __LINE__, __func__, __DATE__, __TIME__, ##ARGS); \ | |
} \ | |
}) | |
#endif//TRACE | |
#ifndef WARN | |
#define WARN(FMT, ARGS...) ({ \ | |
if (ConsoleLogLevelWarn >= consoleLogLevel) { \ | |
std::lock_guard<std::mutex> guard(consolePrintMutex); \ | |
fprintf(stdout, "%s" "%s [%s:%d:%s() %s %s] " FMT CONSOLE_COLOR_NONE "\n", \ | |
CONSOLE_COLOR_YELLOW, "WARN ", __FILE__, __LINE__, __func__, __DATE__, __TIME__, ##ARGS); \ | |
} \ | |
}) | |
#endif//WARN | |
#ifndef ERROR | |
#define ERROR(FMT, ARGS...) ({ \ | |
if (ConsoleLogLevelError >= consoleLogLevel) { \ | |
std::lock_guard<std::mutex> guard(consolePrintMutex); \ | |
fprintf(stderr, "%s" "[%s][%s:%d:%s() %s %s] " FMT CONSOLE_COLOR_NONE "\n", \ | |
CONSOLE_COLOR_RED, "ERROR", __FILE__, __LINE__, __func__, __DATE__, __TIME__, ##ARGS); \ | |
} \ | |
}) | |
#endif//ERROR | |
#ifndef FATAL | |
#define FATAL(FMT, ARGS...) ({ \ | |
if (ConsoleLogLevelFatal >= consoleLogLevel) { \ | |
std::lock_guard<std::mutex> guard(consolePrintMutex); \ | |
fprintf(stderr, "%s" "[%s][%s:%d:%s() %s %s] " FMT CONSOLE_COLOR_NONE "\n", \ | |
CONSOLE_COLOR_MAGENTA, "FATAL", __FILE__, __LINE__, __func__, __DATE__, __TIME__, ##ARGS); \ | |
exit(1); \ | |
} \ | |
}) | |
#endif//FATAL | |
#endif//CONSOLE_PRINT_HPP |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment