LittleVGL 源码分析--src/lv_misc/lv_log.h

这是log配置信息:

/*================
 * Log settings   日志设置
 *===============*/

/*1: Enable the log module 启用日志模块 */
#define LV_USE_LOG      1
#if LV_USE_LOG
/* How important log should be added:
 * LV_LOG_LEVEL_TRACE       A lot of logs to give detailed information跟踪大量日志以提供详细信息
 * LV_LOG_LEVEL_INFO        Log important events重要事件
 * LV_LOG_LEVEL_WARN        Log if something unwanted happened but didn't cause a problem如果发生了不需要发生的事情但没有引起问题,请记录
 * LV_LOG_LEVEL_ERROR       Only critical issue, when the system may fail只有在系统可能出现故障时才会出现的严重问题
 * LV_LOG_LEVEL_NONE        Do not log anything不要记录任何内容
 */
#  define LV_LOG_LEVEL    LV_LOG_LEVEL_WARN

/* 1: Print the log with 'printf';
 * 0: user need to register a callback with `lv_log_register_print` 用户需要使用“lv”log“register”print注册回调*/
#  define LV_LOG_PRINTF   1
#endif  /*LV_USE_LOG*/

lv_log.h

/**
 * @file lv_log.h
 *
 */

#ifndef LV_LOG_H
#define LV_LOG_H

#ifdef __cplusplus
extern "C" {
#endif

/*********************
 *      INCLUDES
 *********************/
#ifdef LV_CONF_INCLUDE_SIMPLE
#include "lv_conf.h"
#else
#include "../../../lv_conf.h"
#endif
#include 

/*********************
 *      DEFINES
 *********************/

/*Possible log level. For compatibility declare it independently from `LV_USE_LOG`*/
//可能的日志级别。为了兼容性,请独立于“LV”USE\u LOG声明它`

#define LV_LOG_LEVEL_TRACE 0 /**< A lot of logs to give detailed information*/
#define LV_LOG_LEVEL_INFO 1  /**< Log important events*/
#define LV_LOG_LEVEL_WARN 2  /**< Log if something unwanted happened but didn't caused problem*/
#define LV_LOG_LEVEL_ERROR 3 /**< Only critical issue, when the system may fail*/
#define LV_LOG_LEVEL_NONE 4 /**< Do not log anything*/
#define _LV_LOG_LEVEL_NUM 5 /**< Number of log levels */

typedef int8_t lv_log_level_t;

#if LV_USE_LOG
/**********************
 *      TYPEDEFS
 **********************/

/**
 * Log print function. Receives "Log Level", "File path", "Line number" and "Description".
 * 日志打印功能。   接收“日志级别”、“文件路径”、“行号”和“说明”。
 */
typedef void (*lv_log_print_g_cb_t)(lv_log_level_t level, const char *, uint32_t, const char *);

/**********************
 * GLOBAL PROTOTYPES
 **********************/

/**
 * Register custom print/write function to call when a log is added.
 * It can format its "File path", "Line number" and "Description" as required
 * and send the formatted log message to a consol or serial port.
 * @param print_cb a function pointer to print a log
 *注册自定义打印/写入函数以在添加日志时调用。
 *它可以根据需要格式化“文件路径”、“行号”和“说明”
 *并将格式化的日志消息发送到控制台或串行端口。
 */
void lv_log_register_print_cb(lv_log_print_g_cb_t print_cb);

/**
 * Add a log *添加日志
 * @param level the level of log. (From `lv_log_level_t` enum)
 * @param file name of the file when the log added
 * @param line line number in the source code where the log added
 * @param dsc description of the log
 */
void lv_log_add(lv_log_level_t level, const char * file, int line, const char * dsc);

/**********************
 *      MACROS
 **********************/

#if LV_LOG_LEVEL <= LV_LOG_LEVEL_TRACE
#define LV_LOG_TRACE(dsc) lv_log_add(LV_LOG_LEVEL_TRACE, __FILE__, __LINE__, dsc);
#else
#define LV_LOG_TRACE(dsc)                                                                                              \
    {                                                                                                                  \
        ;                                                                                                              \
    }
#endif

#if LV_LOG_LEVEL <= LV_LOG_LEVEL_INFO
#define LV_LOG_INFO(dsc) lv_log_add(LV_LOG_LEVEL_INFO, __FILE__, __LINE__, dsc);
#else
#define LV_LOG_INFO(dsc)                                                                                               \
    {                                                                                                                  \
        ;                                                                                                              \
    }
#endif

#if LV_LOG_LEVEL <= LV_LOG_LEVEL_WARN
#define LV_LOG_WARN(dsc) lv_log_add(LV_LOG_LEVEL_WARN, __FILE__, __LINE__, dsc);
#else
#define LV_LOG_WARN(dsc)                                                                                               \
    {                                                                                                                  \
        ;                                                                                                              \
    }
#endif

#if LV_LOG_LEVEL <= LV_LOG_LEVEL_ERROR
#define LV_LOG_ERROR(dsc) lv_log_add(LV_LOG_LEVEL_ERROR, __FILE__, __LINE__, dsc);
#else
#define LV_LOG_ERROR(dsc)                                                                                              \
    {                                                                                                                  \
        ;                                                                                                              \
    }
#endif

#else /*LV_USE_LOG*/

/*Do nothing if `LV_USE_LOG  0`*/
#define lv_log_add(level, file, line, dsc)                                                                             \
    {                                                                                                                  \
        ;                                                                                                              \
    }
#define LV_LOG_TRACE(dsc)                                                                                              \
    {                                                                                                                  \
        ;                                                                                                              \
    }
#define LV_LOG_INFO(dsc)                                                                                               \
    {                                                                                                                  \
        ;                                                                                                              \
    }
#define LV_LOG_WARN(dsc)                                                                                               \
    {                                                                                                                  \
        ;                                                                                                              \
    }
#define LV_LOG_ERROR(dsc)                                                                                              \
    {                                                                                                                  \
        ;                                                                                                              \
    }
#endif /*LV_USE_LOG*/

#ifdef __cplusplus
} /* extern "C" */
#endif

