test and update the printf functionality

Signed-off-by: Ava Hahn <ava@aidanis.online>
This commit is contained in:
Ava Hahn 2022-08-11 11:15:42 -07:00
parent eeaa710f68
commit 56f87419f1
No known key found for this signature in database
GPG key ID: 18D8AD0591CA303E
3 changed files with 35 additions and 17 deletions

View file

@ -20,7 +20,7 @@ ALOG_LIB = $(TARGET_DIR)/alog.so
include $(dir $(lastword $(MAKEFILE_LIST)))tests/tests.mk include $(dir $(lastword $(MAKEFILE_LIST)))tests/tests.mk
.PHONY: alog-so .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) $(ALOG_LIB): $(TARGET_DIR) $(ALOG_OBJ)
$(CC) $(LDFLAGS) -o $(ALOG_LIB) $(ALOG_OBJ) $(CC) $(LDFLAGS) -o $(ALOG_LIB) $(ALOG_OBJ)

45
alog.c
View file

@ -144,20 +144,19 @@ void alog_remove_target(int fd) {
// TODO: use preprocessor directives to gate off the posix timestamp stuff // TODO: use preprocessor directives to gate off the posix timestamp stuff
// unless the right variable is passed in at compile time // unless the right variable is passed in at compile time
void alog ( int _alog (
alog_sev severity, alog_sev severity,
const char * message, const char * message,
... va_list fmt
) { ) {
TEST_ALOG_STATE();
if (severity == DEBUG && if (severity == DEBUG &&
!ALOG_LOGGING_STATE->log_debug_messages) { !ALOG_LOGGING_STATE->log_debug_messages) {
// nop :) // nop :)
return; return -1;
} }
va_list fmt_list; va_list fmt_list;
va_start(fmt_list, message); va_copy(fmt_list, fmt);
char *buffer; char *buffer;
int size; 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? // 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); size = vsnprintf(NULL, 0, msg_and_timestamp, fmt_list);
va_start(fmt_list, message);
va_end(fmt_list); va_end(fmt_list);
va_copy(fmt_list, fmt);
buffer = malloc(size + 1); buffer = malloc(size + 1);
vsprintf(buffer, msg_and_timestamp, fmt_list); vsprintf(buffer, msg_and_timestamp, fmt_list);
va_end(fmt_list); va_end(fmt_list);
@ -187,14 +186,18 @@ void alog (
// if severity is PRINT we avoid timestamp // if severity is PRINT we avoid timestamp
} else { } else {
size = vsnprintf(NULL, 0, message, fmt_list); 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); buffer = malloc(size + 1);
vsprintf(buffer, message, fmt_list); vsprintf(buffer, message, fmt_list);
va_end(fmt_list); va_end(fmt_list);
} }
int written = 0;
for (int i = 0; i < _alog_num_out_fds; i++) { 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 // TODO: how to handle? probably cant log another message lmao
// perhaps it should be yanked from the list? maybe not? // perhaps it should be yanked from the list? maybe not?
;; ;;
@ -204,7 +207,9 @@ void alog (
if (severity == ERROR) { if (severity == ERROR) {
for (int i = 0; i < _alog_num_err_fds; i++) { 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 // TODO: see above
;; ;;
} }
@ -213,19 +218,31 @@ void alog (
} }
free(buffer); 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 #ifdef ALOG_HIJACK_PRINTF
int printf(const char *format, ...) { int printf(const char *format, ...) {
TEST_ALOG_STATE(); TEST_ALOG_STATE();
va_list fmt_list; va_list fmt_list;
va_start(fmt_list, format); va_start(fmt_list, format);
/* TODO: this is a duplicate call given the implementation of alog. int i = _alog(PRINT, format, fmt_list);
* Lets figure out a better way to handle this */
int size = snprintf(NULL, 0, format, fmt_list);
alog(PRINT, NULL, format, fmt_list);
va_end(fmt_list); va_end(fmt_list);
return size; return i;
/* potentially accept a mem leak at end of program for alog state. /* 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 * 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 */ * or if ALOG has been LD_PRELOAD'ed into a program to override its printf */

5
alog.h
View file

@ -78,8 +78,9 @@ void alog_add_target(int, char);
/* removes an fd from both out fds and err fds */ /* removes an fd from both out fds and err fds */
void alog_remove_target(int); void alog_remove_target(int);
/* call to log a message. Takes a severity, a message, and format stuff */ /* call to log a message. Takes a severity, a message, and format stuff
void alog(alog_sev, const char *, ...); * returns number of bytes written*/
int alog(alog_sev, const char *, ...);
/* overrides all calls to printf with a call to alog */ /* overrides all calls to printf with a call to alog */
#ifdef ALOG_HIJACK_PRINTF #ifdef ALOG_HIJACK_PRINTF