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/core/federated/RTI/CMakeLists.txt b/core/federated/RTI/CMakeLists.txt index 56217d06a..4540c49b8 100644 --- a/core/federated/RTI/CMakeLists.txt +++ b/core/federated/RTI/CMakeLists.txt @@ -44,6 +44,18 @@ IF(CMAKE_BUILD_TYPE MATCHES DEBUG) ENDIF(CMAKE_BUILD_TYPE MATCHES DEBUG) target_compile_definitions(${RTI_LIB} PUBLIC LOG_LEVEL=${LOG_LEVEL}) +include(${LF_ROOT}/low_level_platform/impl/CMakeLists.txt) +target_link_libraries(${RTI_LIB} PUBLIC lf::low-level-platform-impl) + +include(${LF_ROOT}/low_level_platform/api/CMakeLists.txt) +target_link_libraries(${RTI_LIB} PUBLIC lf::low-level-platform-api) + +include(${LF_ROOT}/platform/api/CMakeLists.txt) +target_link_libraries(${RTI_LIB} PUBLIC lf::platform-api) + +include(${LF_ROOT}/platform/impl/CMakeLists.txt) +target_link_libraries(${RTI_LIB} PUBLIC lf::platform-impl) + include(${LF_ROOT}/version/api/CMakeLists.txt) target_link_libraries(${RTI_LIB} PUBLIC lf::version-api) @@ -53,24 +65,12 @@ target_link_libraries(${RTI_LIB} PUBLIC lf::logging-api) include(${LF_ROOT}/tag/api/CMakeLists.txt) target_link_libraries(${RTI_LIB} PUBLIC lf::tag-api) -include(${LF_ROOT}/platform/api/CMakeLists.txt) -target_link_libraries(${RTI_LIB} PUBLIC lf::platform-api) - -include(${LF_ROOT}/platform/impl/CMakeLists.txt) -target_link_libraries(${RTI_LIB} PUBLIC lf::platform-impl) - include(${LF_ROOT}/trace/api/CMakeLists.txt) target_link_libraries(${RTI_LIB} PUBLIC lf::trace-api) include(${LF_ROOT}/trace/impl/CMakeLists.txt) target_link_libraries(${RTI_LIB} PUBLIC lf::trace-impl) -include(${LF_ROOT}/low_level_platform/impl/CMakeLists.txt) -target_link_libraries(${RTI_LIB} PUBLIC lf::low-level-platform-impl) - -include(${LF_ROOT}/low_level_platform/api/CMakeLists.txt) -target_link_libraries(${RTI_LIB} PUBLIC lf::low-level-platform-api) - # Set the STANDALONE_RTI flag to include the rti_remote and rti_common. target_compile_definitions(${RTI_LIB} PUBLIC STANDALONE_RTI=1) 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; +}