Skip to content

Commit 2a7251f

Browse files
committed
Add debug-log- updated
1 parent 55fcabe commit 2a7251f

File tree

3 files changed

+75
-6
lines changed

3 files changed

+75
-6
lines changed

include/wolfprovider/wp_logging.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,13 @@ 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
241+
/* Initialize the logging system for file based logging */
242+
int wp_log_file_init(void);
243+
/* Cleanup the logging system for file based logging */
244+
void wp_log_file_cleanup(void);
245+
#endif /* WOLFPROV_LOG_FILE */
246+
240247
#ifdef WOLFPROV_DEBUG
241248

242249
#define WOLFPROV_STRINGIZE_HELPER(x) #x

src/wp_logging.c

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,51 @@ static int providerLogLevel = WP_LOG_LEVEL_ALL;
4949
* in wolfProv_LogComponents. Default components include all. */
5050
static int providerLogComponents = WP_LOG_COMPONENTS_ALL;
5151

52+
#ifdef WOLFPROV_LOG_FILE
53+
/* 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;
57+
#endif
58+
5259
#endif /* WOLFPROV_DEBUG */
5360

61+
#ifdef WOLFPROV_LOG_FILE
62+
/**
63+
* Initialize the persistent log file handle.
64+
* Called once during provider initialization.
65+
*
66+
* @return 0 on success, negative value on failure.
67+
*/
68+
int wp_log_file_init(void)
69+
{
70+
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) {
75+
/* 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+
return -1;
78+
}
79+
}
80+
81+
return 0;
82+
}
83+
84+
/**
85+
* Cleanup the persistent log file handle.
86+
* Called during provider teardown.
87+
*/
88+
void wp_log_file_cleanup(void)
89+
{
90+
if (logFileHandle != NULL) {
91+
fprintf(stderr, "wolfProvider: Closing log file %s\n", WOLFPROV_LOG_FILE);
92+
fclose(logFileHandle);
93+
logFileHandle = NULL;
94+
}
95+
}
96+
#endif /* WOLFPROV_LOG_FILE */
5497

5598
/**
5699
* Registers wolfProv logging callback.
@@ -175,13 +218,18 @@ static void wolfprovider_log(const int logLevel, const int component,
175218
printf("%s\n", logMessage);
176219
#elif defined(WOLFPROV_LOG_FILE)
177220
{
178-
FILE* logFile = fopen(WOLFPROV_LOG_FILE, "a");
179-
if (logFile != NULL) {
180-
fprintf(logFile, "%s\n", logMessage);
181-
fclose(logFile);
221+
if (logFileHandle != NULL) {
222+
fprintf(stderr, "wolfProvider: Logging to file %s\n", WOLFPROV_LOG_FILE);
223+
fprintf(logFileHandle, "%s\n", logMessage);
224+
fflush(logFileHandle);
182225
} else {
183-
fprintf(stderr, "wolfProvider: Failed to open log file %s\n",
184-
WOLFPROV_LOG_FILE);
226+
/* Only report file error once to avoid spam */
227+
if (!logFileErrorReported) {
228+
fprintf(stderr, "wolfProvider: Failed to open log file %s, "
229+
"falling back to stderr\n",
230+
WOLFPROV_LOG_FILE);
231+
logFileErrorReported = 1;
232+
}
185233
fprintf(stderr, "%s\n", logMessage);
186234
}
187235
}

src/wp_wolfprov.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,6 +1178,10 @@ 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 */
1183+
wp_log_file_cleanup();
1184+
#endif
11811185
wolfssl_prov_ctx_free(provCtx);
11821186
}
11831187

@@ -1250,6 +1254,16 @@ int wolfssl_provider_init(const OSSL_CORE_HANDLE* handle,
12501254
wolfSSL_SetLoggingPrefix("wolfSSL");
12511255
}
12521256
}
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
12531267
#endif
12541268

12551269
#ifdef HAVE_FIPS

0 commit comments

Comments
 (0)