logMsg: use enum instead of magic number for log level

This commit is contained in:
dogeystamp 2023-02-04 10:46:22 -05:00
parent 7d9e96d259
commit 68907e137a
Signed by: dogeystamp
GPG Key ID: 7225FE3592EFFA38
5 changed files with 42 additions and 41 deletions

View File

@ -35,15 +35,16 @@ static const linkStruct links[] = {
}, },
}; };
/* enum logLevels {
* 0: Fatal errors LOG_FATAL,
* 1: Errors LOG_ERROR,
* 2: Normal output LOG_OUTPUT,
* 3: Info messages LOG_INFO,
* 4: Verbose mode 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. // Set the maximum amount of redirects curl will follow.
// Use 0 to disable redirects, and -1 for no limit. // Use 0 to disable redirects, and -1 for no limit.

View File

@ -52,7 +52,7 @@ void
copyField(itemStruct *item, enum fields field, char *str) copyField(itemStruct *item, enum fields field, char *str)
{ {
if (!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; return;
} }
@ -66,7 +66,7 @@ atomLink(itemStruct *item, xmlNodePtr node)
xmlChar *rel = xmlGetProp(node, (xmlChar *) "rel"); xmlChar *rel = xmlGetProp(node, (xmlChar *) "rel");
if (!href) { if (!href) {
logMsg(1, "Invalid link tag.\n"); logMsg(LOG_ERROR, "Invalid link tag.\n");
if (rel) if (rel)
xmlFree(rel); xmlFree(rel);
return 1; return 1;
@ -89,7 +89,7 @@ rssEnclosure(itemStruct *item, xmlNodePtr node)
{ {
xmlChar *href = xmlGetProp(node, (xmlChar *) "url"); xmlChar *href = xmlGetProp(node, (xmlChar *) "url");
if (!href) { if (!href) {
logMsg(1, "Invalid enclosure URL.\n"); logMsg(LOG_ERROR, "Invalid enclosure URL.\n");
return 1; return 1;
} }
@ -107,13 +107,13 @@ openFile(const char *folder, char *fileName, char *fileExt)
// caller's responsibility to sanitize names, but frees fileName // caller's responsibility to sanitize names, but frees fileName
if (!folder) { if (!folder) {
logMsg(1, "NULL folder"); logMsg(LOG_ERROR, "NULL folder");
return NULL; return NULL;
} else if (!fileName) { } else if (!fileName) {
logMsg(1, "NULL file base name"); logMsg(LOG_ERROR, "NULL file base name");
return NULL; return NULL;
} else if (!fileExt) { } else if (!fileExt) {
logMsg(1, "NULL file extension"); logMsg(LOG_ERROR, "NULL file extension");
return NULL; return NULL;
} }
@ -129,7 +129,7 @@ openFile(const char *folder, char *fileName, char *fileExt)
if (fileName[0]) if (fileName[0])
filePath = ecalloc(pathLen, sizeof(char)); filePath = ecalloc(pathLen, sizeof(char));
else { else {
logMsg(1, "Invalid filename.\n"); logMsg(LOG_ERROR, "Invalid filename.\n");
return NULL; return NULL;
} }
@ -220,7 +220,7 @@ itemAction(itemStruct *item, const char *folder)
#endif //JSON #endif //JSON
default: default:
logMsg(0, "Output format is invalid."); logMsg(LOG_FATAL, "Output format is invalid.");
break; break;
} }
@ -238,7 +238,7 @@ itemAction(itemStruct *item, const char *folder)
} }
if (newItems) if (newItems)
logMsg(2, "%s : %d new articles\n", folder, newItems); logMsg(LOG_OUTPUT, "%s : %d new articles\n", folder, newItems);
} }
void void
@ -247,9 +247,9 @@ finish(char *url, long responseCode)
// Executed after a download finishes // Executed after a download finishes
if (responseCode == 200) if (responseCode == 200)
logMsg(4, "Finished downloading %s\n", url); logMsg(LOG_VERBOSE, "Finished downloading %s\n", url);
else if (!responseCode) 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 else
logMsg(1, "HTTP %ld for %s\n", responseCode, url); logMsg(LOG_ERROR, "HTTP %ld for %s\n", responseCode, url);
} }

View File

@ -41,7 +41,7 @@ parseXml(xmlDocPtr doc,
// Parse the XML in a single document. // Parse the XML in a single document.
if (!feedName || !feedName[0]) { 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; return 1;
} }
@ -50,7 +50,7 @@ parseXml(xmlDocPtr doc,
rootNode = xmlDocGetRootElement(doc); rootNode = xmlDocGetRootElement(doc);
if (!rootNode) { if (!rootNode) {
logMsg(1, "Empty document for feed.\n"); logMsg(LOG_ERROR, "Empty document for feed.\n");
return 1; return 1;
} }
@ -65,7 +65,7 @@ parseXml(xmlDocPtr doc,
if (format == NONE) { 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; return 1;
} }
@ -79,7 +79,7 @@ parseXml(xmlDocPtr doc,
cur = cur->next; cur = cur->next;
if (!cur || !tagIs(cur, "channel")) { if (!cur || !tagIs(cur, "channel")) {
logMsg(1, "Invalid RSS syntax.\n"); logMsg(LOG_ERROR, "Invalid RSS syntax.\n");
return 1; return 1;
} }
@ -93,7 +93,7 @@ parseXml(xmlDocPtr doc,
break; break;
default: default:
logMsg(1, "Missing starting tag for format\n"); logMsg(LOG_ERROR, "Missing starting tag for format\n");
return 1; return 1;
} }
@ -113,7 +113,7 @@ parseXml(xmlDocPtr doc,
isArticle = tagIs(cur, "entry"); isArticle = tagIs(cur, "entry");
break; break;
default: default:
logMsg(1, "Missing article tag name for format\n"); logMsg(LOG_ERROR, "Missing article tag name for format\n");
return 1; return 1;
} }
@ -168,7 +168,7 @@ parseXml(xmlDocPtr doc,
int stat = mkdir((const char* ) feedName, S_IRWXU); int stat = mkdir((const char* ) feedName, S_IRWXU);
if (!stat && errno && errno != EEXIST) { if (!stat && errno && errno != EEXIST) {
logMsg(1, "Error creating directory for feed.\n"); logMsg(LOG_ERROR, "Error creating directory for feed.\n");
return 1; return 1;
} }
@ -188,14 +188,14 @@ readDoc(char *content,
doc = xmlReadMemory(content, strlen(content), "noname.xml", NULL, 0); doc = xmlReadMemory(content, strlen(content), "noname.xml", NULL, 0);
if (!doc) { if (!doc) {
logMsg(1, "XML parser error.\n"); logMsg(LOG_ERROR, "XML parser error.\n");
return 1; return 1;
} }
int stat = parseXml(doc, feedName, itemAction); int stat = parseXml(doc, feedName, itemAction);
if (stat) 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); xmlFreeDoc(doc);
@ -206,9 +206,9 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
if (argc == 2 && !strcmp("-v", argv[1])) if (argc == 2 && !strcmp("-v", argv[1]))
logMsg(0, "MinRSS %s\n", VERSION); logMsg(LOG_FATAL, "MinRSS %s\n", VERSION);
else if (argc != 1) else if (argc != 1)
logMsg(0, "Usage: minrss [-v]\n"); logMsg(LOG_FATAL, "Usage: minrss [-v]\n");
unsigned int i = 0; unsigned int i = 0;
@ -223,7 +223,7 @@ main(int argc, char *argv[])
struct stat feedDir; struct stat feedDir;
if (links[0].url[0] == '\0') 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) { if (stat(links[i].feedName, &feedDir) == 0) {
time_t deltaTime = timeNow - feedDir.st_atime; time_t deltaTime = timeNow - feedDir.st_atime;
@ -231,16 +231,16 @@ main(int argc, char *argv[])
continue; continue;
} }
logMsg(4, "Requesting %s\n", links[i].url); logMsg(LOG_VERBOSE, "Requesting %s\n", links[i].url);
createRequest(links[i].url, &outputs[i]); createRequest(links[i].url, &outputs[i]);
} }
performRequests(finish); performRequests(finish);
logMsg(3, "Finished downloads.\n"); logMsg(LOG_INFO, "Finished downloads.\n");
for (i = 0; i < LEN(links); i++) { 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 (outputs[i].buffer && outputs[i].buffer[0]) {
if (readDoc(outputs[i].buffer, links[i].feedName, itemAction) == 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; return 0;
} }

4
net.c
View File

@ -60,14 +60,14 @@ createRequest(const char* url, outputStruct *output)
CURL *requestHandle = curl_easy_init(); CURL *requestHandle = curl_easy_init();
if (!requestHandle) if (!requestHandle)
logMsg(0, "Can't initialise curl.\n"); logMsg(LOG_FATAL, "Can't initialise curl.\n");
output->buffer = NULL; output->buffer = NULL;
output->size = 0; output->size = 0;
CURLcode stat; CURLcode stat;
if (curl_easy_setopt(requestHandle, CURLOPT_URL, url)) { 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); stat = curl_easy_setopt(requestHandle, CURLOPT_WRITEFUNCTION, writeCallback);

6
util.c
View File

@ -24,7 +24,7 @@ logMsg(int lvl, char *msg, ...)
va_start(args, msg); va_start(args, msg);
if (lvl <= logLevel) { if (lvl <= logLevel) {
if (lvl == 2) { if (lvl == LOG_OUTPUT) {
vfprintf(stdout, msg, args); vfprintf(stdout, msg, args);
} else { } else {
fprintf(stderr, "minrss: "); fprintf(stderr, "minrss: ");
@ -44,7 +44,7 @@ ecalloc(size_t nmemb, size_t size)
void *p = calloc(nmemb, size); void *p = calloc(nmemb, size);
if (!p) if (!p)
logMsg(0, "Error allocating memory.\n"); logMsg(LOG_FATAL, "Error allocating memory.\n");
return p; return p;
} }
@ -55,7 +55,7 @@ erealloc(void *p, size_t size)
p = realloc(p, size); p = realloc(p, size);
if (!p) if (!p)
logMsg(0, "Error reallocating memory.\n"); logMsg(LOG_FATAL, "Error reallocating memory.\n");
return p; return p;
} }