From c3f421e4046faf8b5f39cd1ad36bc4869405fcc9 Mon Sep 17 00:00:00 2001 From: Luke Bratch Date: Sat, 1 Jun 2019 19:11:00 +0100 Subject: Put debug logs in subdirectory and have max number to keep be configurable. Also fix possible crash in debugprint() if debug directory doesn't exist yet. --- TODO | 2 +- blabouncer.c | 30 ++++++++++++++++++++++++------ blabouncer.conf.example | 3 +++ config.c | 5 ++++- functions.c | 6 ++++++ 5 files changed, 38 insertions(+), 8 deletions(-) diff --git a/TODO b/TODO index 2b3b030..5b78261 100644 --- a/TODO +++ b/TODO @@ -9,4 +9,4 @@ Might need to #include in blabouncer.c to make some operating systems Send a PING to the server before assuming a timeout is definite. -Put debug logs in subdirectory and have max number to keep be configurable. +Use errno to handle getconfint() failing. diff --git a/blabouncer.c b/blabouncer.c index 02c62ad..2410625 100644 --- a/blabouncer.c +++ b/blabouncer.c @@ -60,7 +60,6 @@ #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; // Debug verbosity ("0" for critical only, "1" for some extra info, "2" for full debug mode) @@ -121,6 +120,7 @@ struct settings { char basedir[PATH_MAX]; int logging; int replaylogging; + int debugkeep; int background; // Whether or not we're running in the background (detached from the terminal as a daemon) }; @@ -2794,27 +2794,45 @@ int main(int argc, char *argv[]) { // Is replay logging enabled? settings.replaylogging = getconfint("replaylogging", settings.conffile); + // How many debug logs should we keep? + settings.debugkeep = getconfint("debugkeep", settings.conffile); + // Is debugging enabled? debug = getconfint("debug", settings.conffile); - if (!snprintf(debugpath, PATH_MAX, "%s/debug.txt", settings.basedir)) { + if (!snprintf(debugpath, PATH_MAX, "%s/debug/debug.txt", settings.basedir)) { + fprintf(stderr, "Error while preparing debug path location!\n"); + exit(1); + } + // debugfile goes in its own directory + char debugdir[PATH_MAX]; + if (!snprintf(debugdir, PATH_MAX, "%s/debug/", settings.basedir)) { fprintf(stderr, "Error while preparing debug path location!\n"); exit(1); } + // Make sure the debug directory exists + if (stat(debugdir, &st) == -1) { + if (mkdir(debugdir, 0700)) { + printf("Error creating debug directory '%s'.\n", debugdir); + exit(1); + } else { + printf("Created debug directory '%s'.\n", debugdir); + } + } // Prepare the debug file - // (Keep DEBUGFILESKEEP number of debug files around for past debugging) - if (DEBUGFILESKEEP > 0) { + // (Keep settings.debugkeep number of debug files around for past debugging) + if (settings.debugkeep > 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--) { + for (int i = settings.debugkeep; 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 (i == settings.debugkeep) { if (remove(tmppath)) { printf("error deleting old debug file '%s'.\n", tmppath); exit(1); diff --git a/blabouncer.conf.example b/blabouncer.conf.example index dd98c72..f4de92e 100644 --- a/blabouncer.conf.example +++ b/blabouncer.conf.example @@ -69,3 +69,6 @@ replaylogging = "1" # Debug verbosity ("0" for critical only, "1" for some extra info, "2" for full debug mode) # (All output goes to /debug.txt) debug = "2" + +# Number of debug logs to keep +debugkeep = "5" diff --git a/config.c b/config.c index bd1aeb8..41f07d6 100644 --- a/config.c +++ b/config.c @@ -233,7 +233,10 @@ int createconfigfile(char *filename) { "\n" "# Debug verbosity (\"0\" for critical only, \"1\" for some extra info, \"2\" for full debug mode)\n" "# (All output goes to /debug.txt)\n" - "debug = \"2\"\n"; + "debug = \"2\"\n" + "\n" + "# Number of debug logs to keep\n" + "debugkeep = \"5\"\n"; // Write complete string to file if ((fprintf(fp, "%s", string)) < 0) { diff --git a/functions.c b/functions.c index dbcc54e..cdb594c 100644 --- a/functions.c +++ b/functions.c @@ -80,6 +80,12 @@ void debugprint(int level, char *format, ...) { FILE *fp; int bytes = 0; fp = fopen(debugpath, "a"); + + if (fp == NULL) { + printf("Couldn't open debugpath '%s'!\n", debugpath); + return; + } + if ((bytes = vfprintf(fp, format, args)) < 0) { debugprint(DEBUG_CRIT, "error: could not write to debug file.\n"); } -- cgit v1.2.3