0%

ESP32打印日志

ESP32日志报文打印API测试,掌握Debug,Info,Warn以及Error四种日志报文输出,获取ESP32芯片相关信息并打印输出

1.日志打印API介绍

函数名 描述 案例
esp_chip_info(esp_chip_info_t* info) info:芯片信息结构体指针 esp_chip_info(&info)
ESP_LOGI(const char* tag,char* format_msg) 打印信息: [tag:format_msg] ESP_LOGI("Info","This is a Information");
ESP_LOGI("Info","ESP32 Chip Cores Count: %d",chip_info.cores);
ESP_LOGI("Info","ESP32 Chip, WiFi%s%s,",(chip_info.features & CHIP_FEATURE_BT)?"/BT":"",(chip_info.features&CHIP_FEATURE_BLE) ? "/BLE":"");
ESP_LOGI("Info","SPI Flash Chip Size: %dMByte %s flash",spi_flash_get_chip_size()/(1024*1024),(chip_info.features & CHIP_FEATURE_EMB_FLASH)?"Embedded" : "External");
esp_base_mac_addr_get(uint8_t MAC[6]) WiFi的MAC地址 esp_base_mac_addr_get(MAC)
ESP_LOGI("Info", "Base MAC Addr : %02X.%02X.%02X.%02X.%02X.%02X",MAC[0],MAC[1],MAC[2],MAC[3],MAC[4],MAC[5]);
ESP_LOGW(const char* tag,char* msg) 打印报警:[tag,msg] ESP_LOGW(“Warn”, “This is a Warn”)
ESP_LOGE(const char* tag,char* msg) 打印错误:[tag,msg] ESP_LOGE(“Error”, “This is a Error”)
ESP_LOGD (const char* tag,char* msg) 打印调试:[tag,msg] ESP_LOGD(“Debug”, “This is a Debug”)
ESP_LOGV(const char* tag,char* msg) 详细:[tag,msg] ESP_LOGV(“Detial”, “This is a Detail”)
esp_restart() 软件复位 esp_restart();

2.功能设计

Info:CPU基本信息 如Flash Size,Core Num,RAM Size等
打印输出: Warn,Error信息

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_spi_flash.h"
#include <esp_log.h>

const static char *InfoTag = "Info";
const static char *WarnTag = "Warn";
const static char *ErrorTag = "Error";

void app_main()
{
uint8_t MAC[6];
ESP_LOGI(InfoTag, ">>>===== 打印ESP32芯片信息 =====<<<");
// 打印芯片信息
esp_chip_info_t chip_info;
esp_chip_info(&chip_info);
ESP_LOGI(InfoTag, ">>> ESP32芯片内核数目: %d",chip_info.cores);
if(chip_info.model == 1){
ESP_LOGI(InfoTag, ">>> ESP32芯片架构: ESP32");
}else if(chip_info.model == 2){
ESP_LOGI(InfoTag, ">>> ESP32芯片架构: ESP32S2");
}else{
ESP_LOGI(InfoTag, ">>> ESP32芯片架构: Unknown Model");
}
ESP_LOGI(InfoTag, ">>> ESP32芯片特征量: %d",chip_info.features);
ESP_LOGI(InfoTag, ">>> ESP32芯片版本号: %d",chip_info.revision);

ESP_LOGI(InfoTag, ">>> ESP32芯片支持通讯方式: WIFI%s%s, ",
(chip_info.features & CHIP_FEATURE_BT) ? "/BT" : "",
(chip_info.features & CHIP_FEATURE_BLE) ? "/BLE" : "");

ESP_LOGI(InfoTag, ">>> ESP32 Flash容量: %dMB %s flash", spi_flash_get_chip_size() / (1024 * 1024),
(chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "Embedded" : "External");

ESP_LOGI(InfoTag, ">>> Free Heap Size is: %d B",esp_get_free_heap_size());
ESP_LOGI(InfoTag, ">>> Free Internal Heap Size is: %d B",esp_get_free_internal_heap_size());
ESP_LOGI(InfoTag, ">>> Free minimum Heap Size is: %d B",esp_get_minimum_free_heap_size());

esp_base_mac_addr_get(MAC);
ESP_LOGI(InfoTag, ">>> 网络MAC地址: %02X.%02X.%02X.%02X.%02X.%02X",MAC[0],MAC[1],MAC[2],MAC[3],MAC[4],MAC[5]);

ESP_LOGI(InfoTag, ">>>===== ESP32芯片介绍结束 =====<<<");
vTaskDelay(1000 / portTICK_PERIOD_MS);

ESP_LOGW(WarnTag, ">>> This is a Warning!");

ESP_LOGE(ErrorTag, ">>> This is a Error!");

while(1){
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
// ESP_LOGI(TAG, "Restarting now.");
// esp_restart();
}

输出结果

日志输出

Post not found: ESP32-LED