diff --git a/config.def.h b/config.def.h index 08aa4ca..f100b45 100644 --- a/config.def.h +++ b/config.def.h @@ -35,15 +35,16 @@ static const linkStruct links[] = { }, }; -/* - * 0: Fatal errors - * 1: Errors - * 2: Normal output - * 3: Info messages - * 4: Verbose mode -*/ +enum logLevels { + LOG_FATAL, + LOG_ERROR, + LOG_OUTPUT, + LOG_INFO, + LOG_VERBOSE, +}; -static const int logLevel = 2; +// Print all messages at least as important as this level. +static const int logLevel = LOG_OUTPUT; // Set the maximum amount of redirects curl will follow. // Use 0 to disable redirects, and -1 for no limit. diff --git a/handlers.c b/handlers.c index d9d7c35..a8edd9e 100644 --- a/handlers.c +++ b/handlers.c @@ -52,7 +52,7 @@ void copyField(itemStruct *item, enum fields field, char *str) { if (!str) { - logMsg(1, "Attempted to assign a null pointer to a field!\n"); + logMsg(LOG_ERROR, "Attempted to assign a null pointer to a field!\n"); return; } @@ -66,7 +66,7 @@ atomLink(itemStruct *item, xmlNodePtr node) xmlChar *rel = xmlGetProp(node, (xmlChar *) "rel"); if (!href) { - logMsg(1, "Invalid link tag.\n"); + logMsg(LOG_ERROR, "Invalid link tag.\n"); if (rel) xmlFree(rel); return 1; @@ -89,7 +89,7 @@ rssEnclosure(itemStruct *item, xmlNodePtr node) { xmlChar *href = xmlGetProp(node, (xmlChar *) "url"); if (!href) { - logMsg(1, "Invalid enclosure URL.\n"); + logMsg(LOG_ERROR, "Invalid enclosure URL.\n"); return 1; } @@ -107,13 +107,13 @@ openFile(const char *folder, char *fileName, char *fileExt) // caller's responsibility to sanitize names, but frees fileName if (!folder) { - logMsg(1, "NULL folder"); + logMsg(LOG_ERROR, "NULL folder"); return NULL; } else if (!fileName) { - logMsg(1, "NULL file base name"); + logMsg(LOG_ERROR, "NULL file base name"); return NULL; } else if (!fileExt) { - logMsg(1, "NULL file extension"); + logMsg(LOG_ERROR, "NULL file extension"); return NULL; } @@ -129,7 +129,7 @@ openFile(const char *folder, char *fileName, char *fileExt) if (fileName[0]) filePath = ecalloc(pathLen, sizeof(char)); else { - logMsg(1, "Invalid filename.\n"); + logMsg(LOG_ERROR, "Invalid filename.\n"); return NULL; } @@ -220,7 +220,7 @@ itemAction(itemStruct *item, const char *folder) #endif //JSON default: - logMsg(0, "Output format is invalid."); + logMsg(LOG_FATAL, "Output format is invalid."); break; } @@ -238,7 +238,7 @@ itemAction(itemStruct *item, const char *folder) } if (newItems) - logMsg(2, "%s : %d new articles\n", folder, newItems); + logMsg(LOG_OUTPUT, "%s : %d new articles\n", folder, newItems); } void @@ -247,9 +247,9 @@ finish(char *url, long responseCode) // Executed after a download finishes if (responseCode == 200) - logMsg(4, "Finished downloading %s\n", url); + logMsg(LOG_VERBOSE, "Finished downloading %s\n", url); else if (!responseCode) - logMsg(1, "Can not reach %s: ensure the protocol is enabled and the site is accessible.\n", url); + logMsg(LOG_ERROR, "Can not reach %s: ensure the protocol is enabled and the site is accessible.\n", url); else - logMsg(1, "HTTP %ld for %s\n", responseCode, url); + logMsg(LOG_ERROR, "HTTP %ld for %s\n", responseCode, url); } diff --git a/minrss.c b/minrss.c index 2020f5b..204e1d1 100644 --- a/minrss.c +++ b/minrss.c @@ -41,7 +41,7 @@ parseXml(xmlDocPtr doc, // Parse the XML in a single document. if (!feedName || !feedName[0]) { - logMsg(1, "Missing feed name, please set one.\n"); + logMsg(LOG_ERROR, "Missing feed name, please set one.\n"); return 1; } @@ -50,7 +50,7 @@ parseXml(xmlDocPtr doc, rootNode = xmlDocGetRootElement(doc); if (!rootNode) { - logMsg(1, "Empty document for feed.\n"); + logMsg(LOG_ERROR, "Empty document for feed.\n"); return 1; } @@ -65,7 +65,7 @@ parseXml(xmlDocPtr doc, if (format == NONE) { - logMsg(1, "XML document is not an RSS or Atom feed.\n"); + logMsg(LOG_ERROR, "XML document is not an RSS or Atom feed.\n"); return 1; } @@ -79,7 +79,7 @@ parseXml(xmlDocPtr doc, cur = cur->next; if (!cur || !tagIs(cur, "channel")) { - logMsg(1, "Invalid RSS syntax.\n"); + logMsg(LOG_ERROR, "Invalid RSS syntax.\n"); return 1; } @@ -93,7 +93,7 @@ parseXml(xmlDocPtr doc, break; default: - logMsg(1, "Missing starting tag for format\n"); + logMsg(LOG_ERROR, "Missing starting tag for format\n"); return 1; } @@ -113,7 +113,7 @@ parseXml(xmlDocPtr doc, isArticle = tagIs(cur, "entry"); break; default: - logMsg(1, "Missing article tag name for format\n"); + logMsg(LOG_ERROR, "Missing article tag name for format\n"); return 1; } @@ -168,7 +168,7 @@ parseXml(xmlDocPtr doc, int stat = mkdir((const char* ) feedName, S_IRWXU); if (!stat && errno && errno != EEXIST) { - logMsg(1, "Error creating directory for feed.\n"); + logMsg(LOG_ERROR, "Error creating directory for feed.\n"); return 1; } @@ -188,14 +188,14 @@ readDoc(char *content, doc = xmlReadMemory(content, strlen(content), "noname.xml", NULL, 0); if (!doc) { - logMsg(1, "XML parser error.\n"); + logMsg(LOG_ERROR, "XML parser error.\n"); return 1; } int stat = parseXml(doc, feedName, itemAction); if (stat) - logMsg(1, "Skipped feed %s due to errors.\n", feedName); + logMsg(LOG_ERROR, "Skipped feed %s due to errors.\n", feedName); xmlFreeDoc(doc); @@ -206,9 +206,9 @@ int main(int argc, char *argv[]) { if (argc == 2 && !strcmp("-v", argv[1])) - logMsg(0, "MinRSS %s\n", VERSION); + logMsg(LOG_FATAL, "MinRSS %s\n", VERSION); else if (argc != 1) - logMsg(0, "Usage: minrss [-v]\n"); + logMsg(LOG_FATAL, "Usage: minrss [-v]\n"); unsigned int i = 0; @@ -223,7 +223,7 @@ main(int argc, char *argv[]) struct stat feedDir; if (links[0].url[0] == '\0') - logMsg(0, "No feeds, add them in config.def.h\n"); + logMsg(LOG_FATAL, "No feeds, add them in config.def.h\n"); if (stat(links[i].feedName, &feedDir) == 0) { time_t deltaTime = timeNow - feedDir.st_atime; @@ -231,16 +231,16 @@ main(int argc, char *argv[]) continue; } - logMsg(4, "Requesting %s\n", links[i].url); + logMsg(LOG_VERBOSE, "Requesting %s\n", links[i].url); createRequest(links[i].url, &outputs[i]); } performRequests(finish); - logMsg(3, "Finished downloads.\n"); + logMsg(LOG_INFO, "Finished downloads.\n"); for (i = 0; i < LEN(links); i++) { - logMsg(5, "Parsing %s\n", links[i].url); + logMsg(LOG_VERBOSE, "Parsing %s\n", links[i].url); if (outputs[i].buffer && outputs[i].buffer[0]) { if (readDoc(outputs[i].buffer, links[i].feedName, itemAction) == 0) { @@ -258,7 +258,7 @@ main(int argc, char *argv[]) } } - logMsg(3, "Finished parsing feeds.\n"); + logMsg(LOG_INFO, "Finished parsing feeds.\n"); return 0; } diff --git a/net.c b/net.c index d7879cc..a0c7aea 100644 --- a/net.c +++ b/net.c @@ -60,14 +60,14 @@ createRequest(const char* url, outputStruct *output) CURL *requestHandle = curl_easy_init(); if (!requestHandle) - logMsg(0, "Can't initialise curl.\n"); + logMsg(LOG_FATAL, "Can't initialise curl.\n"); output->buffer = NULL; output->size = 0; CURLcode stat; if (curl_easy_setopt(requestHandle, CURLOPT_URL, url)) { - logMsg(1, "Invalid URL: %s\n", url); + logMsg(LOG_ERROR, "Invalid URL: %s\n", url); } stat = curl_easy_setopt(requestHandle, CURLOPT_WRITEFUNCTION, writeCallback); diff --git a/util.c b/util.c index 7448b10..5a61dd8 100644 --- a/util.c +++ b/util.c @@ -24,7 +24,7 @@ logMsg(int lvl, char *msg, ...) va_start(args, msg); if (lvl <= logLevel) { - if (lvl == 2) { + if (lvl == LOG_OUTPUT) { vfprintf(stdout, msg, args); } else { fprintf(stderr, "minrss: "); @@ -44,7 +44,7 @@ ecalloc(size_t nmemb, size_t size) void *p = calloc(nmemb, size); if (!p) - logMsg(0, "Error allocating memory.\n"); + logMsg(LOG_FATAL, "Error allocating memory.\n"); return p; } @@ -55,7 +55,7 @@ erealloc(void *p, size_t size) p = realloc(p, size); if (!p) - logMsg(0, "Error reallocating memory.\n"); + logMsg(LOG_FATAL, "Error reallocating memory.\n"); return p; }