#include "common.h"
#include "options.h"
-static int log_level = LOG_LEVEL_ERR; /* LEVEL = ERROR */
+#define PREFIX_PACKAGE_NAME 1
+#define ADD_LINEFEED 1
-void
-log_set_level(int new_log_level)
+extern struct options_t options;
+
+static void
+log_prefix_package_name(FILE *stream, const char *severity)
{
- log_level = new_log_level;
+#if PREFIX_PACKAGE_NAME
+ /* Printing the name of the program first if desired. */
+ fprintf(stream, "%s %s: ", PACKAGE_NAME, severity);
+#endif /* PREFIX_PACKAGE_NAME */
+}
+
+static void
+log_suffix_newline(FILE *stream)
+{
+#if ADD_LINEFEED
+ fprintf(stream, "\n");
+#endif /* ADD_LINEFEED */
}
void
{
FILE *stream = stdout;
- if (log_level >= LOG_LEVEL_DEBUG) {
+ if (options.log >= LOG_LEVEL_DEBUG) {
va_list ap;
- /* Printing the name of the program first. */
- fprintf(stream, "%s debug : ", PACKAGE_NAME);
+ log_prefix_package_name(stream, "debug");
va_start(ap, format);
vfprintf(stream, format, ap);
va_end(ap);
- fprintf(stream, "\n");
+ log_suffix_newline(stream);
}
}
{
FILE *stream = stdout;
- if (log_level >= LOG_LEVEL_INFO) {
+ if (options.log >= LOG_LEVEL_INFO) {
va_list ap;
- /* Printing the name of the program first. */
- fprintf(stream, "%s info : ", PACKAGE_NAME);
+ log_prefix_package_name(stream, "info");
va_start(ap, format);
vfprintf(stream, format, ap);
va_end(ap);
- fprintf(stream, "\n");
+ log_suffix_newline(stream);
}
}
{
FILE *stream = stderr;
- if (log_level >= LOG_LEVEL_WARN) {
+ if (options.log >= LOG_LEVEL_WARN) {
va_list ap;
- /* Printing the name of the program first. */
- fprintf(stream, "%s warning: ", PACKAGE_NAME);
+ log_prefix_package_name(stream, "warn");
va_start(ap, format);
vfprintf(stream, format, ap);
va_end(ap);
- fprintf(stream, "\n");
+ log_suffix_newline(stream);
}
}
void
-log_fail(const char *format, ...)
+log_err(const char *format, ...)
{
FILE *stream = stderr;
va_list ap;
- /* Printing the name of the program first. */
- fprintf(stream, "%s error : ", PACKAGE_NAME);
+ log_prefix_package_name(stream, "error");
va_start(ap, format);
vfprintf(stream, format, ap);
va_end(ap);
- fprintf(stream, "\n");
-
- exit(EXIT_FAILURE);
+ log_suffix_newline(stream);
}
+/* Log error message and exits with error code. */
void
-log_fail_no_exit(const char *format, ...)
+log_fail(const char *format, ...)
{
FILE *stream = stderr;
va_list ap;
- /* Printing the name of the program first. */
- fprintf(stream, "%s error: ", PACKAGE_NAME);
+ log_prefix_package_name(stream, "error");
va_start(ap, format);
vfprintf(stream, format, ap);
va_end(ap);
- fprintf(stream, "\n");
+ log_suffix_newline(stream);
+
+ exit(EXIT_FAILURE);
}
#include "common.h"
enum LOG_LEVEL {
- LOG_LEVEL_ERR,
- LOG_LEVEL_WARN,
- LOG_LEVEL_INFO,
- LOG_LEVEL_DEBUG
+ LOG_LEVEL_ERR = 0, /* Display only errors */
+ LOG_LEVEL_WARN, /* Display warnings */
+ LOG_LEVEL_INFO, /* Display information messages */
+ LOG_LEVEL_DEBUG, /* Display all messages */
};
-void
-log_set_level(int new_log_level);
-
void
log_debug(const char *format, ...);
log_warn(const char *format, ...);
void
-log_fail(const char *format, ...);
+log_err(const char *format, ...);
+/* Log error message and exits with error code. */
void
-log_fail_no_exit(const char *format, ...);
+log_fail(const char *format, ...);
#endif /* LOG_H */
log_level = strtol(arg, &endptr, 0);
if (*endptr != '\0') {
- log_fail_no_exit("Invalid log level");
+ log_err("Invalid log level");
argp_usage(state);
}
if (log_level > LOG_LEVEL_DEBUG) {
- log_fail_no_exit("Invalid log level");
+ log_err("Invalid log level (0 to 3)");
argp_usage(state);
}
- log_set_level(log_level);
+ options.log = log_level;
}
static void
*dest = strtol(arg, &endptr, 0);
if (*endptr != '\0') {
- log_fail_no_exit("Invalid memory size");
+ log_err("Invalid memory size");
argp_usage(state);
}
}
options.pram_size = PGM_MEM_DEFAULT_SIZE;
options.iram_size = INT_MEM_MAX_SIZE;
options.xram_size = EXT_MEM_DEFAULT_SIZE;
+ options.log = LOG_LEVEL_ERR;
/* Parse our arguments. */
argp_parse(&argp, argc, argv, 0, 0, NULL);