fix(debug): 确保根据条件正确启用断言和日志记录
``` 修改Assert和Log宏,以便根据__DEBUG_LEVEL__的值正确处理断言和日志记录。以前,某些情况下断言不会按预期触 发,而日志记录在__DEBUG_LEVEL__设置为1时没有被正确禁用。现在,Assert将在调试级别大于等于1时启用,Log 将在调试级别为2时启用。感谢[@yourname]的贡献。 ```
This commit is contained in:
parent
23ccebb4ec
commit
553e4f3df1
18
debug.h
18
debug.h
@ -20,6 +20,16 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
|
#ifdef __DEBUG_LEVEL__
|
||||||
|
#undef __DEBUG_LEVEL__
|
||||||
|
#endif
|
||||||
|
/* You can change the debug level here
|
||||||
|
* 0: no log
|
||||||
|
* 1: Assert
|
||||||
|
* 2: Assert + Log
|
||||||
|
*/
|
||||||
|
#define __DEBUG_LEVEL__ 9
|
||||||
|
|
||||||
// ----------- log -----------
|
// ----------- log -----------
|
||||||
|
|
||||||
#define ANSI_FG_BLACK "\33[1;30m"
|
#define ANSI_FG_BLACK "\33[1;30m"
|
||||||
@ -52,10 +62,15 @@
|
|||||||
printf(__VA_ARGS__); \
|
printf(__VA_ARGS__); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
|
#if __DEBUG_LEVEL__ >= 2
|
||||||
#define Log(format, ...) \
|
#define Log(format, ...) \
|
||||||
_Log(ANSI_FMT("[%s:%d %s] " format, ANSI_FG_BLUE) "\n", \
|
_Log(ANSI_FMT("[%s:%d %s] " format, ANSI_FG_BLUE) "\n", \
|
||||||
__FILE__, __LINE__, __func__, ## __VA_ARGS__)
|
__FILE__, __LINE__, __func__, ## __VA_ARGS__)
|
||||||
|
#else
|
||||||
|
#define Log(format, ...) (NULL)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef __DEBUG_LEVEL__ >= 1
|
||||||
#define Assert(cond, format, ...) \
|
#define Assert(cond, format, ...) \
|
||||||
do { \
|
do { \
|
||||||
if (!(cond)) { \
|
if (!(cond)) { \
|
||||||
@ -64,6 +79,9 @@
|
|||||||
assert(cond); \
|
assert(cond); \
|
||||||
} \
|
} \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
#else
|
||||||
|
#define Assert(cond, format, ...) (NULL)
|
||||||
|
#endif
|
||||||
|
|
||||||
#define panic(format, ...) Assert(0, format, ## __VA_ARGS__)
|
#define panic(format, ...) Assert(0, format, ## __VA_ARGS__)
|
||||||
|
|
||||||
|
2
main.c
2
main.c
@ -45,7 +45,7 @@ static struct {
|
|||||||
} cmd_table [] = {
|
} cmd_table [] = {
|
||||||
{ "help", "Display information about all supported commands", cmd_help },
|
{ "help", "Display information about all supported commands", cmd_help },
|
||||||
{ "p", "p EXPR: To evaluate an expression EXPR and output the result", cmd_p },
|
{ "p", "p EXPR: To evaluate an expression EXPR and output the result", cmd_p },
|
||||||
{ "q", "Exit NEMU", cmd_q },
|
{ "q", "Exit EXPR", cmd_q },
|
||||||
|
|
||||||
/* TODO: Add more commands */
|
/* TODO: Add more commands */
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user