Skip to content

Commit 3ee282d

Browse files
committed
initialize logger once (except tests)
1 parent 0f2870e commit 3ee282d

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

src/coarse/coarse.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -876,9 +876,7 @@ static umf_result_t coarse_get_stats_no_lock(coarse_t *coarse,
876876
// PUBLIC API
877877

878878
umf_result_t coarse_new(coarse_params_t *coarse_params, coarse_t **pcoarse) {
879-
#ifdef _WIN32
880-
utils_init_once(&Log_initialized, utils_log_init);
881-
#endif /* _WIN32 */
879+
utils_log_init();
882880

883881
if (coarse_params == NULL || pcoarse == NULL) {
884882
LOG_ERR("coarse parameters or handle is missing");

src/utils/utils_log.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
#include "utils_common.h"
3333
#include "utils_log.h"
3434

35+
#ifndef DISABLE_SINGLE_LOGGER
36+
#include "utils_concurrency.h"
37+
#endif
38+
3539
#define UMF_MAGIC_STR "\x00@(#) "
3640
#define UMF_PREF_STR "Intel(R) "
3741
#define UMF_PREFIX UMF_MAGIC_STR UMF_PREF_STR
@@ -60,6 +64,10 @@ char const __umf_str_1__all_cmake_vars[] =
6064
#define MAX_FILE_PATH 256
6165
#define MAX_ENV_LEN 2048
6266

67+
#ifndef DISABLE_SINGLE_LOGGER
68+
static UTIL_ONCE_FLAG initOnce = UTIL_ONCE_FLAG_INIT;
69+
#endif
70+
6371
typedef struct {
6472
bool enableTimestamp;
6573
bool enablePid;
@@ -247,7 +255,7 @@ void utils_plog(utils_log_level_t level, const char *func, const char *format,
247255

248256
static const char *bool_to_str(int b) { return b ? "yes" : "no"; }
249257

250-
void utils_log_init(void) {
258+
static void utils_log_init_once(void) {
251259
const char *envVar = getenv("UMF_LOG");
252260

253261
if (!envVar) {
@@ -343,6 +351,12 @@ void utils_log_init(void) {
343351
}
344352

345353
// this is needed for logger unit test
354+
#ifndef DISABLE_SINGLE_LOGGER
355+
void utils_log_init(void) { utils_init_once(&initOnce, utils_log_init_once); }
356+
#else
357+
void utils_log_init(void) { utils_log_init_once(); }
358+
#endif
359+
346360
#ifndef DISABLE_CTL_LOGGER
347361
static umf_result_t
348362
CTL_READ_HANDLER(timestamp)(void *ctx, umf_ctl_query_source_t source, void *arg,

test/utils/utils_log.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ int mock_strerror_windows(char *buff, size_t s, int errnum) {
9797

9898
extern "C" {
9999
#define DISABLE_CTL_LOGGER 1
100+
#define DISABLE_SINGLE_LOGGER 1
100101
const char *env_variable = "";
101102
#define fopen(A, B) mock_fopen(A, B)
102103
#define fputs(A, B) mock_fputs(A, B)
@@ -161,8 +162,8 @@ TEST_F(test, parseEnv_errors) {
161162
helper_checkConfig(&b, &loggerConfig);
162163
helper_log_init("_level:debug");
163164
helper_checkConfig(&b, &loggerConfig);
164-
expected_message =
165-
"[ERROR UMF] utils_log_init: Cannot open output file - path too long\n";
165+
expected_message = "[ERROR UMF] utils_log_init_once: Cannot open output "
166+
"file - path too long\n";
166167
std::string test_env = "output:file," + std::string(300, 'x');
167168
helper_log_init(test_env.c_str());
168169
}
@@ -247,7 +248,8 @@ TEST_F(test, parseEnv) {
247248
expect_fput_count = 1;
248249
if (expected_filename.size() > MAX_FILE_PATH) {
249250
expected_message =
250-
"[ERROR UMF] utils_log_init: Cannot open "
251+
"[ERROR UMF] utils_log_init_once: Cannot "
252+
"open "
251253
"output file - path too long\n";
252254
}
253255
}

0 commit comments

Comments
 (0)