From: Hugo Villeneuve Date: Thu, 10 Oct 2013 02:29:04 +0000 (-0400) Subject: Add debug log functions X-Git-Tag: v2.0.0~136 X-Git-Url: http://gitweb.hugovil.com/?a=commitdiff_plain;h=a38ca01fb692688a3579630649fe43162df5990f;p=emu8051.git Add debug log functions --- 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 */