From d36b59bb1e501ec018faba3d9a60dbfb2b58114a Mon Sep 17 00:00:00 2001 From: sanine Date: Tue, 14 Jun 2022 11:31:00 -0500 Subject: add basic logging function --- src/logging/.logging.h.swp | Bin 0 -> 12288 bytes src/logging/logging.c | 13 ++++++ src/logging/logging.h | 22 +++++++++ src/logging/logging.test.c | 110 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 145 insertions(+) create mode 100644 src/logging/.logging.h.swp create mode 100644 src/logging/logging.c create mode 100644 src/logging/logging.h create mode 100644 src/logging/logging.test.c (limited to 'src/logging') diff --git a/src/logging/.logging.h.swp b/src/logging/.logging.h.swp new file mode 100644 index 0000000..e2c510a Binary files /dev/null and b/src/logging/.logging.h.swp differ diff --git a/src/logging/logging.c b/src/logging/logging.c new file mode 100644 index 0000000..4f717cb --- /dev/null +++ b/src/logging/logging.c @@ -0,0 +1,13 @@ +#include +#include "logging/logging.h" + +int _honey_log_level = HONEY_WARN; + +void honey_log(int level, const char *fmt, ...) { + //if (level > _honey_log_level) return; + + va_list args; + va_start(args, fmt); + vfprintf(stderr, fmt, args); + va_end(args); +} diff --git a/src/logging/logging.h b/src/logging/logging.h new file mode 100644 index 0000000..2618d2b --- /dev/null +++ b/src/logging/logging.h @@ -0,0 +1,22 @@ +#ifndef HONEY_LOGGING_H +#define HONEY_LOGGING_H + +#define HONEY_FATAL 0 +#define HONEY_ERROR 1 +#define HONEY_WARN 2 +#define HONEY_INFO 3 +#define HONEY_DEBUG 4 +#define HONEY_TRACE 5 + +extern int _honey_log_level; + +void honey_log(int level, const char *fmt, ...); + +#define honey_fatal(...) honey_log(HONEY_FATAL, "[FATAL] "__VA_ARGS__) +#define honey_error(...) honey_log(HONEY_ERROR, "[ERROR] "__VA_ARGS__) +#define honey_warn(...) honey_log(HONEY_WARN, "[WARN] " __VA_ARGS__) +#define honey_info(...) honey_log(HONEY_INFO, "[INFO] " __VA_ARGS__) +#define honey_debug(...) honey_log(HONEY_DEBUG, "[DEBUG] "__VA_ARGS__) +#define honey_trace(...) honey_log(HONEY_TRACE, "[TRACE] "__VA_ARGS__) + +#endif diff --git a/src/logging/logging.test.c b/src/logging/logging.test.c new file mode 100644 index 0000000..e227381 --- /dev/null +++ b/src/logging/logging.test.c @@ -0,0 +1,110 @@ +#include +#include +#include + +#include "test/lily-test.h" +#include "test/honey-test.h" + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * + * include C file and declare mocks + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + */ +#define vfprintf(file, fmt, vl) mock_vfprintf(file, fmt, vl) +void mock_vfprintf(FILE*, const char*, va_list vl); +#include "logging.c" +#undef vfprintf + + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * + * define mock functions + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + */ + +lily_mock_t *vfprintf_mock_data; +void mock_vfprintf(FILE *file, const char *fmt, va_list vl) +{ + /* to avoid basically just re-implementing printf parsing here, + i am limiting this function to be able to receive strings only */ + + /* count format specifiers */ + char *ptr = strchr(fmt, '%'); + int n_args = 0; + while (ptr != NULL) { + n_args += 1; + ptr = strchr(ptr+1, '%'); + } + + /* store arguments */ + struct lily_mock_arg_t args[] = { + { sizeof(FILE*), &file }, + { sizeof(const char*), &fmt }, + { sizeof(int), &n_args }, + }; + lily_mock_call(vfprintf_mock_data, args); + + /* store format arguments */ + lily_queue_t *queue = vfprintf_mock_data->values; + for (int i=0; in_calls, 1); + + FILE *file; const char *fmt; int n_strings; + struct lily_mock_arg_t args[] = { + { sizeof(FILE*), &file }, + { sizeof(const char*), &fmt }, + { sizeof(int), &n_strings }, + }; + lily_get_call(vfprintf_mock_data, args, 0); + + lily_assert_ptr_equal(file, stderr); + lily_assert_string_equal(fmt, "[FATAL] some message"); + lily_assert_int_equal(n_strings, 0); +} -- cgit v1.2.1