test and update the printf functionality
Signed-off-by: Ava Hahn <ava@aidanis.online>
This commit is contained in:
parent
eeaa710f68
commit
56f87419f1
3 changed files with 35 additions and 17 deletions
2
Makefile
2
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)
|
||||
|
|
|
|||
45
alog.c
45
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 */
|
||||
|
|
|
|||
5
alog.h
5
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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue