Skip to content

Commit 980cc3f

Browse files
committed
Use file macros from wc_port.h, minor updates after testing
1 parent 2a7251f commit 980cc3f

File tree

4 files changed

+34
-36
lines changed

4 files changed

+34
-36
lines changed

include/wolfprovider/wp_logging.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,12 +237,10 @@ int wolfProv_SetLogLevel(int levelMask);
237237
/* Set which components are logged, bitmask of wolfProv_LogComponents */
238238
int wolfProv_SetLogComponents(int componentMask);
239239

240-
#ifdef WOLFPROV_LOG_FILE
241240
/* Initialize the logging system for file based logging */
242241
int wp_log_file_init(void);
243242
/* Cleanup the logging system for file based logging */
244243
void wp_log_file_cleanup(void);
245-
#endif /* WOLFPROV_LOG_FILE */
246244

247245
#ifdef WOLFPROV_DEBUG
248246

scripts/build-wolfprovider.sh

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +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"
13-
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)"
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."
1414
echo " --debug-asn-template Enable debug information for asn within wolfSSL"
1515
echo " --disable-err-trace No debug trace messages from library errors in wolfSSL"
1616
echo " --openssl-ver=VER Which version of OpenSSL to clone"
@@ -140,6 +140,11 @@ if [ "${WOLFPROV_LEAVE_SILENT}" = "1" ] && [ -z "$WOLFPROV_DEBUG" ] && [ -z "$de
140140
exit 1
141141
fi
142142

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+
143148
if [ -n "$build_debian" ]; then
144149
echo "Building Debian package..."
145150
WOLFSSL_ISFIPS=${WOLFSSL_ISFIPS:-0} WOLFPROV_DEBUG=${WOLFPROV_DEBUG:-0} ./scripts/build-debian.sh

src/wp_logging.c

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,33 +51,34 @@ static int providerLogComponents = WP_LOG_COMPONENTS_ALL;
5151

5252
#ifdef WOLFPROV_LOG_FILE
5353
/* Persistent file handle for logging to file */
54-
static FILE* logFileHandle = NULL;
55-
/* Flag to track if we've already reported file open failure to avoid spam */
56-
static int logFileErrorReported = 0;
54+
static XFILE* logFileHandle = NULL;
5755
#endif
5856

5957
#endif /* WOLFPROV_DEBUG */
6058

61-
#ifdef WOLFPROV_LOG_FILE
6259
/**
6360
* Initialize the persistent log file handle.
6461
* Called once during provider initialization.
6562
*
6663
* @return 0 on success, negative value on failure.
6764
*/
65+
6866
int wp_log_file_init(void)
6967
{
68+
#if defined(WOLFPROV_LOG_FILE) && defined(WOLFPROV_DEBUG)
7069
if (logFileHandle == NULL) {
71-
fprintf(stderr, "wolfProvider: Opening log file %s\n", WOLFPROV_LOG_FILE);
72-
logFileHandle = fopen(WOLFPROV_LOG_FILE, "a");
73-
fprintf(stderr, "wolfProvider: Log file handle: %p\n", logFileHandle);
74-
if (logFileHandle == NULL) {
70+
logFileHandle = XFOPEN(WOLFPROV_LOG_FILE, "a");
71+
if (logFileHandle) {
72+
XFPRINTF(stderr, "wolfProvider: Using log file %s\n", WOLFPROV_LOG_FILE);
73+
fflush(stderr);
74+
}
75+
else {
7576
/* File open failed - will fall back to stderr on first log */
76-
fprintf(stderr, "wolfProvider: Failed to open log file %s\n", WOLFPROV_LOG_FILE);
77+
XFPRINTF(stderr, "wolfProvider: Failed to open log file %s\n", WOLFPROV_LOG_FILE);
7778
return -1;
7879
}
7980
}
80-
81+
#endif /* WOLFPROV_LOG_FILE && WOLFPROV_DEBUG */
8182
return 0;
8283
}
8384

@@ -87,13 +88,14 @@ int wp_log_file_init(void)
8788
*/
8889
void wp_log_file_cleanup(void)
8990
{
91+
#if defined(WOLFPROV_LOG_FILE) && defined(WOLFPROV_DEBUG)
9092
if (logFileHandle != NULL) {
91-
fprintf(stderr, "wolfProvider: Closing log file %s\n", WOLFPROV_LOG_FILE);
92-
fclose(logFileHandle);
93+
XFPRINTF(stderr, "wolfProvider: Closing log file %s\n", WOLFPROV_LOG_FILE);
94+
XFCLOSE(logFileHandle);
9395
logFileHandle = NULL;
9496
}
97+
#endif /* WOLFPROV_LOG_FILE && WOLFPROV_DEBUG */
9598
}
96-
#endif /* WOLFPROV_LOG_FILE */
9799

98100
/**
99101
* Registers wolfProv logging callback.
@@ -218,23 +220,27 @@ static void wolfprovider_log(const int logLevel, const int component,
218220
printf("%s\n", logMessage);
219221
#elif defined(WOLFPROV_LOG_FILE)
220222
{
223+
/* Flag to track if we've already reported file open failure to avoid spam */
224+
static int logFileErrorReported = 0;
225+
221226
if (logFileHandle != NULL) {
222-
fprintf(stderr, "wolfProvider: Logging to file %s\n", WOLFPROV_LOG_FILE);
223-
fprintf(logFileHandle, "%s\n", logMessage);
224-
fflush(logFileHandle);
227+
XFWRITE(logMessage, strlen(logMessage), 1, logFileHandle);
228+
XFWRITE("\n", 1, 1, logFileHandle);
229+
XFFLUSH(logFileHandle);
225230
} else {
226231
/* Only report file error once to avoid spam */
227232
if (!logFileErrorReported) {
228-
fprintf(stderr, "wolfProvider: Failed to open log file %s, "
233+
XFPRINTF(stderr, "wolfProvider: Log file not open: %s, "
229234
"falling back to stderr\n",
230235
WOLFPROV_LOG_FILE);
231236
logFileErrorReported = 1;
232237
}
233-
fprintf(stderr, "%s\n", logMessage);
238+
XFWRITE(logMessage, strlen(logMessage), 1, stderr);
239+
XFWRITE("\n", 1, 1, stderr);
234240
}
235241
}
236242
#else
237-
fprintf(stderr, "%s\n", logMessage);
243+
XFPRINTF(stderr, "%s\n", logMessage);
238244
#endif
239245
}
240246
}

src/wp_wolfprov.c

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,10 +1178,7 @@ static const OSSL_ALGORITHM* wolfprov_query(void* provCtx, int id,
11781178
*/
11791179
static void wolfprov_teardown(void* provCtx)
11801180
{
1181-
#ifdef WOLFPROV_LOG_FILE
1182-
/* Cleanup logging system */
11831181
wp_log_file_cleanup();
1184-
#endif
11851182
wolfssl_prov_ctx_free(provCtx);
11861183
}
11871184

@@ -1239,6 +1236,8 @@ int wolfssl_provider_init(const OSSL_CORE_HANDLE* handle,
12391236
{
12401237
int ok = 1;
12411238

1239+
wp_log_file_init();
1240+
12421241
WOLFPROV_ENTER(WP_LOG_PROVIDER, "wolfssl_provider_init");
12431242

12441243
#ifdef WOLFPROV_DEBUG
@@ -1254,16 +1253,6 @@ int wolfssl_provider_init(const OSSL_CORE_HANDLE* handle,
12541253
wolfSSL_SetLoggingPrefix("wolfSSL");
12551254
}
12561255
}
1257-
1258-
#ifdef WOLFPROV_LOG_FILE
1259-
/* Initialize logging system for file based logging */
1260-
if (ok) {
1261-
if (wp_log_file_init() != 0) {
1262-
/* Logging init failure is not fatal, but log it */
1263-
fprintf(stderr, "wolfProvider: Warning - Failed to initialize file logging\n");
1264-
}
1265-
}
1266-
#endif
12671256
#endif
12681257

12691258
#ifdef HAVE_FIPS

0 commit comments

Comments
 (0)