diff --git a/src/gurt/debug.c b/src/gurt/debug.c index 50f765d35bb..546fd522a2a 100644 --- a/src/gurt/debug.c +++ b/src/gurt/debug.c @@ -1,6 +1,6 @@ /* * (C) Copyright 2016-2022 Intel Corporation. - * (C) Copyright 2025 Hewlett Packard Enterprise Development LP + * (C) Copyright 2025-2026 Hewlett Packard Enterprise Development LP * * SPDX-License-Identifier: BSD-2-Clause-Patent */ @@ -385,6 +385,18 @@ debug_prio_err_load_env(void) if (env == NULL) return; + /* ERROR can be used as an alias for ERR */ + if (strncasecmp(env, "ERROR", sizeof("ERROR")) == 0) { + d_dbglog_data.dd_prio_err = DLOG_ERR; + goto out_env; + } + /* DBUG can be used as an alias for DEBUG */ + if (strncasecmp(env, "DBUG", sizeof("DBUG")) == 0) { + d_dbglog_data.dd_prio_err = DLOG_DBG; + goto out_env; + } + + d_dbglog_data.dd_prio_err = 0; for (i = 0; i < NUM_DBG_PRIO_ENTRIES; i++) { if (d_dbg_prio_dict[i].dd_name != NULL && strncasecmp(env, d_dbg_prio_dict[i].dd_name, @@ -396,6 +408,8 @@ debug_prio_err_load_env(void) /* invalid DD_STDERR option */ if (d_dbglog_data.dd_prio_err == 0) D_PRINT_ERR("DD_STDERR = %s - invalid option\n", env); + +out_env: d_freeenv_str(&env); } diff --git a/src/gurt/tests/test_gurt.c b/src/gurt/tests/test_gurt.c index 762a3a55239..e94a65beb56 100644 --- a/src/gurt/tests/test_gurt.c +++ b/src/gurt/tests/test_gurt.c @@ -746,6 +746,89 @@ test_log(void **state) d_log_fini(); } +static void +test_dd_stderr(void **state) +{ + int rc; + int i; + + struct { + const char *env_val; + d_dbug_t expected; + } test_cases[] = { + /* Standard dictionary values */ + {"emit", DLOG_EMIT}, + {"fatal", DLOG_EMERG}, + {"alert", DLOG_ALERT}, + {"crit", DLOG_CRIT}, + {"err", DLOG_ERR}, + {"error", DLOG_ERR}, + {"warn", DLOG_WARN}, + {"note", DLOG_NOTE}, + {"info", DLOG_INFO}, + {"debug", DLOG_DBG}, + {"dbug", DLOG_DBG}, + /* Case insensitivity */ + {"ERR", DLOG_ERR}, + {"Warn", DLOG_WARN}, + {"CRIT", DLOG_CRIT}, + {"Info", DLOG_INFO}, + {"eRRor", DLOG_ERR}, + {"DBUG", DLOG_DBG}, + }; + + /* Tear down the log initialized by init_tests */ + d_log_fini(); + + for (i = 0; i < ARRAY_SIZE(test_cases); i++) { + setenv("DD_STDERR", test_cases[i].env_val, 1); + rc = d_log_init(); + assert_int_equal(rc, 0); + assert_int_equal(d_dbglog_data.dd_prio_err, test_cases[i].expected); + d_log_fini(); + } + + /* Test invalid value: dd_prio_err should be 0 */ + setenv("DD_STDERR", "INVALID", 1); + rc = d_log_init(); + assert_int_equal(rc, 0); + assert_int_equal(d_dbglog_data.dd_prio_err, 0); + d_log_fini(); + + /* Test invalid value with valid prefix: dd_prio_err should be 0 */ + setenv("DD_STDERR", "DEBUG_FOO", 1); + rc = d_log_init(); + assert_int_equal(rc, 0); + assert_int_equal(d_dbglog_data.dd_prio_err, 0); + d_log_fini(); + + /* Test invalid value with incomplete name: dd_prio_err should be 0 */ + setenv("DD_STDERR", "DEB", 1); + rc = d_log_init(); + assert_int_equal(rc, 0); + assert_int_equal(d_dbglog_data.dd_prio_err, 0); + d_log_fini(); + + /* Test invalid value with incomplete name: dd_prio_err should be 0 */ + setenv("DD_STDERR", "ERRO", 1); /* codespell:ignore */ + rc = d_log_init(); + assert_int_equal(rc, 0); + assert_int_equal(d_dbglog_data.dd_prio_err, 0); + d_log_fini(); + + /* Test unset: dd_prio_err should remain 0 */ + unsetenv("DD_STDERR"); + rc = d_log_init(); + assert_int_equal(rc, 0); + assert_int_equal(d_dbglog_data.dd_prio_err, 0); + d_log_fini(); + + /* Re-initialize for the group teardown (fini_tests) */ + unsetenv("DD_STDERR"); + rc = d_log_init(); + assert_int_equal(rc, 0); +} + #define TEST_GURT_HASH_NUM_BITS (D_ON_VALGRIND ? 4 : 12) #define TEST_GURT_HASH_NUM_ENTRIES (1 << TEST_GURT_HASH_NUM_BITS) #define TEST_GURT_HASH_NUM_THREADS (D_ON_VALGRIND ? 4 : 16) @@ -2740,6 +2823,7 @@ main(int argc, char **argv) cmocka_unit_test(test_gurt_hlist), cmocka_unit_test(test_binheap), cmocka_unit_test(test_log), + cmocka_unit_test(test_dd_stderr), cmocka_unit_test(test_gurt_hash_empty), cmocka_unit_test(test_gurt_hash_insert_lookup_delete), cmocka_unit_test(test_gurt_hash_decref), @@ -2772,5 +2856,12 @@ main(int argc, char **argv) d_register_alt_assert(mock_assert); +#if CMOCKA_FILTER_SUPPORTED == 1 /** requires cmocka 1.1.5 */ + if (argc > 1) + cmocka_set_test_filter(argv[1]); +#else + printf("Test filtering disabled at compile time.\n"); +#endif + return cmocka_run_group_tests_name("test_gurt", tests, init_tests, fini_tests); }