feat: added basic logging

c-web-server
Jef Roosens 2023-05-25 21:04:55 +02:00 committed by Chewing_Bever
parent 32687e0b49
commit 5dc772e507
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
3 changed files with 76 additions and 5 deletions

38
include/log.h 100644
View File

@ -0,0 +1,38 @@
#ifndef LANDER_LOG
#define LANDER_LOG
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
typedef enum log_level {
log_level_debug = 0,
log_level_info = 1,
log_level_warning = 2,
log_level_error = 3,
log_level_critical = 4,
} log_level;
extern const char *log_level_names[];
void _lander_log(log_level level, FILE *f, const char *fmt, ...);
#define flog(level, f, ...) _lander_log(level, f, __VA_ARGS__)
#define log(level, ...) _lander_log(level, NULL, __VA_ARGS__)
#define debug(...) _lander_log(log_level_debug, NULL, __VA_ARGS__)
#define fdebug(f, ...) _lander_log(log_level_debug, f, __VA_ARGS__)
#define info(...) _lander_log(log_level_info, NULL, __VA_ARGS__)
#define finfo(f, ...) _lander_log(log_level_info, f, __VA_ARGS__)
#define warning(...) _lander_log(log_level_warning, NULL, __VA_ARGS__)
#define fwarning(f, ...) _lander_log(log_level_warning, f, __VA_ARGS__)
#define error(...) _lander_log(log_level_error, NULL, __VA_ARGS__)
#define ferror(f, ...) _lander_log(log_level_error, f, __VA_ARGS__)
#define critical(status, ...) \
_lander_log(log_level_critical, NULL, __VA_ARGS__); \
exit(status)
#define fcritical(status, f, ...) \
_lander_log(log_level_critical, f, __VA_ARGS__); \
exit(status)
#endif

View File

@ -11,6 +11,7 @@
#include <unistd.h>
#include "event_loop_internal.h"
#include "log.h"
#include "picohttpparser.h"
static int event_loop_fd_set_nb(int fd) {
@ -97,7 +98,7 @@ void event_loop_run(event_loop *el, int port) {
int fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0) {
return;
critical(1, "Failed to open listening socket, errno: %i", errno);
}
int val = 1;
@ -111,20 +112,24 @@ void event_loop_run(event_loop *el, int port) {
int res = bind(fd, (const struct sockaddr *)&addr, sizeof(addr));
if (res) {
return;
critical(1, "Failed to bind listening socket, errno: %i", errno);
}
debug("Listening socket bound to fd %i", fd);
res = listen(fd, SOMAXCONN);
if (res) {
return;
critical(1, "Failed to start listening on listening socket, errno: %i",
errno);
}
// The listening socket is always poll'ed in non-blocking mode as well
res = event_loop_fd_set_nb(fd);
if (res != 0) {
return;
critical(1, "Failed to set listening socket to non-blocking, errno: %i",
errno);
}
// TODO don't hardcode the number 32
@ -138,6 +143,8 @@ void event_loop_run(event_loop *el, int port) {
event_loop_conn *conn;
int events;
info("Starting event loop on port %i", port);
while (1) {
poll_args_count = 1;
@ -165,8 +172,9 @@ void event_loop_run(event_loop *el, int port) {
// poll for active fds
// the timeout argument doesn't matter here
int rv = poll(poll_args, (nfds_t)poll_args_count, 1000);
if (rv < 0) {
return;
critical(1, "Poll failed, errno: %i", errno);
}
// process active connections

25
src/log.c 100644
View File

@ -0,0 +1,25 @@
#include <time.h>
#include "log.h"
const char *log_level_names[] = {"DEBUG", "INFO ", "WARN ", "ERROR",
"CRITICAL"};
void _lander_log(log_level level, FILE *f, const char *fmt, ...) {
// Log to stdout by default
f = (f == NULL) ? stdout : f;
char date_str[32];
time_t now = time(NULL);
strftime(date_str, sizeof(date_str) - 1, "%Y-%m-%d %H:%M:%S",
localtime(&now));
fprintf(f, "[%s][%s] ", date_str, log_level_names[level]);
va_list ap;
va_start(ap, fmt);
vfprintf(f, fmt, ap);
va_end(ap);
fprintf(f, "\n");
}