#include <stdlib.h> #include <stdarg.h> #include <assert.h> #include <logger.h> bool logger_init(logger_t* logger, FILE* fd) { assert(logger); assert(fd); logger->fd = fd; int result = mtx_init(&logger->mutex, mtx_plain); return result == thrd_success; } void logger_deinit(logger_t* logger) { assert(logger); logger->fd = NULL; mtx_destroy(&logger->mutex); } logger_t* logger_open(const char* filename) { assert(filename); logger_t* logger = calloc(1, sizeof(logger_t)); if (!logger) { return NULL; } FILE* fd = fopen(filename, "w+"); if (!fd) { free(logger); return NULL; } if (!logger_init(logger, fd)) { free(logger); fclose(fd); return NULL; } return logger; } void logger_close(logger_t* logger) { assert(logger); fclose(logger->fd); logger_deinit(logger); free(logger); } int logger_log(logger_t* logger, const char* format, ...) { assert(logger); assert(format); va_list args; va_start(args, format); int result = 0; // TODO: Possibly log the current time similar to how ruby logger does it mtx_lock(&logger->mutex); result = vfprintf(logger->fd, format, args); mtx_unlock(&logger->mutex); va_end(args); return result; }