diff --git a/Makefile b/Makefile index 5ef77a3..39f8b4f 100644 --- a/Makefile +++ b/Makefile @@ -72,6 +72,10 @@ CFLAGS += -DDATADIR=\"$(RDDIR)\" CFLAGS += -DDATABASE=\"$(DATABASE)\" VERSION = 0.0.0 +.if defined(DEBUG) + CFLAGS += -DDEBUG +.endif + all: $(TARGET) $(HTMLS) $(JSMINS) api: swagger.json diff --git a/extern.h b/extern.h index c80b818..fc7a2b0 100644 --- a/extern.h +++ b/extern.h @@ -19,6 +19,12 @@ #ifndef EXTERN_H #define EXTERN_H +#ifdef DEBUG +#define debug(r, ...) kutil_logx(r, "DEBUG", NULL, __VA_ARGS__) +#else +#define debug(r, ...) +#endif + /* * A book series. */ diff --git a/main.c b/main.c index 3a48e9b..ba99131 100644 --- a/main.c +++ b/main.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include @@ -272,6 +273,7 @@ sendbooks(struct kreq *r) BookFull *b = NULL; int res, i = 0; + debug(r, "sendbooks"); int page = initpage(r); int per = initperpage(r); const char* sort = initsort(r); @@ -333,6 +335,7 @@ sendauthors(struct kreq *r) int res, i = 0; AuthorFull *a = NULL; + debug(r, "sendauthors"); int page = initpage(r); int per = initperpage(r); const char* sort = initsort(r); @@ -385,6 +388,7 @@ sendseries(struct kreq *r) int res, i = 0; SeriesFull *s = NULL; + debug(r, "sendseries"); int page = initpage(r); int per = initperpage(r); const char* sort = initsort(r); @@ -454,6 +458,7 @@ sendindex(struct kreq *r) { struct kjsonreq req; + debug(r, "sendindex"); http_open(r, KHTTP_200); kjson_open(&req, r); kjson_obj_open(&req); @@ -468,6 +473,8 @@ main(void) struct kreq r; enum kcgi_err er; + kutil_openlog(LOGFILE); + /* Actually parse HTTP document. */ er = khttp_parsex(&r, ksuffixmap, @@ -488,6 +495,7 @@ main(void) } #endif + /* * Front line of defence: make sure we're a proper method, make * sure we're a page, make sure we're a JSON file. @@ -506,12 +514,15 @@ main(void) return(EXIT_SUCCESS); } + debug(&r, "Opening database at %s/%s", DATADIR, DATABASE); if ( ! db_open(&r, DATADIR "/" DATABASE)) { + kutil_warn(&r, NULL, "Error opening database at %s/%s", DATADIR, DATABASE); http_open(&r, KHTTP_500); json_emptydoc(&r); khttp_free(&r); return(EXIT_SUCCESS); } + debug(&r, "Database ready %s/%s", DATADIR, DATABASE); switch (r.page) { case (PAGE_INDEX):