guowenxue
2024-06-25 5fc803d51ca097f07b4efbe0290ccc540b0660df
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "board_common.h"
 
static osMutexId PrintMutex;
static int PrintCurLevel = TH_PRINT_CUR;
osMutexDef(PrintMutex);
 
static void Board_DebugMutexInit(void)
{
    PrintMutex = osMutexCreate(osMutex(PrintMutex));
}
 
static void Board_DebugMutexDeInit(void)
{
    osMutexDelete(PrintMutex);
}
 
static void Board_DebugSetLevel(int Level)
{    
    PrintCurLevel = Level;    
}
 
static void PrintToConsole(char *Color, char *Module, int Level, char *LevelName, char *FileName, int LineNum, const char *Func, char *Fmt, va_list argp)
{
    osMutexWait(PrintMutex, osWaitForever);
    if (Level < TH_PRINT_MAX)
        PRINTF("%s[%s][%s]>%s(%d,%s)>|", Color, Module, LevelName, FileName, LineNum, Func);
 
    //ÒÔÏ´úÂ룬²Î¿¼Reportº¯Êý¡£
    int8_t i8Ret;
    char *Buffer = NULL;
    uint32_t u8Size = 300;
 
    //va_list list;
    Buffer = (char*)pvPortMalloc(u8Size);
    if(Buffer == NULL)
    {
        osMutexRelease(PrintMutex);
        return;
    }
    memset(Buffer, 0x0, u8Size);
 
    while(1)
    {
        i8Ret = TH_VSNPRINTF(Buffer, u8Size, Fmt, argp);
        if(i8Ret > -1 && i8Ret < u8Size)
            break;
    }
    
    PRINTF("%s", Buffer);
 
    if (Buffer != NULL)
        vPortFree(Buffer);
    Buffer = NULL;
 
    /* »¹Ô­Ä¬ÈÏ×ÖÌåÑÕÉ« */
    if (Level < TH_PRINT_MAX)
       PRINTF("[%d]%c%c%s", (int32_t)uxTaskGetStackHighWaterMark(NULL), '\r', '\n', COLOR_NORMAL);
 
    osMutexRelease(PrintMutex);
}
 
//´úÂëÖеļõ1,¼Ó1,ÊÇ×Ö·û´®µÄ½ØÖ¹·û'\0'
void Board_DebugPrintf(char *Color, char *Module, int Level, char *LevelName, char *FileName, int LineNum, const char *Func, char *Fmt, ...)
{
    //ÇëÇó´òÓ¡µÄ¼¶±ð£¬±Èµ±Ç°ÉèÖõļ¶±ðµÍ£¬²»»á±»´òÓ¡
    if(Level < PrintCurLevel)
        return;
 
    va_list argp;
    va_start(argp, Fmt);
    PrintToConsole(Color, Module, Level, LevelName, FileName, LineNum, Func, Fmt, argp);
    va_end(argp);
}
 
void Board_DebugInit(int Level)
{
    Board_DebugMutexInit();
    Board_DebugSetLevel(Level);
}
 
void Board_DebugDeInit(void)
{
    Board_DebugMutexDeInit();
}