add rudimentary unit tests, and logging timestamps

Signed-off-by: Aidan Hahn <aidan@aidanis.online>
This commit is contained in:
Aidan Hahn 2022-02-28 01:09:33 -08:00
parent 7fafcd344d
commit 1e9b701542
No known key found for this signature in database
GPG key ID: 327711E983899316
6 changed files with 188 additions and 37 deletions

89
alog.c
View file

@ -20,6 +20,8 @@
#include <stdarg.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#define TEST_ALOG_STATE() if (!ALOG_LOGGING_STATE) { _init_alog(); }
@ -37,7 +39,7 @@ void _init_alog() {
_alog_num_err_fds = 1;
_alog_err_fds = malloc(sizeof(int) * 1);
// stderr
_alog_err_fds[1] = 2;
_alog_err_fds[0] = 2;
}
// TODO: should I dont close stdin and stdout?
@ -81,18 +83,25 @@ void alog_remove_target(int fd) {
}
}
int *tmp_outs = malloc(sizeof(int) * out_fds);
int tmp_iter_idx = 0;
for (int i = 0; i < _alog_num_out_fds; i++) {
if (_alog_out_fds[i] != fd) {
tmp_outs[tmp_iter_idx] = _alog_out_fds[i];
tmp_iter_idx++;
}
}
if (!out_fds) {
_alog_num_out_fds = 0;
free(_alog_out_fds);
_alog_out_fds = NULL;
free(_alog_out_fds);
_alog_out_fds = tmp_outs;
_alog_num_out_fds = out_fds;
} else {
int *tmp_outs = malloc(sizeof(int) * out_fds);
int tmp_iter_idx = 0;
for (int i = 0; i < _alog_num_out_fds; i++) {
if (_alog_out_fds[i] != fd) {
tmp_outs[tmp_iter_idx] = _alog_out_fds[i];
tmp_iter_idx++;
}
}
free(_alog_out_fds);
_alog_out_fds = tmp_outs;
_alog_num_out_fds = out_fds;
}
int err_fds = 0;
for (int i = 0; i < _alog_num_err_fds; i++) {
@ -101,20 +110,29 @@ void alog_remove_target(int fd) {
}
}
int *tmp_errs = malloc(sizeof(int) * err_fds);
tmp_iter_idx = 0;
for (int i = 0; i < _alog_num_err_fds; i++) {
if (_alog_err_fds[i] != fd) {
tmp_errs[tmp_iter_idx] = _alog_err_fds[i];
tmp_iter_idx++;
}
}
if (!err_fds) {
_alog_num_err_fds = 0;
free(_alog_err_fds);
_alog_err_fds = NULL;
free(_alog_err_fds);
_alog_err_fds = tmp_errs;
_alog_num_err_fds = err_fds;
} else {
int *tmp_errs = malloc(sizeof(int) * err_fds);
int tmp_iter_idx = 0;
for (int i = 0; i < _alog_num_err_fds; i++) {
if (_alog_err_fds[i] != fd) {
tmp_errs[tmp_iter_idx] = _alog_err_fds[i];
tmp_iter_idx++;
}
}
free(_alog_err_fds);
_alog_err_fds = tmp_errs;
_alog_num_err_fds = err_fds;
}
}
// TODO: use preprocessor directives to gate off the posix timestamp stuff
// unless the right variable is passed in at compile time
void alog (
alog_sev severity,
const char * message,
@ -130,9 +148,28 @@ void alog (
va_list fmt_list;
va_start(fmt_list, message);
int size = snprintf(NULL, 0, message, fmt_list);
char *buffer = malloc(size + 1);
sprintf(buffer, message, fmt_list);
char *buffer;
int size;
if (severity != PRINT) {
// GET TIMESTAMP
time_t t = time(NULL);
struct tm * p = localtime(&t);
int timestamp_size = strftime(NULL, 0, "[%c]", p);
char *timestamp = malloc(sizeof(char) * (timestamp_size+1));
strftime(timestamp, timestamp_size, "[%c]", p);
char *msg_and_timestamp = malloc(sizeof(char) * (timestamp_size + strlen(message) + 3));
sprintf(msg_and_timestamp, "%s %s\n", timestamp, message);
free(timestamp);
size = snprintf(NULL, 0, msg_and_timestamp, fmt_list);
buffer = malloc(size + 1);
sprintf(buffer, msg_and_timestamp, fmt_list);
free(msg_and_timestamp);
// if severity is PRINT we avoid timestamp
} else {
size = snprintf(NULL, 0, message, fmt_list);
buffer = malloc(size + 1);
sprintf(buffer, message, fmt_list);
}
for (int i = 0; i < _alog_num_out_fds; i++) {
if (write(_alog_out_fds[i], buffer, size) < size) {