Skip to content

Commit 44f1c63

Browse files
authored
add fileline to logs in developer mode (#1525)
1 parent 5ef0869 commit 44f1c63

File tree

4 files changed

+72
-37
lines changed

4 files changed

+72
-37
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ List of options provided by CMake:
128128
| UMF_BUILD_EXAMPLES | Build UMF examples | ON/OFF | ON |
129129
| UMF_BUILD_FUZZTESTS | Build UMF fuzz tests (supported only on Linux with Clang) | ON/OFF | OFF |
130130
| UMF_BUILD_GPU_EXAMPLES | Build UMF GPU examples | ON/OFF | OFF |
131-
| UMF_DEVELOPER_MODE | Enable additional developer checks | ON/OFF | OFF |
131+
| UMF_DEVELOPER_MODE | Enable additional developer checks and logs | ON/OFF | OFF |
132132
| UMF_FORMAT_CODE_STYLE | Add clang, cmake, and black -format-check and -format-apply targets to make | ON/OFF | OFF |
133133
| UMF_TESTS_FAIL_ON_SKIP | Treat skips in tests as fail | ON/OFF | OFF |
134134
| UMF_USE_ASAN | Enable AddressSanitizer checks | ON/OFF | OFF |

src/utils/utils_log.c

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ static const char *level_to_str(utils_log_level_t l) {
9999
#endif // _MSC_VER
100100

101101
static void utils_log_internal(utils_log_level_t level, int perror,
102-
const char *func, const char *format,
103-
va_list args) {
102+
const char *fileline, const char *func,
103+
const char *format, va_list args) {
104104
if (!loggerConfig.output && level != LOG_FATAL) {
105105
return; //logger not enabled
106106
}
@@ -115,7 +115,12 @@ static void utils_log_internal(utils_log_level_t level, int perror,
115115
char *b_pos = buffer;
116116
int b_size = sizeof(buffer);
117117

118-
int tmp = snprintf(b_pos, b_size, "%s: ", func);
118+
int tmp = 0;
119+
if (fileline == NULL) {
120+
tmp = snprintf(b_pos, b_size, "%s: ", func);
121+
} else {
122+
tmp = snprintf(b_pos, b_size, "%s %s: ", fileline, func);
123+
}
119124
ASSERT(tmp > 0);
120125

121126
b_pos += (int)tmp;
@@ -229,19 +234,19 @@ static void utils_log_internal(utils_log_level_t level, int perror,
229234
#pragma warning(pop)
230235
#endif // _MSC_VER
231236

232-
void utils_log(utils_log_level_t level, const char *func, const char *format,
233-
...) {
237+
void utils_log(utils_log_level_t level, const char *fileline, const char *func,
238+
const char *format, ...) {
234239
va_list args;
235240
va_start(args, format);
236-
utils_log_internal(level, 0, func, format, args);
241+
utils_log_internal(level, 0, fileline, func, format, args);
237242
va_end(args);
238243
}
239244

240-
void utils_plog(utils_log_level_t level, const char *func, const char *format,
241-
...) {
245+
void utils_plog(utils_log_level_t level, const char *fileline, const char *func,
246+
const char *format, ...) {
242247
va_list args;
243248
va_start(args, format);
244-
utils_log_internal(level, 1, func, format, args);
249+
utils_log_internal(level, 1, fileline, func, format, args);
245250
va_end(args);
246251
}
247252

src/utils/utils_log.h

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,47 @@ typedef enum {
2424
LOG_FATAL
2525
} utils_log_level_t;
2626

27-
#define LOG_DEBUG(...) utils_log(LOG_DEBUG, __func__, __VA_ARGS__);
28-
#define LOG_INFO(...) utils_log(LOG_INFO, __func__, __VA_ARGS__);
29-
#define LOG_WARN(...) utils_log(LOG_WARNING, __func__, __VA_ARGS__);
30-
#define LOG_ERR(...) utils_log(LOG_ERROR, __func__, __VA_ARGS__);
31-
#define LOG_FATAL(...) utils_log(LOG_FATAL, __func__, __VA_ARGS__);
32-
33-
#define LOG_PDEBUG(...) utils_plog(LOG_DEBUG, __func__, __VA_ARGS__);
34-
#define LOG_PINFO(...) utils_plog(LOG_INFO, __func__, __VA_ARGS__);
35-
#define LOG_PWARN(...) utils_plog(LOG_WARNING, __func__, __VA_ARGS__);
36-
#define LOG_PERR(...) utils_plog(LOG_ERROR, __func__, __VA_ARGS__);
37-
#define LOG_PFATAL(...) utils_plog(LOG_FATAL, __func__, __VA_ARGS__);
27+
#ifdef UMF_DEVELOPER_MODE
28+
#define UMF_STRINGIFY(x) #x
29+
#define UMF_TOSTRING(x) UMF_STRINGIFY(x)
30+
#define UMF_FILELINE_DESC() __FILE__ ":" UMF_TOSTRING(__LINE__)
31+
#else
32+
#define UMF_FILELINE_DESC() NULL
33+
#endif
34+
35+
#define LOG_DEBUG(...) \
36+
utils_log(LOG_DEBUG, UMF_FILELINE_DESC(), __func__, __VA_ARGS__);
37+
#define LOG_INFO(...) \
38+
utils_log(LOG_INFO, UMF_FILELINE_DESC(), __func__, __VA_ARGS__);
39+
#define LOG_WARN(...) \
40+
utils_log(LOG_WARNING, UMF_FILELINE_DESC(), __func__, __VA_ARGS__);
41+
#define LOG_ERR(...) \
42+
utils_log(LOG_ERROR, UMF_FILELINE_DESC(), __func__, __VA_ARGS__);
43+
#define LOG_FATAL(...) \
44+
utils_log(LOG_FATAL, UMF_FILELINE_DESC(), __func__, __VA_ARGS__);
45+
46+
#define LOG_PDEBUG(...) \
47+
utils_plog(LOG_DEBUG, UMF_FILELINE_DESC(), __func__, __VA_ARGS__);
48+
#define LOG_PINFO(...) \
49+
utils_plog(LOG_INFO, UMF_FILELINE_DESC(), __func__, __VA_ARGS__);
50+
#define LOG_PWARN(...) \
51+
utils_plog(LOG_WARNING, UMF_FILELINE_DESC(), __func__, __VA_ARGS__);
52+
#define LOG_PERR(...) \
53+
utils_plog(LOG_ERROR, UMF_FILELINE_DESC(), __func__, __VA_ARGS__);
54+
#define LOG_PFATAL(...) \
55+
utils_plog(LOG_FATAL, UMF_FILELINE_DESC(), __func__, __VA_ARGS__);
3856

3957
void utils_log_init(void);
4058
#ifdef _WIN32
41-
void utils_log(utils_log_level_t level, const char *func, const char *format,
42-
...);
43-
void utils_plog(utils_log_level_t level, const char *func, const char *format,
44-
...);
59+
void utils_log(utils_log_level_t level, const char *fileline, const char *func,
60+
const char *format, ...);
61+
void utils_plog(utils_log_level_t level, const char *fileline, const char *func,
62+
const char *format, ...);
4563
#else
46-
void utils_log(utils_log_level_t level, const char *func, const char *format,
47-
...) __attribute__((format(printf, 3, 4)));
48-
void utils_plog(utils_log_level_t level, const char *func, const char *format,
49-
...) __attribute__((format(printf, 3, 4)));
64+
void utils_log(utils_log_level_t level, const char *fileline, const char *func,
65+
const char *format, ...) __attribute__((format(printf, 4, 5)));
66+
void utils_plog(utils_log_level_t level, const char *fileline, const char *func,
67+
const char *format, ...) __attribute__((format(printf, 4, 5)));
5068
#endif
5169

5270
extern const umf_ctl_node_t CTL_NODE(logger)[];

test/utils/utils_log.cpp

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ FILE *expected_stream = stderr;
2929
int expect_fput_count = 0;
3030
int fput_count = 0;
3131

32+
// Some test of logging are disabled because log macros add filename and line
33+
// number to logs in developer mode, therefore check on exact log content fail.
34+
#ifdef UMF_DEVELOPER_MODE
35+
#define DISABLE_IN_DEVELOPER_MODE(TESTNAME) DISABLED_##TESTNAME
36+
#else
37+
#define DISABLE_IN_DEVELOPER_MODE(TESTNAME) TESTNAME
38+
#endif
39+
3240
int mock_fputs(const char *s, FILE *stream) {
3341
fput_count++;
3442
if (!expected_message.empty()) {
@@ -142,7 +150,7 @@ void helper_checkConfig(utils_log_config_t *expected, utils_log_config_t *is) {
142150
EXPECT_EQ(expected->enablePid, is->enablePid);
143151
}
144152

145-
TEST_F(test, parseEnv_errors) {
153+
TEST_F(test, DISABLE_IN_DEVELOPER_MODE(parseEnv_errors)) {
146154
expected_message = "";
147155
loggerConfig =
148156
utils_log_config_t{false, false, LOG_ERROR, LOG_ERROR, NULL, ""};
@@ -168,7 +176,7 @@ TEST_F(test, parseEnv_errors) {
168176
helper_log_init(test_env.c_str());
169177
}
170178

171-
TEST_F(test, parseEnv) {
179+
TEST_F(test, DISABLE_IN_DEVELOPER_MODE(parseEnv)) {
172180
utils_log_config_t b = loggerConfig;
173181
expected_message = "";
174182

@@ -261,10 +269,12 @@ TEST_F(test, parseEnv) {
261269
}
262270
}
263271

264-
template <typename... Args> void helper_test_log(Args... args) {
272+
template <typename... Args>
273+
void helper_test_log(utils_log_level_t level, const char *func,
274+
const char *format, Args... args) {
265275
fput_count = 0;
266276
fflush_count = 0;
267-
utils_log(args...);
277+
utils_log(level, NULL, func, format, args...);
268278
EXPECT_EQ(fput_count, expect_fput_count);
269279
EXPECT_EQ(fflush_count, expect_fflush_count);
270280
}
@@ -392,7 +402,7 @@ TEST_F(test, log_fatal) {
392402
helper_test_log(LOG_FATAL, MOCK_FN_NAME.c_str(), "%s", "example log");
393403
}
394404

395-
TEST_F(test, log_macros) {
405+
TEST_F(test, DISABLE_IN_DEVELOPER_MODE(log_macros)) {
396406
expected_stream = stderr;
397407
expect_fput_count = 1;
398408
expect_fflush_count = 1;
@@ -435,10 +445,12 @@ TEST_F(test, log_macros) {
435445
EXPECT_EQ(fflush_count, expect_fflush_count);
436446
}
437447

438-
template <typename... Args> void helper_test_plog(Args... args) {
448+
template <typename... Args>
449+
void helper_test_plog(utils_log_level_t level, const char *func,
450+
const char *format, Args... args) {
439451
fput_count = 0;
440452
fflush_count = 0;
441-
utils_plog(args...);
453+
utils_plog(level, NULL, func, format, args...);
442454
EXPECT_EQ(fput_count, expect_fput_count);
443455
EXPECT_EQ(fflush_count, expect_fflush_count);
444456
}
@@ -523,7 +535,7 @@ TEST_F(test, plog_long_error) {
523535
strerr = NULL; // do not use tmp.c_str() beyond its scope
524536
}
525537

526-
TEST_F(test, log_pmacros) {
538+
TEST_F(test, DISABLE_IN_DEVELOPER_MODE(log_pmacros)) {
527539
expected_stream = stderr;
528540
expect_fput_count = 1;
529541
expect_fflush_count = 1;

0 commit comments

Comments
 (0)