HW_Lib/lib/inc/log.h

172 lines
5.6 KiB
C
Raw Normal View History

2024-05-09 05:56:55 +00:00
/**
* 便
* 使stdoutstderr
*
*
* LOG_LINE_END_CRLF \n \r\n
* LOG_FOR_MCU MCU MCU
* LOG_NDEBUG LOGD
* LOG_SHOW_VERBOSE LOGV
* LOG_NOT_EXIT_ON_FATAL FATAL退 退
*/
#ifndef HW_LIB_LOG_H
#define HW_LIB_LOG_H
#pragma once
#ifdef __cplusplus
#include <cstdio>
#include <cstring>
#include <cstdlib>
#else
2024-06-20 13:40:39 +00:00
2024-05-09 05:56:55 +00:00
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
2024-06-20 13:40:39 +00:00
2024-05-09 05:56:55 +00:00
#endif
/* 输出日志等级 */
#define LOG_LVL_FATAL 0
#define LOG_LVL_ERROR 1
#define LOG_LVL_WARN 2
#define LOG_LVL_INFO 3
#define LOG_LVL_DEBUG 4
#define LOG_LVL_VERBOSE 5
// 配置
#define LOG_TIMER
//#define LOG_FOR_MCU
#define LOG_LINE_END_CRLF
2024-06-20 13:40:39 +00:00
#define LOG_WITH_RUN_TIMER
2024-05-09 05:56:55 +00:00
#define LOG_OUTPUT_LVL LOG_LVL_INFO
////////////////////////
2024-06-20 13:40:39 +00:00
#ifdef LOG_WITH_RUN_TIMER
#define LOG_RUN_TIMER_FUN GetSysCnt32()
#define LOG_RUN_TIMER_FMT "-T(%lums)"
#else
#define LOG_RUN_TIMER_FUN 0
#define LOG_RUN_TIMER_FMT "-T(%lu)"
#endif
2024-05-09 05:56:55 +00:00
#ifdef LOG_LINE_END_CRLF
#define LOG_LINE_END "\r\n"
#else
#define LOG_LINE_END "\n"
#endif
#ifdef LOG_NOT_EXIT_ON_FATAL
#define LOG_EXIT_PROGRAM()
#else
#ifdef LOG_FOR_MCU
#define LOG_EXIT_PROGRAM() do{ for(;;); } while(0)
#else
#define LOG_EXIT_PROGRAM() exit(EXIT_FAILURE)
#endif
#endif
#define LOG_BASE_FILENAME \
(strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : \
strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__)
#define LOG_WITH_COLOR
#if defined(_WIN32) || defined(LOG_FOR_MCU)
#undef LOG_WITH_COLOR
#endif
2024-06-20 13:40:39 +00:00
2024-05-09 05:56:55 +00:00
#ifdef LOG_WITH_COLOR
#define LOG_COLOR_RED "\033[31m"
#define LOG_COLOR_GREEN "\033[32m"
#define LOG_COLOR_YELLOW "\033[33m"
#define LOG_COLOR_BLUE "\033[34m"
#define LOG_COLOR_CARMINE "\033[35m"
#define LOG_COLOR_CYAN "\033[36m"
#define LOG_COLOR_DEFAULT
#define LOG_COLOR_END "\033[m"
#else
#define LOG_COLOR_RED
#define LOG_COLOR_GREEN
#define LOG_COLOR_YELLOW
#define LOG_COLOR_BLUE
#define LOG_COLOR_CARMINE
#define LOG_COLOR_CYAN
#define LOG_COLOR_DEFAULT
#define LOG_COLOR_END
#endif
#define LOG_END LOG_COLOR_END LOG_LINE_END
// 带时钟输出
#if defined(LOG_TIMER)
2024-06-20 13:40:39 +00:00
#ifdef LOG_WITH_RUN_TIMER
2024-05-09 05:56:55 +00:00
#define LOGT(tag, fmt, ...) do{ printf(LOG_COLOR_BLUE "[" tag "]" LOG_RUN_TIMER_FMT ": " fmt LOG_END,LOG_RUN_TIMER_FUN, ##__VA_ARGS__); } while(0)
#else
2024-06-20 13:40:39 +00:00
#define LOGT(tag, fmt, ...) do{ printf(LOG_COLOR_BLUE "[" tag "]: " fmt LOG_END, ##__VA_ARGS__); } while(0)
#endif
#else
2024-05-09 05:56:55 +00:00
#define LOGT(fmt, ...) ((void)0)
#endif
// 等级输出
#if LOG_OUTPUT_LVL >= LOG_LVL_FATAL
2024-06-20 13:40:39 +00:00
#ifdef LOG_WITH_RUN_TIMER
#define LOGF(fmt, ...) do{ printf(LOG_COLOR_CYAN "[F]" LOG_RUN_TIMER_FMT ": %s: %s: %d: " fmt LOG_END,LOG_RUN_TIMER_FUN, LOG_BASE_FILENAME, __func__, __LINE__, ##__VA_ARGS__); LOG_EXIT_PROGRAM(); } while(0)
#else
2024-05-09 05:56:55 +00:00
#define LOGF(fmt, ...) do{ printf(LOG_COLOR_CYAN "[F]: %s: %s: %d: " fmt LOG_END, LOG_BASE_FILENAME, __func__, __LINE__, ##__VA_ARGS__); LOG_EXIT_PROGRAM(); } while(0)
2024-06-20 13:40:39 +00:00
#endif
2024-05-09 05:56:55 +00:00
#else
#define LOGF(fmt, ...) ((void)0)
#endif
#if LOG_OUTPUT_LVL >= LOG_LVL_ERROR
2024-06-20 13:40:39 +00:00
#ifdef LOG_WITH_RUN_TIMER
#define LOGE(fmt, ...) do{ printf(LOG_COLOR_RED "[E]" LOG_RUN_TIMER_FMT ": %s: %s: %d: " fmt LOG_END,LOG_RUN_TIMER_FUN, LOG_BASE_FILENAME, __func__, __LINE__, ##__VA_ARGS__); } while(0)
#else
2024-05-09 05:56:55 +00:00
#define LOGE(fmt, ...) do{ printf(LOG_COLOR_RED "[E]: %s: %s: %d: " fmt LOG_END, LOG_BASE_FILENAME, __func__, __LINE__, ##__VA_ARGS__); } while(0)
2024-06-20 13:40:39 +00:00
#endif
2024-05-09 05:56:55 +00:00
#else
#define LOGE(fmt, ...) ((void)0)
#endif
#if LOG_OUTPUT_LVL >= LOG_LVL_WARN
2024-06-20 13:40:39 +00:00
#ifdef LOG_WITH_RUN_TIMER
#define LOGW(fmt, ...) do{ printf(LOG_COLOR_CARMINE "[W]" LOG_RUN_TIMER_FMT ": %s: %s: %d: " fmt LOG_END,LOG_RUN_TIMER_FUN, LOG_BASE_FILENAME, __func__, __LINE__, ##__VA_ARGS__); } while(0)
#else
2024-05-09 05:56:55 +00:00
#define LOGW(fmt, ...) do{ printf(LOG_COLOR_CARMINE "[W]: %s: %s: %d: " fmt LOG_END, LOG_BASE_FILENAME, __func__, __LINE__, ##__VA_ARGS__); } while(0)
2024-06-20 13:40:39 +00:00
#endif
2024-05-09 05:56:55 +00:00
#else
#define LOGW(fmt, ...) ((void)0)
#endif
#if LOG_OUTPUT_LVL >= LOG_LVL_INFO
2024-06-20 13:40:39 +00:00
#ifdef LOG_WITH_RUN_TIMER
#define LOGI(fmt, ...) do{ printf(LOG_COLOR_YELLOW "[I]" LOG_RUN_TIMER_FMT ": %s: " fmt LOG_END,LOG_RUN_TIMER_FUN, LOG_BASE_FILENAME, ##__VA_ARGS__); } while(0)
#else
2024-05-09 05:56:55 +00:00
#define LOGI(fmt, ...) do{ printf(LOG_COLOR_YELLOW "[I]: %s: " fmt LOG_END, LOG_BASE_FILENAME, ##__VA_ARGS__); } while(0)
2024-06-20 13:40:39 +00:00
#endif
2024-05-09 05:56:55 +00:00
#else
#define LOGI(fmt, ...) ((void)0)
#endif
#if LOG_OUTPUT_LVL >= LOG_LVL_DEBUG
2024-06-20 13:40:39 +00:00
#ifdef LOG_WITH_RUN_TIMER
#define LOGD(fmt, ...) do{ printf(LOG_COLOR_DEFAULT "[D]" LOG_RUN_TIMER_FMT ": %s: " fmt LOG_END,LOG_RUN_TIMER_FUN, LOG_BASE_FILENAME, ##__VA_ARGS__); } while(0)
#else
2024-05-09 05:56:55 +00:00
#define LOGD(fmt, ...) do{ printf(LOG_COLOR_DEFAULT "[D]: %s: " fmt LOG_END, LOG_BASE_FILENAME, ##__VA_ARGS__); } while(0)
2024-06-20 13:40:39 +00:00
#endif
2024-05-09 05:56:55 +00:00
#else
#define LOGD(fmt, ...) ((void)0)
#endif
#if LOG_OUTPUT_LVL >= LOG_LVL_VERBOSE
2024-06-20 13:40:39 +00:00
#ifdef LOG_WITH_RUN_TIMER
#define LOGV(fmt, ...) do{ printf(LOG_COLOR_DEFAULT "[V]" LOG_RUN_TIMER_FMT ": %s: " fmt LOG_END,LOG_RUN_TIMER_FUN, LOG_BASE_FILENAME, ##__VA_ARGS__); } while(0)
#else
2024-05-09 05:56:55 +00:00
#define LOGV(fmt, ...) do{ printf(LOG_COLOR_DEFAULT "[V]: %s: " fmt LOG_END, LOG_BASE_FILENAME, ##__VA_ARGS__); } while(0)
2024-06-20 13:40:39 +00:00
#endif
2024-05-09 05:56:55 +00:00
#else
#define LOGV(fmt, ...) ((void)0)
#endif
#endif //HW_LIB_LOG_H