Created
September 2, 2016 16:01
-
-
Save warmwaffles/9a4cff9af78108562fcbb2a8551426c5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <stdio.h> | |
#include <stdbool.h> | |
#include <tinycthread.h> | |
typedef struct logger | |
{ | |
mtx_t mutex; | |
FILE* fd; | |
} logger_t; | |
bool logger_init(logger_t* logger, FILE* fd); | |
void logger_deinit(logger_t* logger); | |
logger_t* logger_open(const char* filename); | |
int logger_log(logger_t* logger, const char* format, ...); | |
void logger_close(logger_t* logger); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment