From f5dbd6f6355e32e2a888aaba6902c40448caefe0 Mon Sep 17 00:00:00 2001 From: ponty Date: Fri, 22 Feb 2013 16:38:14 +0100 Subject: [PATCH] refactor logger --- simavr/sim/sim_avr.c | 15 +++++++++------ simavr/sim/sim_avr.h | 36 ++++++++++++++++++------------------ simavr/sim/sim_elf.c | 10 +++++----- 3 files changed, 32 insertions(+), 29 deletions(-) diff --git a/simavr/sim/sim_avr.c b/simavr/sim/sim_avr.c index 188636d..79560d9 100644 --- a/simavr/sim/sim_avr.c +++ b/simavr/sim/sim_avr.c @@ -35,7 +35,7 @@ #define AVR_KIND_DECL #include "sim_core_decl.h" -void std_logger(const int level, const char * format, ... ); +void std_logger(avr_t* avr, const int level, const char * format, ... ); logger_t global_logger = std_logger; int avr_init(avr_t * avr) @@ -352,11 +352,14 @@ avr_make_mcu_by_name( return avr; } -void std_logger(const int level, const char * format, ... ) { - va_list args; - va_start(args, format); - vfprintf((level > LOG_ERROR) ? stdout : stderr , format, args); - va_end(args); +void std_logger(avr_t* avr, const int level, const char * format, ... ) { + if (!avr || avr->log >= level) + { + va_list args; + va_start(args, format); + vfprintf((level > LOG_ERROR) ? stdout : stderr , format, args); + va_end(args); + } } diff --git a/simavr/sim/sim_avr.h b/simavr/sim/sim_avr.h index b2d935a..80f7341 100644 --- a/simavr/sim/sim_avr.h +++ b/simavr/sim/sim_avr.h @@ -61,24 +61,6 @@ enum { #define AVR_DATA_TO_IO(v) ((v) - 32) #define AVR_IO_TO_DATA(v) ((v) + 32) -/** - * Logging macros and associated log levels. - * The current log level is kept in avr->log. - */ -enum { - LOG_ERROR = 1, - LOG_WARNING, - LOG_TRACE, -}; -typedef void (*logger_t)(const int level, const char * format, ... ); -extern logger_t global_logger; -#ifndef AVR_LOG -#define AVR_LOG(avr, level, ...) \ - do { \ - if (!avr || avr->log >= level) \ - global_logger( level, __VA_ARGS__); \ - } while(0) -#endif /* * Core states. @@ -379,6 +361,24 @@ void avr_callback_run_gdb(avr_t * avr); void avr_callback_sleep_raw(avr_t * avr, avr_cycle_count_t howLong); void avr_callback_run_raw(avr_t * avr); +/** + * Logging macros and associated log levels. + * The current log level is kept in avr->log. + */ +enum { + LOG_ERROR = 1, + LOG_WARNING, + LOG_TRACE, +}; +typedef void (*logger_t)(avr_t* avr, const int level, const char * format, ... ); +extern logger_t global_logger; +#ifndef AVR_LOG +#define AVR_LOG(avr, level, ...) \ + do { \ + global_logger( avr, level, __VA_ARGS__); \ + } while(0) +#endif + #ifdef __cplusplus }; #endif diff --git a/simavr/sim/sim_elf.c b/simavr/sim/sim_elf.c index 5d19128..6231fea 100644 --- a/simavr/sim/sim_elf.c +++ b/simavr/sim/sim_elf.c @@ -147,7 +147,7 @@ static void elf_parse_mmcu_section(elf_firmware_t * firmware, uint8_t * src, uin uint8_t mask = src[0]; uint16_t addr = src[1] | (src[2] << 8); char * name = (char*)src + 3; - AVR_LOG(((avr_t*)0), LOG_TRACE, "AVR_MMCU_TAG_VCD_TRACE %04x:%02x - %s\n", addr, mask, name); + AVR_LOG(NULL, LOG_TRACE, "AVR_MMCU_TAG_VCD_TRACE %04x:%02x - %s\n", addr, mask, name); firmware->trace[firmware->tracecount].mask = mask; firmware->trace[firmware->tracecount].addr = addr; strncpy(firmware->trace[firmware->tracecount].name, name, @@ -181,7 +181,7 @@ int elf_read_firmware(const char * file, elf_firmware_t * firmware) if ((fd = open(file, O_RDONLY | O_BINARY)) == -1 || (read(fd, &elf_header, sizeof(elf_header))) < sizeof(elf_header)) { - AVR_LOG(((avr_t*)0), LOG_ERROR, "could not read %s\n", file); + AVR_LOG(NULL, LOG_ERROR, "could not read %s\n", file); perror(file); close(fd); return -1; @@ -290,12 +290,12 @@ int elf_read_firmware(const char * file, elf_firmware_t * firmware) // hdump("code", data_text->d_buf, data_text->d_size); memcpy(firmware->flash + offset, data_text->d_buf, data_text->d_size); offset += data_text->d_size; - AVR_LOG(((avr_t*)0), LOG_TRACE, "Loaded %u .text\n", (unsigned int)data_text->d_size); + AVR_LOG(NULL, LOG_TRACE, "Loaded %u .text\n", (unsigned int)data_text->d_size); } if (data_data) { // hdump("data", data_data->d_buf, data_data->d_size); memcpy(firmware->flash + offset, data_data->d_buf, data_data->d_size); - AVR_LOG(((avr_t*)0), LOG_TRACE, "Loaded %u .data\n", (unsigned int)data_data->d_size); + AVR_LOG(NULL, LOG_TRACE, "Loaded %u .data\n", (unsigned int)data_data->d_size); offset += data_data->d_size; firmware->datasize = data_data->d_size; } @@ -303,7 +303,7 @@ int elf_read_firmware(const char * file, elf_firmware_t * firmware) // hdump("eeprom", data_ee->d_buf, data_ee->d_size); firmware->eeprom = malloc(data_ee->d_size); memcpy(firmware->eeprom, data_ee->d_buf, data_ee->d_size); - AVR_LOG(((avr_t*)0), LOG_TRACE, "Loaded %u .eeprom\n", (unsigned int)data_ee->d_size); + AVR_LOG(NULL, LOG_TRACE, "Loaded %u .eeprom\n", (unsigned int)data_ee->d_size); firmware->eesize = data_ee->d_size; } // hdump("flash", avr->flash, offset); -- 2.39.5