From 55fcabe8d7cc7da04bffe8d4bb2fcc5dbe06d6b1 Mon Sep 17 00:00:00 2001 From: Test User Date: Wed, 24 Sep 2025 11:39:09 -0700 Subject: [PATCH 1/4] Add option to force debug output to log file --- include/wolfprovider/wp_logging.h | 3 +++ scripts/build-wolfprovider.sh | 10 ++++++++++ scripts/utils-openssl.sh | 2 ++ scripts/utils-wolfprovider.sh | 4 ++++ src/wp_logging.c | 12 ++++++++++++ 5 files changed, 31 insertions(+) diff --git a/include/wolfprovider/wp_logging.h b/include/wolfprovider/wp_logging.h index 4c7e09b8..782fa95d 100644 --- a/include/wolfprovider/wp_logging.h +++ b/include/wolfprovider/wp_logging.h @@ -78,6 +78,9 @@ * WOLFPROV_LOG_PRINTF Define to Use printf instead of fprintf (to stderr) * for logs. Not applicable if using WOLFPROV_USER_LOG * or custom logging callback. + * WOLFPROV_LOG_FILE Define to specify a file path for debug output instead + * of stderr. This is typically set via --debug-log=FILE + * build script argument. * * COMPILE-TIME MACRO CONFIGURATIONS: * Define these macros in this header to control logging at compile time: diff --git a/scripts/build-wolfprovider.sh b/scripts/build-wolfprovider.sh index d4696e98..b8547d49 100755 --- a/scripts/build-wolfprovider.sh +++ b/scripts/build-wolfprovider.sh @@ -10,6 +10,7 @@ show_help() { echo " --clean Run make clean in OpenSSL, wolfSSL, and wolfProvider" echo " --distclean Remove source and install directories of OpenSSL, wolfSSL, and wolfProvider" echo " --debug Builds OpenSSL, wolfSSL, and WolfProvider with debugging enabled. This is the same as setting WOLFPROV_DEBUG=1" + echo " --debug-log=FILE Force all wolfProvider debug output to specified log file instead of stderr/stdout (FILE = path to log file you want to use)" echo " --debug-asn-template Enable debug information for asn within wolfSSL" echo " --disable-err-trace No debug trace messages from library errors in wolfSSL" echo " --openssl-ver=VER Which version of OpenSSL to clone" @@ -33,6 +34,7 @@ show_help() { echo " WOLFPROV_CLEAN If set to 1, run make clean in OpenSSL, wolfSSL, and wolfProvider" echo " WOLFPROV_DISTCLEAN If set to 1, remove the source and install directories of OpenSSL, wolfSSL, and wolfProvider" echo " WOLFPROV_DEBUG If set to 1, builds OpenSSL, wolfSSL, and wolfProvider with debug options enabled" + echo " WOLFPROV_LOG_FILE Path to log file for wolfProvider debug output (alternative to stderr)" echo " WOLFPROV_QUICKTEST If set to 1, disables some tests in the test suite to increase test speed" echo " WOLFPROV_DISABLE_ERR_TRACE If set to 1, wolfSSL will not be configured with --enable-debug-trace-errcodes=backtrace" echo " WOLFPROV_LEAVE_SILENT If set to 1, suppress logging of return 0 in functions where return 0 is expected behavior sometimes." @@ -57,6 +59,14 @@ for arg in "$@"; do --debug) WOLFPROV_DEBUG=1 ;; + --debug-log=*) + IFS='=' read -r trash log_file <<< "$arg" + if [ -z "$log_file" ]; then + echo "No file path given for --debug-log" + args_wrong+="$arg, " + fi + WOLFPROV_LOG_FILE="$log_file" + ;; --debug-asn-template) WOLFSSL_DEBUG_ASN_TEMPLATE=1 ;; diff --git a/scripts/utils-openssl.sh b/scripts/utils-openssl.sh index 7f6507c8..e2e6f5bf 100755 --- a/scripts/utils-openssl.sh +++ b/scripts/utils-openssl.sh @@ -348,6 +348,8 @@ install_openssl() { } init_openssl() { + WOLFPROV_BUILD_DEBIAN=${WOLFPROV_BUILD_DEBIAN:-0} + if [ $WOLFPROV_BUILD_DEBIAN -eq 1 ]; then install_openssl_deb else diff --git a/scripts/utils-wolfprovider.sh b/scripts/utils-wolfprovider.sh index 4f46a2d9..9923a094 100644 --- a/scripts/utils-wolfprovider.sh +++ b/scripts/utils-wolfprovider.sh @@ -100,6 +100,10 @@ install_wolfprov() { WOLFPROV_CONFIG_CFLAGS="${WOLFPROV_CONFIG_CFLAGS} -DWOLFPROV_LEAVE_SILENT_MODE" fi + if [ -n "${WOLFPROV_LOG_FILE}" ]; then + WOLFPROV_CONFIG_CFLAGS="${WOLFPROV_CONFIG_CFLAGS} -DWOLFPROV_LOG_FILE=\\\"${WOLFPROV_LOG_FILE}\\\"" + fi + ./configure ${WOLFPROV_CONFIG_OPTS} CFLAGS="${WOLFPROV_CONFIG_CFLAGS}" >>$LOG_FILE 2>&1 RET=$? if [ $RET != 0 ]; then diff --git a/src/wp_logging.c b/src/wp_logging.c index d60d79ab..95179ee4 100644 --- a/src/wp_logging.c +++ b/src/wp_logging.c @@ -173,6 +173,18 @@ static void wolfprovider_log(const int logLevel, const int component, WOLFPROV_USER_LOG(logMessage); #elif defined(WOLFPROV_LOG_PRINTF) printf("%s\n", logMessage); +#elif defined(WOLFPROV_LOG_FILE) + { + FILE* logFile = fopen(WOLFPROV_LOG_FILE, "a"); + if (logFile != NULL) { + fprintf(logFile, "%s\n", logMessage); + fclose(logFile); + } else { + fprintf(stderr, "wolfProvider: Failed to open log file %s\n", + WOLFPROV_LOG_FILE); + fprintf(stderr, "%s\n", logMessage); + } + } #else fprintf(stderr, "%s\n", logMessage); #endif From 2a7251f7ebc98b8a906c8b7dfa7600d24a36a280 Mon Sep 17 00:00:00 2001 From: Paul Adelsbach Date: Fri, 3 Oct 2025 08:48:51 -0700 Subject: [PATCH 2/4] Add debug-log- updated --- include/wolfprovider/wp_logging.h | 7 ++++ src/wp_logging.c | 60 +++++++++++++++++++++++++++---- src/wp_wolfprov.c | 14 ++++++++ 3 files changed, 75 insertions(+), 6 deletions(-) diff --git a/include/wolfprovider/wp_logging.h b/include/wolfprovider/wp_logging.h index 782fa95d..152859ca 100644 --- a/include/wolfprovider/wp_logging.h +++ b/include/wolfprovider/wp_logging.h @@ -237,6 +237,13 @@ int wolfProv_SetLogLevel(int levelMask); /* Set which components are logged, bitmask of wolfProv_LogComponents */ int wolfProv_SetLogComponents(int componentMask); +#ifdef WOLFPROV_LOG_FILE +/* Initialize the logging system for file based logging */ +int wp_log_file_init(void); +/* Cleanup the logging system for file based logging */ +void wp_log_file_cleanup(void); +#endif /* WOLFPROV_LOG_FILE */ + #ifdef WOLFPROV_DEBUG #define WOLFPROV_STRINGIZE_HELPER(x) #x diff --git a/src/wp_logging.c b/src/wp_logging.c index 95179ee4..0b360557 100644 --- a/src/wp_logging.c +++ b/src/wp_logging.c @@ -49,8 +49,51 @@ static int providerLogLevel = WP_LOG_LEVEL_ALL; * in wolfProv_LogComponents. Default components include all. */ static int providerLogComponents = WP_LOG_COMPONENTS_ALL; +#ifdef WOLFPROV_LOG_FILE +/* Persistent file handle for logging to file */ +static FILE* logFileHandle = NULL; +/* Flag to track if we've already reported file open failure to avoid spam */ +static int logFileErrorReported = 0; +#endif + #endif /* WOLFPROV_DEBUG */ +#ifdef WOLFPROV_LOG_FILE +/** + * Initialize the persistent log file handle. + * Called once during provider initialization. + * + * @return 0 on success, negative value on failure. + */ +int wp_log_file_init(void) +{ + if (logFileHandle == NULL) { + fprintf(stderr, "wolfProvider: Opening log file %s\n", WOLFPROV_LOG_FILE); + logFileHandle = fopen(WOLFPROV_LOG_FILE, "a"); + fprintf(stderr, "wolfProvider: Log file handle: %p\n", logFileHandle); + if (logFileHandle == NULL) { + /* File open failed - will fall back to stderr on first log */ + fprintf(stderr, "wolfProvider: Failed to open log file %s\n", WOLFPROV_LOG_FILE); + return -1; + } + } + + return 0; +} + +/** + * Cleanup the persistent log file handle. + * Called during provider teardown. + */ +void wp_log_file_cleanup(void) +{ + if (logFileHandle != NULL) { + fprintf(stderr, "wolfProvider: Closing log file %s\n", WOLFPROV_LOG_FILE); + fclose(logFileHandle); + logFileHandle = NULL; + } +} +#endif /* WOLFPROV_LOG_FILE */ /** * Registers wolfProv logging callback. @@ -175,13 +218,18 @@ static void wolfprovider_log(const int logLevel, const int component, printf("%s\n", logMessage); #elif defined(WOLFPROV_LOG_FILE) { - FILE* logFile = fopen(WOLFPROV_LOG_FILE, "a"); - if (logFile != NULL) { - fprintf(logFile, "%s\n", logMessage); - fclose(logFile); + if (logFileHandle != NULL) { + fprintf(stderr, "wolfProvider: Logging to file %s\n", WOLFPROV_LOG_FILE); + fprintf(logFileHandle, "%s\n", logMessage); + fflush(logFileHandle); } else { - fprintf(stderr, "wolfProvider: Failed to open log file %s\n", - WOLFPROV_LOG_FILE); + /* Only report file error once to avoid spam */ + if (!logFileErrorReported) { + fprintf(stderr, "wolfProvider: Failed to open log file %s, " + "falling back to stderr\n", + WOLFPROV_LOG_FILE); + logFileErrorReported = 1; + } fprintf(stderr, "%s\n", logMessage); } } diff --git a/src/wp_wolfprov.c b/src/wp_wolfprov.c index 84e08db2..19ea2973 100644 --- a/src/wp_wolfprov.c +++ b/src/wp_wolfprov.c @@ -1178,6 +1178,10 @@ static const OSSL_ALGORITHM* wolfprov_query(void* provCtx, int id, */ static void wolfprov_teardown(void* provCtx) { +#ifdef WOLFPROV_LOG_FILE + /* Cleanup logging system */ + wp_log_file_cleanup(); +#endif wolfssl_prov_ctx_free(provCtx); } @@ -1250,6 +1254,16 @@ int wolfssl_provider_init(const OSSL_CORE_HANDLE* handle, wolfSSL_SetLoggingPrefix("wolfSSL"); } } + +#ifdef WOLFPROV_LOG_FILE + /* Initialize logging system for file based logging */ + if (ok) { + if (wp_log_file_init() != 0) { + /* Logging init failure is not fatal, but log it */ + fprintf(stderr, "wolfProvider: Warning - Failed to initialize file logging\n"); + } + } +#endif #endif #ifdef HAVE_FIPS From 980cc3fe4241a5b917a0568a6d0921f35d9f15cd Mon Sep 17 00:00:00 2001 From: Paul Adelsbach Date: Fri, 3 Oct 2025 10:48:36 -0700 Subject: [PATCH 3/4] Use file macros from wc_port.h, minor updates after testing --- include/wolfprovider/wp_logging.h | 2 -- scripts/build-wolfprovider.sh | 9 +++++-- src/wp_logging.c | 44 ++++++++++++++++++------------- src/wp_wolfprov.c | 15 ++--------- 4 files changed, 34 insertions(+), 36 deletions(-) diff --git a/include/wolfprovider/wp_logging.h b/include/wolfprovider/wp_logging.h index 152859ca..182e5bae 100644 --- a/include/wolfprovider/wp_logging.h +++ b/include/wolfprovider/wp_logging.h @@ -237,12 +237,10 @@ int wolfProv_SetLogLevel(int levelMask); /* Set which components are logged, bitmask of wolfProv_LogComponents */ int wolfProv_SetLogComponents(int componentMask); -#ifdef WOLFPROV_LOG_FILE /* Initialize the logging system for file based logging */ int wp_log_file_init(void); /* Cleanup the logging system for file based logging */ void wp_log_file_cleanup(void); -#endif /* WOLFPROV_LOG_FILE */ #ifdef WOLFPROV_DEBUG diff --git a/scripts/build-wolfprovider.sh b/scripts/build-wolfprovider.sh index b8547d49..4000371d 100755 --- a/scripts/build-wolfprovider.sh +++ b/scripts/build-wolfprovider.sh @@ -9,8 +9,8 @@ show_help() { echo " --help, -help, -h Display this help menu and exit" echo " --clean Run make clean in OpenSSL, wolfSSL, and wolfProvider" echo " --distclean Remove source and install directories of OpenSSL, wolfSSL, and wolfProvider" - echo " --debug Builds OpenSSL, wolfSSL, and WolfProvider with debugging enabled. This is the same as setting WOLFPROV_DEBUG=1" - echo " --debug-log=FILE Force all wolfProvider debug output to specified log file instead of stderr/stdout (FILE = path to log file you want to use)" + echo " --debug Builds OpenSSL, wolfSSL, and WolfProvider in debug mode with logging enabled. This is the same as setting WOLFPROV_DEBUG=1" + echo " --debug-log=FILE Force all wolfProvider runtime output to specified log file instead of stderr/stdout (FILE = path to log file you want to use). Logs are appended to existing file." echo " --debug-asn-template Enable debug information for asn within wolfSSL" echo " --disable-err-trace No debug trace messages from library errors in wolfSSL" echo " --openssl-ver=VER Which version of OpenSSL to clone" @@ -140,6 +140,11 @@ if [ "${WOLFPROV_LEAVE_SILENT}" = "1" ] && [ -z "$WOLFPROV_DEBUG" ] && [ -z "$de exit 1 fi +if [ -n "$WOLFPROV_LOG_FILE" ] && [ -z "$WOLFPROV_DEBUG" ]; then + echo "Error: --debug-log requires --debug to be set." + exit 1 +fi + if [ -n "$build_debian" ]; then echo "Building Debian package..." WOLFSSL_ISFIPS=${WOLFSSL_ISFIPS:-0} WOLFPROV_DEBUG=${WOLFPROV_DEBUG:-0} ./scripts/build-debian.sh diff --git a/src/wp_logging.c b/src/wp_logging.c index 0b360557..71ff25d1 100644 --- a/src/wp_logging.c +++ b/src/wp_logging.c @@ -51,33 +51,34 @@ static int providerLogComponents = WP_LOG_COMPONENTS_ALL; #ifdef WOLFPROV_LOG_FILE /* Persistent file handle for logging to file */ -static FILE* logFileHandle = NULL; -/* Flag to track if we've already reported file open failure to avoid spam */ -static int logFileErrorReported = 0; +static XFILE* logFileHandle = NULL; #endif #endif /* WOLFPROV_DEBUG */ -#ifdef WOLFPROV_LOG_FILE /** * Initialize the persistent log file handle. * Called once during provider initialization. * * @return 0 on success, negative value on failure. */ + int wp_log_file_init(void) { +#if defined(WOLFPROV_LOG_FILE) && defined(WOLFPROV_DEBUG) if (logFileHandle == NULL) { - fprintf(stderr, "wolfProvider: Opening log file %s\n", WOLFPROV_LOG_FILE); - logFileHandle = fopen(WOLFPROV_LOG_FILE, "a"); - fprintf(stderr, "wolfProvider: Log file handle: %p\n", logFileHandle); - if (logFileHandle == NULL) { + logFileHandle = XFOPEN(WOLFPROV_LOG_FILE, "a"); + if (logFileHandle) { + XFPRINTF(stderr, "wolfProvider: Using log file %s\n", WOLFPROV_LOG_FILE); + fflush(stderr); + } + else { /* File open failed - will fall back to stderr on first log */ - fprintf(stderr, "wolfProvider: Failed to open log file %s\n", WOLFPROV_LOG_FILE); + XFPRINTF(stderr, "wolfProvider: Failed to open log file %s\n", WOLFPROV_LOG_FILE); return -1; } } - +#endif /* WOLFPROV_LOG_FILE && WOLFPROV_DEBUG */ return 0; } @@ -87,13 +88,14 @@ int wp_log_file_init(void) */ void wp_log_file_cleanup(void) { +#if defined(WOLFPROV_LOG_FILE) && defined(WOLFPROV_DEBUG) if (logFileHandle != NULL) { - fprintf(stderr, "wolfProvider: Closing log file %s\n", WOLFPROV_LOG_FILE); - fclose(logFileHandle); + XFPRINTF(stderr, "wolfProvider: Closing log file %s\n", WOLFPROV_LOG_FILE); + XFCLOSE(logFileHandle); logFileHandle = NULL; } +#endif /* WOLFPROV_LOG_FILE && WOLFPROV_DEBUG */ } -#endif /* WOLFPROV_LOG_FILE */ /** * Registers wolfProv logging callback. @@ -218,23 +220,27 @@ static void wolfprovider_log(const int logLevel, const int component, printf("%s\n", logMessage); #elif defined(WOLFPROV_LOG_FILE) { + /* Flag to track if we've already reported file open failure to avoid spam */ + static int logFileErrorReported = 0; + if (logFileHandle != NULL) { - fprintf(stderr, "wolfProvider: Logging to file %s\n", WOLFPROV_LOG_FILE); - fprintf(logFileHandle, "%s\n", logMessage); - fflush(logFileHandle); + XFWRITE(logMessage, strlen(logMessage), 1, logFileHandle); + XFWRITE("\n", 1, 1, logFileHandle); + XFFLUSH(logFileHandle); } else { /* Only report file error once to avoid spam */ if (!logFileErrorReported) { - fprintf(stderr, "wolfProvider: Failed to open log file %s, " + XFPRINTF(stderr, "wolfProvider: Log file not open: %s, " "falling back to stderr\n", WOLFPROV_LOG_FILE); logFileErrorReported = 1; } - fprintf(stderr, "%s\n", logMessage); + XFWRITE(logMessage, strlen(logMessage), 1, stderr); + XFWRITE("\n", 1, 1, stderr); } } #else - fprintf(stderr, "%s\n", logMessage); + XFPRINTF(stderr, "%s\n", logMessage); #endif } } diff --git a/src/wp_wolfprov.c b/src/wp_wolfprov.c index 19ea2973..54c1fa51 100644 --- a/src/wp_wolfprov.c +++ b/src/wp_wolfprov.c @@ -1178,10 +1178,7 @@ static const OSSL_ALGORITHM* wolfprov_query(void* provCtx, int id, */ static void wolfprov_teardown(void* provCtx) { -#ifdef WOLFPROV_LOG_FILE - /* Cleanup logging system */ wp_log_file_cleanup(); -#endif wolfssl_prov_ctx_free(provCtx); } @@ -1239,6 +1236,8 @@ int wolfssl_provider_init(const OSSL_CORE_HANDLE* handle, { int ok = 1; + wp_log_file_init(); + WOLFPROV_ENTER(WP_LOG_PROVIDER, "wolfssl_provider_init"); #ifdef WOLFPROV_DEBUG @@ -1254,16 +1253,6 @@ int wolfssl_provider_init(const OSSL_CORE_HANDLE* handle, wolfSSL_SetLoggingPrefix("wolfSSL"); } } - -#ifdef WOLFPROV_LOG_FILE - /* Initialize logging system for file based logging */ - if (ok) { - if (wp_log_file_init() != 0) { - /* Logging init failure is not fatal, but log it */ - fprintf(stderr, "wolfProvider: Warning - Failed to initialize file logging\n"); - } - } -#endif #endif #ifdef HAVE_FIPS From 9ac1a632db123686900d283cfad62e7fdc0e0707 Mon Sep 17 00:00:00 2001 From: Paul Adelsbach Date: Fri, 3 Oct 2025 12:02:52 -0700 Subject: [PATCH 4/4] Optimization to reduce code --- include/wolfprovider/wp_logging.h | 9 +--- src/wp_logging.c | 81 +++++++++---------------------- src/wp_wolfprov.c | 3 -- 3 files changed, 24 insertions(+), 69 deletions(-) diff --git a/include/wolfprovider/wp_logging.h b/include/wolfprovider/wp_logging.h index 182e5bae..d9b0c818 100644 --- a/include/wolfprovider/wp_logging.h +++ b/include/wolfprovider/wp_logging.h @@ -169,7 +169,7 @@ enum wolfProv_LogComponents { WP_LOG_QUERY = 0x80000, /* wolfprov_query operations */ WP_LOG_TLS1_PRF = 0x100000, /* TLS1 PRF operations */ - /* log all compoenents */ + /* log all components */ WP_LOG_COMPONENTS_ALL = (WP_LOG_RNG | WP_LOG_DIGEST | WP_LOG_MAC @@ -200,7 +200,7 @@ enum wolfProv_LogComponents { | WP_LOG_QUERY | WP_LOG_TLS1_PRF), - /* default compoenents logged */ + /* default components logged */ WP_LOG_COMPONENTS_DEFAULT = WP_LOG_COMPONENTS_ALL }; @@ -237,11 +237,6 @@ int wolfProv_SetLogLevel(int levelMask); /* Set which components are logged, bitmask of wolfProv_LogComponents */ int wolfProv_SetLogComponents(int componentMask); -/* Initialize the logging system for file based logging */ -int wp_log_file_init(void); -/* Cleanup the logging system for file based logging */ -void wp_log_file_cleanup(void); - #ifdef WOLFPROV_DEBUG #define WOLFPROV_STRINGIZE_HELPER(x) #x diff --git a/src/wp_logging.c b/src/wp_logging.c index 71ff25d1..16d21190 100644 --- a/src/wp_logging.c +++ b/src/wp_logging.c @@ -49,54 +49,8 @@ static int providerLogLevel = WP_LOG_LEVEL_ALL; * in wolfProv_LogComponents. Default components include all. */ static int providerLogComponents = WP_LOG_COMPONENTS_ALL; -#ifdef WOLFPROV_LOG_FILE -/* Persistent file handle for logging to file */ -static XFILE* logFileHandle = NULL; -#endif - #endif /* WOLFPROV_DEBUG */ -/** - * Initialize the persistent log file handle. - * Called once during provider initialization. - * - * @return 0 on success, negative value on failure. - */ - -int wp_log_file_init(void) -{ -#if defined(WOLFPROV_LOG_FILE) && defined(WOLFPROV_DEBUG) - if (logFileHandle == NULL) { - logFileHandle = XFOPEN(WOLFPROV_LOG_FILE, "a"); - if (logFileHandle) { - XFPRINTF(stderr, "wolfProvider: Using log file %s\n", WOLFPROV_LOG_FILE); - fflush(stderr); - } - else { - /* File open failed - will fall back to stderr on first log */ - XFPRINTF(stderr, "wolfProvider: Failed to open log file %s\n", WOLFPROV_LOG_FILE); - return -1; - } - } -#endif /* WOLFPROV_LOG_FILE && WOLFPROV_DEBUG */ - return 0; -} - -/** - * Cleanup the persistent log file handle. - * Called during provider teardown. - */ -void wp_log_file_cleanup(void) -{ -#if defined(WOLFPROV_LOG_FILE) && defined(WOLFPROV_DEBUG) - if (logFileHandle != NULL) { - XFPRINTF(stderr, "wolfProvider: Closing log file %s\n", WOLFPROV_LOG_FILE); - XFCLOSE(logFileHandle); - logFileHandle = NULL; - } -#endif /* WOLFPROV_LOG_FILE && WOLFPROV_DEBUG */ -} - /** * Registers wolfProv logging callback. * Callback will be used by wolfProv for debug/log messages. @@ -220,24 +174,33 @@ static void wolfprovider_log(const int logLevel, const int component, printf("%s\n", logMessage); #elif defined(WOLFPROV_LOG_FILE) { + /* Persistent file handle for logging to file */ + static XFILE* logFileHandle = NULL; /* Flag to track if we've already reported file open failure to avoid spam */ static int logFileErrorReported = 0; - if (logFileHandle != NULL) { - XFWRITE(logMessage, strlen(logMessage), 1, logFileHandle); - XFWRITE("\n", 1, 1, logFileHandle); - XFFLUSH(logFileHandle); - } else { - /* Only report file error once to avoid spam */ - if (!logFileErrorReported) { - XFPRINTF(stderr, "wolfProvider: Log file not open: %s, " - "falling back to stderr\n", - WOLFPROV_LOG_FILE); - logFileErrorReported = 1; + if (logFileHandle == NULL) { + logFileHandle = XFOPEN(WOLFPROV_LOG_FILE, "a"); + if (logFileHandle) { + XFPRINTF(stderr, "wolfProvider: Using log file %s\n", WOLFPROV_LOG_FILE); + fflush(stderr); + } + else { + /* Fall back to stderr when file open fails */ + logFileHandle = stderr; + /* Only report file error once to avoid spam */ + if (!logFileErrorReported) { + logFileErrorReported = 1; + XFPRINTF(stderr, "wolfProvider: Log file not open: %s, " + "falling back to stderr\n", + WOLFPROV_LOG_FILE); + } } - XFWRITE(logMessage, strlen(logMessage), 1, stderr); - XFWRITE("\n", 1, 1, stderr); } + + XFWRITE(logMessage, strlen(logMessage), 1, logFileHandle); + XFWRITE("\n", 1, 1, logFileHandle); + XFFLUSH(logFileHandle); } #else XFPRINTF(stderr, "%s\n", logMessage); diff --git a/src/wp_wolfprov.c b/src/wp_wolfprov.c index 54c1fa51..84e08db2 100644 --- a/src/wp_wolfprov.c +++ b/src/wp_wolfprov.c @@ -1178,7 +1178,6 @@ static const OSSL_ALGORITHM* wolfprov_query(void* provCtx, int id, */ static void wolfprov_teardown(void* provCtx) { - wp_log_file_cleanup(); wolfssl_prov_ctx_free(provCtx); } @@ -1236,8 +1235,6 @@ int wolfssl_provider_init(const OSSL_CORE_HANDLE* handle, { int ok = 1; - wp_log_file_init(); - WOLFPROV_ENTER(WP_LOG_PROVIDER, "wolfssl_provider_init"); #ifdef WOLFPROV_DEBUG