]> Pileus Git - lackey/commitdiff
Add error command and update debug messages
authorAndy Spencer <andy753421@gmail.com>
Sun, 2 Jun 2013 06:51:12 +0000 (06:51 +0000)
committerAndy Spencer <andy753421@gmail.com>
Tue, 4 Jun 2013 04:24:15 +0000 (04:24 +0000)
src/cal.c
src/main.c
src/util.c
src/util.h

index 838bf8723895656cf194725133a04c7d5912eacc..da835da7225f9280e9463031be8fb6b76983fc08 100644 (file)
--- 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);
 }
index 3a20fe87feaa9ae6fa00efd4c68dd8a9bf0ccd93..bec3b1c09616555ff72d6308a51735f92e056b69 100644 (file)
@@ -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;
 }
index 582b65deff8b70adc0c2f06dd248ac7fd92f6250..e3f3a43a8015cb630302d507b45c66f8f7b43a02 100644 (file)
 #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);
 }
index 6a619d5ba974c3a3ef3838919ad77cd7cb13c084..a424b7bd73e531dcd2e6a7fce0b197ede10cf3d9 100644 (file)
@@ -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, ...);