From a95f71f7a5ae571ca3747ff1e20dfceedbd2a11b Mon Sep 17 00:00:00 2001 From: javalsai Date: Wed, 11 Jun 2025 17:15:30 +0200 Subject: [PATCH] dev: start to implement logger in some places --- src/main.c | 4 ++-- src/sessions.c | 14 +++++++++----- src/users.c | 9 +++++++-- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/main.c b/src/main.c index f060034..8efa6c1 100644 --- a/src/main.c +++ b/src/main.c @@ -14,8 +14,6 @@ #include "util.h" int main(int argc, char* argv[]) { - if (argc == 2) chvt_str(argv[1]); - char* log_output = getenv("LIDM_LOG"); if (log_output) { FILE* log_fd = fopen(log_output, "w"); @@ -28,6 +26,8 @@ int main(int argc, char* argv[]) { log_init(log_fd); } + if (argc == 2) chvt_str(argv[1]); + char* conf_override = getenv("LIDM_CONF"); struct config* config = parse_config(conf_override == NULL ? "/etc/lidm.ini" : conf_override); diff --git a/src/sessions.c b/src/sessions.c index 4f0b010..4c745bd 100644 --- a/src/sessions.c +++ b/src/sessions.c @@ -8,6 +8,7 @@ #include #include "desktop.h" +#include "log.h" #include "sessions.h" #include "util.h" @@ -48,8 +49,10 @@ struct status cb(void* _ctx, char* NULLABLE table, char* key, char* value) { if (copy_at != NULL) { *copy_at = strdup(value); if (*copy_at == NULL) { + log_perror("strdup"); + log_puts("[E] failed to allocate memory"); ret.finish = true; - ret.ret = -1; // malloc error + ret.ret = -1; } } @@ -70,6 +73,7 @@ static int fn(const char* fpath, const struct stat* sb, int typeflag) { // - FTW_PHYS if set doesn't follow symlinks, so ftw() has no flags and it // follows symlinks, we should never get to handle that if (typeflag != FTW_F) return 0; + log_printf("[I] found file %s\n", fpath); struct ctx_typ ctx = { .name = NULL, @@ -79,14 +83,14 @@ static int fn(const char* fpath, const struct stat* sb, int typeflag) { FILE* fd = fopen(fpath, "r"); if (fd == NULL) { - perror("fopen"); - // NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling) - (void)fprintf(stderr, "error opening file '%s' for read\n", fpath); + log_perror("fopen"); + log_printf("[E] error opening file '%s' for read\n", fpath); return 0; } int ret = read_desktop(fd, &ctx, &cb); if (ret < 0) { // any error + log_printf("[E] format error parsing %s", fpath); return 0; } @@ -118,7 +122,7 @@ struct Vector get_avaliable_sessions() { cb_sessions = &sessions; for (size_t i = 0; i < (sizeof(sources) / sizeof(sources[0])); i++) { - /*printf("recurring into %s\n", sources[i].dir);*/ + log_printf("[I] parsing into %s\n", sources[i].dir); session_type = sources[i].type; ftw(sources[i].dir, &fn, 1); } diff --git a/src/users.c b/src/users.c index 3f32aef..6ecb0b7 100644 --- a/src/users.c +++ b/src/users.c @@ -5,6 +5,7 @@ #include #include +#include "log.h" #include "macros.h" #include "users.h" #include "util.h" @@ -46,15 +47,19 @@ struct Vector get_human_users() { struct passwd* NULLABLE pwd; while ((pwd = getpwent()) != NULL) { + log_printf("[I] handling user %s\n", pwd->pw_name); // `- 1` bcs sizeof counts the nullbyte - if (pwd->pw_dir && strncmp(pwd->pw_dir, "/home/", sizeof("/home/") - 1) != 0) + if (pwd->pw_dir && + strncmp(pwd->pw_dir, "/home/", sizeof("/home/") - 1) != 0) continue; + log_printf("[I] found %s\n", pwd->pw_name); struct user* user_i = malloc(sizeof(struct user)); if (user_i == NULL) continue; // malloc error if (build_user(user_i, pwd) != 0) { - // ... + log_printf("[E] failed to allocate allocate memory for %s\n", + pwd->pw_name); } vec_push(&users, user_i); }