Line data Source code
1 : /* SPDX-License-Identifier: GPL-3.0-or-later */
2 : /* Copyright 2026 Peter Csaszar */
3 :
4 : /**
5 : * @file app/bootstrap.c
6 : * @brief Shared startup path: directories, logger.
7 : */
8 :
9 : #include "app/bootstrap.h"
10 :
11 : #include "logger.h"
12 : #include "fs_util.h"
13 : #include "platform/path.h"
14 :
15 : #include <stdio.h>
16 : #include <string.h>
17 :
18 10 : int app_bootstrap(AppContext *ctx, const char *program_name) {
19 10 : if (!ctx || !program_name) return -1;
20 :
21 10 : memset(ctx, 0, sizeof(*ctx));
22 10 : ctx->cache_dir = platform_cache_dir();
23 10 : ctx->config_dir = platform_config_dir();
24 :
25 10 : if (ctx->cache_dir) fs_mkdir_p(ctx->cache_dir, 0700);
26 10 : if (ctx->config_dir) fs_mkdir_p(ctx->config_dir, 0700);
27 :
28 10 : const char *base = ctx->cache_dir ? ctx->cache_dir : "/tmp/tg-cli";
29 10 : snprintf(ctx->log_path, sizeof(ctx->log_path), "%s/logs", base);
30 10 : fs_mkdir_p(ctx->log_path, 0700);
31 :
32 10 : size_t dir_len = strlen(ctx->log_path);
33 10 : snprintf(ctx->log_path + dir_len, sizeof(ctx->log_path) - dir_len,
34 : "/%s.log", program_name);
35 :
36 10 : logger_init(ctx->log_path, LOG_INFO);
37 10 : logger_log(LOG_INFO, "%s starting", program_name);
38 10 : return 0;
39 : }
40 :
41 10 : void app_shutdown(AppContext *ctx) {
42 : (void)ctx;
43 10 : logger_log(LOG_INFO, "shutdown");
44 10 : logger_close();
45 10 : }
|