Skip to content

Commit cd152ea

Browse files
authored
Merge pull request #359 from lplewa/attribute
add printf format attribute to logger log function
2 parents 4cb4200 + 1009ff3 commit cd152ea

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
@@ -220,7 +220,7 @@ TEST_F(test, log_levels) {
220220
expect_fflush_count = 1;
221221
}
222222
expected_message = "[" + helper_log_str(j) + " UMF] example log\n";
223-
helper_test_log((util_log_level_t)j, "example log");
223+
helper_test_log((util_log_level_t)j, "%s", "example log");
224224
}
225225
}
226226
}
@@ -233,7 +233,7 @@ TEST_F(test, log_outputs) {
233233
for (auto o : outs) {
234234
loggerConfig = {0, 0, LOG_DEBUG, LOG_DEBUG, o};
235235
expected_stream = o;
236-
helper_test_log(LOG_DEBUG, "example log");
236+
helper_test_log(LOG_DEBUG, "%s", "example log");
237237
}
238238
}
239239

@@ -249,7 +249,7 @@ TEST_F(test, flush_levels) {
249249
expect_fflush_count = 1;
250250
}
251251
expected_message = "[" + helper_log_str(j) + " UMF] example log\n";
252-
helper_test_log((util_log_level_t)j, "example log");
252+
helper_test_log((util_log_level_t)j, "%s", "example log");
253253
}
254254
}
255255
}
@@ -259,10 +259,10 @@ TEST_F(test, long_log) {
259259
expect_fflush_count = 1;
260260
loggerConfig = {0, 0, LOG_DEBUG, LOG_DEBUG, stderr};
261261
expected_message = "[DEBUG UMF] " + std::string(8191, 'x') + "\n";
262-
helper_test_log(LOG_DEBUG, std::string(8191, 'x').c_str());
262+
helper_test_log(LOG_DEBUG, "%s", std::string(8191, 'x').c_str());
263263
expected_message =
264264
"[DEBUG UMF] " + std::string(8191, 'x') + "[truncated...]\n";
265-
helper_test_log(LOG_DEBUG, +std::string(8192, 'x').c_str());
265+
helper_test_log(LOG_DEBUG, "%s", std::string(8192, 'x').c_str());
266266
}
267267

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

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

288288
TEST_F(test, log_macros) {

0 commit comments

Comments
 (0)