From 035bf3a6553362883c2451c36afc7766002941a3 Mon Sep 17 00:00:00 2001 From: Andy Spencer Date: Sun, 2 Jun 2013 06:51:12 +0000 Subject: [PATCH] Add error command and update debug messages --- src/cal.c | 4 ++-- src/main.c | 4 ++-- src/util.c | 70 ++++++++++++++++++++++++++++++++++++++---------------- src/util.h | 7 ++---- 4 files changed, 56 insertions(+), 29 deletions(-) diff --git a/src/cal.c b/src/cal.c index 838bf87..da835da 100644 --- a/src/cal.c +++ b/src/cal.c @@ -31,11 +31,11 @@ void cal_init(void) /* Debug */ for (event_t *e = EVENTS; e; e = e->next) - debug("event: %04d-%02d-%02d %02d:%02d: %s - %s\n", + debug("event: %04d-%02d-%02d %02d:%02d: %s - %s", e->start.year, e->start.month, e->start.day, e->start.hour, e->start.min, e->name, e->desc); for (todo_t *e = TODOS; e; e = e->next) - debug("todo: %04d-%02d-%02d %02d:%02d: %s - %s\n", + debug("todo: %04d-%02d-%02d %02d:%02d: %s - %s", e->start.year, e->start.month, e->start.day, e->start.hour, e->start.min, e->name, e->desc); } diff --git a/src/main.c b/src/main.c index 3a20fe8..bec3b1c 100644 --- a/src/main.c +++ b/src/main.c @@ -96,11 +96,11 @@ int main(int argc, char **argv) } if (view_run(chr, btn.bstate, btn.y, btn.x)) continue; - debug("main: Unhandled key - Dec %3d, Hex %02x, Oct %03o, Chr <%c>\n", + debug("main: Unhandled key - Dec %3d, Hex %02x, Oct %03o, Chr <%c>", chr, chr, chr, chr); } - /* Cleanup, see also on_sigint */ + /* Cleanup, see also on_sigint, error */ endwin(); return 0; } diff --git a/src/util.c b/src/util.c index 582b65d..e3f3a43 100644 --- a/src/util.c +++ b/src/util.c @@ -23,10 +23,44 @@ #include "date.h" #include "cal.h" #include "view.h" +#include "util.h" /* Static data */ static FILE *debug_fd = NULL; +/* Helper functions */ +static void message(FILE *output_fd, const char *prefix, const char *fmt, va_list ap) +{ + va_list tmp; + + /* Log to standard out */ + if (output_fd) { + va_copy(tmp, ap); + fprintf(output_fd, "%s: ", prefix); + vfprintf(output_fd, fmt, tmp); + fprintf(output_fd, "\n"); + } + + /* Log to debug file */ + if (debug_fd) { + va_copy(tmp, ap); + fprintf(debug_fd, "%s: ", prefix); + vfprintf(debug_fd, fmt, tmp); + fprintf(debug_fd, "\n"); + } + + /* Log to status bar */ + if (stdscr) { + va_copy(tmp, ap); + mvhline(LINES-2, 0, ACS_HLINE, COLS); + move(LINES-1, 0); + attron(COLOR_PAIR(COLOR_ERROR)); + vwprintw(stdscr, fmt, tmp); + attroff(COLOR_PAIR(COLOR_ERROR)); + clrtoeol(); + } +} + /* Initialize */ void util_init(void) { @@ -42,29 +76,25 @@ void strsub(char *str, char find, char repl) } /* Debugging functions */ -int debug(char *fmt, ...) +void debug(char *fmt, ...) { - int rval; va_list ap; + va_start(ap, fmt); + message(NULL, "debug", fmt, ap); + va_end(ap); +} - /* Log to debug file */ - if (debug_fd) { - va_start(ap, fmt); - vfprintf(debug_fd, "debug: ", ap); - rval = vfprintf(debug_fd, fmt, ap); - } - - /* Log to status bar */ +void error(char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + fflush(stdout); + fflush(stderr); + message(stderr, "error", fmt, ap); + va_end(ap); if (stdscr) { - va_start(ap, fmt); - mvhline(LINES-2, 0, ACS_HLINE, COLS); - move(LINES-1, 0); - attron(COLOR_PAIR(COLOR_ERROR)); - vwprintw(stdscr, fmt, ap); - attroff(COLOR_PAIR(COLOR_ERROR)); - clrtoeol(); + getch(); + endwin(); } - - va_end(ap); - return rval; + exit(-1); } diff --git a/src/util.h b/src/util.h index 6a619d5..a424b7b 100644 --- a/src/util.h +++ b/src/util.h @@ -29,8 +29,5 @@ void util_init(void); void strsub(char *str, char find, char repl); /* Debug functions */ -#ifdef DEBUG -int debug(char *fmt, ...); -#else -#define debug(...) -#endif +void debug(char *fmt, ...); +void error(char *fmt, ...); -- 2.43.2