From 5dc772e5072fd80ddf3757ff0fb1093e6ccd1af6 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Thu, 25 May 2023 21:04:55 +0200 Subject: [PATCH] feat: added basic logging --- include/log.h | 38 +++++++++++++++++++++++++++++++++++++ src/event_loop/event_loop.c | 18 +++++++++++++----- src/log.c | 25 ++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 5 deletions(-) create mode 100644 include/log.h create mode 100644 src/log.c diff --git a/include/log.h b/include/log.h new file mode 100644 index 0000000..2a33278 --- /dev/null +++ b/include/log.h @@ -0,0 +1,38 @@ +#ifndef LANDER_LOG +#define LANDER_LOG + +#include +#include +#include +#include + +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 diff --git a/src/event_loop/event_loop.c b/src/event_loop/event_loop.c index 6dddf3c..3cf28ec 100644 --- a/src/event_loop/event_loop.c +++ b/src/event_loop/event_loop.c @@ -11,6 +11,7 @@ #include #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 diff --git a/src/log.c b/src/log.c new file mode 100644 index 0000000..b6b0ab6 --- /dev/null +++ b/src/log.c @@ -0,0 +1,25 @@ +#include + +#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"); +}