Skip to content

Commit 6208cb2

Browse files
authored
Merge pull request #322 from padelsbach/wp-logging-unification
Update logging logic
2 parents b98d576 + 1dcb557 commit 6208cb2

File tree

4 files changed

+178
-284
lines changed

4 files changed

+178
-284
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,6 @@ debian/libssl3*
122122
!debian/*.triggers
123123
!debian/shlib.local
124124

125+
# clangd
126+
.cache/
127+
compile_commands.json

include/wolfprovider/wp_logging.h

Lines changed: 152 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -114,98 +114,107 @@
114114
* When modifying the enum values, ensure the corresponding strings in the
115115
* wp_logging.c file are updated to match.
116116
*/
117-
enum wolfProv_LogLevels {
118-
WP_LOG_LEVEL_ERROR = 0x0001, /* logs errors */
119-
WP_LOG_LEVEL_ENTER = 0x0002, /* logs function enter*/
120-
WP_LOG_LEVEL_LEAVE = 0x0004, /* logs function leave */
121-
WP_LOG_LEVEL_INFO = 0x0008, /* logs informative messages */
122-
WP_LOG_LEVEL_VERBOSE = 0x0010, /* logs encrypted/decrypted/digested data */
123-
/* To see the return code from wolfssl, you must add WP_LOG_LEVEL_DEBUG to the
124-
* WOLFPROV_LOG_LEVEL_FILTER */
125-
WP_LOG_LEVEL_DEBUG = 0x0020, /* logs debug-level detailed information */
126-
WP_LOG_LEVEL_TRACE = 0x0040, /* logs trace-level ultra-detailed information */
127-
128-
/* default log level when logging is turned on */
129-
WP_LOG_LEVEL_DEFAULT = (WP_LOG_LEVEL_ERROR | WP_LOG_LEVEL_LEAVE | WP_LOG_LEVEL_INFO),
130-
131-
/* log all, including verbose */
132-
WP_LOG_LEVEL_ALL = (WP_LOG_LEVEL_ERROR
133-
| WP_LOG_LEVEL_ENTER
134-
| WP_LOG_LEVEL_LEAVE
135-
| WP_LOG_LEVEL_INFO
136-
| WP_LOG_LEVEL_VERBOSE
137-
| WP_LOG_LEVEL_DEBUG
138-
| WP_LOG_LEVEL_TRACE)
139-
};
140-
141-
enum wolfProv_LogComponents {
142-
/* Legacy component categories */
143-
WP_LOG_COMP_RNG = 0x0001, /* random number generation */
144-
WP_LOG_COMP_DIGEST = 0x0002, /* digest (SHA-1/2/3) */
145-
WP_LOG_COMP_MAC = 0x0004, /* mac functions: HMAC, CMAC */
146-
WP_LOG_COMP_CIPHER = 0x0008, /* cipher (AES, 3DES) */
147-
WP_LOG_COMP_PK = 0x0010, /* public key algorithms (RSA, ECC) */
148-
WP_LOG_COMP_KE = 0x0020, /* key agreement (DH, ECDH) */
149-
WP_LOG_COMP_KDF = 0x0040, /* password base key derivation algorithms */
150-
WP_LOG_COMP_PROVIDER = 0x0080, /* all provider specific logs */
151-
152-
/* Granular algorithm family categories */
153-
WP_LOG_COMP_RSA = 0x0100, /* RSA operations */
154-
WP_LOG_COMP_ECC = 0x0200, /* ECC operations */
155-
WP_LOG_COMP_DH = 0x0400, /* Diffie-Hellman operations */
156-
WP_LOG_COMP_AES = 0x0800, /* AES cipher operations */
157-
WP_LOG_COMP_DES = 0x1000, /* 3DES cipher operations */
158-
WP_LOG_COMP_SHA = 0x2000, /* SHA digest operations */
159-
WP_LOG_COMP_MD5 = 0x4000, /* MD5 digest operations */
160-
WP_LOG_COMP_HMAC = 0x8000, /* HMAC operations */
161-
WP_LOG_COMP_CMAC = 0x10000, /* CMAC operations */
162-
WP_LOG_COMP_HKDF = 0x20000, /* HKDF operations */
163-
WP_LOG_COMP_PBKDF2 = 0x40000, /* PBKDF2 operations */
164-
WP_LOG_COMP_KRB5KDF = 0x80000, /* KRB5KDF operations */
165-
WP_LOG_COMP_DRBG = 0x100000, /* DRBG operations */
166-
WP_LOG_COMP_ECDSA = 0x200000, /* ECDSA signature operations */
167-
WP_LOG_COMP_ECDH = 0x400000, /* ECDH key exchange operations */
168-
WP_LOG_COMP_ED25519 = 0x800000, /* Ed25519 operations */
169-
WP_LOG_COMP_ED448 = 0x1000000, /* Ed448 operations */
170-
WP_LOG_COMP_X25519 = 0x2000000, /* X25519 operations */
171-
WP_LOG_COMP_X448 = 0x4000000, /* X448 operations */
172-
WP_LOG_COMP_QUERY = 0x8000000, /* wolfprov_query operations */
173-
WP_LOG_COMP_TLS1_PRF = 0x10000000, /* TLS1 PRF operations */
174-
175-
/* log all components */
176-
WP_LOG_COMP_ALL = (WP_LOG_COMP_RNG
177-
| WP_LOG_COMP_DIGEST
178-
| WP_LOG_COMP_MAC
179-
| WP_LOG_COMP_CIPHER
180-
| WP_LOG_COMP_PK
181-
| WP_LOG_COMP_KE
182-
| WP_LOG_COMP_KDF
183-
| WP_LOG_COMP_PROVIDER
184-
| WP_LOG_COMP_RSA
185-
| WP_LOG_COMP_ECC
186-
| WP_LOG_COMP_DH
187-
| WP_LOG_COMP_AES
188-
| WP_LOG_COMP_DES
189-
| WP_LOG_COMP_SHA
190-
| WP_LOG_COMP_MD5
191-
| WP_LOG_COMP_HMAC
192-
| WP_LOG_COMP_CMAC
193-
| WP_LOG_COMP_HKDF
194-
| WP_LOG_COMP_PBKDF2
195-
| WP_LOG_COMP_KRB5KDF
196-
| WP_LOG_COMP_DRBG
197-
| WP_LOG_COMP_ECDSA
198-
| WP_LOG_COMP_ECDH
199-
| WP_LOG_COMP_ED25519
200-
| WP_LOG_COMP_ED448
201-
| WP_LOG_COMP_X25519
202-
| WP_LOG_COMP_X448
203-
| WP_LOG_COMP_QUERY
204-
| WP_LOG_COMP_TLS1_PRF),
205-
206-
/* default components logged */
207-
WP_LOG_COMP_DEFAULT = WP_LOG_COMP_ALL
208-
};
117+
/*-------------------------------------------------------------*
118+
* wolfProv_LogLevels
119+
*-------------------------------------------------------------*/
120+
121+
#define WP_LOG_LEVEL_ERROR 0x0001 /* logs errors */
122+
#define WP_LOG_LEVEL_ENTER 0x0002 /* logs function enter */
123+
#define WP_LOG_LEVEL_LEAVE 0x0004 /* logs function leave */
124+
#define WP_LOG_LEVEL_INFO 0x0008 /* logs informative messages */
125+
#define WP_LOG_LEVEL_VERBOSE 0x0010 /* logs encrypted/decrypted/digested data */
126+
/* To see the return code from wolfssl, you must add WP_LOG_LEVEL_DEBUG to the
127+
* WOLFPROV_LOG_LEVEL_FILTER */
128+
#define WP_LOG_LEVEL_DEBUG 0x0020 /* logs debug-level detailed information */
129+
#define WP_LOG_LEVEL_TRACE 0x0040 /* logs trace-level ultra-detailed information */
130+
131+
/* default log level when logging is turned on */
132+
#define WP_LOG_LEVEL_DEFAULT ( \
133+
WP_LOG_LEVEL_ERROR | \
134+
WP_LOG_LEVEL_LEAVE | \
135+
WP_LOG_LEVEL_INFO)
136+
137+
/* log all, including verbose */
138+
#define WP_LOG_LEVEL_ALL ( \
139+
WP_LOG_LEVEL_ERROR | \
140+
WP_LOG_LEVEL_ENTER | \
141+
WP_LOG_LEVEL_LEAVE | \
142+
WP_LOG_LEVEL_INFO | \
143+
WP_LOG_LEVEL_VERBOSE | \
144+
WP_LOG_LEVEL_DEBUG | \
145+
WP_LOG_LEVEL_TRACE )
146+
147+
/*-------------------------------------------------------------*
148+
* wolfProv_LogComponents
149+
*-------------------------------------------------------------*/
150+
151+
/* Legacy component categories */
152+
#define WP_LOG_COMP_RNG 0x0001 /* random number generation */
153+
#define WP_LOG_COMP_DIGEST 0x0002 /* digest (SHA-1/2/3) */
154+
#define WP_LOG_COMP_MAC 0x0004 /* mac functions: HMAC, CMAC */
155+
#define WP_LOG_COMP_CIPHER 0x0008 /* cipher (AES, 3DES) */
156+
#define WP_LOG_COMP_PK 0x0010 /* public key algorithms (RSA, ECC) */
157+
#define WP_LOG_COMP_KE 0x0020 /* key agreement (DH, ECDH) */
158+
#define WP_LOG_COMP_KDF 0x0040 /* password base key derivation algorithms */
159+
#define WP_LOG_COMP_PROVIDER 0x0080 /* all provider specific logs */
160+
161+
/* Granular algorithm family categories */
162+
#define WP_LOG_COMP_RSA 0x0100 /* RSA operations */
163+
#define WP_LOG_COMP_ECC 0x0200 /* ECC operations */
164+
#define WP_LOG_COMP_DH 0x0400 /* Diffie-Hellman operations */
165+
#define WP_LOG_COMP_AES 0x0800 /* AES cipher operations */
166+
#define WP_LOG_COMP_DES 0x1000 /* 3DES cipher operations */
167+
#define WP_LOG_COMP_SHA 0x2000 /* SHA digest operations */
168+
#define WP_LOG_COMP_MD5 0x4000 /* MD5 digest operations */
169+
#define WP_LOG_COMP_HMAC 0x8000 /* HMAC operations */
170+
#define WP_LOG_COMP_CMAC 0x10000 /* CMAC operations */
171+
#define WP_LOG_COMP_HKDF 0x20000 /* HKDF operations */
172+
#define WP_LOG_COMP_PBKDF2 0x40000 /* PBKDF2 operations */
173+
#define WP_LOG_COMP_KRB5KDF 0x80000 /* KRB5KDF operations */
174+
#define WP_LOG_COMP_DRBG 0x100000 /* DRBG operations */
175+
#define WP_LOG_COMP_ECDSA 0x200000 /* ECDSA signature operations */
176+
#define WP_LOG_COMP_ECDH 0x400000 /* ECDH key exchange operations */
177+
#define WP_LOG_COMP_ED25519 0x800000 /* Ed25519 operations */
178+
#define WP_LOG_COMP_ED448 0x1000000 /* Ed448 operations */
179+
#define WP_LOG_COMP_X25519 0x2000000 /* X25519 operations */
180+
#define WP_LOG_COMP_X448 0x4000000 /* X448 operations */
181+
#define WP_LOG_COMP_QUERY 0x8000000 /* wolfprov_query operations */
182+
#define WP_LOG_COMP_TLS1_PRF 0x10000000 /* TLS1 PRF operations */
183+
184+
/* log all components */
185+
#define WP_LOG_COMP_ALL ( \
186+
WP_LOG_COMP_RNG | \
187+
WP_LOG_COMP_DIGEST | \
188+
WP_LOG_COMP_MAC | \
189+
WP_LOG_COMP_CIPHER | \
190+
WP_LOG_COMP_PK | \
191+
WP_LOG_COMP_KE | \
192+
WP_LOG_COMP_KDF | \
193+
WP_LOG_COMP_PROVIDER | \
194+
WP_LOG_COMP_RSA | \
195+
WP_LOG_COMP_ECC | \
196+
WP_LOG_COMP_DH | \
197+
WP_LOG_COMP_AES | \
198+
WP_LOG_COMP_DES | \
199+
WP_LOG_COMP_SHA | \
200+
WP_LOG_COMP_MD5 | \
201+
WP_LOG_COMP_HMAC | \
202+
WP_LOG_COMP_CMAC | \
203+
WP_LOG_COMP_HKDF | \
204+
WP_LOG_COMP_PBKDF2 | \
205+
WP_LOG_COMP_KRB5KDF | \
206+
WP_LOG_COMP_DRBG | \
207+
WP_LOG_COMP_ECDSA | \
208+
WP_LOG_COMP_ECDH | \
209+
WP_LOG_COMP_ED25519 | \
210+
WP_LOG_COMP_ED448 | \
211+
WP_LOG_COMP_X25519 | \
212+
WP_LOG_COMP_X448 | \
213+
WP_LOG_COMP_QUERY | \
214+
WP_LOG_COMP_TLS1_PRF )
215+
216+
/* default components logged */
217+
#define WP_LOG_COMP_DEFAULT WP_LOG_COMP_ALL
209218

