|
1、功能特点
支持日志等级显示
支持日志颜色显示
支持日志时间显示
效果如图:
2、使用方法
需要单片机重定向printf
3、源码
AWT_log.h
- #ifndef __AWT_LOG_H
- #define __AWT_LOG_H
- #include <stdint.h>
- // 前景色
- #define BLACK 0
- #define RED 1
- #define GREEN 2
- #define YELLOW 3
- #define BLUE 4
- #define MAGENTA 5
- #define CYAN 6
- #define WHITE 7
- #define AWT_LOG_FATAL 1
- #define AWT_LOG_ERROR 2
- #define AWT_LOG_WARN 3
- #define AWT_LOG_INFO 4
- #define AWT_LOG_TRUE 5
- #define AWT_LOG_DEBUG 6
- #define AWT_LOG_ALL 7
- // log打印长度设置
- #define AWT_BUG_LEN 1024
- // 是否打印时间
- #define AWT_PTIME 1
- // 日志时间结构体,打印时间通过这个结构体
- struct AWT_Log_time{
-
- uint16_t Year;
- uint8_t Month;
- uint8_t Date;
-
- uint8_t Hours;
- uint8_t Minutes;
- uint8_t Seconds;
- };
- // 日志等级结构体
- struct AWT_Log_Lever_Info_ST{
- char name[10];
- uint8_t lerver;
- uint8_t color;
- };
- // 配置工具的配置参数结构体
- struct AWT_Log_Config_ST{
- // 调试模式等级
- uint8_t Debug_Lever;
- // 判断方向(0:小于 1:等于 2:大于)
- uint8_t Debug_Direction;
- };
- // ======================== 公用接口 ===============================
- int AWT_Log_Init(int (* pTime)(struct AWT_Log_time * log_time));
- void AWT_log(uint8_t logLaval, char *fmt,...);
- // ======================== 设置参数接口 ===============================
- int AWT_SetDirection(uint8_t direction);
- int AWT_SetLever(uint8_t lever);
- int AWT_SetGetTimer(int (* pTime)(struct AWT_Log_time * log_time)); // 设置获取时间函数
- // ======================== 获取参数接口 ===============================
- #endif
复制代码
AWT_log.c
- #include <stdlib.h> /* atoi, exit */
- #include <stdio.h>
- #include <time.h>
- #include <string.h>
- #include <stdarg.h>
- #include "AWT_log.h"
- struct AWT_Log_Lever_Info_ST AWT_Log_Lever_Info[7] = {
- {"AWT FATAL", AWT_LOG_FATAL, RED},
- {"AWT ERROR", AWT_LOG_ERROR, RED},
- {"AWT WARN ", AWT_LOG_WARN, YELLOW},
- {"AWT INFO ", AWT_LOG_INFO, YELLOW},
- {"AWT TRACE", AWT_LOG_TRUE, GREEN},
- {"AWT DEBUG", AWT_LOG_DEBUG, CYAN},
- {"AWT TRUE ", AWT_LOG_ALL, WHITE},
- };
- struct AWT_Log_Lever_Info_ST AWT_Log_Lever_Info[7];
- struct AWT_Log_Config_ST AWT_Log_Config;
- int (* AWT_pTime)(struct AWT_Log_time * log_time);
- /**
- * @brief 初始化日志显示
- * @param pTime 获取log打印时间的回调函数,不然会打印时间错误。
- 关于回调函数实现参考以下:
- 主要是要将log_time中的参数一一赋值
- int LogPTime(struct AWT_Log_time * log_time){
- RTC_TimeTypeDef RTC_TimeStruct = {0};
- RTC_DateTypeDef RTC_DateStruct = {0};
- if(HAL_OK == HAL_RTC_GetTime(&hrtc, &RTC_TimeStruct, RTC_FORMAT_BIN)
- && HAL_OK == HAL_RTC_GetDate(&hrtc, &RTC_DateStruct, RTC_FORMAT_BIN))
- {
- log_time->Year = RTC_DateStruct.Year+2000;
- log_time->Month = RTC_DateStruct.Month;
- log_time->Date = RTC_DateStruct.Date;
-
- log_time->Hours = RTC_TimeStruct.Hours;
- log_time->Minutes = RTC_TimeStruct.Minutes;
- log_time->Seconds = RTC_TimeStruct.Seconds;
-
- }else{
- return 0;
- }
-
- return 1;
- }
-
- * @retval log等级字符串
- */
- int AWT_Log_Init(int (* pTime)(struct AWT_Log_time * log_time)){
-
- AWT_Log_Config.Debug_Lever = AWT_LOG_ALL;
- AWT_Log_Config.Debug_Direction = 0;
-
- AWT_SetGetTimer(pTime);
-
- AWT_log(AWT_LOG_TRUE, "AWR log init success\r\n");
- return 1;
- }
- /**
- * @brief 获取log级别的字符串
- * @param iLogLevel: log等级
-
- * @retval log等级字符串
- */
- static char *LogLevel(uint32_t iLogLevel)
- {
- return AWT_Log_Lever_Info[iLogLevel-1].name;
- }
- int AWT_SetGetTimer(int (* pTime)(struct AWT_Log_time * log_time)){
- AWT_pTime = pTime;
- return 0;
- }
- // 用于返回打印日志等级对应颜色
- void AWT_ColorPrint(uint32_t logLaval, char * writeBuf){
- char Pbuffer[AWT_BUG_LEN+100];
- sprintf(Pbuffer, "\x1b[%d;%dm%s\x1b[0m", 1, AWT_Log_Lever_Info[logLaval-1].color+30, writeBuf);
- printf("%s", Pbuffer);
- }
- // 写日志接口
- void AWT_log(uint8_t ilogLaval, char *fmt,...)
- {
- char buf[AWT_BUG_LEN];
- char writeBuf[AWT_BUG_LEN+50];
- uint8_t logLaval;
-
- logLaval = ilogLaval;
-
- // 判断合法性
- if(logLaval > AWT_LOG_ALL){
- AWT_log(AWT_LOG_WARN, "logLaval Wrongful!\n");
- logLaval = AWT_LOG_ALL;
- }else{
- logLaval = ilogLaval;
- }
-
- //time(&t); lt = localtime(&t);
-
- va_list va_args;
- // Start the varargs processing.
- va_start(va_args, fmt);
- vsnprintf((char *)buf, sizeof(buf), fmt, va_args);
- // End the varargs processing.
- va_end(va_args);
- if(AWT_PTIME){
- struct AWT_Log_time log_time;
- if(AWT_pTime(&log_time) == 1){
- sprintf(writeBuf, " [ %s ] %d/%d/%d %d:%d:%02d %s", LogLevel(logLaval), log_time.Year, log_time.Month, log_time.Date, log_time.Hours, log_time.Minutes, log_time.Seconds, buf);
- }else{
- sprintf(writeBuf, " [ %s ] getTimeErr %s", LogLevel(logLaval), buf);
- }
- }else{
- sprintf(writeBuf, " [ %s ] %s", LogLevel(logLaval), buf);
- }
-
- if(AWT_Log_Config.Debug_Direction == 0){
- if(logLaval < AWT_Log_Config.Debug_Lever){
- AWT_ColorPrint(logLaval ,writeBuf);
- }
- }else if(AWT_Log_Config.Debug_Direction == 1){
- if(logLaval == AWT_Log_Config.Debug_Lever){
- AWT_ColorPrint(logLaval ,writeBuf);
- }
- }else if(AWT_Log_Config.Debug_Direction == 2){
- if(logLaval > AWT_Log_Config.Debug_Lever){
- AWT_ColorPrint(logLaval ,writeBuf);
- }
- }
- }
- int AWT_SetDirection(uint8_t direction){
- if(direction > 3){
- return 0;
- }
- AWT_Log_Config.Debug_Direction = direction;
- return 1;
- }
- int AWT_SetLever(uint8_t lever){
- if(lever > AWT_LOG_ALL){
- return 0;
- }
- AWT_Log_Config.Debug_Lever = lever;
- return 1;
- }
复制代码
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|