Skip to content

Commit 1cbe1fe

Browse files
authored
Merge pull request wolfSSL#290 from aidangarske/debug-log-file
Add option to force debug output to log file
2 parents e5a9ae5 + 9ac1a63 commit 1cbe1fe

File tree

5 files changed

+58
-5
lines changed

5 files changed

+58
-5
lines changed

include/wolfprovider/wp_logging.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@
7878
* WOLFPROV_LOG_PRINTF Define to Use printf instead of fprintf (to stderr)
7979
* for logs. Not applicable if using WOLFPROV_USER_LOG
8080
* or custom logging callback.
81+
* WOLFPROV_LOG_FILE Define to specify a file path for debug output instead
82+
* of stderr. This is typically set via --debug-log=FILE
83+
* build script argument.
8184
*
8285
* COMPILE-TIME MACRO CONFIGURATIONS:
8386
* Define these macros in this header to control logging at compile time:
@@ -166,7 +169,7 @@ enum wolfProv_LogComponents {
166169
WP_LOG_QUERY = 0x80000, /* wolfprov_query operations */
167170
WP_LOG_TLS1_PRF = 0x100000, /* TLS1 PRF operations */
168171

169-
/* log all compoenents */
172+
/* log all components */
170173
WP_LOG_COMPONENTS_ALL = (WP_LOG_RNG
171174
| WP_LOG_DIGEST
172175
| WP_LOG_MAC
@@ -197,7 +200,7 @@ enum wolfProv_LogComponents {
197200
| WP_LOG_QUERY
198201
| WP_LOG_TLS1_PRF),
199202

200-
/* default compoenents logged */
203+
/* default components logged */
201204
WP_LOG_COMPONENTS_DEFAULT = WP_LOG_COMPONENTS_ALL
202205
};
203206

scripts/build-wolfprovider.sh

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ show_help() {
99
echo " --help, -help, -h Display this help menu and exit"
1010
echo " --clean Run make clean in OpenSSL, wolfSSL, and wolfProvider"
1111
echo " --distclean Remove source and install directories of OpenSSL, wolfSSL, and wolfProvider"
12-
echo " --debug Builds OpenSSL, wolfSSL, and WolfProvider with debugging enabled. This is the same as setting WOLFPROV_DEBUG=1"
12+
echo " --debug Builds OpenSSL, wolfSSL, and WolfProvider in debug mode with logging enabled. This is the same as setting WOLFPROV_DEBUG=1"
13+
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."
1314
echo " --debug-asn-template Enable debug information for asn within wolfSSL"
1415
echo " --disable-err-trace No debug trace messages from library errors in wolfSSL"
1516
echo " --openssl-ver=VER Which version of OpenSSL to clone"
@@ -33,6 +34,7 @@ show_help() {
3334
echo " WOLFPROV_CLEAN If set to 1, run make clean in OpenSSL, wolfSSL, and wolfProvider"
3435
echo " WOLFPROV_DISTCLEAN If set to 1, remove the source and install directories of OpenSSL, wolfSSL, and wolfProvider"
3536
echo " WOLFPROV_DEBUG If set to 1, builds OpenSSL, wolfSSL, and wolfProvider with debug options enabled"
37+
echo " WOLFPROV_LOG_FILE Path to log file for wolfProvider debug output (alternative to stderr)"
3638
echo " WOLFPROV_QUICKTEST If set to 1, disables some tests in the test suite to increase test speed"
3739
echo " WOLFPROV_DISABLE_ERR_TRACE If set to 1, wolfSSL will not be configured with --enable-debug-trace-errcodes=backtrace"
3840
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
5759
--debug)
5860
WOLFPROV_DEBUG=1
5961
;;
62+
--debug-log=*)
63+
IFS='=' read -r trash log_file <<< "$arg"
64+
if [ -z "$log_file" ]; then
65+
echo "No file path given for --debug-log"
66+
args_wrong+="$arg, "
67+
fi
68+
WOLFPROV_LOG_FILE="$log_file"
69+
;;
6070
--debug-asn-template)
6171
WOLFSSL_DEBUG_ASN_TEMPLATE=1
6272
;;
@@ -130,6 +140,11 @@ if [ "${WOLFPROV_LEAVE_SILENT}" = "1" ] && [ -z "$WOLFPROV_DEBUG" ] && [ -z "$de
130140
exit 1
131141
fi
132142

143+
if [ -n "$WOLFPROV_LOG_FILE" ] && [ -z "$WOLFPROV_DEBUG" ]; then
144+
echo "Error: --debug-log requires --debug to be set."
145+
exit 1
146+
fi
147+
133148
if [ -n "$build_debian" ]; then
134149
echo "Building Debian package..."
135150
WOLFSSL_ISFIPS=${WOLFSSL_ISFIPS:-0} WOLFPROV_DEBUG=${WOLFPROV_DEBUG:-0} ./scripts/build-debian.sh

scripts/utils-openssl.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,8 @@ install_openssl() {
348348
}
349349

350350
init_openssl() {
351+
WOLFPROV_BUILD_DEBIAN=${WOLFPROV_BUILD_DEBIAN:-0}
352+
351353
if [ $WOLFPROV_BUILD_DEBIAN -eq 1 ]; then
352354
install_openssl_deb
353355
else

scripts/utils-wolfprovider.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ install_wolfprov() {
100100
WOLFPROV_CONFIG_CFLAGS="${WOLFPROV_CONFIG_CFLAGS} -DWOLFPROV_LEAVE_SILENT_MODE"
101101
fi
102102

103+
if [ -n "${WOLFPROV_LOG_FILE}" ]; then
104+
WOLFPROV_CONFIG_CFLAGS="${WOLFPROV_CONFIG_CFLAGS} -DWOLFPROV_LOG_FILE=\\\"${WOLFPROV_LOG_FILE}\\\""
105+
fi
106+
103107
./configure ${WOLFPROV_CONFIG_OPTS} CFLAGS="${WOLFPROV_CONFIG_CFLAGS}" >>$LOG_FILE 2>&1
104108
RET=$?
105109
if [ $RET != 0 ]; then

src/wp_logging.c

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ static int providerLogComponents = WP_LOG_COMPONENTS_ALL;
5151

5252
#endif /* WOLFPROV_DEBUG */
5353

54-
5554
/**
5655
* Registers wolfProv logging callback.
5756
* Callback will be used by wolfProv for debug/log messages.
@@ -173,8 +172,38 @@ static void wolfprovider_log(const int logLevel, const int component,
173172
WOLFPROV_USER_LOG(logMessage);
174173
#elif defined(WOLFPROV_LOG_PRINTF)
175174
printf("%s\n", logMessage);
175+
#elif defined(WOLFPROV_LOG_FILE)
176+
{
177+
/* Persistent file handle for logging to file */
178+
static XFILE* logFileHandle = NULL;
179+
/* Flag to track if we've already reported file open failure to avoid spam */
180+
static int logFileErrorReported = 0;
181+
182+
if (logFileHandle == NULL) {
183+
logFileHandle = XFOPEN(WOLFPROV_LOG_FILE, "a");
184+
if (logFileHandle) {
185+
XFPRINTF(stderr, "wolfProvider: Using log file %s\n", WOLFPROV_LOG_FILE);
186+
fflush(stderr);
187+
}
188+
else {
189+
/* Fall back to stderr when file open fails */
190+
logFileHandle = stderr;
191+
/* Only report file error once to avoid spam */
192+
if (!logFileErrorReported) {
193+
logFileErrorReported = 1;
194+
XFPRINTF(stderr, "wolfProvider: Log file not open: %s, "
195+
"falling back to stderr\n",
196+
WOLFPROV_LOG_FILE);
197+
}
198+
}
199+
}
200+
201+
XFWRITE(logMessage, strlen(logMessage), 1, logFileHandle);
202+
XFWRITE("\n", 1, 1, logFileHandle);
203+
XFFLUSH(logFileHandle);
204+
}
176205
#else
177-
fprintf(stderr, "%s\n", logMessage);
206+
XFPRINTF(stderr, "%s\n", logMessage);
178207
#endif
179208
}
180209
}

0 commit comments

Comments
 (0)