1. 增加日志锁

2. 增加支持系统堆栈检查
This commit is contained in:
coffee 2025-07-26 18:26:51 +08:00
parent 60b4068289
commit 32ca8cf7b4
2 changed files with 102 additions and 29 deletions

View File

@ -7,16 +7,24 @@
// 使用宏开关管理日志
#ifndef LOG_CLOSE_OUT
#define USE_HD_LOG_DEBUG 1
#define USE_HD_LOG_ERROR 1
#define USE_HD_LOG_DATA 1
#define USE_HD_TRACE 1
#define USE_HD_FPGA_CHECK 1
#define USE_HD_LOG_DEBUG (1)
#define USE_HD_LOG_ERROR (1)
#define USE_HD_LOG_DATA (1)
#define USE_HD_TRACE (1)
#define USE_HD_FPGA_CHECK (1)
#endif
// 当前检查栈数量的线程总数
#define USE_CHECK_STACK_NUM (1)
// 使用系统的栈检查
#define USE_SYS_CHECK_STACK (0)
// 使用系统的堆检查
#define USE_SYS_CHECK_HEAP (0)
// 使用RTOS的日志锁
#define USE_RTOS_LOG_LOCK (0)
// 标准日志输出
#define _LogDExtLevel(level, format, ...) HDLogOut(1, level, __FILE__, __func__, __LINE__, format, ##__VA_ARGS__)
@ -64,6 +72,7 @@ enum eLogLevel {
kLogLevelError, ///< 错误信息
kLogLevelHex, ///< 十六进制日志
kLogLevelStack, ///< 打印栈大小信息
kLogLevelHeap, ///< 打印堆大小信息
kLogLevelTrace, ///< trace级别日志
kLogLevelMax,
};
@ -139,4 +148,10 @@ typedef uint32_t(*HDLogGetStackSizeType_t)(unsigned long currTask);
*/
void HDLogSetGetCurrStackSizeCall(HDLogGetStackSizeType_t getStackSize);
/**
* @brief , RTOS
* @param getHeadSize
*/
void HDLogSetGetCurrHeapSizeCall(HDLogGetStackSizeType_t getHeapSize);
#endif // __HD_NEW_LOG_H__

View File

