请选择 进入手机版 | 继续访问电脑版
查看: 1001|回复: 0

[技术交流] 分享一个简陋的适用嵌入式的日志系统

[复制链接]

8

主题

13

帖子

61

积分

利尔达员工

Rank: 9Rank: 9Rank: 9

积分
61
发表于 2020-11-27 17:26:16 | 显示全部楼层 |阅读模式
1、功能特点
支持日志等级显示
支持日志颜色显示
支持日志时间显示
效果如图:


2、使用方法
需要单片机重定向printf

3、源码
AWT_log.h

  1. #ifndef __AWT_LOG_H
  2. #define __AWT_LOG_H
  3. #include <stdint.h>

  4. // 前景色
  5. #define BLACK           0
  6. #define RED             1
  7. #define GREEN           2
  8. #define YELLOW          3
  9. #define BLUE            4
  10. #define MAGENTA         5
  11. #define CYAN            6
  12. #define WHITE           7

  13. #define AWT_LOG_FATAL                                 1
  14. #define AWT_LOG_ERROR                                 2
  15. #define AWT_LOG_WARN                                  3
  16. #define AWT_LOG_INFO                                  4
  17. #define AWT_LOG_TRUE                                 5
  18. #define AWT_LOG_DEBUG                                 6
  19. #define AWT_LOG_ALL                                 7



  20. // log打印长度设置
  21. #define AWT_BUG_LEN  1024
  22. // 是否打印时间
  23. #define AWT_PTIME 1

  24. // 日志时间结构体,打印时间通过这个结构体
  25. struct AWT_Log_time{
  26.        
  27.         uint16_t  Year;
  28.         uint8_t Month;
  29.         uint8_t Date;
  30.        
  31.         uint8_t Hours;
  32.         uint8_t Minutes;
  33.         uint8_t Seconds;
  34. };

  35. // 日志等级结构体
  36. struct AWT_Log_Lever_Info_ST{
  37.         char name[10];
  38.         uint8_t lerver;
  39.         uint8_t color;
  40. };

  41. // 配置工具的配置参数结构体
  42. struct AWT_Log_Config_ST{
  43.         // 调试模式等级
  44.         uint8_t Debug_Lever;
  45.     // 判断方向(0:小于 1:等于 2:大于)
  46.         uint8_t Debug_Direction;   

  47. };



  48. // ======================== 公用接口 ===============================
  49. int AWT_Log_Init(int (* pTime)(struct AWT_Log_time * log_time));  
  50. void  AWT_log(uint8_t logLaval, char *fmt,...);

  51. // ======================== 设置参数接口 ===============================
  52. int AWT_SetDirection(uint8_t direction);
  53. int AWT_SetLever(uint8_t lever);
  54. int AWT_SetGetTimer(int (* pTime)(struct AWT_Log_time * log_time)); //  设置获取时间函数

  55. // ======================== 获取参数接口 ===============================


  56. #endif
复制代码

