Add debug log functions
authorHugo Villeneuve <hugo@hugovil.com>
Thu, 10 Oct 2013 02:29:04 +0000 (22:29 -0400)
committerHugo Villeneuve <hugo@hugovil.com>
Wed, 6 Nov 2013 02:50:16 +0000 (21:50 -0500)
src/Makefile.am
src/common.h
src/log.c [new file with mode: 0644]
src/log.h [new file with mode: 0644]

index c980442..05c56da 100644 (file)
@@ -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 \
index 2fa6dcc..51a8c1c 100644 (file)
@@ -35,6 +35,8 @@
 #  include <strings.h>
 #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 (file)
index 0000000..2810390
--- /dev/null
+++ b/src/log.c
@@ -0,0 +1,120 @@
+/*
+ * log.c -- debug functions for logging.
+ *
+ * Copyright (C) 2011 Hugo Villeneuve <hugo@hugovil.com>
+ *
+ * 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 <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+
+#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 (file)
index 0000000..f305797
--- /dev/null
+++ b/src/log.h
@@ -0,0 +1,48 @@
+/*
+ * log.h
+ *
+ * Copyright (C) 2011 Hugo Villeneuve <hugo@hugovil.com>
+ *
+ * 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 <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <errno.h>
+#include <string.h>
+
+#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 */