Skip to content

Commit e506db6

Browse files
committed
API: Introduce a new minimal logger mode
In this mode, LibOSDP will not do any log filtering/formatting and will report logs as-is to the application. The idea is to use this with an existing logging subsystem that the app may already be using. This also lowers the RAM usage by making the logging context static. Related-to: #281 Signed-off-by: Siddharth Chandrasekaran <sidcha.dev@gmail.com>
1 parent 297ac66 commit e506db6

13 files changed

Lines changed: 219 additions & 17 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ option(OPT_OSDP_PACKET_TRACE "Enable raw packet trace for diagnostics" OFF)
2828
option(OPT_OSDP_DATA_TRACE "Enable command/reply data buffer tracing" OFF)
2929
option(OPT_OSDP_SKIP_MARK_BYTE "Don't send the leading mark byte (0xFF)" OFF)
3030
option(OPT_OSDP_RX_ZERO_COPY "Enable zero-copy RX buffers (requires recv_pkt/release_pkt)" OFF)
31+
option(OPT_OSDP_LOG_MINIMAL "Minimize logger RAM/stack usage for embedded targets" OFF)
3132
option(OPT_DISABLE_PRETTY_LOGGING "Don't colorize log ouputs" OFF)
3233
option(OPT_BUILD_SANITIZER "Enable different sanitizers during build" OFF)
3334
option(OPT_BUILD_STATIC "Build static library" ON)

