summaryrefslogtreecommitdiff
path: root/functions.c
diff options
context:
space:
mode:
authorLuke Bratch <luke@bratch.co.uk>2019-05-30 21:23:44 +0100
committerLuke Bratch <luke@bratch.co.uk>2019-05-30 21:23:44 +0100
commit6d183c02a50a42743c3031532f458ab5cea0685d (patch)
treed1463e8f53df26ec5f8c3c13a77880d11d3bc57f /functions.c
parentef93f4d61f03faa0edd60f11c7790ad3b67b44d4 (diff)
Convert debugprint() from being to file/screen/disabled to always being to file with configurable verbosity.
Diffstat (limited to 'functions.c')
-rw-r--r--functions.c77
1 files changed, 41 insertions, 36 deletions
diff --git a/functions.c b/functions.c
index 0c814a3..13b910b 100644
--- a/functions.c
+++ b/functions.c
@@ -8,7 +8,7 @@ extern int background;
// Internal function just to replace nick in server greeting strings
// (as in ":servername 00x oldnick :Blablabla" -> ":servername 00x newnick :Blablabla")
void updategreetingnick(char *greeting, char *greetingnum, char *newnick, char *oldnick) {
- debugprint("updategreetingnick(): '%s' '%s' '%s' '%s'.\n", greeting, greetingnum, newnick, oldnick);
+ debugprint(DEBUG_FULL, "updategreetingnick(): '%s' '%s' '%s' '%s'.\n", greeting, greetingnum, newnick, oldnick);
// Find the position of the old nick in the current greeting
char searchstr[MAXDATASIZE];
@@ -27,13 +27,14 @@ void updategreetingnick(char *greeting, char *greetingnum, char *newnick, char *
snprintf(searchstr, MAXDATASIZE, " %s %s ", greetingnum, newnick);
ret = strstr(greeting, searchstr);
if (ret != NULL) {
- debugprint("updategreetingnick(): newnick is already present, returning.\n");
+ debugprint(DEBUG_FULL, "updategreetingnick(): newnick is already present, returning.\n");
return;
}
}
// If ret *still* not found, abandon ship
if (ret == NULL) {
+ debugprint(DEBUG_CRIT, "Error updating greeting string, substring not found. Exiting!\n");
printf("Error updating greeting string, substring not found. Exiting!\n");
exit(1);
}
@@ -57,29 +58,32 @@ void updategreetingnick(char *greeting, char *greetingnum, char *newnick, char *
// And finally copy back to source string
strcpy(greeting, greetingtmp2);
- debugprint("updategreetingnick(): Built new greeting '%s' '%s', length '%ld'.\n", greetingnum, greeting, strlen(greeting));
+ debugprint(DEBUG_FULL, "updategreetingnick(): Built new greeting '%s' '%s', length '%ld'.\n", greetingnum, greeting, strlen(greeting));
}
-// Print debugging output if enabled
-// (To screen or to file depending on settings)
-void debugprint(char *format, ...) {
- if (debug == DEBUG_NONE) return;
+// Write debug string to file.
+// Debug level is provided by level, set to one of DEBUG_CRIT, DEBUG_SOME or DEBUG_FULL.
+// Debug is only written if the global int "debug" is greater than or equal to the level.
+void debugprint(int level, char *format, ...) {
+ // Stop here if the user's debug level is less than the level of the current message
+ if (debug < level) return;
+
+ if (strlen(debugpath) < 1) {
+ // debugpath isn't set, we can't do anything here
+ return;
+ }
va_list args;
va_start(args, format);
- if (debug == DEBUG_FILE) {
- FILE *fp;
- int bytes = 0;
- fp = fopen(debugpath, "a");
- if ((bytes = vfprintf(fp, format, args)) < 0) {
- printf("error: could not write to debug file.\n");
- exit(1);
- }
- fclose(fp);
- } else if (debug == DEBUG_SCREEN) {
- vprintf(format, args);
+ FILE *fp;
+ int bytes = 0;
+ fp = fopen(debugpath, "a");
+ if ((bytes = vfprintf(fp, format, args)) < 0) {
+ printf("error: could not write to debug file.\n");
+ exit(1);
}
+ fclose(fp);
va_end(args);
}
@@ -135,11 +139,11 @@ void stripprefix(char *string) {
// Make a copy to work with
char string2[strlen(string)];
- debugprint("stripprefix(): starting with '%s', strlen: %zd.\n", string, strlen(string));
+ debugprint(DEBUG_FULL, "stripprefix(): starting with '%s', strlen: %zd.\n", string, strlen(string));
// Don't bother if this isn't a prefix with a leading ':'
if (string[0] != ':') {
- debugprint("stripprefix(): no leading ':', returning.\n");
+ debugprint(DEBUG_FULL, "stripprefix(): no leading ':', returning.\n");
return;
}
@@ -154,7 +158,7 @@ void stripprefix(char *string) {
// Finish with null terminator
string[strlen(string) - 1] = '\0';
- debugprint("stripprefix(): finishing with '%s', strlen: %zd.\n", string, strlen(string));
+ debugprint(DEBUG_FULL, "stripprefix(): finishing with '%s', strlen: %zd.\n", string, strlen(string));
}
// Extract final parameter from IRC message, removing the leading colon ':'
@@ -169,7 +173,7 @@ void extractfinalparameter(char *string) {
// Position of colon
int colonpos = -1;
- debugprint("extractfinalparameter(): starting with '%s', strlen: %zd.\n", string, strlen(string));
+ debugprint(DEBUG_FULL, "extractfinalparameter(): starting with '%s', strlen: %zd.\n", string, strlen(string));
// Strip the colon at position 0 if there is one
stripprefix(string);
@@ -177,14 +181,14 @@ void extractfinalparameter(char *string) {
// Find the colon
for (size_t i = 0; i < strlen(string); i++) {
if (string[i] == ':') {
- debugprint("Found colon at position %zd!\n", i);
+ debugprint(DEBUG_FULL, "Found colon at position %zd!\n", i);
colonpos = i;
break;
}
}
if (colonpos == -1) {
- debugprint("no colon found, returning\n");
+ debugprint(DEBUG_FULL, "no colon found, returning\n");
return;
}
@@ -201,7 +205,7 @@ void extractfinalparameter(char *string) {
// Finish with null terminator
string[counter] = '\0';
- debugprint("extractfinalparameter(): finishing with '%s', strlen: %zd.\n", string, strlen(string));
+ debugprint(DEBUG_FULL, "extractfinalparameter(): finishing with '%s', strlen: %zd.\n", string, strlen(string));
}
// Extract the IRC nick from a prefix
@@ -213,7 +217,7 @@ void extractnickfromprefix(char *string) {
// Position of bang
int bangpos = -1;
- debugprint("extractnickfromprefix(): starting with '%s', strlen: %zd.\n", string, strlen(string));
+ debugprint(DEBUG_FULL, "extractnickfromprefix(): starting with '%s', strlen: %zd.\n", string, strlen(string));
// Strip the colon at position 0 if there is one
stripprefix(string);
@@ -221,26 +225,26 @@ void extractnickfromprefix(char *string) {
// Find the bang
for (size_t i = 0; i < strlen(string); i++) {
if (string[i] == '!') {
- debugprint("Found bang at position %zd!\n", i);
+ debugprint(DEBUG_FULL, "Found bang at position %zd!\n", i);
bangpos = i;
break;
}
}
if (bangpos == -1) {
- debugprint("no bang found, returning\n");
+ debugprint(DEBUG_FULL, "no bang found, returning\n");
return;
}
// Terminate the string at whatever position we found the bang
string[bangpos] = '\0';
- debugprint("extractnickfromprefix(): finishing with '%s', strlen: %zd.\n", string, strlen(string));
+ debugprint(DEBUG_FULL, "extractnickfromprefix(): finishing with '%s', strlen: %zd.\n", string, strlen(string));
}
// Update an existing nickuserhost string with a new nick
void updatenickuserhost(char *nickuserhost, char *nick) {
- debugprint("updatenickuserhost(): updating '%s' with '%s'.\n", nickuserhost, nick);
+ debugprint(DEBUG_FULL, "updatenickuserhost(): updating '%s' with '%s'.\n", nickuserhost, nick);
// Position of bang
int bangpos = -1;
@@ -248,14 +252,14 @@ void updatenickuserhost(char *nickuserhost, char *nick) {
// Find the bang
for (size_t i = 0; i < strlen(nickuserhost); i++) {
if (nickuserhost[i] == '!') {
- debugprint("Found bang at position %ld!\n", i);
+ debugprint(DEBUG_FULL, "Found bang at position %ld!\n", i);
bangpos = i;
break;
}
}
if (bangpos == -1) {
- debugprint("No bang found in existing nickuserhost, quitting!\n");
+ debugprint(DEBUG_FULL, "No bang found in existing nickuserhost, quitting!\n");
return;
}
@@ -267,12 +271,13 @@ void updatenickuserhost(char *nickuserhost, char *nick) {
// Copy back to source string
strcpy(nickuserhost, newstr);
- debugprint("updatenickuserhost(): new nickuserhost '%s', length '%ld'.\n", nickuserhost, strlen(nickuserhost));
+ debugprint(DEBUG_FULL, "updatenickuserhost(): new nickuserhost '%s', length '%ld'.\n", nickuserhost, strlen(nickuserhost));
}
// Update existing greeting strings with a new nickuserhost and new nick
-void updategreetings(char *greeting001, char *greeting002, char *greeting003, char *greeting004, char *greeting005a, char *greeting005b, char *greeting005c, char *newnickuserhost, char *oldnickuserhost, char *newnick, char *oldnick) {
- debugprint("updategreetings(): updating greetings with new nickuserhost '%s' and nick '%s'.\n", newnickuserhost, newnick);
+void updategreetings(char *greeting001, char *greeting002, char *greeting003, char *greeting004, char *greeting005a, char *greeting005b,
+ char *greeting005c, char *newnickuserhost, char *oldnickuserhost, char *newnick, char *oldnick) {
+ debugprint(DEBUG_FULL, "updategreetings(): updating greetings with new nickuserhost '%s' and nick '%s'.\n", newnickuserhost, newnick);
// nickuserhost and greeting001's final component first
// (final component as in ":servername 001 nick :Blablabla final!com@ponent"
@@ -303,7 +308,7 @@ void updategreetings(char *greeting001, char *greeting002, char *greeting003, ch
// Copy back to source greeting 001
strcpy(greeting001, greetingtmp);
- debugprint("updategreetings(): new greeting 001 '%s', length '%ld'.\n", greeting001, strlen(greeting001));
+ debugprint(DEBUG_FULL, "updategreetings(): new greeting 001 '%s', length '%ld'.\n", greeting001, strlen(greeting001));
// Get the new nick