Skip to content

Commit 84d93d8

Browse files
committed
fix logger when enabled thru ctl
1 parent 7cc203e commit 84d93d8

File tree

5 files changed

+84
-31
lines changed

5 files changed

+84
-31
lines changed

src/ctl/ctl.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,13 @@ static void *ctl_parse_args(const struct ctl_argument *arg_proto, char *arg) {
198198
* structure as required by the node callback
199199
*/
200200
static void *ctl_query_get_real_args(const umf_ctl_node_t *n, void *write_arg,
201-
umf_ctl_query_source_t source) {
201+
umf_ctl_query_source_t source,
202+
size_t *size) {
202203
void *real_arg = NULL;
203204
switch (source) {
204205
case CTL_QUERY_CONFIG_INPUT:
205206
real_arg = ctl_parse_args(n->arg, write_arg);
207+
*size = n->arg->dest_size;
206208
break;
207209
case CTL_QUERY_PROGRAMMATIC:
208210
real_arg = write_arg;
@@ -251,7 +253,7 @@ static umf_result_t ctl_exec_query_write(void *ctx, const umf_ctl_node_t *n,
251253
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
252254
}
253255

254-
void *real_arg = ctl_query_get_real_args(n, arg, source);
256+
void *real_arg = ctl_query_get_real_args(n, arg, source, &size);
255257
if (real_arg == NULL) {
256258
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
257259
}

src/utils/utils_log.c

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
*
88
*/
99

10+
#include "base_alloc_global.h"
11+
#include "umf/base.h"
1012
#ifdef _WIN32
1113
#include <windows.h>
1214
#else
@@ -66,11 +68,11 @@ typedef struct {
6668
utils_log_level_t level;
6769
utils_log_level_t flushLevel;
6870
FILE *output;
69-
const char *file_name;
71+
char file_name[MAX_FILE_PATH];
7072
} utils_log_config_t;
7173

7274
utils_log_config_t loggerConfig = {false, false, LOG_ERROR,
73-
LOG_ERROR, NULL, NULL};
75+
LOG_ERROR, NULL, ""};
7476

7577
static const char *level_to_str(utils_log_level_t l) {
7678
switch (l) {
@@ -257,10 +259,10 @@ void utils_log_init(void) {
257259
const char *arg;
258260
if (utils_parse_var(envVar, "output:stdout", NULL)) {
259261
loggerConfig.output = stdout;
260-
loggerConfig.file_name = "stdout";
262+
strncpy(loggerConfig.file_name, "stdout", MAX_FILE_PATH);
261263
} else if (utils_parse_var(envVar, "output:stderr", NULL)) {
262264
loggerConfig.output = stderr;
263-
loggerConfig.file_name = "stderr";
265+
strncpy(loggerConfig.file_name, "stderr", MAX_FILE_PATH);
264266
} else if (utils_parse_var(envVar, "output:file", &arg)) {
265267
loggerConfig.output = NULL;
266268
const char *argEnd = strstr(arg, ";");
@@ -289,7 +291,9 @@ void utils_log_init(void) {
289291
loggerConfig.output = NULL;
290292
return;
291293
}
292-
loggerConfig.file_name = file;
294+
strncpy(loggerConfig.file_name, file, MAX_FILE_PATH - 1);
295+
loggerConfig.file_name[MAX_FILE_PATH - 1] =
296+
'\0'; // ensure null-termination
293297
} else {
294298
loggerConfig.output = stderr;
295299
LOG_ERR("Logging output not set - logging disabled (UMF_LOG = \"%s\")",
@@ -506,17 +510,29 @@ static umf_result_t CTL_READ_HANDLER(output)(void *ctx,
506510
/* suppress unused-parameter errors */
507511
(void)source, (void)indexes, (void)ctx;
508512

509-
const char **arg_out = (const char **)arg;
510-
if (arg_out == NULL || size < sizeof(const char *)) {
513+
char *arg_out = (char *)arg;
514+
if (arg_out == NULL) {
511515
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
512516
}
513517

514518
if (loggerConfig.output == NULL) {
515-
*arg_out = "disabled";
519+
const char disabled[] = "disabled";
520+
if (size < sizeof(disabled)) {
521+
LOG_ERR("Invalid output argument size: %zu, expected at least %zu",
522+
size, sizeof(disabled));
523+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
524+
}
525+
526+
strncpy(arg_out, disabled, size);
516527
return UMF_RESULT_SUCCESS;
517528
}
529+
if (size < strlen(loggerConfig.file_name)) {
530+
LOG_ERR("Invalid output argument size: %zu, expected at least %zu",
531+
size, strlen(loggerConfig.file_name));
532+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
533+
}
518534

519-
*arg_out = loggerConfig.file_name;
535+
strncpy(arg_out, loggerConfig.file_name, size);
520536
return UMF_RESULT_SUCCESS;
521537
}
522538

@@ -525,16 +541,13 @@ static umf_result_t CTL_WRITE_HANDLER(output)(void *ctx,
525541
void *arg, size_t size,
526542
umf_ctl_index_utlist_t *indexes) {
527543
/* suppress unused-parameter errors */
528-
(void)source, (void)indexes, (void)ctx;
544+
(void)source, (void)indexes, (void)ctx, (void)size;
529545

530-
const char *arg_in = *(const char **)arg;
531-
if (size < sizeof(const char *)) {
532-
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
533-
}
546+
const char *arg_in = (const char *)arg;
534547

535548
FILE *oldHandle = loggerConfig.output;
536549
const char *oldName =
537-
loggerConfig.file_name ? loggerConfig.file_name : "disabled";
550+
*loggerConfig.file_name == '\0' ? loggerConfig.file_name : "disabled";
538551

539552
if (arg_in == NULL) {
540553
if (loggerConfig.output) {
@@ -543,7 +556,7 @@ static umf_result_t CTL_WRITE_HANDLER(output)(void *ctx,
543556
fclose(oldHandle);
544557
}
545558
loggerConfig.output = NULL;
546-
loggerConfig.file_name = NULL;
559+
loggerConfig.file_name[0] = '\0';
547560
}
548561
return UMF_RESULT_SUCCESS;
549562
}
@@ -552,16 +565,18 @@ static umf_result_t CTL_WRITE_HANDLER(output)(void *ctx,
552565

553566
if (strcmp(arg_in, "stdout") == 0) {
554567
newHandle = stdout;
555-
loggerConfig.file_name = "stdout";
568+
strncpy(loggerConfig.file_name, "stdout", MAX_FILE_PATH);
556569
} else if (strcmp(arg_in, "stderr") == 0) {
557570
newHandle = stderr;
558-
loggerConfig.file_name = "stderr";
571+
strncpy(loggerConfig.file_name, "stderr", MAX_FILE_PATH);
559572
} else {
560573
newHandle = fopen(arg_in, "a");
561574
if (!newHandle) {
562575
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
563576
}
564-
loggerConfig.file_name = arg_in;
577+
strncpy(loggerConfig.file_name, arg_in, MAX_FILE_PATH - 1);
578+
loggerConfig.file_name[MAX_FILE_PATH - 1] =
579+
'\0'; // ensure null-termination
565580
}
566581

567582
loggerConfig.output = newHandle;

test/ctl/ctl_api.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -414,21 +414,23 @@ TEST_F(test, ctl_logger_basic_rw) {
414414
UMF_RESULT_SUCCESS);
415415
EXPECT_EQ(flush_get, 2);
416416

417-
const char *out_name = "stdout";
418-
ASSERT_EQ(umfCtlSet("umf.logger.output", &out_name, sizeof(out_name)),
419-
UMF_RESULT_SUCCESS);
420-
const char *out_get = NULL;
421-
ASSERT_EQ(umfCtlGet("umf.logger.output", &out_get, sizeof(out_get)),
417+
const char out_name[] = "stdout";
418+
ASSERT_EQ(
419+
umfCtlSet("umf.logger.output", (void *)out_name, sizeof(out_name)),
420+
UMF_RESULT_SUCCESS);
421+
const char out_get[256] = "";
422+
ASSERT_EQ(umfCtlGet("umf.logger.output", (void *)out_get, sizeof(out_get)),
422423
UMF_RESULT_SUCCESS);
423424
EXPECT_STREQ(out_get, "stdout");
424425
}
425426

426427
TEST_F(test, ctl_logger_output_file) {
427-
const char *file_name = "ctl_log.txt";
428-
ASSERT_EQ(umfCtlSet("umf.logger.output", &file_name, sizeof(file_name)),
429-
UMF_RESULT_SUCCESS);
430-
const char *out_get = NULL;
431-
ASSERT_EQ(umfCtlGet("umf.logger.output", &out_get, sizeof(out_get)),
428+
const char file_name[] = "ctl_log.txt";
429+
ASSERT_EQ(
430+
umfCtlSet("umf.logger.output", (void *)file_name, sizeof(file_name)),
431+
UMF_RESULT_SUCCESS);
432+
const char out_get[256] = "";
433+
ASSERT_EQ(umfCtlGet("umf.logger.output", (void *)out_get, sizeof(out_get)),
432434
UMF_RESULT_SUCCESS);
433435
EXPECT_STREQ(out_get, file_name);
434436
}

test/ctl/ctl_env_app.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,31 @@ static int test_env_defaults(int argc, char **argv) {
4141
return 0;
4242
}
4343

44+
static int test_logger(int argc, char **argv) {
45+
char buf[256] = {0};
46+
47+
if (argc != 2) {
48+
std::cerr << "expected even number of arguments" << std::endl;
49+
std::cerr << "Usage: env_defaults log_output log_level..." << std::endl;
50+
return 1;
51+
}
52+
umfCtlGet("umf.logger.output", buf, sizeof(buf));
53+
if (strcmp(buf, argv[0]) != 0) {
54+
std::cerr << "Expected log_output to be '" << argv[0] << "', but got '"
55+
<< buf << "'" << std::endl;
56+
return 1;
57+
}
58+
int level = 0;
59+
umfCtlGet("umf.logger.level", &level, sizeof(level));
60+
if (level != atoi(argv[1])) {
61+
std::cerr << "Expected log_level to be '" << argv[1] << "', but got '"
62+
<< level << "'" << std::endl;
63+
return 1;
64+
}
65+
66+
return 0;
67+
}
68+
4469
int main(int argc, char **argv) {
4570
if (argc < 2) {
4671
std::cerr << "Usage: " << argv[0] << " <test_name> args..."
@@ -53,5 +78,9 @@ int main(int argc, char **argv) {
5378
if (strcmp(test_name, "env_defaults") == 0) {
5479
return test_env_defaults(argc, argv);
5580
}
81+
82+
if (strcmp(test_name, "logger") == 0) {
83+
return test_logger(argc, argv);
84+
}
5685
return 1;
5786
}

test/ctl/ctl_env_driver.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,8 @@ TEST_F(test, ctl_env_plus_file) {
121121
"opt_two_value2", "umf.pool.default.test_pool.opt_three",
122122
"second"});
123123
}
124+
125+
TEST_F(test, ctl_env_logger) {
126+
run_case({{"UMF_CONF", "umf.logger.output=stdout;umf.logger.level=0"}},
127+
{"logger", "stdout", "0"});
128+
}

0 commit comments

Comments
 (0)