Moved logging macros to base.h and use them everywhere

The macros used in the testing library are actually useful everywhere
so may as well use them.
This commit is contained in:
2024-07-07 03:16:42 +01:00
parent 74c5746bff
commit 7a5eee932a
5 changed files with 31 additions and 40 deletions

View File

@@ -27,6 +27,16 @@
#define TERM_RED "\033[31m"
#define TERM_RESET "\033[0m"
#define MESSAGE(FILE, COLOUR, NAME, FORMAT, ...) \
fprintf(FILE, "\t[" COLOUR "%s" TERM_RESET "]: " FORMAT, NAME, __VA_ARGS__)
#define INFO(NAME, FORMAT, ...) \
MESSAGE(stdout, TERM_YELLOW, NAME, FORMAT, __VA_ARGS__)
#define FAIL(NAME, FORMAT, ...) \
MESSAGE(stderr, TERM_RED, NAME, FORMAT, __VA_ARGS__)
#define SUCCESS(NAME, FORMAT, ...) \
MESSAGE(stdout, TERM_GREEN, NAME, FORMAT, __VA_ARGS__)
// Flags for program behaviour (usually related to printing)
#ifndef VERBOSE
#define VERBOSE 0

View File

@@ -24,16 +24,6 @@
#include <stdlib.h>
#include <string.h>
#define MESSAGE(FILE, COLOUR, NAME, FORMAT, ...) \
fprintf(FILE, "\t[" COLOUR "%s" TERM_RESET "]: " FORMAT, NAME, __VA_ARGS__)
#define INFO(NAME, FORMAT, ...) \
MESSAGE(stdout, TERM_YELLOW, NAME, FORMAT, __VA_ARGS__)
#define FAIL(NAME, FORMAT, ...) \
MESSAGE(stderr, TERM_RED, NAME, FORMAT, __VA_ARGS__)
#define SUCCESS(NAME, FORMAT, ...) \
MESSAGE(stdout, TERM_GREEN, NAME, FORMAT, __VA_ARGS__)
typedef void (*test_fn)(void);
struct Test
{

View File

@@ -13,6 +13,7 @@
* Description: Entrypoint to program
*/
#include "lib/base.h"
#include <stdio.h>
#include <stdlib.h>
@@ -39,7 +40,7 @@ int main(int argc, char *argv[])
const char *filename = argv[1];
#if VERBOSE >= 1
printf("[" TERM_YELLOW "INTERPRETER" TERM_RESET "]: `%s`\n", filename);
INFO("INTERPRETER", "`%s`\n", filename);
#endif
FILE *fp = fopen(filename, "rb");
@@ -52,8 +53,7 @@ int main(int argc, char *argv[])
if (!header_read)
{
fprintf(stderr, "[ERROR]: Could not deserialise program header in `%s`\n",
filename);
FAIL("ERROR", "Could not deserialise program header in `%s`\n", filename);
return 1;
}
// Ensure that we MUST have something to read
@@ -69,7 +69,7 @@ int main(int argc, char *argv[])
if (bytes_read == 0)
{
fprintf(stderr, "[ERROR]:%s [%lu]:", filename, read_err.index);
FAIL("ERROR", "%s [%lu]:", filename, read_err.index);
switch (read_err.type)
{
case READ_ERR_INVALID_OPCODE:
@@ -91,8 +91,7 @@ int main(int argc, char *argv[])
}
#if VERBOSE >= 1
printf("\t[" TERM_GREEN "SETUP" TERM_RESET "]: Read %lu instructions\n",
program.count);
SUCCESS("SETUP", "Read %lu instructions\n", program.count);
#endif
size_t stack_size = 256;
@@ -112,11 +111,8 @@ int main(int argc, char *argv[])
vm_load_call_stack(&vm, call_stack, call_stack_size);
#if VERBOSE >= 1
printf("\t[" TERM_GREEN "SETUP" TERM_RESET
"]: Loaded stack and program into VM\n");
#endif
#if VERBOSE >= 1
printf("[" TERM_YELLOW "INTERPRETER" TERM_RESET "]: Beginning execution\n");
SUCCESS("SETUP", "Loaded internals\n%s", "");
INFO("INTERPRETER", "Beginning execution\n%s", "");
#endif
err_t err = vm_execute_all(&vm);
@@ -124,7 +120,7 @@ int main(int argc, char *argv[])
if (err)
{
const char *error_str = err_as_cstr(err);
fprintf(stderr, "[ERROR]: %s\n", error_str);
FAIL("ERROR", "%s\n", error_str);
vm_print_all(&vm, stderr);
ret = 255 - err;
}
@@ -132,7 +128,7 @@ int main(int argc, char *argv[])
vm_stop(&vm);
#if VERBOSE >= 1
printf("[%sINTERPRETER%s]: Finished execution\n", TERM_GREEN, TERM_RESET);
SUCCESS("INTEPRETER", "Finished execution\n%s", "");
#endif
return ret;
}

View File

@@ -263,7 +263,7 @@ err_t vm_execute_all(vm_t *vm)
program->data.instructions[program->ptr].opcode != OP_HALT)
{
#if VERBOSE >= 2
fprintf(stdout, "[vm_execute_all]: Trace(Cycle %lu)\n", cycles);
INFO("vm_execute_all", "Trace(Cycle%lu)\n", cycles);
fputs(
"----------------------------------------------------------------------"
"----------\n",
@@ -319,8 +319,7 @@ err_t vm_execute_all(vm_t *vm)
}
#if VERBOSE >= 1
fprintf(stdout, "[%svm_execute_all%s]: Final VM state(Cycle %lu)\n",
TERM_YELLOW, TERM_RESET, cycles);
INFO("vm_execute_all", "Final VM State(Cycle %lu)\n", cycles);
vm_print_all(vm, stdout);
#endif
return err;

View File

@@ -52,18 +52,16 @@ void vm_stop(vm_t *vm)
{
#if VERBOSE >= 1
bool leaks = false;
printf("[" TERM_YELLOW "DATA" TERM_RESET "]: Checking for leaks...\n");
INFO("vm_stop", "Checking for leaks...\n%s", "");
if (vm->call_stack.ptr > 0)
{
leaks = true;
printf("\t[" TERM_RED "DATA" TERM_RESET "]: Call stack at %lu\n\t[" TERM_RED
"DATA" TERM_RESET "]\n\t[" TERM_RED "DATA" TERM_RESET "]: Call "
"stack trace:",
vm->call_stack.ptr);
FAIL("vm_stop", "Call stack at %lu\n", vm->call_stack.ptr);
FAIL("vm_stop", "Call stack trace:%s", "");
for (size_t i = vm->call_stack.ptr; i > 0; --i)
{
word_t w = vm->call_stack.address_pointers[i - 1];
printf("\t\t%lu: %lX", vm->call_stack.ptr - i, w);
printf("\t[%lu]: %lX", vm->call_stack.ptr - i, w);
if (i != 1)
printf(", ");
printf("\n");
@@ -80,22 +78,20 @@ void vm_stop(vm_t *vm)
capacities[i] = cur->available;
total_capacity += capacities[i];
}
printf("\t[" TERM_RED "DATA" TERM_RESET
"]: Heap: %luB (over %lu %s) not reclaimed\n",
total_capacity, size_pages, size_pages == 1 ? "page" : "pages");
FAIL("vm_stop", "Heap: %luB (over %lu %s) not reclaimed\n", total_capacity,
size_pages, size_pages == 1 ? "page" : "pages");
for (size_t i = 0; i < size_pages; i++)
printf("\t\t[%lu]: %luB lost\n", i, capacities[i]);
printf("\t[%lu]: %luB lost\n", i, capacities[i]);
}
if (vm->stack.ptr > 0)
{
leaks = true;
printf("\t[" TERM_RED "DATA" TERM_RESET "]: Stack: %luB not reclaimed\n",
vm->stack.ptr);
FAIL("vm_stop", "Stack: %luB not reclaimed\n", vm->stack.ptr);
}
if (leaks)
printf("[" TERM_RED "DATA" TERM_RESET "]: Leaks found\n");
FAIL("vm_stop", "Leaks found\n%s", "");
else
printf("[" TERM_GREEN "DATA" TERM_RESET "]: No leaks found\n");
SUCCESS("vm_stop", "No leaks found\n%s", "");
#endif
vm->registers = (struct Registers){0};