diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ae670e1fa..32abf4e24 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -21,12 +21,12 @@ jobs: unit-tests-single: uses: ./.github/workflows/unit-tests.yml with: - cmake-args: '-UNUMBER_OF_WORKERS -DLF_SINGLE_THREADED=1' + cmake-args: '-UNUMBER_OF_WORKERS -DLF_SINGLE_THREADED=1 -DLOG_LEVEL=4' unit-tests-multi: uses: ./.github/workflows/unit-tests.yml with: - cmake-args: '-DNUMBER_OF_WORKERS=4 -ULF_SINGLE_THREADED' + cmake-args: '-DNUMBER_OF_WORKERS=4 -ULF_SINGLE_THREADED -DLOG_LEVEL=4' build-rti: uses: ./.github/workflows/build-rti.yml diff --git a/Makefile b/Makefile index e7baab409..b0f86c3b1 100644 --- a/Makefile +++ b/Makefile @@ -29,7 +29,7 @@ clean: .PHONY: unit-tests unit-tests: clean # In case NUMBER_OF_WORKERS has been set, unset it. - cmake -B build -UNUMBER_OF_WORKERS + cmake -B build -UNUMBER_OF_WORKERS -DLOG_LEVEL=4 cmake --build build cd build && make test diff --git a/logging/api/logging_macros.h b/logging/api/logging_macros.h index 2a8a04c1b..3a8441160 100644 --- a/logging/api/logging_macros.h +++ b/logging/api/logging_macros.h @@ -12,6 +12,7 @@ #define LOGGING_MACROS_H #include "logging.h" #include +#include "low_level_platform.h" /** Default log level. */ #ifndef LOG_LEVEL @@ -72,7 +73,7 @@ static const bool _lf_log_level_is_debug = LOG_LEVEL >= LOG_LEVEL_DEBUG; #define LF_PRINT_DEBUG(format, ...) \ do { \ if (_lf_log_level_is_debug) { \ - lf_print_debug(format, ##__VA_ARGS__); \ + lf_print_debug("[" PRINTF_TIME "]" format, lf_time_physical_elapsed(), ##__VA_ARGS__); \ } \ } while (0) diff --git a/test/general/logging_test.c b/test/general/logging_test.c new file mode 100644 index 000000000..a20f7d5e3 --- /dev/null +++ b/test/general/logging_test.c @@ -0,0 +1,71 @@ +#include "logging_macros.h" +#include +#include +#include +#include +#include +#include + +#define TIMEED_DEBUG_CHAR_LEN 30 +#define SOME_EXTRA_SPACE 10 +/** + * @brief unit for LF_PRINT_DEBUG macro + * LOG_LEVEL must be in LOG_LEVEL_DEBUG + */ +void test_logging_macro(const char* expected, int st_len) { + + FILE* tmp = tmpfile(); // auto-deletes when closed + char* buffer; + char pattern[256]; + regex_t re; + int result; + + // Computing the buffer size based on strlen("DEBUG: ") + \0 + \n + extra space + int buffer_size = st_len + TIMEED_DEBUG_CHAR_LEN + SOME_EXTRA_SPACE; + + if (!tmp) { + perror("tmpfile"); + exit(1); + } + + // Redirect stdout -> tmp + fflush(stdout); + int fd = fileno(tmp); + if (fd == -1 || dup2(fd, STDOUT_FILENO) == -1) { + perror("redirect stdout"); + exit(1); + } + + // Call code under test + LF_PRINT_DEBUG("%s", expected); + + fflush(stdout); // flush so data goes into tmp + rewind(tmp); // reset read position + + // Read back + buffer = (char*)malloc(buffer_size); + size_t n = fread(buffer, 1, buffer_size - 1, tmp); + buffer[n] = '\0'; + + // Regex to check format: DEBUG: [number]expected\n + snprintf(pattern, sizeof(pattern), "^DEBUG: \\[-?[0-9]+\\]%s\\n?$", expected); + + if (regcomp(&re, pattern, REG_EXTENDED | REG_NEWLINE) != 0) { + perror("regcomp"); + exit(1); + } + + result = regexec(&re, buffer, 0, NULL, 0); + regfree(&re); + + assert(result == 0); // match succeeded + + fclose(tmp); // deletes the file + free(buffer); +} + +int main() { + char* str_test = "Hello World"; + test_logging_macro(str_test, strlen(str_test)); + return 0; +} diff --git a/trace/impl/CMakeLists.txt b/trace/impl/CMakeLists.txt index f4a6b8b55..2da8e5bfa 100644 --- a/trace/impl/CMakeLists.txt +++ b/trace/impl/CMakeLists.txt @@ -5,6 +5,7 @@ add_library(lf-trace-impl STATIC) add_library(lf::trace-impl ALIAS lf-trace-impl) target_link_libraries(lf-trace-impl PRIVATE lf::trace-api) target_link_libraries(lf-trace-impl PRIVATE lf::platform-api) +target_link_libraries(lf-trace-impl PRIVATE lf::low-level-platform-api) target_link_libraries(lf-trace-impl PRIVATE lf::logging-api) target_link_libraries(lf-trace-impl PRIVATE lf::version-api) lf_enable_compiler_warnings(lf-trace-impl)