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+
20131uint16_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+
179292void 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+
196311void 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
205326const char * osdp_get_version ()
0 commit comments