From 1e529830cb1407b13bb0dc86587ec5618a3698f6 Mon Sep 17 00:00:00 2001 From: kaotisk Date: Mon, 17 Mar 2025 00:17:06 +0200 Subject: implemented: int ak_log_write_to_file(char* message) --- src/aklog.c | 17 +++++++++++++++++ src/build_tree/ak_log_build.sh | 6 +++++- src/include/aklog.h | 2 +- src/tests/test_aklog.c | 6 ++++++ src/tests/test_aklogwrite.c | 13 +++++++++++++ 5 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 src/tests/test_aklogwrite.c diff --git a/src/aklog.c b/src/aklog.c index d453353..10062b0 100644 --- a/src/aklog.c +++ b/src/aklog.c @@ -5,6 +5,22 @@ #include #define AK_DEBUG true +#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) + +int ak_log_write_to_file(char* message) +{ + FILE *fp; + fp = fopen("/home/kaotisk/.arching-kaos/logs/log", "ab"); + if (!fp) + { + perror("fopen"); + return EXIT_FAILURE; + } + fwrite(message, strlen(message),1,fp); + fwrite("\n", strlen("\n"),1,fp); + fclose(fp); + return 0; +} void ak_log_print_log_line(char* line) { @@ -207,6 +223,7 @@ void ak_log_message(char* program, char* type, char* message) { asprintf(&some_string, "%ld <%s> [%s] %s", ts, program, type, message); ak_log_print_log_line(some_string); + ak_log_write_to_file(some_string); // fprintf(stderr, "%ld <%s> [%s] %s\n", ts, program, type, message); } } diff --git a/src/build_tree/ak_log_build.sh b/src/build_tree/ak_log_build.sh index ddd4e64..ab83378 100755 --- a/src/build_tree/ak_log_build.sh +++ b/src/build_tree/ak_log_build.sh @@ -5,5 +5,9 @@ gcc -c -shared -Wextra -Wall -Werror -pedantic -ggdb -fPIC -I./include aklog.c - echo "Building tests/test_aklog" && \ gcc -Wextra -Wall -Werror -pedantic -ggdb -Wl,-rpath=lib -I./include tests/test_aklog.c lib/aklog.so -o tests/test_aklog && \ echo "Running test_aklog" && \ -time ./tests/test_aklog +time ./tests/test_aklog && \ rm ./tests/test_aklog +gcc -Wextra -Wall -Werror -pedantic -ggdb -Wl,-rpath=lib -I./include tests/test_aklogwrite.c lib/aklog.so -o tests/test_aklogwrite && \ +echo "Running test_aklogwrite" && \ +time ./tests/test_aklogwrite && \ +rm ./tests/test_aklogwrite diff --git a/src/include/aklog.h b/src/include/aklog.h index 5c82c21..ff396ea 100644 --- a/src/include/aklog.h +++ b/src/include/aklog.h @@ -5,12 +5,12 @@ void ak_log_print_log_line(char* line); void ak_log_follow(); void ak_log_grep(char* string); void ak_log_rotate(); +int ak_log_write_to_file(char* message); void ak_log_message(const char* program, char* type, char* message); void ak_log_exit(const char* program, char* message); void ak_log_warning(const char* program, char* message); void ak_log_debug(const char* program, char* message); void ak_log_error(const char* program, char* message); void ak_log_info(const char* program, char* message); -void ak_log_write_to_file(char* message); #endif // AKLOG diff --git a/src/tests/test_aklog.c b/src/tests/test_aklog.c index a2408a9..dad065f 100644 --- a/src/tests/test_aklog.c +++ b/src/tests/test_aklog.c @@ -50,6 +50,11 @@ void test_info() ak_log_info(__func__, "test message info"); } +void test_write() +{ + ak_log_info(__func__, "test message info"); +} + int main (void) { test_follow(); @@ -62,5 +67,6 @@ int main (void) test_log_message(); test_rotate(); test_grep(); + test_write(); return 0; } diff --git a/src/tests/test_aklogwrite.c b/src/tests/test_aklogwrite.c new file mode 100644 index 0000000..a6267b0 --- /dev/null +++ b/src/tests/test_aklogwrite.c @@ -0,0 +1,13 @@ +#include + +void test_write() +{ + ak_log_info(__func__, "test message info"); +} + +int main (void) +{ + test_write(); + return 0; +} + -- cgit v1.2.3