AWT_log.c

  1. #include <stdlib.h>         /* atoi, exit */
  2. #include <stdio.h>
  3. #include <time.h>
  4. #include <string.h>
  5. #include <stdarg.h>

  6. #include "AWT_log.h"



  7. struct AWT_Log_Lever_Info_ST AWT_Log_Lever_Info[7] = {
  8.         {"AWT FATAL", AWT_LOG_FATAL, RED},
  9.         {"AWT ERROR", AWT_LOG_ERROR, RED},
  10.         {"AWT WARN ", AWT_LOG_WARN, YELLOW},
  11.         {"AWT INFO ", AWT_LOG_INFO, YELLOW},
  12.         {"AWT TRACE", AWT_LOG_TRUE, GREEN},
  13.         {"AWT DEBUG", AWT_LOG_DEBUG, CYAN},
  14.         {"AWT TRUE ", AWT_LOG_ALL, WHITE},
  15. };

  16. struct AWT_Log_Lever_Info_ST AWT_Log_Lever_Info[7];

  17. struct AWT_Log_Config_ST AWT_Log_Config;

  18. int (* AWT_pTime)(struct AWT_Log_time * log_time);


  19. /**
  20.   * @brief  初始化日志显示
  21.   * @param  pTime 获取log打印时间的回调函数,不然会打印时间错误。
  22.                                                 关于回调函数实现参考以下:
  23. 主要是要将log_time中的参数一一赋值
  24. int LogPTime(struct AWT_Log_time * log_time){
  25.         RTC_TimeTypeDef RTC_TimeStruct = {0};
  26.   RTC_DateTypeDef RTC_DateStruct = {0};
  27.         if(HAL_OK   == HAL_RTC_GetTime(&hrtc, &RTC_TimeStruct, RTC_FORMAT_BIN)
  28.   && HAL_OK == HAL_RTC_GetDate(&hrtc, &RTC_DateStruct, RTC_FORMAT_BIN))
  29.   {
  30.                 log_time->Year = RTC_DateStruct.Year+2000;
  31.                 log_time->Month = RTC_DateStruct.Month;
  32.                 log_time->Date = RTC_DateStruct.Date;
  33.                                                
  34.                 log_time->Hours = RTC_TimeStruct.Hours;
  35.                 log_time->Minutes = RTC_TimeStruct.Minutes;
  36.                 log_time->Seconds = RTC_TimeStruct.Seconds;
  37.                
  38.         }else{
  39.                 return 0;
  40.         }
  41.        
  42.         return 1;
  43. }
  44.   
  45.   * @retval log等级字符串
  46.   */
  47. int AWT_Log_Init(int (* pTime)(struct AWT_Log_time * log_time)){
  48.        
  49.         AWT_Log_Config.Debug_Lever = AWT_LOG_ALL;
  50.         AWT_Log_Config.Debug_Direction = 0;
  51.        
  52.         AWT_SetGetTimer(pTime);
  53.        
  54.         AWT_log(AWT_LOG_TRUE,  "AWR log init success\r\n");

  55.         return 1;
  56. }



  57. /**
  58.   * @brief  获取log级别的字符串
  59.   * @param  iLogLevel: log等级
  60.   
  61.   * @retval log等级字符串
  62.   */
  63. static char *LogLevel(uint32_t iLogLevel)
  64. {
  65.     return AWT_Log_Lever_Info[iLogLevel-1].name;
  66. }

  67. int AWT_SetGetTimer(int (* pTime)(struct AWT_Log_time * log_time)){
  68.         AWT_pTime = pTime;
  69.         return 0;
  70. }



  71. // 用于返回打印日志等级对应颜色
  72. void AWT_ColorPrint(uint32_t logLaval, char * writeBuf){

  73.         char Pbuffer[AWT_BUG_LEN+100];
  74.         sprintf(Pbuffer, "\x1b[%d;%dm%s\x1b[0m", 1, AWT_Log_Lever_Info[logLaval-1].color+30, writeBuf);
  75.         printf("%s", Pbuffer);
  76. }

  77. // 写日志接口
  78. void  AWT_log(uint8_t ilogLaval, char *fmt,...)
  79. {
  80.         char buf[AWT_BUG_LEN];
  81.         char writeBuf[AWT_BUG_LEN+50];

  82.         uint8_t logLaval;
  83.        
  84.         logLaval = ilogLaval;
  85.        
  86.         // 判断合法性
  87.         if(logLaval > AWT_LOG_ALL){
  88.                 AWT_log(AWT_LOG_WARN, "logLaval Wrongful!\n");
  89.                 logLaval = AWT_LOG_ALL;
  90.         }else{
  91.                 logLaval = ilogLaval;
  92.         }
  93.        
  94.         //time(&t); lt = localtime(&t);
  95.        
  96.     va_list va_args;

  97.     // Start the varargs processing.
  98.     va_start(va_args, fmt);

  99.     vsnprintf((char *)buf, sizeof(buf), fmt, va_args);

  100.     // End the varargs processing.
  101.     va_end(va_args);

  102.         if(AWT_PTIME){
  103.                 struct AWT_Log_time log_time;
  104.                 if(AWT_pTime(&log_time) == 1){
  105.                         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);
  106.                 }else{
  107.                         sprintf(writeBuf, " [ %s ] getTimeErr %s", LogLevel(logLaval), buf);
  108.                 }
  109.                 }else{
  110.                 sprintf(writeBuf, " [ %s ] %s", LogLevel(logLaval), buf);
  111.         }

  112.        

  113.         if(AWT_Log_Config.Debug_Direction == 0){
  114.                 if(logLaval < AWT_Log_Config.Debug_Lever){
  115.                         AWT_ColorPrint(logLaval ,writeBuf);
  116.                 }
  117.         }else if(AWT_Log_Config.Debug_Direction == 1){
  118.                 if(logLaval == AWT_Log_Config.Debug_Lever){
  119.                         AWT_ColorPrint(logLaval ,writeBuf);
  120.                 }
  121.         }else if(AWT_Log_Config.Debug_Direction == 2){
  122.                 if(logLaval > AWT_Log_Config.Debug_Lever){
  123.                         AWT_ColorPrint(logLaval ,writeBuf);
  124.                 }
  125.         }

  126. }

  127. int AWT_SetDirection(uint8_t direction){
  128.         if(direction > 3){
  129.                 return 0;
  130.         }
  131.         AWT_Log_Config.Debug_Direction = direction;
  132.         return 1;
  133. }

  134. int AWT_SetLever(uint8_t lever){
  135.         if(lever > AWT_LOG_ALL){
  136.                 return 0;
  137.         }
  138.         AWT_Log_Config.Debug_Lever = lever;
  139.         return 1;
  140. }
复制代码






本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表