logging improvement

This commit is contained in:
Riley 2025-05-13 19:48:20 -05:00
parent 18f30fd963
commit 03b7afbabf
3 changed files with 29 additions and 7 deletions

View file

@ -1,4 +1,4 @@
char* appdata_path(); char* appdata_home_path();
int folder_exists(const char* path); int folder_exists(const char* path);
int setup_appdata(); int setup_appdata();
char* custom_appdata_path(const char* folder_name); char* custom_appdata_path(const char* folder_name);

View file

@ -2,6 +2,8 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <paths.h> #include <paths.h>
#include <cwalk.h>
#include <log.h>
#ifdef _WIN32 #ifdef _WIN32
#include <windows.h> #include <windows.h>
#elif __linux__ || __APPLE__ #elif __linux__ || __APPLE__
@ -9,11 +11,26 @@
#endif #endif
int main(void) { int main(void) {
puts("Hello, World!"); char *log_path = malloc(256);
cwk_path_join(appdata_home_path(), "error.log", log_path, 256);
FILE *fp_e = fopen(log_path, "w");
log_add_fp(fp_e, LOG_ERROR);
free(log_path);
log_path = malloc(256);
cwk_path_join(appdata_home_path(), "debug.log", log_path, 256);
FILE *fp_d = fopen(log_path, "w");
log_add_fp(fp_d, LOG_DEBUG);
free(log_path);
setup_appdata(); setup_appdata();
printf_s("Appdata path: %s\n", appdata_path()); log_info("Appdata path: %s", appdata_home_path());
const char *cupath = custom_appdata_path("ToDoLists"); const char *cupath = custom_appdata_path("ToDoLists");
printf_s("Custom appdata path: %s\n", cupath); log_info("Custom appdata path: %s", cupath);
return 0; return 0;
} }

View file

@ -12,10 +12,15 @@
// if (result == NULL) { // if (result == NULL) {
// return NULL; // Handle memory allocation failure // return NULL; // Handle memory allocation failure
char* appdata_path() { char* appdata_home_path() {
log_debug("Getting appdata path"); log_debug("Getting appdata path");
char* path = malloc(256); char* path = malloc(256);
#ifdef _WIN32
cwk_path_join(getenv("APPDATA"), APPDATA_FOLDER, path, 256); cwk_path_join(getenv("APPDATA"), APPDATA_FOLDER, path, 256);
#elif __linux__ || __APPLE__
cwk_path_join(getenv("HOME"), ".local", path, 256);
cwk_path_join(path, APPDATA_FOLDER, path, 256);
#endif
log_debug("Appdata path: %s", path); log_debug("Appdata path: %s", path);
return path; return path;
} }
@ -31,7 +36,7 @@ int folder_exists(const char* path) {
} }
int setup_appdata() { int setup_appdata() {
char* path = appdata_path(); char* path = appdata_home_path();
if (!folder_exists(path)) { if (!folder_exists(path)) {
mkdir(path); mkdir(path);
@ -50,7 +55,7 @@ int setup_appdata() {
char* custom_appdata_path(const char* folder_name) { char* custom_appdata_path(const char* folder_name) {
char *path = malloc(256); char *path = malloc(256);
cwk_path_join(appdata_path(), folder_name, path, 256); cwk_path_join(appdata_home_path(), folder_name, path, 256);
if (!folder_exists(path)) { if (!folder_exists(path)) {
log_debug("Custom appdata folder does not exist, creating: %s", path); log_debug("Custom appdata folder does not exist, creating: %s", path);