configure.sh

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ usage() {
1818
--data-trace Enable command/reply data buffer tracing
1919
--skip-mark Don't send the leading mark byte (0xFF)
2020
--zero-copy Enable zero-copy RX buffers (requires recv_pkt/release_pkt)
21+
--log-minimal Minimize logger RAM/stack usage
2122
--crypto LIB Use methods from LIB (openssl/mbedtls/*tinyaes)
2223
--crypto-include-dir DIR Include directory for crypto LIB if not in system path
2324
--crypto-ld-flags Args to pass to linker for the crypto LIB
@@ -45,6 +46,7 @@ while [ $# -gt 0 ]; do
4546
--data-trace) DATA_TRACE=1;;
4647
--skip-mark) SKIP_MARK_BYTE=1;;
4748
--zero-copy) ZERO_COPY=1;;
49+
--log-minimal) LOG_MINIMAL=1;;
4850
--cross-compile) CROSS_COMPILE=$2; shift;;
4951
--prefix) PREFIX=$2; shift;;
5052
--crypto) CRYPTO=$2; shift;;
@@ -107,6 +109,10 @@ if [[ ! -z "${ZERO_COPY}" ]]; then
107109
CCFLAGS+=" -DOPT_OSDP_RX_ZERO_COPY"
108110
fi
109111

112+
if [[ ! -z "${LOG_MINIMAL}" ]]; then
113+
CCFLAGS+=" -DOPT_OSDP_LOG_MINIMAL"
114+
fi
115+
110116
if [[ ! -z "${STATIC}" ]]; then
111117
CCFLAGS+=" -DOPT_OSDP_STATIC"
112118
fi
@@ -166,7 +172,10 @@ fi
166172
LIBOSDP_SOURCES+=" src/osdp_common.c src/osdp_phy.c src/osdp_sc.c src/osdp_file.c src/osdp_pd.c"
167173
LIBOSDP_SOURCES+=" src/osdp_cp.c"
168174
LIBOSDP_SOURCES+=" utils/src/list.c utils/src/queue.c utils/src/utils.c"
169-
LIBOSDP_SOURCES+=" utils/src/disjoint_set.c utils/src/logger.c utils/src/crc16.c"
175+
LIBOSDP_SOURCES+=" utils/src/disjoint_set.c utils/src/crc16.c"
176+
if [[ -z "${LOG_MINIMAL}" ]]; then
177+
LIBOSDP_SOURCES+=" utils/src/logger.c"
178+
fi
170179

171180
UTILS_SOURCES+=" utils/src/workqueue.c utils/src/circbuf.c utils/src/event.c utils/src/fdutils.c"
172181

include/osdp.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,14 +1372,17 @@ typedef int (*osdp_log_puts_fn_t)(const char *msg);
13721372
/**
13731373
* @brief A callback function to be used with external loggers
13741374
*
1375+
* @param pd Address of PD associated with this message; -1 for non-PD/system logs
13751376
* @param log_level A syslog style log level. See `enum osdp_log_level_e`
1377+
* @param msg The log message
13761378
* @param file Relative path to file which produced the log message
13771379
* @param line Line number in `file` which produced the log message
1378-
* @param msg The log message
13791380
*/
1380-
typedef void (*osdp_log_callback_fn_t)(int log_level, const char *file,
1381-
unsigned long line, const char *msg);
1381+
typedef void (*osdp_log_callback_fn_t)(int pd, int log_level,
1382+
const char *msg, const char *file,
1383+
unsigned long line);
13821384

1385+
#ifndef OPT_OSDP_LOG_MINIMAL
13831386
/**
13841387
* @brief Configure OSDP Logging.
13851388
*
@@ -1399,11 +1402,10 @@ OSDP_EXPORT
13991402
void osdp_logger_init(const char *name, int log_level,
14001403
osdp_log_puts_fn_t puts_fn);
14011404

1405+
#endif /* OPT_OSDP_LOG_MINIMAL */
1406+
14021407
/**
1403-
* @brief A callback function that gets called when LibOSDP wants to emit a log
1404-
* line. All messages (of all log levels) are passed on to this callback
1405-
* without any log formatting. This API is for users who may already have a
1406-
* logger configured in their application.
1408+
* @brief Set logging callback for LibOSDP.
14071409
*
14081410
* @param cb The callback function. See `osdp_log_callback_fn_t` for more
14091411
* details.

include/osdp.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,18 @@ class OSDP_EXPORT Common {
1919
public:
2020
Common() : _ctx(nullptr) {}
2121

22+
#ifndef OPT_OSDP_LOG_MINIMAL
2223
void logger_init(const char *name, int log_level,
2324
osdp_log_puts_fn_t puts_fn)
2425
{
2526
osdp_logger_init(name, log_level, puts_fn);
2627
}
28+
#endif
29+
30+
void set_log_callback(osdp_log_callback_fn_t cb)
31+
{
32+
osdp_set_log_callback(cb);
33+
}
2734

2835
const char *get_version()
2936
{

src/CMakeLists.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ if (OPT_DISABLE_PRETTY_LOGGING)
2828
list(APPEND LIB_OSDP_DEFINITIONS "-DOPT_DISABLE_PRETTY_LOGGING=1")
2929
endif()
3030

31+
if (OPT_OSDP_LOG_MINIMAL)
32+
list(APPEND LIB_OSDP_DEFINITIONS "-DOPT_OSDP_LOG_MINIMAL=1")
33+
endif()
34+
3135
if (OPT_OSDP_STATIC)
3236
list(APPEND LIB_OSDP_DEFINITIONS "-DOPT_OSDP_STATIC=1")
3337
endif()
@@ -94,9 +98,13 @@ list(APPEND LIB_OSDP_UTILS_SRC
9498
${PROJECT_SOURCE_DIR}/utils/src/list.c
9599
${PROJECT_SOURCE_DIR}/utils/src/queue.c
96100
${PROJECT_SOURCE_DIR}/utils/src/disjoint_set.c
97-
${PROJECT_SOURCE_DIR}/utils/src/logger.c
98101
${PROJECT_SOURCE_DIR}/utils/src/crc16.c
99102
)
103+
if (NOT OPT_OSDP_LOG_MINIMAL)
104+
list(APPEND LIB_OSDP_UTILS_SRC
105+
${PROJECT_SOURCE_DIR}/utils/src/logger.c
106+
)
107+
endif()
100108

101109
list(APPEND LIB_OSDP_HEADERS
102110
${PROJECT_SOURCE_DIR}/include/osdp.h

src/osdp_common.c

Lines changed: 124 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,124 @@
1010

1111
#include <stdarg.h>
1212
#include <stdlib.h>
13-
#ifndef OPT_DISABLE_PRETTY_LOGGING
14-
#endif
13+
#include <string.h>
1514

1615
#include "osdp_common.h"
1716

1817
#include <utils/crc16.h>
1918

19+
#ifdef OPT_OSDP_LOG_MINIMAL
20+
#define OSDP_MIN_LOG_BUF_LEN 160
21+
22+
static struct {
23+
osdp_log_callback_fn_t cb;
24+
} g_osdp_log_cfg = {
25+
.cb = NULL,
26+
};
27+
28+
static char g_osdp_log_buf[OSDP_MIN_LOG_BUF_LEN + 2];
29+
30+
static const char *osdp_basename(const char *file)
31+
{
32+
const char *base = strrchr(file, PATH_SEPARATOR);
33+
34+
return base ? base + 1 : file;
35+
}
36+
37+
static int osdp_log_emit_v(int log_level, int pd_address,
38+
const char *file, unsigned long line,
39+
const char *fmt, va_list ap)
40+
{
41+
int n;
42+
const char *base = osdp_basename(file);
43+
44+
if (log_level < LOG_EMERG || log_level >= LOG_MAX_LEVEL) {
45+
return 0;
46+
}
47+
if (!g_osdp_log_cfg.cb) {
48+
return 0;
49+
}
50+
51+
n = vsnprintf(g_osdp_log_buf, sizeof(g_osdp_log_buf), fmt, ap);
52+
if (n < 0) {
53+
return n;
54+
}
55+
if (n >= (int)sizeof(g_osdp_log_buf)) {
56+
n = sizeof(g_osdp_log_buf) - 1;
57+
}
58+
59+
g_osdp_log_cfg.cb(pd_address, log_level, g_osdp_log_buf, base, line);
60+
return n;
61+
}
62+
63+
__format_printf(6, 7)
64+
int osdp_log_emit(bool is_cp, int pd_address, int log_level,
65+
const char *file, unsigned long line,
66+
const char *fmt, ...)
67+
{
68+
va_list ap;
69+
int ret;
70+
71+
va_start(ap, fmt);
72+
ret = osdp_log_emit_v(log_level, pd_address, file, line, fmt, ap);
73+
va_end(ap);
74+
75+
ARG_UNUSED(is_cp);
76+
return ret;
77+
}
78+
79+
#else /* OPT_OSDP_LOG_MINIMAL */
80+
81+
static osdp_log_callback_fn_t g_osdp_log_callback;
82+
83+
static void osdp_log_callback_trampoline(int log_level, const char *file,
84+
unsigned long line, const char *msg)
85+
{
86+
if (!g_osdp_log_callback) {
87+
return;
88+
}
89+
g_osdp_log_callback(-1, log_level, msg, file, line);
90+
}
91+
92+
__format_printf(6, 7)
93+
int osdp_log_cb_emit(bool is_cp, int pd_address, int log_level,
94+
const char *file, unsigned long line,
95+
const char *fmt, ...)
96+
{
97+
char msg[192];
98+
va_list args;
99+
int len;
100+
const char *base;
101+
const char *prefix = is_cp ? "CP: PD[%d]: " : "PD[%d]: ";
102+
103+
if (!g_osdp_log_callback) {
104+
return 0;
105+
}
106+
107+
len = snprintf(msg, sizeof(msg), prefix, pd_address);
108+
if (len < 0) {
109+
return len;
110+
}
111+
if (len >= (int)sizeof(msg)) {
112+
len = sizeof(msg) - 1;
113+
}
114+
115+
va_start(args, fmt);
116+
len += vsnprintf(msg + len, sizeof(msg) - len, fmt, args);
117+
va_end(args);
118+
if (len >= (int)sizeof(msg)) {
119+
len = sizeof(msg) - 1;
120+
}
121+
122+
base = strrchr(file, PATH_SEPARATOR);
123+
base = base ? base + 1 : file;
124+
125+
g_osdp_log_callback(pd_address, log_level, msg, base, line);
126+
return len;
127+
}
128+
129+
#endif /* OPT_OSDP_LOG_MINIMAL */
130+
20131
uint16_t osdp_compute_crc16(const uint8_t *buf, size_t len)
21132
{
22133
return crc16_itu_t(0x1D0F, buf, len);
@@ -176,6 +287,8 @@ int osdp_rb_pop_buf(struct osdp_rb *p, uint8_t *buf, int max_len)
176287

177288
/* --- Exported Methods --- */
178289

290+
#ifndef OPT_OSDP_LOG_MINIMAL
291+
179292
void osdp_logger_init(const char *name, int log_level,
180293
osdp_log_puts_fn_t log_fn)
181294
{
@@ -193,13 +306,21 @@ void osdp_logger_init(const char *name, int log_level,
193306
logger_set_default(&ctx); /* Mark this config as logging default */
194307
}
195308

309+
#endif /* OPT_OSDP_LOG_MINIMAL */
310+
196311
void osdp_set_log_callback(osdp_log_callback_fn_t cb)
197312
{
313+
#ifdef OPT_OSDP_LOG_MINIMAL
314+
g_osdp_log_cfg.cb = cb;
315+
#else
198316
logger_t ctx;
199317
int flags = LOGGER_FLAG_NONE;
318+
g_osdp_log_callback = cb;
200319

201-
logger_init(&ctx, 0, NULL, REPO_ROOT, NULL, NULL, cb, flags);
320+
logger_init(&ctx, 0, NULL, REPO_ROOT, NULL, NULL,
321+
osdp_log_callback_trampoline, flags);
202322
logger_set_default(&ctx); /* Mark this config as logging default */
323+
#endif
203324
}
204325

205326
const char *osdp_get_version()

src/osdp_common.h

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,36 @@
3636

3737
#define ARG_UNUSED(x) (void)(x)
3838

39+
#ifdef OPT_OSDP_LOG_MINIMAL
40+
41+
__format_printf(4, 5)
42+
int osdp_log_emit_sys(int log_level, const char *file, unsigned long line,
43+
const char *fmt, ...);
44+
__format_printf(6, 7)
45+
int osdp_log_emit(bool is_cp, int pd_address, int log_level,
46+
const char *file, unsigned long line,
47+
const char *fmt, ...);
48+
49+
#define OSDP_PD_LOG(_level, ...) \
50+
do { \
51+
osdp_log_emit(is_cp_mode(pd), pd->address, _level, \
52+
__FILE__, __LINE__, __VA_ARGS__); \
53+
} while (0)
54+
55+
#undef LOG_PRINT
56+
#define LOG_PRINT(...) \
57+
do { \
58+
osdp_log_emit(false, -1, LOG_INFO, __FILE__, \
59+
__LINE__, __VA_ARGS__); \
60+
} while (0)
61+
62+
#else
63+
64+
__format_printf(6, 7)
65+
int osdp_log_cb_emit_pd(bool is_cp, int pd_address, int log_level,
66+
const char *file, unsigned long line,
67+
const char *fmt, ...);
68+
3969
#define OSDP_PD_LOG(_level, ...) \
4070
do { \
4171
struct osdp *__ctx = pd_to_osdp(pd); \
@@ -52,10 +82,18 @@
5282
} else { \
5383
snprintf(__name, sizeof(__name), "OSDP: PD-%d", pd->address);\
5484
} \
55-
logger_set_name(&__log_ctx, __name); \
56-
__logger_log(&__log_ctx, _level, __FILE__, __LINE__, __VA_ARGS__);\
85+
logger_set_name(&__log_ctx, __name); \
86+
if (__ctx->logger.cb) { \
87+
osdp_log_cb_emit_pd(is_cp_mode(pd), pd->address, \
88+
_level, __FILE__, __LINE__, \
89+
__VA_ARGS__); \
90+
break; \
91+
} \
92+
__logger_log(&__log_ctx, _level, __FILE__, __LINE__, __VA_ARGS__);\
5793
} while (0)
5894

95+
#endif /* OPT_OSDP_LOG_MINIMAL */
96+
5997
#define LOG_EM(...) OSDP_PD_LOG(LOG_EMERG, __VA_ARGS__)
6098
#define LOG_ALERT(...) OSDP_PD_LOG(LOG_ALERT, __VA_ARGS__)
6199
#define LOG_CRIT(...) OSDP_PD_LOG(LOG_CRIT, __VA_ARGS__)
@@ -436,14 +474,16 @@ struct osdp {
436474
struct osdp_pd *pd; /* base of PD list (must be at lest one) */
437475
struct osdp_channel channel; /* OSDP channel */
438476
uint8_t tx_buf[OSDP_PACKET_BUF_SIZE];
439-
/* logger context (from utils/logger.h) */
440-
logger_t logger;
441477

442478
/* CP event callback to app with opaque arg pointer as passed by app */
443479
void *event_callback_arg;
444480
cp_event_callback_t event_callback;
445481
void *command_completion_callback_arg;
446482
cp_command_completion_callback_t command_completion_callback;
483+
484+
#ifndef OPT_OSDP_LOG_MINIMAL
485+
logger_t logger; /* logger context (from utils/logger.h) */
486+
#endif
447487
};
448488

449489
void osdp_keyset_complete(struct osdp_pd *pd);

src/osdp_cp.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1532,7 +1532,10 @@ osdp_t *osdp_cp_setup(const struct osdp_channel *channel, int num_pd,
15321532
}
15331533

15341534
input_check_init(ctx);
1535+
1536+
#ifndef OPT_OSDP_LOG_MINIMAL
15351537
logger_get_default(&ctx->logger);
1538+
#endif
15361539
memcpy(&ctx->channel, channel, sizeof(ctx->channel));
15371540

15381541
if (num_pd && cp_add_pd(ctx, num_pd, info)) {

src/osdp_file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ int osdp_file_cmd_tx_build(struct osdp_pd *pd, uint8_t *buf, int max_len)
6969
BUG_ON(f->state != OSDP_FILE_INPROG && f->state != OSDP_FILE_KEEP_ALIVE);
7070

7171
if ((size_t)max_len <= FILE_TRANSFER_HEADER_SIZE) {
72-
LOG_ERR("TX_Build: insufficient space; need:%zu have:%d",
72+
LOG_ERR("TX_Build: insufficient space; need:%d have:%d",
7373
FILE_TRANSFER_HEADER_SIZE, max_len);
7474
goto reply_abort;
7575
}

src/osdp_pd.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,7 +1258,10 @@ osdp_t *osdp_pd_setup(struct osdp_channel *channel, const osdp_pd_info_t *info)
12581258

12591259
input_check_init(ctx);
12601260
ctx->_num_pd = 1;
1261+
1262+
#ifndef OPT_OSDP_LOG_MINIMAL
12611263
logger_get_default(&ctx->logger);
1264+
#endif
12621265

12631266
SET_CURRENT_PD(ctx, 0);
12641267
pd = osdp_to_pd(ctx, 0);

0 commit comments

Comments
 (0)