X-Git-Url: https://ruderich.org/simon/gitweb/?p=coloredstderr%2Fcoloredstderr.git;a=blobdiff_plain;f=src%2Fdebug.h;h=dadec826cb2f11bb0196528c1bdcc2db1e50c7bc;hp=1a45c0f993b5a402fe69ba4e5fd044d4742fa6b7;hb=7337375e052aa1eeebc681c7c5b78aa242329339;hpb=51377106d1bd632546fa66bc27ac1a02a647f026 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