Skip to content

Commit

Permalink
add missing logger
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-courtis committed Feb 27, 2024
1 parent 1cfc177 commit 194cdc1
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ pro/*.c

/.ccls-cache

log.*
/log.*
24 changes: 24 additions & 0 deletions inc/log.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef LOG_H
#define LOG_H

#include <stdbool.h>

#include "enum.h"

void log_debug(const char *__restrict __format, ...);

void log_info(const char *__restrict __format, ...);

void log_warn(const char *__restrict __format, ...);

void log_error(const char *__restrict __format, ...);

void log_error_errno(const char *__restrict __format, ...);

enum LogThreshold log_get_threshold(void);

// false if not valid
bool log_set_threshold(const char *s);

#endif // LOG_H

116 changes: 116 additions & 0 deletions src/log.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#include <errno.h>
#include <stdbool.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <time.h>

#include "enum.h"

#include "log.h"

char THRESHOLD_CHAR[] = {
'?',
'D',
'I',
'W',
'E',
};

char *THRESHOLD_LABEL[] = {
"",
"",
"",
"WARNING: ",
"ERROR: ",
};

enum LogThreshold log_threshold = LOG_THRESHOLD_DEFAULT;

bool log_prefix = true;

void print_prefix(const enum LogThreshold threshold, FILE *__restrict __stream) {
static char buf[16];
static time_t t;

t = time(NULL);

strftime(buf, sizeof(buf), "%H:%M:%S", localtime(&t));

fprintf(__stream, "%c [%s] ", THRESHOLD_CHAR[threshold], buf);
}

void log_(const enum LogThreshold threshold, const int eno, const char *__restrict __format, va_list __args) {
if (threshold < log_threshold) {
return;
}

static FILE *stream;

stream = threshold == ERROR ? stderr : stdout;

if (log_prefix) {
print_prefix(threshold, stream);
}

fprintf(stream, "%s", THRESHOLD_LABEL[threshold]);

vfprintf(stream, __format, __args);

if (eno) {
fprintf(stream, " %d: %s", eno, strerror(eno));
}

fprintf(stream, "\n");

fflush(stream);
}

void log_debug(const char *__restrict __format, ...) {
va_list args;
va_start(args, __format);
log_(DEBUG, 0, __format, args);
va_end(args);
}

void log_info(const char *__restrict __format, ...) {
va_list args;
va_start(args, __format);
log_(INFO, 0, __format, args);
va_end(args);
}

void log_warn(const char *__restrict __format, ...) {
va_list args;
va_start(args, __format);
log_(WARNING, 0, __format, args);
va_end(args);
}

void log_error(const char *__restrict __format, ...) {
va_list args;
va_start(args, __format);
log_(ERROR, 0, __format, args);
va_end(args);
}

void log_error_errno(const char *__restrict __format, ...) {
va_list args;
va_start(args, __format);
log_(ERROR, errno, __format, args);
va_end(args);
}

enum LogThreshold log_get_threshold(void) {
return log_threshold;
}

bool log_set_threshold(const char *s) {
enum LogThreshold t = log_threshold_val(s);
if (t) {
log_threshold = t;
return true;
}
return false;
}

0 comments on commit 194cdc1

Please sign in to comment.