diff --git a/README.md b/README.md index e73b8b7279..8ee5b62b76 100644 --- a/README.md +++ b/README.md @@ -128,7 +128,7 @@ List of options provided by CMake: | UMF_BUILD_EXAMPLES | Build UMF examples | ON/OFF | ON | | UMF_BUILD_FUZZTESTS | Build UMF fuzz tests (supported only on Linux with Clang) | ON/OFF | OFF | | UMF_BUILD_GPU_EXAMPLES | Build UMF GPU examples | ON/OFF | OFF | -| UMF_DEVELOPER_MODE | Enable additional developer checks | ON/OFF | OFF | +| UMF_DEVELOPER_MODE | Enable additional developer checks and logs | ON/OFF | OFF | | UMF_FORMAT_CODE_STYLE | Add clang, cmake, and black -format-check and -format-apply targets to make | ON/OFF | OFF | | UMF_TESTS_FAIL_ON_SKIP | Treat skips in tests as fail | ON/OFF | OFF | | UMF_USE_ASAN | Enable AddressSanitizer checks | ON/OFF | OFF | diff --git a/src/utils/utils_log.c b/src/utils/utils_log.c index 57ab0ef937..73836cd9e4 100644 --- a/src/utils/utils_log.c +++ b/src/utils/utils_log.c @@ -99,8 +99,8 @@ static const char *level_to_str(utils_log_level_t l) { #endif // _MSC_VER static void utils_log_internal(utils_log_level_t level, int perror, - const char *func, const char *format, - va_list args) { + const char *fileline, const char *func, + const char *format, va_list args) { if (!loggerConfig.output && level != LOG_FATAL) { return; //logger not enabled } @@ -115,7 +115,12 @@ static void utils_log_internal(utils_log_level_t level, int perror, char *b_pos = buffer; int b_size = sizeof(buffer); - int tmp = snprintf(b_pos, b_size, "%s: ", func); + int tmp = 0; + if (fileline == NULL) { + tmp = snprintf(b_pos, b_size, "%s: ", func); + } else { + tmp = snprintf(b_pos, b_size, "%s %s: ", fileline, func); + } ASSERT(tmp > 0); b_pos += (int)tmp; @@ -229,19 +234,19 @@ static void utils_log_internal(utils_log_level_t level, int perror, #pragma warning(pop) #endif // _MSC_VER -void utils_log(utils_log_level_t level, const char *func, const char *format, - ...) { +void utils_log(utils_log_level_t level, const char *fileline, const char *func, + const char *format, ...) { va_list args; va_start(args, format); - utils_log_internal(level, 0, func, format, args); + utils_log_internal(level, 0, fileline, func, format, args); va_end(args); } -void utils_plog(utils_log_level_t level, const char *func, const char *format, - ...) { +void utils_plog(utils_log_level_t level, const char *fileline, const char *func, + const char *format, ...) { va_list args; va_start(args, format); - utils_log_internal(level, 1, func, format, args); + utils_log_internal(level, 1, fileline, func, format, args); va_end(args); } diff --git a/src/utils/utils_log.h b/src/utils/utils_log.h index c0e0a95723..15fdabec66 100644 --- a/src/utils/utils_log.h +++ b/src/utils/utils_log.h @@ -24,29 +24,47 @@ typedef enum { LOG_FATAL } utils_log_level_t; -#define LOG_DEBUG(...) utils_log(LOG_DEBUG, __func__, __VA_ARGS__); -#define LOG_INFO(...) utils_log(LOG_INFO, __func__, __VA_ARGS__); -#define LOG_WARN(...) utils_log(LOG_WARNING, __func__, __VA_ARGS__); -#define LOG_ERR(...) utils_log(LOG_ERROR, __func__, __VA_ARGS__); -#define LOG_FATAL(...) utils_log(LOG_FATAL, __func__, __VA_ARGS__); - -#define LOG_PDEBUG(...) utils_plog(LOG_DEBUG, __func__, __VA_ARGS__); -#define LOG_PINFO(...) utils_plog(LOG_INFO, __func__, __VA_ARGS__); -#define LOG_PWARN(...) utils_plog(LOG_WARNING, __func__, __VA_ARGS__); -#define LOG_PERR(...) utils_plog(LOG_ERROR, __func__, __VA_ARGS__); -#define LOG_PFATAL(...) utils_plog(LOG_FATAL, __func__, __VA_ARGS__); +#ifdef UMF_DEVELOPER_MODE +#define UMF_STRINGIFY(x) #x +#define UMF_TOSTRING(x) UMF_STRINGIFY(x) +#define UMF_FILELINE_DESC() __FILE__ ":" UMF_TOSTRING(__LINE__) +#else +#define UMF_FILELINE_DESC() NULL +#endif + +#define LOG_DEBUG(...) \ + utils_log(LOG_DEBUG, UMF_FILELINE_DESC(), __func__, __VA_ARGS__); +#define LOG_INFO(...) \ + utils_log(LOG_INFO, UMF_FILELINE_DESC(), __func__, __VA_ARGS__); +#define LOG_WARN(...) \ + utils_log(LOG_WARNING, UMF_FILELINE_DESC(), __func__, __VA_ARGS__); +#define LOG_ERR(...) \ + utils_log(LOG_ERROR, UMF_FILELINE_DESC(), __func__, __VA_ARGS__); +#define LOG_FATAL(...) \ + utils_log(LOG_FATAL, UMF_FILELINE_DESC(), __func__, __VA_ARGS__); + +#define LOG_PDEBUG(...) \ + utils_plog(LOG_DEBUG, UMF_FILELINE_DESC(), __func__, __VA_ARGS__); +#define LOG_PINFO(...) \ + utils_plog(LOG_INFO, UMF_FILELINE_DESC(), __func__, __VA_ARGS__); +#define LOG_PWARN(...) \ + utils_plog(LOG_WARNING, UMF_FILELINE_DESC(), __func__, __VA_ARGS__); +#define LOG_PERR(...) \ + utils_plog(LOG_ERROR, UMF_FILELINE_DESC(), __func__, __VA_ARGS__); +#define LOG_PFATAL(...) \ + utils_plog(LOG_FATAL, UMF_FILELINE_DESC(), __func__, __VA_ARGS__); void utils_log_init(void); #ifdef _WIN32 -void utils_log(utils_log_level_t level, const char *func, const char *format, - ...); -void utils_plog(utils_log_level_t level, const char *func, const char *format, - ...); +void utils_log(utils_log_level_t level, const char *fileline, const char *func, + const char *format, ...); +void utils_plog(utils_log_level_t level, const char *fileline, const char *func, + const char *format, ...); #else -void utils_log(utils_log_level_t level, const char *func, const char *format, - ...) __attribute__((format(printf, 3, 4))); -void utils_plog(utils_log_level_t level, const char *func, const char *format, - ...) __attribute__((format(printf, 3, 4))); +void utils_log(utils_log_level_t level, const char *fileline, const char *func, + const char *format, ...) __attribute__((format(printf, 4, 5))); +void utils_plog(utils_log_level_t level, const char *fileline, const char *func, + const char *format, ...) __attribute__((format(printf, 4, 5))); #endif extern const umf_ctl_node_t CTL_NODE(logger)[]; diff --git a/test/utils/utils_log.cpp b/test/utils/utils_log.cpp index 16d0358615..3a3dd19fa7 100644 --- a/test/utils/utils_log.cpp +++ b/test/utils/utils_log.cpp @@ -29,6 +29,14 @@ FILE *expected_stream = stderr; int expect_fput_count = 0; int fput_count = 0; +// Some test of logging are disabled because log macros add filename and line +// number to logs in developer mode, therefore check on exact log content fail. +#ifdef UMF_DEVELOPER_MODE +#define DISABLE_IN_DEVELOPER_MODE(TESTNAME) DISABLED_##TESTNAME +#else +#define DISABLE_IN_DEVELOPER_MODE(TESTNAME) TESTNAME +#endif + int mock_fputs(const char *s, FILE *stream) { fput_count++; if (!expected_message.empty()) { @@ -142,7 +150,7 @@ void helper_checkConfig(utils_log_config_t *expected, utils_log_config_t *is) { EXPECT_EQ(expected->enablePid, is->enablePid); } -TEST_F(test, parseEnv_errors) { +TEST_F(test, DISABLE_IN_DEVELOPER_MODE(parseEnv_errors)) { expected_message = ""; loggerConfig = utils_log_config_t{false, false, LOG_ERROR, LOG_ERROR, NULL, ""}; @@ -168,7 +176,7 @@ TEST_F(test, parseEnv_errors) { helper_log_init(test_env.c_str()); } -TEST_F(test, parseEnv) { +TEST_F(test, DISABLE_IN_DEVELOPER_MODE(parseEnv)) { utils_log_config_t b = loggerConfig; expected_message = ""; @@ -261,10 +269,12 @@ TEST_F(test, parseEnv) { } } -template void helper_test_log(Args... args) { +template +void helper_test_log(utils_log_level_t level, const char *func, + const char *format, Args... args) { fput_count = 0; fflush_count = 0; - utils_log(args...); + utils_log(level, NULL, func, format, args...); EXPECT_EQ(fput_count, expect_fput_count); EXPECT_EQ(fflush_count, expect_fflush_count); } @@ -392,7 +402,7 @@ TEST_F(test, log_fatal) { helper_test_log(LOG_FATAL, MOCK_FN_NAME.c_str(), "%s", "example log"); } -TEST_F(test, log_macros) { +TEST_F(test, DISABLE_IN_DEVELOPER_MODE(log_macros)) { expected_stream = stderr; expect_fput_count = 1; expect_fflush_count = 1; @@ -435,10 +445,12 @@ TEST_F(test, log_macros) { EXPECT_EQ(fflush_count, expect_fflush_count); } -template void helper_test_plog(Args... args) { +template +void helper_test_plog(utils_log_level_t level, const char *func, + const char *format, Args... args) { fput_count = 0; fflush_count = 0; - utils_plog(args...); + utils_plog(level, NULL, func, format, args...); EXPECT_EQ(fput_count, expect_fput_count); EXPECT_EQ(fflush_count, expect_fflush_count); } @@ -523,7 +535,7 @@ TEST_F(test, plog_long_error) { strerr = NULL; // do not use tmp.c_str() beyond its scope } -TEST_F(test, log_pmacros) { +TEST_F(test, DISABLE_IN_DEVELOPER_MODE(log_pmacros)) { expected_stream = stderr; expect_fput_count = 1; expect_fflush_count = 1;