From 02894b25b6189c7575b0ef4cdeb82ca172f965c5 Mon Sep 17 00:00:00 2001 From: ax-6 Date: Tue, 9 Jul 2024 23:09:11 +0800 Subject: [PATCH] Updated some debug code. --- aq/aq.c | 4 +- aqvm/memory/memory.c | 95 ++++++++++++++++++-------------- aqvm/runtime/debugger/debugger.c | 42 ++++---------- aqvm/runtime/debugger/debugger.h | 47 ++++------------ 4 files changed, 80 insertions(+), 108 deletions(-) diff --git a/aq/aq.c b/aq/aq.c index e5f1339..fc7ea4d 100644 --- a/aq/aq.c +++ b/aq/aq.c @@ -12,8 +12,8 @@ int main(int argc, char *argv[]) { // TODO(Aqvm): Finish this function after completing AQVM development. if (Aqvm_InitVm() != 0) { - AqvmRuntimeDebugger_OutputReport(2, "Aqmain_InitVmError", - "Initializing Aqvm met error.", NULL); + AqvmRuntimeDebugger_OutputReport("\"ERROR\"", "\"main_InitVmError\"", + "\"Initializing Aqvm met error.\"", NULL); return -1; } return 0; diff --git a/aqvm/memory/memory.c b/aqvm/memory/memory.c index ef851f2..89c0c35 100644 --- a/aqvm/memory/memory.c +++ b/aqvm/memory/memory.c @@ -16,47 +16,51 @@ int AqvmMemory_CheckMemoryConditions() { int warning_count = 0; if (sizeof(aqint) != 4) { AqvmRuntimeDebugger_OutputReport( - 1, "AqvmMemoryCheckMemoryConditions_IntLengthWarning", - "The length requirement for the int type does not conform to the type " - "definition.", + "\"WARNING\"", "\"AqvmMemory_CheckMemoryConditions_IntLengthWarning\"", + "\"The length requirement for the int type does not conform to the " + "type " + "definition.\"", NULL); ++warning_count; } if (sizeof(aqlong) != 8) { AqvmRuntimeDebugger_OutputReport( - 1, "AqvmMemoryCheckMemoryConditions_LongLengthWarning", - "The length requirement for the long type does not conform to the type " - "definition.", + "\"WARNING\"", "\"AqvmMemory_CheckMemoryConditions_LongLengthWarning\"", + "\"The length requirement for the long type does not conform to the " + "type " + "definition.\"", NULL); ++warning_count; } if (sizeof(aqfloat) != 4) { AqvmRuntimeDebugger_OutputReport( - 1, "AqvmMemoryCheckMemoryConditions_FloatLengthWarning", - "The length requirement for the float type does not conform to the " - "type definition.", + "\"WARNING\"", "\"AqvmMemory_CheckMemoryConditions_FloatLengthWarning\"", + "\"The length requirement for the float type does not conform to the " + "type definition.\"", NULL); ++warning_count; } if (sizeof(aqdouble) != 4) { AqvmRuntimeDebugger_OutputReport( - 1, "AqvmMemoryCheckMemoryConditions_DoubleLengthWarning", - "The length requirement for the double type does not conform to the " - "type definition.", + "\"WARNING\"", + "\"AqvmMemory_CheckMemoryConditions_DoubleLengthWarning\"", + "\"The length requirement for the double type does not conform to the " + "type definition.\"", NULL); ++warning_count; } if (sizeof(aqchar) != 1) { AqvmRuntimeDebugger_OutputReport( - 1, "AqvmMemoryCheckMemoryConditions_CharLengthWarning", - "The length requirement for the char type does not conform to the type " - "definition.", + "\"WARNING\"", "\"AqvmMemory_CheckMemoryConditions_CharLengthWarning\"", + "\"The length requirement for the char type does not conform to the " + "type " + "definition.\"", NULL); ++warning_count; } if (sizeof(aqbool) != 1) { AqvmRuntimeDebugger_OutputReport( - 1, "AqvmMemoryCheckMemoryConditions_BoolLengthWarning", + "\"WARNING\"", "\"AqvmMemory_CheckMemoryConditions_BoolLengthWarning\"", "The length requirement for the bool type does not conform to the type " "definition.", NULL); @@ -72,8 +76,8 @@ struct AqvmMemory_Memory* AqvmMemory_CreateMemory(void* data, void* type, (struct AqvmMemory_Memory*)malloc(sizeof(struct AqvmMemory_Memory)); if (memory_ptr == NULL) { AqvmRuntimeDebugger_OutputReport( - 2, "AqvmMemoryCreateMemory_MemoryAllocationFailure", - "Failed to allocate memory.", NULL); + "\"ERROR\"", "\"AqvmMemory_CreateMemory_MemoryAllocationFailure\"", + "\"Failed to allocate memory.\"", NULL); return NULL; } @@ -91,26 +95,30 @@ void AqvmMemory_FreeMemory(struct AqvmMemory_Memory* memory_ptr) { int AqvmMemory_SetType(const struct AqvmMemory_Memory* memory, size_t index, uint8_t type) { if (memory == NULL) { - AqvmRuntimeDebugger_OutputReport(1, "AqvmMemorySetType_NullMemoryPointer", - "The memory pointer is NULL.", NULL); + AqvmRuntimeDebugger_OutputReport("\"ERROR\"", + "\"AqvmMemory_SetType_NullMemoryPointer\"", + "\"The memory pointer is NULL.\"", NULL); return -1; } if (memory->type == NULL) { - AqvmRuntimeDebugger_OutputReport(1, "AqvmMemorySetType_NullTypePointer", - "The type pointer is NULL.", NULL); + AqvmRuntimeDebugger_OutputReport("\"ERROR\"", + "\"AqvmMemory_SetType_NullTypePointer\"", + "\"The type pointer is NULL.\"", NULL); return -2; } if (index > memory->size) { - AqvmRuntimeDebugger_OutputReport(1, "AqvmMemorySetType_OutOfMemoryRange", - "The index is out of memory range.", NULL); + AqvmRuntimeDebugger_OutputReport( + "\"ERROR\"", "\"AqvmMemory_SetType_OutOfMemoryRange\"", + "\"The index is out of memory range.\"", NULL); return -3; } if (type > 0x0F) { - AqvmRuntimeDebugger_OutputReport(1, "AqvmMemorySetType_OutOfTypeRange", - "The type is out of range.", NULL); + AqvmRuntimeDebugger_OutputReport("\"ERROR\"", + "\"AqvmMemory_SetType_OutOfTypeRange\"", + "\"The type is out of range.\"", NULL); return -4; } @@ -125,20 +133,23 @@ int AqvmMemory_SetType(const struct AqvmMemory_Memory* memory, size_t index, uint8_t AqvmMemory_GetType(struct AqvmMemory_Memory* memory, size_t index) { if (memory == NULL) { - AqvmRuntimeDebugger_OutputReport(1, "AqvmMemoryGetType_NullMemoryPointer", - "The memory pointer is NULL.", NULL); + AqvmRuntimeDebugger_OutputReport("\"ERROR\"", + "\"AqvmMemory_GetType_NullMemoryPointer\"", + "\"The memory pointer is NULL.\"", NULL); return 0x11; } if (memory->type == NULL) { - AqvmRuntimeDebugger_OutputReport(1, "AqvmMemoryGetType_NullTypePointer", - "The type pointer is NULL.", NULL); + AqvmRuntimeDebugger_OutputReport("\"ERROR\"", + "\"AqvmMemory_GetType_NullTypePointer\"", + "\"The type pointer is NULL.\"", NULL); return 0x12; } if (index > memory->size) { - AqvmRuntimeDebugger_OutputReport(1, "AqvmMemoryGetType_OutOfMemoryRange", - "The index is out of memory range.", NULL); + AqvmRuntimeDebugger_OutputReport( + "\"ERROR\"", "\"AqvmMemory_GetType_OutOfMemoryRange\"", + "\"The index is out of memory range.\"", NULL); return 0x13; } @@ -152,26 +163,30 @@ uint8_t AqvmMemory_GetType(struct AqvmMemory_Memory* memory, size_t index) { int AqvmMemory_WriteData(struct AqvmMemory_Memory* memory, size_t index, void* data_ptr, size_t size) { if (memory == NULL) { - AqvmRuntimeDebugger_OutputReport(1, "AqvmMemoryWriteData_NullMemoryPointer", - "The memory pointer is NULL.", NULL); + AqvmRuntimeDebugger_OutputReport( + "\"ERROR\"", "\"AqvmMemory_WriteData_NullMemoryPointer\"", + "\"The memory pointer is NULL.\"", NULL); return -1; } if (memory->type == NULL) { - AqvmRuntimeDebugger_OutputReport(1, "AqvmMemoryWriteData_NullTypePointer", - "The type pointer is NULL.", NULL); + AqvmRuntimeDebugger_OutputReport("\"ERROR\"", + "\"AqvmMemory_WriteData_NullTypePointer\"", + "\"The type pointer is NULL.\"", NULL); return -2; } if (index > memory->size) { - AqvmRuntimeDebugger_OutputReport(1, "AqvmMemoryWriteData_OutOfMemoryRange", - "The index is out of memory range.", NULL); + AqvmRuntimeDebugger_OutputReport( + "\"ERROR\"", "\"AqvmMemory_WriteData_OutOfMemoryRange\"", + "\"The index is out of memory range.\"", NULL); return -3; } if (data_ptr == NULL) { - AqvmRuntimeDebugger_OutputReport(1, "AqvmMemoryWriteData_NullDataPointer", - "The data pointer is NULL.", NULL); + AqvmRuntimeDebugger_OutputReport("\"ERROR\"", + "\"AqvmMemory_WriteData_NullDataPointer\"", + "\"The data pointer is NULL.\"", NULL); return -4; } diff --git a/aqvm/runtime/debugger/debugger.c b/aqvm/runtime/debugger/debugger.c index 923c57c..256099b 100644 --- a/aqvm/runtime/debugger/debugger.c +++ b/aqvm/runtime/debugger/debugger.c @@ -10,24 +10,12 @@ #include #include -void AqvmRuntimeDebugger_OutputReport(uint8_t type, const char* code, +void AqvmRuntimeDebugger_OutputReport(const char* type, const char* code, const char* message, const char* other_info) { - const char* type_str = - AqvmRuntimeDebugger_FormatReport(type, code, message, other_info); - - char time[28]; - AqvmRuntimeDebugger_GetCurrentTime(time); - - fprintf(stderr, - "{\"Time\":%s,\"Type\":%s,\"Code\":%s,\"Message\":%s,\"ErrnoInfo\":{" - "\"Errno\":%d,\"Message\":\"%s\"},\"OtherInfo\":%s}\n", - time, type_str, code, message, errno, strerror(errno), other_info); -} - -const char* AqvmRuntimeDebugger_FormatReport(uint8_t type, const char* code, - const char* message, - const char* other_info) { + if (type == NULL) { + type = "NULL"; + } if (code == NULL) { code = "NULL"; } @@ -38,20 +26,12 @@ const char* AqvmRuntimeDebugger_FormatReport(uint8_t type, const char* code, other_info = "NULL"; } - switch (type) { - case 0: - return "\"INFO\""; - case 1: - return "\"WARNING\""; - case 2: - return "\"ERROR\""; - default: - return "NULL"; - } -} - -void AqvmRuntimeDebugger_GetCurrentTime(char* return_time) { + char time_str[28]; time_t current_time = time(NULL); - strftime(return_time, 28, "\"%Y-%m-%dT%H:%M:%S%z\"", - localtime(¤t_time)); + strftime(time_str, 28, "\"%Y-%m-%dT%H:%M:%S%z\"", localtime(¤t_time)); + + fprintf(stderr, + "{\"Time\":%s,\"Type\":%s,\"Code\":%s,\"Message\":%s,\"ErrnoInfo\":{" + "\"Errno\":%d,\"Message\":\"%s\"},\"OtherInfo\":%s}\n", + time_str, type, code, message, errno, strerror(errno), other_info); } \ No newline at end of file diff --git a/aqvm/runtime/debugger/debugger.h b/aqvm/runtime/debugger/debugger.h index b3e752d..aa1b007 100644 --- a/aqvm/runtime/debugger/debugger.h +++ b/aqvm/runtime/debugger/debugger.h @@ -7,44 +7,21 @@ #include -// The struct stores information about the debug report. |type| is the type of -// the report. In |type|, 0 is INFO, 1 is WARNING, and 2 is ERROR. -// |type|, |code|, and |message| are necessary and shouldn't be set to NULL in -// common. |other_info| is a pointer to the other information and it can be set -// to NULL if it is not needed. -// NOTICE: If you need to use it, please use json format. -struct AqvmRuntimeDebugger_DebugReport { - uint8_t type; - char* code; - char* message; - char* other_info; -}; - // Outputs |report| with time, errno and so on to printing to the console or // other devices and writing to a log file. No return. -// |type| is the type of the report. In |type|, 0 is INFO, 1 is WARNING, and 2 -// is ERROR. |type|, |code|, and |message| are necessary and shouldn't be set to -// NULL in common. |other_info| is a pointer to the other information and it can -// be set to NULL if it is not needed. -// NOTICE: If you need to use it, please use json format. The output is json -// format. -void AqvmRuntimeDebugger_OutputReport(uint8_t type, const char* code, +// |type|, |code|, and |message| are necessary and shouldn't be set to NULL in +// common. But |other_info| can be set to NULL if it is not needed. +// In general, |type| should be "ERROR", "WARNING" or "INFO". |code| should be a +// full function name plus a concise description of the error, separated by +// underscores (e.g., AqvmRuntimeDebugger_OutputReport_TestInfo). |message| +// should be a detailed and accurate description. |other_info| on the other hand +// should be an additional information to the current report (e.g. system +// information). +// NOTICE: If you need to use the function, please use json format. The output +// is json format. For example, AqvmRuntimeDebugger_OutputReport("\"type\"", +// "\"code\"", "\"message\"", "\"other_info\""); +void AqvmRuntimeDebugger_OutputReport(const char* type, const char* code, const char* message, const char* other_info); -// INTERNAL USE ONLY. -// Gets the current time in a string format based on ISO 8601 standard and -// stores it in |return_time|. No return. -// |return_time| length is at least 28 bytes. -// NOTICE: The output is json format. -void AqvmRuntimeDebugger_GetCurrentTime(char* return_time); - -// INTERNAL USE ONLY. -// If |code|, |message|, or |other_info| is NULL, It will be set to the string -// "NULL". Returns type string accroding to the |type|. -// NOTICE: The output is json format. -const char* AqvmRuntimeDebugger_FormatReport(uint8_t type, const char* code, - const char* message, - const char* other_info); - #endif \ No newline at end of file