Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
23 changes: 14 additions & 9 deletions src/utils/utils_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down
56 changes: 37 additions & 19 deletions src/utils/utils_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)[];
Expand Down
28 changes: 20 additions & 8 deletions test/utils/utils_log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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, ""};
Expand All @@ -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 = "";

Expand Down Expand Up @@ -261,10 +269,12 @@ TEST_F(test, parseEnv) {
}
}

template <typename... Args> void helper_test_log(Args... args) {
template <typename... Args>
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);
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -435,10 +445,12 @@ TEST_F(test, log_macros) {
EXPECT_EQ(fflush_count, expect_fflush_count);
}

template <typename... Args> void helper_test_plog(Args... args) {
template <typename... Args>
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);
}
Expand Down Expand Up @@ -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;
Expand Down
Loading