210219
/* Manually set the log level */
211220
#ifndef WOLFPROV_LOG_LEVEL_FILTER
@@ -247,36 +256,61 @@ int wolfProv_LogInit(void);
247256
#define WOLFPROV_STRINGIZE_HELPER(x) #x
248257
#define WOLFPROV_STRINGIZE(x) WOLFPROV_STRINGIZE_HELPER(x)
249258

250-
#define WOLFPROV_ERROR(type, err) \
251-
WOLFPROV_ERROR_LINE(type, err, __FILE__, __LINE__)
252-
#define WOLFPROV_ERROR_MSG(type, msg) \
253-
WOLFPROV_ERROR_MSG_LINE(type, msg, __FILE__, __LINE__)
254-
#define WOLFPROV_ERROR_FUNC(type, funcName, ret) \
255-
WOLFPROV_ERROR_FUNC_LINE(type, funcName, ret, __FILE__, __LINE__)
256-
#define WOLFPROV_ERROR_FUNC_NULL(type, funcName, ret) \
257-
WOLFPROV_ERROR_FUNC_NULL_LINE(type, funcName, ret, __FILE__, __LINE__)
258-
void WOLFPROV_ENTER(int type, const char* msg);
259-
void WOLFPROV_ENTER_SILENT(int type, const char* msg);
260-
#define WOLFPROV_LEAVE(type, msg, ret) \
261-
WOLFPROV_LEAVE_EX(type, WOLFPROV_FUNC_NAME, msg, ret)
262-
void WOLFPROV_LEAVE_EX(int type, const char* func, const char* msg, int ret);
263-
#define WOLFPROV_LEAVE_SILENT(type, msg, ret) \
264-
WOLFPROV_LEAVE_SILENT_EX(type, WOLFPROV_FUNC_NAME, msg, ret)
265-
void WOLFPROV_LEAVE_SILENT_EX(int type, const char* func, const char* msg,
259+
void wolfprovider_msg(int component, int logLevel, const char *fmt, ...);
260+
261+
/* Common macro which implements the compile-time check to strip disabled logs */
262+
#define WOLFPROV_MSG_EX(component, level, fmt, ...) \
263+
do { if (WOLFPROV_COMPILE_TIME_CHECK(component, level)) { \
264+
wolfprovider_msg(component, level, fmt, ##__VA_ARGS__); \
265+
} } while(0)
266+
267+
#define WOLFPROV_MSG(component, fmt, ...) \
268+
WOLFPROV_MSG_EX(component, WP_LOG_LEVEL_INFO, fmt, ##__VA_ARGS__)
269+
270+
#define WOLFPROV_ERROR(component, err) \
271+
WOLFPROV_ERROR_LINE(component, err, __FILE__, __LINE__)
272+
#define WOLFPROV_ERROR_MSG(component, msg) \
273+
WOLFPROV_ERROR_MSG_LINE(component, msg, __FILE__, __LINE__)
274+
#define WOLFPROV_ERROR_FUNC(component, funcName, ret) \
275+
WOLFPROV_ERROR_FUNC_LINE(component, funcName, ret, __FILE__, __LINE__)
276+
#define WOLFPROV_ERROR_FUNC_NULL(component, funcName, ret) \
277+
WOLFPROV_ERROR_FUNC_NULL_LINE(component, funcName, ret, __FILE__, __LINE__)
278+
279+
#define WOLFPROV_LEAVE(component, msg, ret) \
280+
WOLFPROV_LEAVE_EX(component, WOLFPROV_FUNC_NAME, msg, ret)
281+
void WOLFPROV_LEAVE_EX(int component, const char* func, const char* msg, int ret);
282+
#define WOLFPROV_LEAVE_SILENT(component, msg, ret) \
283+
WOLFPROV_LEAVE_SILENT_EX(component, WOLFPROV_FUNC_NAME, msg, ret)
284+
void WOLFPROV_LEAVE_SILENT_EX(int component, const char* func, const char* msg,
266285
int ret);
267-
void WOLFPROV_MSG(int type, const char* fmt, ...);
268-
void WOLFPROV_MSG_VERBOSE(int type, const char* fmt, ...);
269-
void WOLFPROV_MSG_DEBUG(int type, const char* fmt, ...);
270-
void WOLFPROV_MSG_DEBUG_RETCODE(int type, const char* func_name, int rc);
271-
void WOLFPROV_MSG_TRACE(int type, const char* fmt, ...);
272-
void WOLFPROV_ERROR_LINE(int type, int err, const char* file, int line);
273-
void WOLFPROV_ERROR_MSG_LINE(int type, const char* msg, const char* file,
286+
287+
#define WOLFPROV_ENTER(component, msg) \
288+
WOLFPROV_MSG_EX(component, WP_LOG_LEVEL_ENTER, "wolfProv Entering %s", msg)
289+
#ifdef WOLFPROV_LEAVE_SILENT_MODE
290+
#define WOLFPROV_ENTER_SILENT(component, msg) \
291+
WOLFPROV_ENTER_SILENT(component, msg)
292+
#else
293+
#define WOLFPROV_ENTER_SILENT(component, msg) \
294+
WOLFPROV_MSG_EX(component, WP_LOG_LEVEL_ENTER, "wolfProv Entering [leaving silently] %s", msg)
295+
#endif
296+
297+
#define WOLFPROV_MSG_VERBOSE(component, fmt, ...) \
298+
WOLFPROV_MSG_EX(component, WP_LOG_LEVEL_VERBOSE, fmt, ##__VA_ARGS__)
299+
#define WOLFPROV_MSG_DEBUG(component, fmt, ...) \
300+
WOLFPROV_MSG_EX(component, WP_LOG_LEVEL_DEBUG, fmt, ##__VA_ARGS__)
301+
#define WOLFPROV_MSG_DEBUG_RETCODE(component, func_name, rc) \
302+
WOLFPROV_MSG_DEBUG(component, "%s failed with rc=%d", func_name, rc)
303+
#define WOLFPROV_MSG_TRACE(component, fmt, ...) \
304+
WOLFPROV_MSG_EX(component, WP_LOG_LEVEL_TRACE, fmt, ##__VA_ARGS__)
305+
306+
void WOLFPROV_ERROR_LINE(int component, int err, const char* file, int line);
307+
void WOLFPROV_ERROR_MSG_LINE(int component, const char* msg, const char* file,
274308
int line);
275-
void WOLFPROV_ERROR_FUNC_LINE(int type, const char* funcName, int ret,
309+
void WOLFPROV_ERROR_FUNC_LINE(int component, const char* funcName, int ret,
276310
const char* file, int line);
277-
void WOLFPROV_ERROR_FUNC_NULL_LINE(int type, const char* funcName,
311+
void WOLFPROV_ERROR_FUNC_NULL_LINE(int component, const char* funcName,
278312
const void *ret, const char* file, int line);
279-
void WOLFPROV_BUFFER(int type, const unsigned char* buffer,
313+
void WOLFPROV_BUFFER(int component, const unsigned char* buffer,
280314
unsigned int length);
281315

282316
#else /* WOLFPROV_DEBUG */
@@ -299,4 +333,3 @@ void WOLFPROV_BUFFER(int type, const unsigned char* buffer,
299333
#endif /* WOLFPROV_DEBUG */
300334

301335
#endif /* WP_LOGGING_H */
302-

0 commit comments

Comments
 (0)