diff --git a/include/wolfprovider/wp_logging.h b/include/wolfprovider/wp_logging.h index 4c7e09b8..d9b0c818 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: @@ -166,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 @@ -197,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 }; diff --git a/scripts/build-wolfprovider.sh b/scripts/build-wolfprovider.sh index d4696e98..4000371d 100755 --- a/scripts/build-wolfprovider.sh +++ b/scripts/build-wolfprovider.sh @@ -9,7 +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 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" @@ -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 ;; @@ -130,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/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..16d21190 100644 --- a/src/wp_logging.c +++ b/src/wp_logging.c @@ -51,7 +51,6 @@ static int providerLogComponents = WP_LOG_COMPONENTS_ALL; #endif /* WOLFPROV_DEBUG */ - /** * Registers wolfProv logging callback. * Callback will be used by wolfProv for debug/log messages. @@ -173,8 +172,38 @@ 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) + { + /* 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) { + 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, logFileHandle); + XFWRITE("\n", 1, 1, logFileHandle); + XFFLUSH(logFileHandle); + } #else - fprintf(stderr, "%s\n", logMessage); + XFPRINTF(stderr, "%s\n", logMessage); #endif } }