feat(lnm): add logging framework

This commit is contained in:
Jef Roosens 2023-12-11 15:00:34 +01:00
parent e44b7d0e5f
commit 3aa0ace863
Signed by: Jef Roosens
GPG key ID: B75D4F293C7052DB
4 changed files with 171 additions and 6 deletions

View file

@ -3,6 +3,7 @@
#include <time.h>
#include "lnm/http/loop.h"
#include "lnm/log.h"
#include "lander.h"
#include "log.h"
@ -61,7 +62,8 @@ lnm_http_loop *loop_init(lander_gctx *gctx, const char *api_key) {
#define ENV(var, env_var) \
const char *var = getenv(env_var); \
if (var == NULL) { \
critical(1, "Missing environment variable %s", env_var); \
lnm_lcritical("main", "Missing environment variable %s", env_var); \
exit(1); \
}
#define ENV_OPT(var, env_var, default) \
@ -71,9 +73,11 @@ lnm_http_loop *loop_init(lander_gctx *gctx, const char *api_key) {
}
int main() {
setvbuf(stdout, NULL, _IONBF, 0);
srand(time(NULL));
lnm_log_init_global();
lnm_log_register_stdout(lnm_log_level_debug);
ENV(api_key, "LANDER_API_KEY");
ENV_OPT(port_str, "LANDER_PORT", "18080");
ENV_OPT(data_dir_s, "LANDER_DATA_DIR", ".");
@ -81,7 +85,8 @@ int main() {
int port = atoi(port_str);
if (port <= 0 || port >= 1 << 16) {
critical(1, "Invalid TCP port %s", port_str);
lnm_lcritical("main", "Invalid TCP port %s", port_str);
exit(1);
}
lander_gctx *c_gctx = lander_gctx_init();
@ -90,13 +95,14 @@ int main() {
lsm_str *data_dir;
lsm_str_init_copy(&data_dir, (char *)data_dir_s);
info("Initializing store from path '%s'", data_dir_s);
lnm_linfo("main", "Initializing store from path '%s'", data_dir_s);
if (lsm_store_load(&c_gctx->store, data_dir) != lsm_error_ok) {
critical(2, "Failed to load existing store.");
lnm_lcritical("main", "%s", "Failed to load existing store.");
}
info("Store loaded containing %lu entries", lsm_store_size(c_gctx->store));
lnm_linfo("main", "Store loaded containing %lu entries",
lsm_store_size(c_gctx->store));
lnm_http_loop *hl = loop_init(c_gctx, api_key);
lnm_http_loop_run(hl, port);