Skip to content

Commit 1009ff3

Browse files
committed
add printf format attribute to logger log function
1 parent cfd1a2a commit 1009ff3

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

src/utils/utils_log.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@ extern "C" {
1717
typedef enum { LOG_DEBUG, LOG_INFO, LOG_WARNING, LOG_ERROR } util_log_level_t;
1818

1919
void util_log_init(void);
20+
#ifdef _WIN32
2021
void util_log(util_log_level_t level, const char *format, ...);
21-
22+
#else
23+
void util_log(util_log_level_t level, const char *format, ...)
24+
__attribute__((format(printf, 2, 3)));
25+
#endif
2226
#define LOG_DEBUG(...) util_log(LOG_DEBUG, __VA_ARGS__);
2327
#define LOG_INFO(...) util_log(LOG_INFO, __VA_ARGS__);
2428
#define LOG_WARN(...) util_log(LOG_WARNING, __VA_ARGS__);

test/utils/utils_log.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ TEST_F(test, log_levels) {
217217
expect_fflush_count = 1;
218218
}
219219
expected_message = "[" + helper_log_str(j) + " UMF] example log\n";
220-
helper_test_log((util_log_level_t)j, "example log");
220+
helper_test_log((util_log_level_t)j, "%s", "example log");
221221
}
222222
}
223223
}
@@ -230,7 +230,7 @@ TEST_F(test, log_outputs) {
230230
for (auto o : outs) {
231231
loggerConfig = {0, 0, LOG_DEBUG, LOG_DEBUG, o};
232232
expected_stream = o;
233-
helper_test_log(LOG_DEBUG, "example log");
233+
helper_test_log(LOG_DEBUG, "%s", "example log");
234234
}
235235
}
236236

@@ -246,7 +246,7 @@ TEST_F(test, flush_levels) {
246246
expect_fflush_count = 1;
247247
}
248248
expected_message = "[" + helper_log_str(j) + " UMF] example log\n";
249-
helper_test_log((util_log_level_t)j, "example log");
249+
helper_test_log((util_log_level_t)j, "%s", "example log");
250250
}
251251
}
252252
}
@@ -256,10 +256,10 @@ TEST_F(test, long_log) {
256256
expect_fflush_count = 1;
257257
loggerConfig = {0, 0, LOG_DEBUG, LOG_DEBUG, stderr};
258258
expected_message = "[DEBUG UMF] " + std::string(8191, 'x') + "\n";
259-
helper_test_log(LOG_DEBUG, std::string(8191, 'x').c_str());
259+
helper_test_log(LOG_DEBUG, "%s", std::string(8191, 'x').c_str());
260260
expected_message =
261261
"[DEBUG UMF] " + std::string(8191, 'x') + "[truncated...]\n";
262-
helper_test_log(LOG_DEBUG, +std::string(8192, 'x').c_str());
262+
helper_test_log(LOG_DEBUG, "%s", std::string(8192, 'x').c_str());
263263
}
264264

265265
TEST_F(test, timestamp_log) {
@@ -269,7 +269,7 @@ TEST_F(test, timestamp_log) {
269269
// TODO: for now we do not check output message,
270270
// as it requires more sophisticated message validation (a.k.a regrex)
271271
expected_message = "";
272-
helper_test_log(LOG_DEBUG, "example log");
272+
helper_test_log(LOG_DEBUG, "%s", "example log");
273273
}
274274

275275
TEST_F(test, pid_log) {
@@ -279,7 +279,7 @@ TEST_F(test, pid_log) {
279279
// TODO: for now we do not check output message,
280280
// as it requires more sophisticated message validation (a.k.a regrex)
281281
expected_message = "";
282-
helper_test_log(LOG_DEBUG, "example log");
282+
helper_test_log(LOG_DEBUG, "%s", "example log");
283283
}
284284

285285
TEST_F(test, log_macros) {

0 commit comments

Comments
 (0)