Skip to content

Commit a5b20b8

Browse files
committed
control logger thru CTL
1 parent fe3aea6 commit a5b20b8

File tree

7 files changed

+371
-28
lines changed

7 files changed

+371
-28
lines changed

src/ctl/ctl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727

2828
#include "base_alloc/base_alloc_global.h"
2929
#include "ctl_internal.h"
30+
#include "uthash/utlist.h"
3031
#include "utils/utils_common.h"
3132
#include "utils_log.h"
32-
#include "utlist.h"
3333

3434
#ifdef _WIN32
3535
#define strtok_r strtok_s

src/libumf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ static UTIL_ONCE_FLAG initMutexOnce = UTIL_ONCE_FLAG_INIT;
3636
static void initialize_init_mutex(void) { utils_mutex_init(&initMutex); }
3737

3838
static umf_ctl_node_t CTL_NODE(umf)[] = {CTL_CHILD(provider), CTL_CHILD(pool),
39-
CTL_NODE_END};
39+
CTL_CHILD(logger), CTL_NODE_END};
4040

4141
void initialize_global_ctl(void) { CTL_REGISTER_MODULE(NULL, umf); }
4242

src/utils/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
include(${UMF_CMAKE_SOURCE_DIR}/cmake/helpers.cmake)
66
include(FindThreads)
77

8-
set(UMF_UTILS_SOURCES_COMMON utils_common.c utils_log.c utils_load_library.c)
8+
set(UMF_UTILS_SOURCES_COMMON utils_common.c utils_log.c utils_load_library.c ../ctl/ctl.c)
99
set(UMF_UTILS_SOURCES_POSIX utils_posix_common.c utils_posix_concurrency.c)
1010
set(UMF_UTILS_SOURCES_LINUX utils_linux_common.c)
1111
set(UMF_UTILS_SOURCES_MACOSX utils_macosx_common.c)
@@ -32,7 +32,7 @@ add_umf_library(
3232
NAME umf_utils
3333
TYPE STATIC
3434
SRCS ${UMF_UTILS_SOURCES}
35-
LIBS ${UMF_UTILS_LIBS} ${CMAKE_THREAD_LIBS_INIT})
35+
LIBS ${UMF_UTILS_LIBS} ${CMAKE_THREAD_LIBS_INIT} umf_ba)
3636

3737
if(NOT UMF_BUILD_SHARED_LIBRARY)
3838
install(TARGETS umf_utils EXPORT ${PROJECT_NAME}-targets)

src/utils/utils_log.c

Lines changed: 251 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#include <umf.h>
3030

31+
#include "ctl/ctl_internal.h"
3132
#include "utils_assert.h"
3233
#include "utils_common.h"
3334
#include "utils_log.h"
@@ -61,14 +62,15 @@ char const __umf_str_1__all_cmake_vars[] =
6162
#define MAX_ENV_LEN 2048
6263

6364
typedef struct {
64-
int timestamp;
65-
int pid;
65+
bool timestamp;
66+
bool pid;
6667
utils_log_level_t level;
6768
utils_log_level_t flushLevel;
6869
FILE *output;
70+
const char *file_name;
6971
} utils_log_config_t;
7072

71-
utils_log_config_t loggerConfig = {0, 0, LOG_ERROR, LOG_ERROR, NULL};
73+
utils_log_config_t loggerConfig = {0, 0, LOG_ERROR, LOG_ERROR, NULL, NULL};
7274

