Skip to content

Commit 3fba076

Browse files
committed
implement printf logging backend
1 parent ed9143f commit 3fba076

File tree

5 files changed

+184
-16
lines changed

5 files changed

+184
-16
lines changed

port/posix/posix_log_file.c

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,6 @@
3939

4040
#ifdef WOLFHSM_CFG_LOGGING
4141

42-
/* Helper function to convert log level to string */
43-
static const char* posixLogFile_LevelToString(whLogLevel level)
44-
{
45-
switch (level) {
46-
case WH_LOG_LEVEL_INFO:
47-
return "INFO";
48-
case WH_LOG_LEVEL_ERROR:
49-
return "ERROR";
50-
case WH_LOG_LEVEL_SECEVENT:
51-
return "SECEVENT";
52-
default:
53-
return "UNKNOWN";
54-
}
55-
}
56-
5742
/* Helper function to convert string to log level */
5843
static whLogLevel posixLogFile_StringToLevel(const char* str)
5944
{
@@ -146,7 +131,7 @@ int posixLogFile_AddEntry(void* c, const whLogEntry* entry)
146131
/* Format log entry: TIMESTAMP|LEVEL|FILE:LINE|FUNCTION|MESSAGE\n */
147132
len = snprintf(buffer, sizeof(buffer), "%llu|%s|%s:%u|%s|%.*s\n",
148133
(unsigned long long)entry->timestamp,
149-
posixLogFile_LevelToString(entry->level),
134+
wh_Log_LevelToString(entry->level),
150135
entry->file ? entry->file : "", entry->line,
151136
entry->function ? entry->function : "", (int)entry->msg_len,
152137
entry->msg);

src/wh_log.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,20 @@
3636

3737
#ifdef WOLFHSM_CFG_LOGGING
3838

39+
const char* wh_Log_LevelToString(whLogLevel level)
40+
{
41+
switch (level) {
42+
case WH_LOG_LEVEL_INFO:
43+
return "INFO";
44+
case WH_LOG_LEVEL_ERROR:
45+
return "ERROR";
46+
case WH_LOG_LEVEL_SECEVENT:
47+
return "SECEVENT";
48+
default:
49+
return "UNKNOWN";
50+
}
51+
}
52+
3953
void wh_Log_AddMsg(whLogContext* ctx, whLogLevel level, const char* file,
4054
const char* function, uint32_t line, const char* src,
4155
size_t src_len)

src/wh_log_printf.c

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright (C) 2024 wolfSSL Inc.
3+
*
4+
* This file is part of wolfHSM.
5+
*
6+
* wolfHSM is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation; either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* wolfHSM is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with wolfHSM. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
/*
20+
* src/wh_log_printf.c
21+
*
22+
* Printf-style logging backend implementation
23+
*/
24+
25+
#include <stddef.h> /* For NULL */
26+
#include <string.h> /* For memset, memcpy */
27+
28+
#include "wolfhsm/wh_settings.h"
29+
30+
#include "wolfhsm/wh_log_printf.h"
31+
#include "wolfhsm/wh_error.h"
32+
#include "wolfhsm/wh_log.h"
33+
34+
#ifdef WOLFHSM_CFG_LOGGING
35+
36+
int whLogPrintf_Init(void* c, const void* cf)
37+
{
38+
whLogPrintfContext* context = (whLogPrintfContext*)c;
39+
const whLogPrintfConfig* config = (const whLogPrintfConfig*)cf;
40+
41+
if (context == NULL) {
42+
return WH_ERROR_BADARGS;
43+
}
44+
45+
/* Initialize context */
46+
memset(context, 0, sizeof(*context));
47+
48+
/* Copy config if provided, otherwise use defaults */
49+
if (config != NULL) {
50+
context->logIfNotDebug = config->logIfNotDebug;
51+
}
52+
else {
53+
context->logIfNotDebug = 0;
54+
}
55+
56+
context->initialized = 1;
57+
58+
return WH_ERROR_OK;
59+
}
60+
61+
62+
int whLogPrintf_AddEntry(void* c, const whLogEntry* entry)
63+
{
64+
whLogPrintfContext* context = (whLogPrintfContext*)c;
65+
66+
if ((context == NULL) || (entry == NULL)) {
67+
return WH_ERROR_BADARGS;
68+
}
69+
70+
if (!context->initialized) {
71+
return WH_ERROR_ABORTED;
72+
}
73+
74+
/* Conditional logging:
75+
* - If logIfNotDebug is true: always log
76+
* - If logIfNotDebug is false: only log if WOLFHSM_CFG_DEBUG is defined
77+
*/
78+
#ifndef WOLFHSM_CFG_DEBUG
79+
if (!context->logIfNotDebug) {
80+
return WH_ERROR_OK;
81+
}
82+
#endif
83+
84+
/* Format: [TIMESTAMP] [LEVEL] [FILE:LINE FUNC] MESSAGE */
85+
WOLFHSM_CFG_PRINTF(
86+
"[%llu] [%s] [%s:%u %s] %.*s\n", (unsigned long long)entry->timestamp,
87+
wh_Log_LevelToString(entry->level), entry->file ? entry->file : "",
88+
entry->line, entry->function ? entry->function : "",
89+
(int)entry->msg_len, entry->msg);
90+
91+
return WH_ERROR_OK;
92+
}
93+
94+
#endif /* WOLFHSM_CFG_LOGGING */

wolfhsm/wh_log.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,17 @@ typedef struct {
8282
void* config; /* Backend-specific config */
8383
} whLogConfig;
8484

85+
/* Utility functions */
86+
87+
/**
88+
* @brief Convert a log level to its string representation.
89+
*
90+
* @param level The log level to convert.
91+
* @return Pointer to a static string representing the log level:
92+
* "INFO", "ERROR", "SECEVENT", or "UNKNOWN".
93+
*/
94+
const char* wh_Log_LevelToString(whLogLevel level);
95+
8596
/* Frontend API */
8697

8798
/**

wolfhsm/wh_log_printf.h

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright (C) 2024 wolfSSL Inc.
3+
*
4+
* This file is part of wolfHSM.
5+
*
6+
* wolfHSM is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation; either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* wolfHSM is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with wolfHSM. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
/*
20+
* wolfhsm/wh_log_printf.h
21+
*
22+
* Printf logging backend that simply prints out log entries as they are added,
23+
* with no backing store.
24+
*/
25+
26+
#ifndef WOLFHSM_WH_LOG_PRINTF_H_
27+
#define WOLFHSM_WH_LOG_PRINTF_H_
28+
29+
#include "wolfhsm/wh_settings.h"
30+
#include "wolfhsm/wh_log.h"
31+
32+
#include <stddef.h>
33+
34+
/* Printf configuration structure */
35+
typedef struct whLogPrintfConfig_t {
36+
int logIfNotDebug; /* When true, always log regardless of WOLFHSM_CFG_DEBUG
37+
*/
38+
} whLogPrintfConfig;
39+
40+
/* Printf context structure */
41+
typedef struct whLogPrintfContext_t {
42+
int initialized; /* Initialization flag */
43+
int logIfNotDebug; /* Copied from config */
44+
} whLogPrintfContext;
45+
46+
/* Callback functions */
47+
int whLogPrintf_Init(void* context, const void* config);
48+
int whLogPrintf_AddEntry(void* context, const whLogEntry* entry);
49+
50+
/* Convenience macro for callback table initialization.
51+
*/
52+
/* clang-format off */
53+
#define WH_LOG_PRINTF_CB \
54+
{ \
55+
.Init = whLogPrintf_Init, \
56+
.Cleanup = NULL, \
57+
.AddEntry = whLogPrintf_AddEntry, \
58+
.Export = NULL, \
59+
.Iterate = NULL, \
60+
.Clear = NULL, \
61+
}
62+
/* clang-format on */
63+
64+
#endif /* !WOLFHSM_WH_LOG_PRINTF_H_ */

0 commit comments

Comments
 (0)