@ -8,6 +8,11 @@
#include <stdio.h>
#include <string.h>
#if USE_RTOS_LOG_LOCK
#include "FreeRTOS.h"
#include "semphr.h"
#endif
typedef struct HDLogStackCheckInfo
{
uint32_t enable : 1; ///< 是否使用
@ -51,8 +56,20 @@ static HBIT_DEFINE(sLogItem, kLogLevelMax);
static uint32_t (*sGetTime)() = NULL;
static HDLogGetTaskType_t sGetCurrTask = NULL;
static HDLogGetStackSizeType_t sGetCurrStackSize = NULL;
#if USE_SYS_CHECK_HEAP
static HDLogGetStackSizeType_t sGetCurrHeapSize = NULL;
#endif
static HDLogStackCheckInfo sStackCheckInfo[USE_CHECK_STACK_NUM];
#if USE_RTOS_LOG_LOCK
static SemaphoreHandle_t sLogMutex = NULL;
#define LOG_LOCK() do { if (sLogMutex && xSemaphoreTake(sLogMutex, pdMS_TO_TICKS(2000)) != pdTRUE) { printf("log mutex timeout\r\n"); } } while (0)
#define LOG_UNLOCK() do { if (sLogMutex) { xSemaphoreGive(sLogMutex); } } while (0)
#else
#define LOG_LOCK()
#define LOG_UNLOCK()
#endif
#if USE_HD_FPGA_CHECK
static HDLogFpgaCheckInfo sFpgaCheckInfo;
#define FPGA_TYPE_INDEX(index, maxValue) ((index) > (maxValue) ? (maxValue) : (index))
@ -116,6 +133,11 @@ void HDLogInit(uint32_t (*getTime)())
HBitSet(sLogItem, kLogLevelError, 1);
HBitSet(sLogItem, kLogLevelHex, 0);
HBitSet(sLogItem, kLogLevelStack, 1);
HBitSet(sLogItem, kLogLevelHeap, 0);
#if USE_RTOS_LOG_LOCK
sLogMutex = xSemaphoreCreateMutex();
#endif
}
void HDLogOut(uint8_t ext, uint8_t level, const char *fileName, const char *funcName, int line, const char *format, ...)
@ -155,7 +177,10 @@ void HDLogOut(uint8_t ext, uint8_t level, const char *fileName, const char *func
"%d" _LOG_COLOR_END \
"]"
#define _LOG_USE_COLOR(outColor) if (color) { printf(outColor); }
uint8_t color = HBitGet(sLogItem, kLogLevelColor);
LOG_LOCK();
if (ext == 0)
{
if (sGetTime)
@ -167,47 +192,47 @@ void HDLogOut(uint8_t ext, uint8_t level, const char *fileName, const char *func
if (color)
{
printf(_LOG_BASE_INFO_COLOR, fileName, funcName, line);
printf(_LOG_COLOR_CYAN);
}
else
{
printf(_LOG_BASE_INFO, fileName, funcName, line);
}
}
else
{
if (color)
{
printf(_LOG_COLOR_CYAN);
}
}
if (HBitGet(sLogItem, kLogLevelStack))
{
_LOG_USE_COLOR(_LOG_COLOR_YELLOW);
printf("[0x%04X]", HDLogDebugGetCurrStackSize());
}
#if USE_SYS_CHECK_HEAP
if (HBitGet(sLogItem, kLogLevelHeap) && sGetCurrHeapSize)
{
_LOG_USE_COLOR(_LOG_COLOR_GREEN);
printf("[0x%04X]", sGetCurrHeapSize(0));
}
#endif
_LOG_USE_COLOR(_LOG_COLOR_CYAN);
va_list args;
va_start(args, format);
vprintf(format, args);
va_end(args);
if (color)
{
printf(_LOG_COLOR_END);
}
_LOG_USE_COLOR(_LOG_COLOR_END);
if (ext == 0)
{
printf("\r\n");
}
LOG_UNLOCK();
}
static uint8_t HDLogFpgaError(const uint8_t *data, int len, uint8_t isWrite, uint8_t checkFpga)
static void HDLogFpgaError(const uint8_t *data, int len, uint8_t isWrite, uint8_t checkFpga)
{
if (checkFpga == 0)
{
return 0;
return ;
}
#if USE_HD_FPGA_CHECK
@ -223,20 +248,21 @@ static uint8_t HDLogFpgaError(const uint8_t *data, int len, uint8_t isWrite, uin
if (HBitGet(sLogItem, kLogLevelHex) == 0)
{
return 0;
return ;
}
const char *opt = isWrite ? "write" : "read";
printf("%s fpga error, len[%d]\r\n", opt, len);
HDLogHex((const uint8_t *)(data), len, 1, NULL);
return 0;
return ;
}
uint8_t HDLogPrintFpgaHeader(const uint8_t *data, int len, uint8_t isWrite, uint8_t checkFpga)
{
const HBitType isPrint = HBitGet(sLogItem, kLogLevelHex);
LOG_LOCK();
// 记录打印时间
if (isPrint && sGetTime)
{
@ -246,20 +272,26 @@ uint8_t HDLogPrintFpgaHeader(const uint8_t *data, int len, uint8_t isWrite, uint
if (len <= 21)
{
return HDLogFpgaError(data, len, isWrite, checkFpga);
HDLogFpgaError(data, len, isWrite, checkFpga);
LOG_UNLOCK();
return 0;
}
for (uint8_t i = 0; i < 7; ++i)
{
if (0x55 != data[i])
{
return HDLogFpgaError(data, len, isWrite, checkFpga);
HDLogFpgaError(data, len, isWrite, checkFpga);
LOG_UNLOCK();
return 0;
}
}
if (0xd5 != data[7])
{
return HDLogFpgaError(data, len, isWrite, checkFpga);
HDLogFpgaError(data, len, isWrite, checkFpga);
LOG_UNLOCK();
return 0;
}
uint8_t type = data[8];
@ -271,12 +303,16 @@ uint8_t HDLogPrintFpgaHeader(const uint8_t *data, int len, uint8_t isWrite, uint
uint16_t effectLen = (uint16_t)((data[15] << 8) | data[16]);
if (effectLen < 16)
{
return HDLogFpgaError(data, len, isWrite, checkFpga);
HDLogFpgaError(data, len, isWrite, checkFpga);
LOG_UNLOCK();
return 0;
}
if (len < 21 + effectLen)
{
return HDLogFpgaError(data, len, isWrite, checkFpga);
HDLogFpgaError(data, len, isWrite, checkFpga);
LOG_UNLOCK();
return 0;
}
uint32_t count = 0;
@ -298,12 +334,14 @@ uint8_t HDLogPrintFpgaHeader(const uint8_t *data, int len, uint8_t isWrite, uint
if (isPrint == 0)
{
LOG_UNLOCK();
return 0;
}
const char *opt = isWrite ? "write" : "read";
printf("%s[%u], typeCount[%u] - type[%02X], sendCard[%d], netPort[%d], cardNumber[%d], subFunc[%04X], group[%04X], effectLen[%d]\r\n", opt, count, typeCount, type, sendCard, port, recvCardNumber, subFunc, group, effectLen);
LOG_UNLOCK();
return 1;
}
@ -319,6 +357,7 @@ void HDLogHex(const uint8_t *data, int len, uint8_t checkPrint, uint32_t *offset
return;
}
LOG_LOCK();
#define PRINT_HEX(...) printf(__VA_ARGS__);
if (offset == NULL || *offset == 0)
@ -364,6 +403,7 @@ void HDLogHex(const uint8_t *data, int len, uint8_t checkPrint, uint32_t *offset
PRINT_HEX("\r\n");
}
#undef PRINT_HEX
LOG_UNLOCK();
}
void HDLogData(const uint8_t *data, int len, uint8_t checkPrint)
@ -373,6 +413,7 @@ void HDLogData(const uint8_t *data, int len, uint8_t checkPrint)
return;
}
LOG_LOCK();
for (int i = 0; i < len; ++i)
{
printf("%02X ", data[i]);
@ -381,6 +422,7 @@ void HDLogData(const uint8_t *data, int len, uint8_t checkPrint)
printf("\r\n");
}
}
LOG_UNLOCK();
}
static int16_t HDLogGetIndex(unsigned long id)
@ -443,6 +485,13 @@ void HDLogDebugDeleteStackAddr(unsigned long currTask)
uint32_t HDLogDebugGetCurrStackSize()
{
uint32_t currSize = 0;
#if USE_SYS_CHECK_STACK
if (sGetCurrStackSize)
{
currSize = sGetCurrStackSize(0);
}
#else
void *curr = NULL;
int16_t index = HDLogGetIndex(0);
if (index == -1)
@ -450,7 +499,7 @@ uint32_t HDLogDebugGetCurrStackSize()
return 0;
}
uint32_t currSize = sStackCheckInfo[index].stackAddr - (unsigned long)&curr;
currSize = sStackCheckInfo[index].stackAddr - (unsigned long)&curr;
if (sGetCurrStackSize)
{
currSize = sGetCurrStackSize(sStackCheckInfo[index].taskID);
@ -460,7 +509,7 @@ uint32_t HDLogDebugGetCurrStackSize()
{
printf("The current stack[%d] is insufficient, size[%d], taskId[%lx]\r\n", currSize, sStackCheckInfo[index].stackSize, sStackCheckInfo[index].taskID);
}
#endif
return currSize;
}
@ -468,3 +517,12 @@ void HDLogSetGetCurrStackSizeCall(HDLogGetStackSizeType_t getStackSize)
{
sGetCurrStackSize = getStackSize;
}
void HDLogSetGetCurrHeapSizeCall(HDLogGetStackSizeType_t getHeapSize)
{
#if USE_SYS_CHECK_HEAP
sGetCurrHeapSize = getHeapSize;
#else
(void)getHeapSize;
#endif
}