基於嵌入式ARM環境下運行的lvgl系統Debug打印配置

  • 在lv_conf.h中定義LV_USE_DEBUG爲1

    #define LV_USE_DEBUG        1
  • 在lv_conf.h中定義LV_USE_LOG爲1

    #define LV_USE_LOG      1
  • 配置打印等級

如果要打印所有的級別,將LV_LOG_LEVEL設成LV_LOG_LEVEL_TRACE,以下是警告及錯誤級別的信息會打印

配置打印接口,如果使用printf方式打印,將LV_LOG_PRINTF設爲1,現使用RTT打印,配置如下

* 1: Print the log with 'printf';
 * 0: user need to register a callback with `lv_log_register_print_cb`*/
#  define LV_LOG_PRINTF   0

五個等級的打印都是調用函數lv_log_add函數,函數實現如下

/**
 * 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)
{
    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);
#else
        if(custom_print_cb) custom_print_cb(level, file, line, dsc);
#endif
    }
}

LV_LOG_PRINTF爲0時,調用了函數指針custom_print_cb

if(custom_print_cb) custom_print_cb(level, file, line, dsc);

那麼這個函數指針需要指向用戶註冊的打印接口函數,使用lv_log_register_print_cb函數來註冊,函數實現

如下

/**
 * 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)
{
    custom_print_cb = print_cb;
}

 準備RTT打印相關文件配置,具體可參考https://blog.csdn.net/mygod2008ok/article/details/102926458

  • 註冊自己的打印函數 

    lv_log_register_print_cb(my_printf);

函數指針類型原型如下

typedef void (*lv_log_print_g_cb_t)(lv_log_level_t level, const char *, uint32_t, const char *);

自己的打印函數


void my_printf(lv_log_level_t level, const char *file, uint32_t line, const char *dsc)
{
	NRF_LOG_INFO("level:%d,file:%s,line:%d,dsc:%s",level,file,line,dsc);
}

測試驗證,故意將用戶數據傳入NULL,等下在lv定時器回調操作空指針,引發的一個打印測試

    /*Create a line meter*/

    lv_obj_t* lmeter;

    lmeter = lv_lmeter_create(lv_scr_act(), NULL);

    lv_lmeter_set_range(lmeter, 0, 60);

    lv_lmeter_set_value(lmeter, 10);

    lv_lmeter_set_scale(lmeter, 360, 15);

 
   
    lv_lmeter_set_style(lmeter, LV_LMETER_STYLE_MAIN,&style_lmeter);

    lv_obj_set_size(lmeter, 80, 80);

    lv_obj_align(lmeter, slider, LV_ALIGN_OUT_BOTTOM_MID, 0, 20);

   

    lv_task_t* t = lv_task_create(line_meter_task, 200, LV_TASK_PRIO_MID, NULL);

    lv_task_ready(t);

回調引用空指針引發異常

void line_meter_task(lv_task_t* t)
{

    static uint8_t progress;

    lv_obj_t* lmeter = t->user_data;

    progress += 10;

    if (progress == 60)
    {

        progress = 0;

    }

    lv_lmeter_set_value(lmeter, progress);
}

 燒錄並運行,打開RTT Viewer

打印的等級定義如下


/*Possible log level. For compatibility declare it independently from `LV_USE_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 */

找到出錯文件lv_debug.c的167行 

 

在這打個斷點調試,然後運行出函數,定位到出錯點

 繼續找到void line_meter_task(lv_task_t* t)函數

 

找到void line_meter_task(lv_task_t* t)函數註冊的地方

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章