From 47aca32da73a7e6c8459adf0b432c10288ba839d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Kujawa?= Date: Wed, 21 Mar 2018 16:22:29 +0100 Subject: [PATCH] Add minimal logging functionality. --- src/Makefile | 3 ++- src/log.c | 37 +++++++++++++++++++++++++++++++++++++ src/log.h | 14 ++++++++++++++ src/rk65c02.c | 3 +++ 4 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 src/log.c create mode 100644 src/log.h diff --git a/src/Makefile b/src/Makefile index ea7ee87..cd01075 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,13 +1,14 @@ CLI=rk65c02cli CLI_OBJS=rk65c02cli.o -LIB_OBJS=rk65c02.o bus.o instruction.o emulation.o debug.o device_ram.o device_serial.o +LIB_OBJS=rk65c02.o bus.o instruction.o emulation.o debug.o device_ram.o device_serial.o log.o LIB_SO=librk65c02.so LIB_STATIC=librk65c02.a LDFLAGS_SO=-shared LDFLAGS_CLI=-lreadline CFLAGS=-Wall -fpic -ggdb +#CFLAGS=-Wall -fpic -ggdb -I/opt/local/include/uthash 65C02ISA=65c02isa EMULATION=emulation diff --git a/src/log.c b/src/log.c new file mode 100644 index 0000000..15dde25 --- /dev/null +++ b/src/log.c @@ -0,0 +1,37 @@ +#include +#include + +#include "log.h" + +static const char *level_str[] = { + "", + "CRITICAL", + "ERROR", + "INFO", + "DEBUG", + "TRACE" +}; + +static uint8_t level = LOG_INFO; + +void rk6502_loglevel_set(uint8_t l) +{ + level = l; +} + +void rk6502_log(uint8_t l, const char* fmt, ...) +{ + va_list args; + + if (l > level) + return; + + fprintf(stderr, "%s:\t", level_str[l]); + + va_start(args, fmt); + vfprintf(stderr, fmt, args); + va_end(args); + + fprintf(stderr, "\n"); +} + diff --git a/src/log.h b/src/log.h new file mode 100644 index 0000000..99c0eac --- /dev/null +++ b/src/log.h @@ -0,0 +1,14 @@ +#include + +#define LOG_TRACE 5 +#define LOG_DEBUG 4 +#define LOG_INFO 3 +#define LOG_ERROR 2 +#define LOG_CRIT 1 +#define LOG_NOTHING 0 /* At 0 nothing will get logged, can be set as + current level, but not when creating new log + messages. */ + +void rk6502_loglevel_set(uint8_t); +void rk6502_log(uint8_t, const char *, ...); + diff --git a/src/rk65c02.c b/src/rk65c02.c index 2a766a8..8b95f06 100644 --- a/src/rk65c02.c +++ b/src/rk65c02.c @@ -9,6 +9,7 @@ #include "bus.h" #include "instruction.h" #include "rk65c02.h" +#include "log.h" #include "debug.h" void rk65c02_exec(rk65c02emu_t *); @@ -34,6 +35,8 @@ rk65c02_init(bus_t *b) e.trace_head = NULL; e.runtime_disassembly = false; + rk6502_log(LOG_DEBUG, "Initialized new emulator."); + return e; }