From fd33289346881b96841a598a1f8980212b0bab62 Mon Sep 17 00:00:00 2001 From: Luke Bratch Date: Thu, 30 May 2019 22:02:07 +0100 Subject: Remove most (all?) exit()s with error handling without exiting where possible. Convert most remaining printing to stdout/stderr to debugprint() instead. --- logging.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) (limited to 'logging.c') diff --git a/logging.c b/logging.c index 973d03c..e3edb02 100644 --- a/logging.c +++ b/logging.c @@ -52,8 +52,8 @@ int logline(char *str, char *ournick, char *basedir, int type) { for (int i = 0; i < 3; i++) { // Try to split if ((token = strsep(&str, " ")) == NULL) { - printf("Error splitting string for logging, exiting!\n"); - exit(1); + debugprint(DEBUG_CRIT, "Error splitting string for logging, returning!\n"); + return 0; } // Copy into the token array (strlen + 1 to get the NULL terminator) strncpy(tokens[i], token, strlen(token) + 1); @@ -156,7 +156,7 @@ int logline(char *str, char *ournick, char *basedir, int type) { if (mkdir(logdir, 0700)) { debugprint(DEBUG_CRIT, "Error creating log directory '%s.\n", logdir); printf("Error creating log directory '%s'.\n", logdir); - exit(1); + return 0; } else { debugprint(DEBUG_FULL, "logline(): log directory '%s'.\n", logdir); } @@ -171,7 +171,8 @@ int logline(char *str, char *ournick, char *basedir, int type) { if (fp == NULL) { debugprint(DEBUG_CRIT, "error: could not open log file '%s' for writing.\n", filename); printf("error: could not open log file '%s' for writing.\n", filename); - exit(1); + fclose(fp); + return 0; } // Get a current time string to prepend - TODO - Make this customisable. @@ -188,14 +189,18 @@ int logline(char *str, char *ournick, char *basedir, int type) { // Prepend the time string and "from" nick if (!snprintf(line, MAXCHAR, "%s <%s> %s", timestr, tokens[0], str)) { fprintf(stderr, "Error while preparing log string to write!\n"); - exit(1); + debugprint(DEBUG_CRIT, "Error while preparing log string to write!\n"); + fclose(fp); + return 0; } } else if (type == LOG_JOINPART || type == LOG_TOPIC) { // Prepend the time string char line2[MAXCHAR]; if (!snprintf(line2, MAXCHAR, "%s %s", timestr, line)) { fprintf(stderr, "Error while preparing log string to write!\n"); - exit(1); + debugprint(DEBUG_CRIT, "Error while preparing log string to write!\n"); + fclose(fp); + return 0; } // Copy back to line to write snprintf(line, MAXCHAR, "%s", line2); @@ -208,9 +213,10 @@ int logline(char *str, char *ournick, char *basedir, int type) { // Write complete line to file if ((bytes = fprintf(fp, "%s", line)) < 0) { - debugprint(DEBUG_CRIT, "error: could not write to log file.\n"); printf("error: could not write to log file.\n"); - exit(1); + debugprint(DEBUG_CRIT, "error: could not write to log file.\n"); + fclose(fp); + return 0; } fclose(fp); -- cgit v1.2.3