From 56f87419f164e4fb0f1b79e53f308084432fdb7e Mon Sep 17 00:00:00 2001 From: Ava Hahn Date: Thu, 11 Aug 2022 11:15:42 -0700 Subject: [PATCH] test and update the printf functionality Signed-off-by: Ava Hahn --- Makefile | 2 +- alog.c | 45 +++++++++++++++++++++++++++++++-------------- alog.h | 5 +++-- 3 files changed, 35 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index d999773..63910eb 100644 --- a/Makefile +++ b/Makefile @@ -20,7 +20,7 @@ ALOG_LIB = $(TARGET_DIR)/alog.so include $(dir $(lastword $(MAKEFILE_LIST)))tests/tests.mk .PHONY: alog-so -alog-so: $(if $(shell stat $(ALOG_LIB)), clean) $(ALOG_LIB) +alog-so: $(if $(shell stat $(ALOG_LIB)), alog-clean) $(ALOG_LIB) $(ALOG_LIB): $(TARGET_DIR) $(ALOG_OBJ) $(CC) $(LDFLAGS) -o $(ALOG_LIB) $(ALOG_OBJ) diff --git a/alog.c b/alog.c index bd603b2..26aecbb 100644 --- a/alog.c +++ b/alog.c @@ -144,20 +144,19 @@ void alog_remove_target(int fd) { // TODO: use preprocessor directives to gate off the posix timestamp stuff // unless the right variable is passed in at compile time -void alog ( +int _alog ( alog_sev severity, const char * message, - ... + va_list fmt ) { - TEST_ALOG_STATE(); if (severity == DEBUG && !ALOG_LOGGING_STATE->log_debug_messages) { // nop :) - return; + return -1; } va_list fmt_list; - va_start(fmt_list, message); + va_copy(fmt_list, fmt); char *buffer; int size; @@ -177,8 +176,8 @@ void alog ( // TODO: Why even use msg_and_timestamp if I am going to write it wholesale into buffer? size = vsnprintf(NULL, 0, msg_and_timestamp, fmt_list); - va_start(fmt_list, message); va_end(fmt_list); + va_copy(fmt_list, fmt); buffer = malloc(size + 1); vsprintf(buffer, msg_and_timestamp, fmt_list); va_end(fmt_list); @@ -187,14 +186,18 @@ void alog ( // if severity is PRINT we avoid timestamp } else { size = vsnprintf(NULL, 0, message, fmt_list); - va_start(fmt_list, message); + va_end(fmt_list); + va_copy(fmt_list, fmt); buffer = malloc(size + 1); vsprintf(buffer, message, fmt_list); va_end(fmt_list); } + int written = 0; for (int i = 0; i < _alog_num_out_fds; i++) { - if (write(_alog_out_fds[i], buffer, size) < size) { + int num = write(_alog_out_fds[i], buffer, size); + written += num; + if (num < size) { // TODO: how to handle? probably cant log another message lmao // perhaps it should be yanked from the list? maybe not? ;; @@ -204,7 +207,9 @@ void alog ( if (severity == ERROR) { for (int i = 0; i < _alog_num_err_fds; i++) { - if (write(_alog_err_fds[i], buffer, size) < size) { + int num = write(_alog_err_fds[i], buffer, size); + written += num; + if (num < size) { // TODO: see above ;; } @@ -213,19 +218,31 @@ void alog ( } free(buffer); + return written; } +int alog ( + alog_sev severity, + const char * message, + ... +) { + TEST_ALOG_STATE(); + va_list fmt_list; + va_start(fmt_list, message); + int res = _alog(severity, message, fmt_list); + va_end(fmt_list); + return res; +} + + #ifdef ALOG_HIJACK_PRINTF int printf(const char *format, ...) { TEST_ALOG_STATE(); va_list fmt_list; va_start(fmt_list, format); - /* TODO: this is a duplicate call given the implementation of alog. - * Lets figure out a better way to handle this */ - int size = snprintf(NULL, 0, format, fmt_list); - alog(PRINT, NULL, format, fmt_list); + int i = _alog(PRINT, format, fmt_list); va_end(fmt_list); - return size; + return i; /* potentially accept a mem leak at end of program for alog state. * That is, if the program using this is not already using alog correctly * or if ALOG has been LD_PRELOAD'ed into a program to override its printf */ diff --git a/alog.h b/alog.h index ef2deb9..4b5c104 100644 --- a/alog.h +++ b/alog.h @@ -78,8 +78,9 @@ void alog_add_target(int, char); /* removes an fd from both out fds and err fds */ void alog_remove_target(int); -/* call to log a message. Takes a severity, a message, and format stuff */ -void alog(alog_sev, const char *, ...); +/* call to log a message. Takes a severity, a message, and format stuff + * returns number of bytes written*/ +int alog(alog_sev, const char *, ...); /* overrides all calls to printf with a call to alog */ #ifdef ALOG_HIJACK_PRINTF