fix(debug): 确保根据条件正确启用断言和日志记录

```
修改Assert和Log宏,以便根据__DEBUG_LEVEL__的值正确处理断言和日志记录。以前,某些情况下断言不会按预期触
发,而日志记录在__DEBUG_LEVEL__设置为1时没有被正确禁用。现在,Assert将在调试级别大于等于1时启用,Log
将在调试级别为2时启用。感谢[@yourname]的贡献。
```
This commit is contained in:
zzy 2024-08-27 09:23:42 +08:00
parent 23ccebb4ec
commit 553e4f3df1
2 changed files with 19 additions and 1 deletions

18
debug.h
View File

@ -20,6 +20,16 @@
#include <stdio.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 -----------
#define ANSI_FG_BLACK "\33[1;30m"
@ -52,10 +62,15 @@
printf(__VA_ARGS__); \
} while (0)
#if __DEBUG_LEVEL__ >= 2
#define Log(format, ...) \
_Log(ANSI_FMT("[%s:%d %s] " format, ANSI_FG_BLUE) "\n", \
__FILE__, __LINE__, __func__, ## __VA_ARGS__)
#else
#define Log(format, ...) (NULL)
#endif
#ifdef __DEBUG_LEVEL__ >= 1
#define Assert(cond, format, ...) \
do { \
if (!(cond)) { \
@ -64,6 +79,9 @@
assert(cond); \
} \
} while (0)
#else
#define Assert(cond, format, ...) (NULL)
#endif
#define panic(format, ...) Assert(0, format, ## __VA_ARGS__)

2
main.c
View File

@ -45,7 +45,7 @@ static struct {
} cmd_table [] = {
{ "help", "Display information about all supported commands", cmd_help },
{ "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 */