7375
static const char *level_to_str(utils_log_level_t l) {
7476
switch (l) {
@@ -254,8 +256,10 @@ void utils_log_init(void) {
254256
const char *arg;
255257
if (utils_parse_var(envVar, "output:stdout", NULL)) {
256258
loggerConfig.output = stdout;
259+
loggerConfig.file_name = "stdout";
257260
} else if (utils_parse_var(envVar, "output:stderr", NULL)) {
258261
loggerConfig.output = stderr;
262+
loggerConfig.file_name = "stderr";
259263
} else if (utils_parse_var(envVar, "output:file", &arg)) {
260264
loggerConfig.output = NULL;
261265
const char *argEnd = strstr(arg, ";");
@@ -284,6 +288,7 @@ void utils_log_init(void) {
284288
loggerConfig.output = NULL;
285289
return;
286290
}
291+
loggerConfig.file_name = file;
287292
} else {
288293
loggerConfig.output = stderr;
289294
LOG_ERR("Logging output not set - logging disabled (UMF_LOG = \"%s\")",
@@ -334,3 +339,246 @@ void utils_log_init(void) {
334339
level_to_str(loggerConfig.level), level_to_str(loggerConfig.flushLevel),
335340
bool_to_str(loggerConfig.pid), bool_to_str(loggerConfig.timestamp));
336341
}
342+
343+
static umf_result_t
344+
CTL_READ_HANDLER(timestamp)(void *ctx, umf_ctl_query_source_t source, void *arg,
345+
size_t size, umf_ctl_index_utlist_t *indexes) {
346+
/* suppress unused-parameter errors */
347+
(void)source, (void)indexes, (void)ctx;
348+
349+
bool *arg_out = (bool *)arg;
350+
351+
if (arg_out == NULL || size < sizeof(bool)) {
352+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
353+
}
354+
355+
*arg_out = loggerConfig.timestamp;
356+
return UMF_RESULT_SUCCESS;
357+
}
358+
359+
static umf_result_t
360+
CTL_WRITE_HANDLER(timestamp)(void *ctx, umf_ctl_query_source_t source,
361+
void *arg, size_t size,
362+
umf_ctl_index_utlist_t *indexes) {
363+
/* suppress unused-parameter errors */
364+
(void)source, (void)indexes, (void)ctx;
365+
366+
bool arg_in = *(bool *)arg;
367+
368+
if (size < sizeof(bool)) {
369+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
370+
}
371+
372+
loggerConfig.timestamp = arg_in;
373+
LOG_INFO("Logger print timestamp set to %s",
374+
bool_to_str(loggerConfig.timestamp));
375+
return UMF_RESULT_SUCCESS;
376+
}
377+
378+
static umf_result_t CTL_READ_HANDLER(pid)(void *ctx,
379+
umf_ctl_query_source_t source,
380+
void *arg, size_t size,
381+
umf_ctl_index_utlist_t *indexes) {
382+
/* suppress unused-parameter errors */
383+
(void)source, (void)indexes, (void)ctx;
384+
385+
bool *arg_out = (bool *)arg;
386+
387+
if (arg_out == NULL || size < sizeof(bool)) {
388+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
389+
}
390+
391+
*arg_out = loggerConfig.pid;
392+
return UMF_RESULT_SUCCESS;
393+
}
394+
395+
static umf_result_t CTL_WRITE_HANDLER(pid)(void *ctx,
396+
umf_ctl_query_source_t source,
397+
void *arg, size_t size,
398+
umf_ctl_index_utlist_t *indexes) {
399+
/* suppress unused-parameter errors */
400+
(void)source, (void)indexes, (void)ctx;
401+
402+
int arg_in = *(int *)arg;
403+
404+
if (arg_in < 0 || arg_in > 1 || size < sizeof(bool)) {
405+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
406+
}
407+
408+
loggerConfig.pid = arg_in;
409+
LOG_INFO("Logger print pid %s set to", bool_to_str(loggerConfig.pid));
410+
return UMF_RESULT_SUCCESS;
411+
}
412+
413+
static umf_result_t CTL_READ_HANDLER(level)(void *ctx,
414+
umf_ctl_query_source_t source,
415+
void *arg, size_t size,
416+
umf_ctl_index_utlist_t *indexes) {
417+
/* suppress unused-parameter errors */
418+
(void)source, (void)indexes, (void)ctx;
419+
420+
int *arg_out = (int *)arg;
421+
422+
if (arg_out == NULL || size < sizeof(utils_log_level_t)) {
423+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
424+
}
425+
426+
*arg_out = loggerConfig.level;
427+
return UMF_RESULT_SUCCESS;
428+
}
429+
430+
static umf_result_t CTL_WRITE_HANDLER(level)(void *ctx,
431+
umf_ctl_query_source_t source,
432+
void *arg, size_t size,
433+
umf_ctl_index_utlist_t *indexes) {
434+
/* suppress unused-parameter errors */
435+
(void)source, (void)indexes, (void)ctx;
436+
437+
utils_log_level_t *arg_in = (utils_log_level_t *)arg;
438+
439+
if (arg_in == NULL || *arg_in < LOG_DEBUG || *arg_in > LOG_FATAL ||
440+
size < sizeof(int)) {
441+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
442+
}
443+
444+
utils_log_level_t old = loggerConfig.level;
445+
446+
// if new log level is higher then LOG_INFO print log before changing log level
447+
// so if user changes from LOG_INFO to higher log level, it will get information about change anyway
448+
if (*arg_in > LOG_INFO) {
449+
LOG_INFO("Logger level changed from %s to %s", level_to_str(old),
450+
level_to_str(*arg_in));
451+
loggerConfig.level = *arg_in;
452+
} else {
453+
loggerConfig.level = *arg_in;
454+
LOG_INFO("Logger level changed from %s to %s", level_to_str(old),
455+
level_to_str(loggerConfig.level));
456+
}
457+
458+
return UMF_RESULT_SUCCESS;
459+
}
460+
461+
static umf_result_t
462+
CTL_READ_HANDLER(flush_level)(void *ctx, umf_ctl_query_source_t source,
463+
void *arg, size_t size,
464+
umf_ctl_index_utlist_t *indexes) {
465+
/* suppress unused-parameter errors */
466+
(void)source, (void)indexes, (void)ctx;
467+
468+
utils_log_level_t *arg_out = (utils_log_level_t *)arg;
469+
470+
if (arg_out == NULL || size < sizeof(utils_log_level_t)) {
471+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
472+
}
473+
474+
*arg_out = loggerConfig.flushLevel;
475+
return UMF_RESULT_SUCCESS;
476+
}
477+
478+
static umf_result_t
479+
CTL_WRITE_HANDLER(flush_level)(void *ctx, umf_ctl_query_source_t source,
480+
void *arg, size_t size,
481+
umf_ctl_index_utlist_t *indexes) {
482+
/* suppress unused-parameter errors */
483+
(void)source, (void)indexes, (void)ctx;
484+
485+
utils_log_level_t *arg_in = (utils_log_level_t *)arg;
486+
487+
if (arg_in == NULL || *arg_in < LOG_DEBUG || *arg_in > LOG_FATAL ||
488+
size < sizeof(int)) {
489+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
490+
}
491+
492+
loggerConfig.flushLevel = *arg_in;
493+
LOG_INFO("Logger flush level set to %s",
494+
level_to_str(loggerConfig.flushLevel));
495+
return UMF_RESULT_SUCCESS;
496+
}
497+
498+
static umf_result_t CTL_READ_HANDLER(output)(void *ctx,
499+
umf_ctl_query_source_t source,
500+
void *arg, size_t size,
501+
umf_ctl_index_utlist_t *indexes) {
502+
/* suppress unused-parameter errors */
503+
(void)source, (void)indexes, (void)ctx;
504+
505+
const char **arg_out = (const char **)arg;
506+
if (arg_out == NULL || size < sizeof(const char *)) {
507+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
508+
}
509+
510+
if (loggerConfig.output == NULL) {
511+
*arg_out = "logger_disabled";
512+
return UMF_RESULT_SUCCESS;
513+
}
514+
515+
*arg_out = loggerConfig.file_name;
516+
return UMF_RESULT_SUCCESS;
517+
}
518+
519+
static umf_result_t CTL_WRITE_HANDLER(output)(void *ctx,
520+
umf_ctl_query_source_t source,
521+
void *arg, size_t size,
522+
umf_ctl_index_utlist_t *indexes) {
523+
/* suppress unused-parameter errors */
524+
(void)source, (void)indexes, (void)ctx;
525+
526+
const char *arg_in = *(const char **)arg;
527+
if (size < sizeof(const char *)) {
528+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
529+
}
530+
531+
FILE *oldHandle = loggerConfig.output;
532+
const char *oldName =
533+
loggerConfig.file_name ? loggerConfig.file_name : "logger_disabled";
534+
535+
if (arg_in == NULL) {
536+
if (loggerConfig.output) {
537+
LOG_INFO("Logger disabled");
538+
if (oldHandle != stdout && oldHandle != stderr) {
539+
fclose(oldHandle);
540+
}
541+
loggerConfig.output = NULL;
542+
loggerConfig.file_name = NULL;
543+
}
544+
return UMF_RESULT_SUCCESS;
545+
}
546+
547+
FILE *newHandle = NULL;
548+
549+
if (strcmp(arg_in, "stdout") == 0) {
550+
newHandle = stdout;
551+
loggerConfig.file_name = "stdout";
552+
} else if (strcmp(arg_in, "stderr") == 0) {
553+
newHandle = stderr;
554+
loggerConfig.file_name = "stderr";
555+
} else {
556+
newHandle = fopen(arg_in, "a");
557+
if (!newHandle) {
558+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
559+
}
560+
loggerConfig.file_name = arg_in;
561+
}
562+
563+
loggerConfig.output = newHandle;
564+
LOG_INFO("Logger output changed from %s to %s", oldName,
565+
loggerConfig.file_name);
566+
567+
if (oldHandle && oldHandle != stdout && oldHandle != stderr) {
568+
fclose(oldHandle);
569+
}
570+
571+
return UMF_RESULT_SUCCESS;
572+
}
573+
574+
static const struct ctl_argument CTL_ARG(timestamp) = CTL_ARG_BOOLEAN;
575+
static const struct ctl_argument CTL_ARG(pid) = CTL_ARG_BOOLEAN;
576+
static const struct ctl_argument CTL_ARG(level) = CTL_ARG_INT;
577+
static const struct ctl_argument CTL_ARG(flush_level) = CTL_ARG_INT;
578+
static const struct ctl_argument
579+
CTL_ARG(output) = CTL_ARG_STRING(MAX_FILE_PATH);
580+
581+
const umf_ctl_node_t CTL_NODE(logger)[] = {
582+
CTL_LEAF_RW(timestamp), CTL_LEAF_RW(pid), CTL_LEAF_RW(level),
583+
CTL_LEAF_RW(flush_level), CTL_LEAF_RW(output), CTL_NODE_END,
584+
};

src/utils/utils_log.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
*
3-
* Copyright (C) 2024 Intel Corporation
3+
* Copyright (C) 2024-2025 Intel Corporation
44
*
55
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
66
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
@@ -14,6 +14,8 @@
1414
extern "C" {
1515
#endif
1616

17+
#include <ctl/ctl_internal.h>
18+
1719
typedef enum {
1820
LOG_DEBUG,
1921
LOG_INFO,
@@ -47,6 +49,8 @@ void utils_plog(utils_log_level_t level, const char *func, const char *format,
4749
...) __attribute__((format(printf, 3, 4)));
4850
#endif
4951

52+
extern const umf_ctl_node_t CTL_NODE(logger)[];
53+
5054
#ifdef __cplusplus
5155
}
5256
#endif

test/CMakeLists.txt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,12 @@ add_umf_test(
195195
SRCS memoryProviderAPI.cpp
196196
LIBS ${UMF_UTILS_FOR_TEST} ${UMF_BA_FOR_TEST})
197197

198-
add_umf_test(
199-
NAME logger
200-
SRCS utils/utils_log.cpp ${UMF_UTILS_SOURCES}
201-
LIBS ${UMF_LOGGER_LIBS})
198+
if(NOT UMF_BUILD_SHARED_LIBRARY)
199+
add_umf_test(
200+
NAME logger
201+
SRCS utils/utils_log.cpp ${UMF_UTILS_SOURCES} ../src/ctl/ctl.c
202+
LIBS ${UMF_LOGGER_LIBS} ${UMF_BA_FOR_TEST})
203+
endif()
202204

203205
add_umf_test(
204206
NAME ctl_unittest

0 commit comments

Comments
 (0)