Skip to content

Commit 9ac1a63

Browse files
committed
Optimization to reduce code
1 parent 980cc3f commit 9ac1a63

File tree

3 files changed

+24
-69
lines changed

3 files changed

+24
-69
lines changed

include/wolfprovider/wp_logging.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ enum wolfProv_LogComponents {
169169
WP_LOG_QUERY = 0x80000, /* wolfprov_query operations */
170170
WP_LOG_TLS1_PRF = 0x100000, /* TLS1 PRF operations */
171171

172-
/* log all compoenents */
172+
/* log all components */
173173
WP_LOG_COMPONENTS_ALL = (WP_LOG_RNG
174174
| WP_LOG_DIGEST
175175
| WP_LOG_MAC
@@ -200,7 +200,7 @@ enum wolfProv_LogComponents {
200200
| WP_LOG_QUERY
201201
| WP_LOG_TLS1_PRF),
202202

203-
/* default compoenents logged */
203+
/* default components logged */
204204
WP_LOG_COMPONENTS_DEFAULT = WP_LOG_COMPONENTS_ALL
205205
};
206206

@@ -237,11 +237,6 @@ int wolfProv_SetLogLevel(int levelMask);
237237
/* Set which components are logged, bitmask of wolfProv_LogComponents */
238238
int wolfProv_SetLogComponents(int componentMask);
239239

240-
/* Initialize the logging system for file based logging */
241-
int wp_log_file_init(void);
242-
/* Cleanup the logging system for file based logging */
243-
void wp_log_file_cleanup(void);
244-
245240
#ifdef WOLFPROV_DEBUG
246241

247242
#define WOLFPROV_STRINGIZE_HELPER(x) #x

src/wp_logging.c

Lines changed: 22 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -49,54 +49,8 @@ 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 XFILE* logFileHandle = NULL;
55-
#endif
56-
5752
#endif /* WOLFPROV_DEBUG */
5853

59-
/**
60-
* Initialize the persistent log file handle.
61-
* Called once during provider initialization.
62-
*
63-
* @return 0 on success, negative value on failure.
64-
*/
65-
66-
int wp_log_file_init(void)
67-
{
68-
#if defined(WOLFPROV_LOG_FILE) && defined(WOLFPROV_DEBUG)
69-
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 {
76-
/* File open failed - will fall back to stderr on first log */
77-
XFPRINTF(stderr, "wolfProvider: Failed to open log file %s\n", WOLFPROV_LOG_FILE);
78-
return -1;
79-
}
80-
}
81-
#endif /* WOLFPROV_LOG_FILE && WOLFPROV_DEBUG */
82-
return 0;
83-
}
84-
85-
/**
86-
* Cleanup the persistent log file handle.
87-
* Called during provider teardown.
88-
*/
89-
void wp_log_file_cleanup(void)
90-
{
91-
#if defined(WOLFPROV_LOG_FILE) && defined(WOLFPROV_DEBUG)
92-
if (logFileHandle != NULL) {
93-
XFPRINTF(stderr, "wolfProvider: Closing log file %s\n", WOLFPROV_LOG_FILE);
94-
XFCLOSE(logFileHandle);
95-
logFileHandle = NULL;
96-
}
97-
#endif /* WOLFPROV_LOG_FILE && WOLFPROV_DEBUG */
98-
}
99-
10054
/**
10155
* Registers wolfProv logging callback.
10256
* Callback will be used by wolfProv for debug/log messages.
@@ -220,24 +174,33 @@ static void wolfprovider_log(const int logLevel, const int component,
220174
printf("%s\n", logMessage);
221175
#elif defined(WOLFPROV_LOG_FILE)
222176
{
177+
/* Persistent file handle for logging to file */
178+
static XFILE* logFileHandle = NULL;
223179
/* Flag to track if we've already reported file open failure to avoid spam */
224180
static int logFileErrorReported = 0;
225181

226-
if (logFileHandle != NULL) {
227-
XFWRITE(logMessage, strlen(logMessage), 1, logFileHandle);
228-
XFWRITE("\n", 1, 1, logFileHandle);
229-
XFFLUSH(logFileHandle);
230-
} else {
231-
/* Only report file error once to avoid spam */
232-
if (!logFileErrorReported) {
233-
XFPRINTF(stderr, "wolfProvider: Log file not open: %s, "
234-
"falling back to stderr\n",
235-
WOLFPROV_LOG_FILE);
236-
logFileErrorReported = 1;
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+
}
237198
}
238-
XFWRITE(logMessage, strlen(logMessage), 1, stderr);
239-
XFWRITE("\n", 1, 1, stderr);
240199
}
200+
201+
XFWRITE(logMessage, strlen(logMessage), 1, logFileHandle);
202+
XFWRITE("\n", 1, 1, logFileHandle);
203+
XFFLUSH(logFileHandle);
241204
}
242205
#else
243206
XFPRINTF(stderr, "%s\n", logMessage);

src/wp_wolfprov.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,7 +1178,6 @@ static const OSSL_ALGORITHM* wolfprov_query(void* provCtx, int id,
11781178
*/
11791179
static void wolfprov_teardown(void* provCtx)
11801180
{
1181-
wp_log_file_cleanup();
11821181
wolfssl_prov_ctx_free(provCtx);
11831182
}
11841183

@@ -1236,8 +1235,6 @@ int wolfssl_provider_init(const OSSL_CORE_HANDLE* handle,
12361235
{
12371236
int ok = 1;
12381237

1239-
wp_log_file_init();
1240-
12411238
WOLFPROV_ENTER(WP_LOG_PROVIDER, "wolfssl_provider_init");
12421239

12431240
#ifdef WOLFPROV_DEBUG

0 commit comments

Comments
 (0)