From 92d106733468893d921dc678296a6716ddf979a4 Mon Sep 17 00:00:00 2001 From: Luke Bratch Date: Tue, 28 May 2019 23:24:54 +0100 Subject: Implement debugging to file and set the default configuration file to be that. --- blabouncer.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++-- blabouncer.conf.example | 4 +-- config.c | 4 +-- functions.c | 21 +++++++++++++--- functions.h | 4 +++ 5 files changed, 91 insertions(+), 9 deletions(-) diff --git a/blabouncer.c b/blabouncer.c index 5fa2e5d..fe8cd1e 100644 --- a/blabouncer.c +++ b/blabouncer.c @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -52,9 +53,11 @@ #define MAXPORTLEN 6 // Up to 65535, so 5 characters + 1 for null #define MAXAUTOCHANLEN 1024 // Randomly picked maximum length of the auto channel list #define SERVERTIMEOUT 300 // How many seconds to wait without hearing from the server before assuming a timeout +#define DEBUGFILESKEEP 5 // How many debug files to keep around // Global debug control int debug = 0; +char debugpath[PATH_MAX]; struct channel { char name[MAXCHANLENGTH]; @@ -2548,6 +2551,10 @@ int main(int argc, char *argv[]) { // Is debugging enabled? debug = getconfint("debug", settings.conffile); + if (!snprintf(debugpath, PATH_MAX, "%s/debug.txt", settings.basedir)) { + fprintf(stderr, "Error while preparing debug path location!\n"); + exit(1); + } // Make sure the base directory exists struct stat st = {0}; @@ -2566,12 +2573,68 @@ int main(int argc, char *argv[]) { // Is replay logging enabled? settings.replaylogging = getconfint("replaylogging", settings.conffile); + // Prepare the debug file + // (Keep DEBUGFILESKEEP number of debug files around for past debugging) + if (DEBUGFILESKEEP > 0) { + // Check each possible debug file and rename it to one higher + char tmppath[PATH_MAX]; + char tmppathnew[PATH_MAX]; + // Delete or rename numbered debug files first + for (int i = DEBUGFILESKEEP; i > 0; i--) { + if (!snprintf(tmppath, PATH_MAX - 1, "%s.%d", debugpath, i)) { + fprintf(stderr, "Error while preparing to check old debug files!\n"); + exit(1); + } + if (!access(tmppath, F_OK)) { + if (i == DEBUGFILESKEEP) { + if (remove(tmppath)) { + printf("error deleting old debug file '%s'.\n", tmppath); + exit(1); + } + } else { + if (!snprintf(tmppathnew, PATH_MAX - 1, "%s.%d", debugpath, i + 1)) { + fprintf(stderr, "Error while preparing to rename old debug file!\n"); + exit(1); + } + if (rename(tmppath, tmppathnew)) { + printf("error renaming old debug file '%s' to '%s'.\n", tmppath, tmppathnew); + exit(1); + } + } + } + } + // Rename the previous debug file next + if (!access(debugpath, F_OK)) { + if (rename(debugpath, tmppath)) { + printf("error renaming old debug file '%s' to '%s'.\n", debugpath, tmppath); + exit(1); + } + } + } + + // Write a friendly debug message to file (even if debug to file is disabled) + // Prepare a friendly time string + time_t rawtime; + struct tm * timeinfo; + time(&rawtime); + timeinfo = localtime(&rawtime); + // Strip the trailing newline + char timestr[MAXCHAR]; + snprintf(timestr, MAXCHAR, "%s", asctime(timeinfo)); + timestr[strlen(timestr) - 1] = '\0'; + // Note the old debug setting + int debugold = debug; + // Temporarily enable debugging to file + debug = 1; + // Print it + debugprint("blabouncer started at %s, debugging is set to %d.\n", timestr, debugold); + // Set debugging back to whatever it was + debug = debugold; + // TODO: see if any of this can be shared (i.e. 1. avoid code duplication, and 2. see if variables can be shared between client/server sockets) // I will try to keep to the notation of "server" meaning the real IRCd, "bouncer" meaning the bouncer, and "client" meaning the real IRC client - // BOUNCER-TO-SERVER socket things - // Create server socket int serversockfd = createserversocket(settings.ircserver, settings.ircserverport); diff --git a/blabouncer.conf.example b/blabouncer.conf.example index 8e0b263..09bc00e 100644 --- a/blabouncer.conf.example +++ b/blabouncer.conf.example @@ -64,5 +64,5 @@ logging = "1" replaylogging = "1" # Debug output control ("2" for print to screen, "1" for print to file, or "0" for disabled) -# (Note: print to file not yet implemented) -debug = "0" +# (The debug file can be found in /debug.txt) +debug = "1" diff --git a/config.c b/config.c index 9b69682..dcbba42 100644 --- a/config.c +++ b/config.c @@ -225,8 +225,8 @@ int createconfigfile(char *filename) { "replaylogging = \"1\"\n" "\n" "# Debug output control (\"2\" for print to screen, \"1\" for print to file, or \"0\" for disabled)\n" - "# (Note: print to file not yet implemented)\n" - "debug = \"0\"\n"; + "# (The debug file can be found in /debug.txt)\n" + "debug = \"1\"\n"; // Write complete string to file if ((fprintf(fp, string)) < 0) { diff --git a/functions.c b/functions.c index 0d4380b..0c814a3 100644 --- a/functions.c +++ b/functions.c @@ -2,6 +2,8 @@ // Global debug control extern int debug; +extern char debugpath[PATH_MAX]; +extern int background; // Internal function just to replace nick in server greeting strings // (as in ":servername 00x oldnick :Blablabla" -> ":servername 00x newnick :Blablabla") @@ -59,13 +61,26 @@ void updategreetingnick(char *greeting, char *greetingnum, char *newnick, char * } // Print debugging output if enabled +// (To screen or to file depending on settings) void debugprint(char *format, ...) { - if (!debug) return; + if (debug == DEBUG_NONE) return; va_list args; - va_start(args, format); - vprintf(format, args); + + 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); + } + va_end(args); } diff --git a/functions.h b/functions.h index 29518aa..d56a295 100644 --- a/functions.h +++ b/functions.h @@ -13,6 +13,7 @@ #include #include #include +#include #define MAXDATASIZE 513 // max number of bytes we can get at once (RFC2812 says 512, plus one for null terminator) @@ -20,6 +21,9 @@ #define OK 0 #define NO_INPUT 1 #define TOO_LONG 2 +#define DEBUG_NONE 0 +#define DEBUG_FILE 1 +#define DEBUG_SCREEN 2 // Print debugging output if enabled void debugprint(char *format, ...); -- cgit v1.2.3