From 7337375e052aa1eeebc681c7c5b78aa242329339 Mon Sep 17 00:00:00 2001 From: Simon Ruderich Date: Fri, 7 Jun 2013 00:58:16 +0200 Subject: [PATCH] Add warning() and use it in DEBUG mode. --- src/constants.h | 2 ++ src/debug.h | 60 ++++++++++++++++++++++++++++++++++++++----------- src/trackfds.h | 15 ++++++++++--- 3 files changed, 61 insertions(+), 16 deletions(-) diff --git a/src/constants.h b/src/constants.h index d577cb2..f7c9b77 100644 --- a/src/constants.h +++ b/src/constants.h @@ -35,6 +35,8 @@ #ifdef DEBUG # define DEBUG_FILE "colored_stderr_debug_log.txt" +/* Created in the user's home directory, appends to existing file. */ +# define WARNING_FILE "colored_stderr_warning_log.txt" #endif #endif diff --git a/src/debug.h b/src/debug.h index 1a45c0f..dadec82 100644 --- a/src/debug.h +++ b/src/debug.h @@ -20,20 +20,10 @@ #ifndef DEBUG_H #define DEBUG_H 1 -static void debug(const char *format, ...) { +static void debug_write(int fd, int first_call, const char *format, va_list ap) { char buffer[1024]; - /* If the file doesn't exist, do nothing. Prevents writing log files in - * unexpected places. The user must create the file manually. */ - int fd = open(DEBUG_FILE, O_WRONLY | O_APPEND); - if (fd == -1) { - return; - } - - va_list ap; - va_start(ap, format); int written = vsnprintf(buffer, sizeof(buffer), format, ap); - va_end(ap); /* Overflow. */ if ((size_t)written >= sizeof(buffer)) { written = sizeof(buffer) - 1; @@ -43,8 +33,7 @@ static void debug(const char *format, ...) { DLSYM_FUNCTION(real_write, "write"); DLSYM_FUNCTION(real_close, "close"); - static int first_call = 0; - if (!first_call++) { + if (first_call) { char nl = '\n'; real_write(fd, &nl, 1); } @@ -52,4 +41,49 @@ static void debug(const char *format, ...) { real_close(fd); } +static void debug(const char *format, ...) { + va_list ap; + + /* If the file doesn't exist, do nothing. Prevents writing log files in + * unexpected places. The user must create the file manually. */ + int fd = open(DEBUG_FILE, O_WRONLY | O_APPEND); + if (fd == -1) { + return; + } + + static int call_count = 0; + call_count++; + + va_start(ap, format); + debug_write(fd, call_count == 1, format, ap); + va_end(ap); +} + +static void warning(const char *format, ...) { + va_list ap; + + char *home = getenv("HOME"); + if (!home) { + return; + } + + char path[strlen(home) + 1 + strlen(WARNING_FILE) + 1]; + strcpy(path, home); + strcat(path, "/"); + strcat(path, WARNING_FILE); + + /* Create the warning file if it doesn't exist yet. */ + int fd = open(path, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR); + if (fd == -1) { + return; + } + + static int call_count = 0; + call_count++; + + va_start(ap, format); + debug_write(fd, call_count == 1, format, ap); + va_end(ap); +} + #endif diff --git a/src/trackfds.h b/src/trackfds.h index 4a71a4a..67b9562 100644 --- a/src/trackfds.h +++ b/src/trackfds.h @@ -81,6 +81,10 @@ static void init_from_environment(void) { tracked_fds_list = malloc(tracked_fds_list_space * sizeof(*tracked_fds_list)); if (!tracked_fds_list) { +#ifdef DEBUG + warning("malloc(%zu): failed [%d]\n", + tracked_fds_list_space * sizeof(*tracked_fds_list), getpid()); +#endif return; } @@ -122,6 +126,10 @@ static void update_environment_buffer(char *x) { int length = snprintf(x, 10 + 1, "%d", tracked_fds_list[i]); if (length >= 10 + 1) { /* Integer too big to fit the buffer, skip it. */ +#ifdef DEBUG + warning("update_environment_buffer_entry(): truncated fd: %d [%d]\n", + tracked_fds_list[i], getpid()); +#endif continue; } @@ -166,14 +174,15 @@ static void update_environment(void) { static void tracked_fds_add(int fd) { if (tracked_fds_list_count >= tracked_fds_list_space) { size_t new_space = tracked_fds_list_space + TRACKFDS_REALLOC_STEP; - int *tmp = realloc(tracked_fds_list, sizeof(*tracked_fds_list) * new_space); + int *tmp = realloc(tracked_fds_list, + sizeof(*tracked_fds_list) * new_space); if (!tmp) { /* We can do nothing, just ignore the error. We made sure not to * destroy our state, so the new descriptor is ignored without any * other consequences. */ #ifdef DEBUG - debug("realloc(tracked_fds_list, %zu) failed! [%d]\n", - sizeof(*tracked_fds_list) * new_space, getpid()); + warning("realloc(tracked_fds_list, %zu) failed! [%d]\n", + sizeof(*tracked_fds_list) * new_space, getpid()); #endif return; } -- 2.43.2