X-Git-Url: http://pileus.org/git/?a=blobdiff_plain;ds=sidebyside;f=src%2Futil.c;h=e3f3a43a8015cb630302d507b45c66f8f7b43a02;hb=035bf3a6553362883c2451c36afc7766002941a3;hp=582b65deff8b70adc0c2f06dd248ac7fd92f6250;hpb=daded0f3fb930e7758e1009683c0cdb46eada3f6;p=lackey 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); }