From a38ca01fb692688a3579630649fe43162df5990f Mon Sep 17 00:00:00 2001 From: Hugo Villeneuve Date: Wed, 9 Oct 2013 22:29:04 -0400 Subject: [PATCH] Add debug log functions --- src/Makefile.am | 1 + src/common.h | 2 + src/log.c | 120 ++++++++++++++++++++++++++++++++++++++++++++++++ src/log.h | 48 +++++++++++++++++++ 4 files changed, 171 insertions(+) create mode 100644 src/log.c create mode 100644 src/log.h diff --git a/src/Makefile.am b/src/Makefile.am index c980442..05c56da 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -17,6 +17,7 @@ common_SOURCES = \ instructions_8051.c \ options.c \ options.h \ + log.c log.h \ app-config.c app-config.h \ hexfile.c \ hexfile.h \ diff --git a/src/common.h b/src/common.h index 2fa6dcc..51a8c1c 100644 --- a/src/common.h +++ b/src/common.h @@ -35,6 +35,8 @@ # include #endif +#include "log.h" + #define FIXED_FONT "monospace 12" #define MAX_FILENAME_LENGTH 1024 diff --git a/src/log.c b/src/log.c new file mode 100644 index 0000000..2810390 --- /dev/null +++ b/src/log.c @@ -0,0 +1,120 @@ +/* + * log.c -- debug functions for logging. + * + * Copyright (C) 2011 Hugo Villeneuve + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + */ + +#include "config.h" + +#include +#include +#include +#include +#include + +#include "common.h" +#include "options.h" + +static int log_level = LOG_LEVEL_ERR; /* LEVEL = ERROR */ + +void +log_set_level(int new_log_level) +{ + log_level = new_log_level; +} + +void +log_debug(const char *format, ...) +{ + FILE *stream = stdout; + + if (log_level >= LOG_LEVEL_DEBUG) { + va_list ap; + + /* Printing the name of the program first. */ + fprintf(stream, "%s debug : ", PACKAGE_NAME); + + va_start(ap, format); + vfprintf(stream, format, ap); + va_end(ap); + + fprintf(stream, "\n"); + } +} + +void +log_info(const char *format, ...) +{ + FILE *stream = stdout; + + if (log_level >= LOG_LEVEL_INFO) { + va_list ap; + + /* Printing the name of the program first. */ + fprintf(stream, "%s info : ", PACKAGE_NAME); + + va_start(ap, format); + vfprintf(stream, format, ap); + va_end(ap); + + fprintf(stream, "\n"); + } +} + +void +log_warn(const char *format, ...) +{ + FILE *stream = stderr; + + if (log_level >= LOG_LEVEL_WARN) { + va_list ap; + + /* Printing the name of the program first. */ + fprintf(stream, "%s warning: ", PACKAGE_NAME); + + va_start(ap, format); + vfprintf(stream, format, ap); + va_end(ap); + + fprintf(stream, "\n"); + } +} + +void +log_fail(const char *format, ...) +{ + FILE *stream = stderr; + va_list ap; + + /* Printing the name of the program first. */ + fprintf(stream, "%s error : ", PACKAGE_NAME); + + va_start(ap, format); + vfprintf(stream, format, ap); + va_end(ap); + + fprintf(stream, "\n"); + + exit(EXIT_FAILURE); +} + +void +log_fail_no_exit(const char *format, ...) +{ + FILE *stream = stderr; + va_list ap; + + /* Printing the name of the program first. */ + fprintf(stream, "%s error: ", PACKAGE_NAME); + + va_start(ap, format); + vfprintf(stream, format, ap); + va_end(ap); + + fprintf(stream, "\n"); +} diff --git a/src/log.h b/src/log.h new file mode 100644 index 0000000..f305797 --- /dev/null +++ b/src/log.h @@ -0,0 +1,48 @@ +/* + * log.h + * + * Copyright (C) 2011 Hugo Villeneuve + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + */ + +#ifndef LOG_H +#define LOG_H 1 + +#include +#include +#include +#include +#include + +#include "common.h" + +enum LOG_LEVEL { + LOG_LEVEL_ERR, + LOG_LEVEL_WARN, + LOG_LEVEL_INFO, + LOG_LEVEL_DEBUG +}; + +void +log_set_level(int new_log_level); + +void +log_debug(const char *format, ...); + +void +log_info(const char *format, ...); + +void +log_warn(const char *format, ...); + +void +log_fail(const char *format, ...); + +void +log_fail_no_exit(const char *format, ...); + +#endif /* LOG_H */ -- 2.20.1