#endif /*LV_LOG_H*/

lv_log.c

/**
 * @file lv_log.c
 *
 */
#include "lv_log.h"
#if LV_USE_LOG

#if LV_LOG_PRINTF
#include 
#endif


static lv_log_print_g_cb_t custom_print_cb;//系统回掉函数

void lv_log_register_print_cb(lv_log_print_g_cb_t print_cb)
{
    custom_print_cb = print_cb;//将自己定义的log回掉函数赋给系统回掉函数
}


void lv_log_add(lv_log_level_t level, const char * file, int line, const char * dsc)
{
    if(level >= _LV_LOG_LEVEL_NUM) return; /*Invalid level*/

    if(level >= LV_LOG_LEVEL) {

#if LV_LOG_PRINTF  //判断
        static const char * lvl_prefix[] = {"Trace", "Info", "Warn", "Error"};
        printf("%s: %s \t(%s #%d)\n", lvl_prefix[level], dsc, file, line);//调用printf
#else
        if(custom_print_cb) custom_print_cb(level, file, line, dsc);//调用自己定义的
#endif
    }
}

总结:
 

log的函数有两个

注册自己的log函数:

lv_log_register_print_cb(lv_log_print_g_cb_t print_cb)

打印函数

lv_log_add(lv_log_level_t level, const char * file, int line, const char * dsc) 

level: LV_LOG_LEVEL_TRACE 0 /**< A lot of logs to give detailed information*/
            LV_LOG_LEVEL_INFO 1  /**< Log important events*/
             LV_LOG_LEVEL_WARN 2  /**< Log if something unwanted happened but didn't caused problem*/
             LV_LOG_LEVEL_ERROR 3 /**< Only critical issue, when the system may fail*/
             LV_LOG_LEVEL_NONE 4 /**< Do not log anything*/
             _LV_LOG_LEVEL_NUM 5 /**< Number of log levels */

 

下面的宏定义直接打印:

#if LV_LOG_LEVEL <= LV_LOG_LEVEL_TRACE
#define LV_LOG_TRACE(dsc) lv_log_add(LV_LOG_LEVEL_TRACE, __FILE__, __LINE__, dsc);
#else
#define LV_LOG_TRACE(dsc)                                                                                              \
    {                                                                                                                  \
        ;                                                                                                              \
    }
#endif

#if LV_LOG_LEVEL <= LV_LOG_LEVEL_INFO
#define LV_LOG_INFO(dsc) lv_log_add(LV_LOG_LEVEL_INFO, __FILE__, __LINE__, dsc);
#else
#define LV_LOG_INFO(dsc)                                                                                               \
    {                                                                                                                  \
        ;                                                                                                              \
    }
#endif

#if LV_LOG_LEVEL <= LV_LOG_LEVEL_WARN
#define LV_LOG_WARN(dsc) lv_log_add(LV_LOG_LEVEL_WARN, __FILE__, __LINE__, dsc);
#else
#define LV_LOG_WARN(dsc)                                                                                               \
    {                                                                                                                  \
        ;                                                                                                              \
    }
#endif

#if LV_LOG_LEVEL <= LV_LOG_LEVEL_ERROR
#define LV_LOG_ERROR(dsc) lv_log_add(LV_LOG_LEVEL_ERROR, __FILE__, __LINE__, dsc);
#else
#define LV_LOG_ERROR(dsc)                                                                                              \
    {                                                                                                                  \
        ;                                                                                                              \
    }
#endif

#else /*LV_USE_LOG*/

/*Do nothing if `LV_USE_LOG  0`*/
#define lv_log_add(level, file, line, dsc)                                                                             \
    {                                                                                                                  \
        ;                                                                                                              \
    }
#define LV_LOG_TRACE(dsc)                                                                                              \
    {                                                                                                                  \
        ;                                                                                                              \
    }
#define LV_LOG_INFO(dsc)                                                                                               \
    {                                                                                                                  \
        ;                                                                                                              \
    }
#define LV_LOG_WARN(dsc)                                                                                               \
    {                                                                                                                  \
        ;                                                                                                              \
    }
#define LV_LOG_ERROR(dsc)                                                                                              \
    {                                                                                                                  \
        ;                                                                                                              \
    }
#endif /*LV_USE_LOG*/

 

 

你可能感兴趣的:(LittleVGl)