3535
3636#include <m-buffer.h>
3737#include <m-string.h>
38+ #include <m-atomic.h>
3839
3940/// Number of events to buffer to I/O thread
4041#define BSL_LOG_QUEUE_SIZE 100
@@ -82,14 +83,37 @@ static void BSL_LogEvent_event_deinit(BSL_LogEvent_event_t *obj)
8283 string_clear (obj -> context );
8384}
8485
86+ static void BSL_LogEvent_event_init_set (BSL_LogEvent_event_t * obj , const BSL_LogEvent_event_t * src )
87+ {
88+ obj -> thread = src -> thread ;
89+ obj -> timestamp = src -> timestamp ;
90+ obj -> severity = src -> severity ;
91+ string_init_set (obj -> context , src -> message );
92+ string_init_set (obj -> message , src -> message );
93+ }
94+
95+ static void BSL_LogEvent_event_set (BSL_LogEvent_event_t * obj , const BSL_LogEvent_event_t * src )
96+ {
97+ obj -> thread = src -> thread ;
98+ obj -> timestamp = src -> timestamp ;
99+ obj -> severity = src -> severity ;
100+ string_set (obj -> context , src -> message );
101+ string_set (obj -> message , src -> message );
102+ }
103+
85104/// OPLIST for BSL_LogEvent_event_t
86- #define M_OPL_BSL_LogEvent_event_t () (INIT(API_2(BSL_LogEvent_event_init)), CLEAR(API_2(BSL_LogEvent_event_deinit)))
105+ #define M_OPL_BSL_LogEvent_event_t () \
106+ (INIT(API_2(BSL_LogEvent_event_init)), INIT_SET(API_6(BSL_LogEvent_event_init_set)), \
107+ SET(API_6(BSL_LogEvent_event_set)), CLEAR(API_2(BSL_LogEvent_event_deinit)))
87108
88109// NOLINTBEGIN
89110/// @cond Doxygen_Suppress
90111M_BUFFER_DEF (BSL_LogEvent_queue , BSL_LogEvent_event_t , BSL_LOG_QUEUE_SIZE , M_BUFFER_THREAD_SAFE | M_BUFFER_BLOCKING )
91112/// @endcond
92113
114+ /// Shared least severity
115+ static atomic_int least_severity = LOG_DEBUG ;
116+
93117/// Shared safe queue
94118static BSL_LogEvent_queue_t event_queue ;
95119/// Sink thread ID
@@ -159,14 +183,12 @@ static void *work_sink(void *arg _U_)
159183 while (true)
160184 {
161185 BSL_LogEvent_event_t event ;
186+ BSL_LogEvent_event_init (& event );
162187 BSL_LogEvent_queue_pop (& event , event_queue );
163- if (string_empty_p (event .message ))
188+ if (! string_empty_p (event .message ))
164189 {
165- BSL_LogEvent_event_deinit (& event );
166- break ;
190+ write_log (& event );
167191 }
168-
169- write_log (& event );
170192 BSL_LogEvent_event_deinit (& event );
171193 }
172194 return NULL ;
@@ -198,6 +220,7 @@ void BSL_closelog(void)
198220 BSL_LogEvent_event_t event ;
199221 BSL_LogEvent_event_init (& event );
200222 BSL_LogEvent_queue_push (event_queue , event );
223+ BSL_LogEvent_event_deinit (& event );
201224
202225 int res = pthread_join (thr_sink , NULL );
203226 if (res )
@@ -216,10 +239,54 @@ void BSL_closelog(void)
216239 }
217240}
218241
242+ int BSL_LogGetSeverity (int * severity , const char * name )
243+ {
244+ CHKERR1 (severity )
245+ CHKERR1 (name )
246+
247+ for (size_t ix = 0 ; ix < sizeof (sev_names ) / sizeof (const char * ); ++ ix )
248+ {
249+ if (!sev_names [ix ])
250+ {
251+ continue ;
252+ }
253+ if (strcasecmp (sev_names [ix ], name ) == 0 )
254+ {
255+ * severity = (int )ix ;
256+ return 0 ;
257+ }
258+ }
259+ return 2 ;
260+ }
261+
262+ void BSL_LogSetLeastSeverity (int severity )
263+ {
264+ if ((severity < 0 ) || (severity > LOG_DEBUG ))
265+ {
266+ return ;
267+ }
268+
269+ atomic_store (& least_severity , severity );
270+ }
271+
272+ bool BSL_LogIsEnabledFor (int severity )
273+ {
274+ if ((severity < 0 ) || (severity > LOG_DEBUG ))
275+ {
276+ return false;
277+ }
278+
279+ const int limit = atomic_load (& least_severity );
280+ // lower severity has higher define value
281+ const bool enabled = (limit >= severity );
282+
283+ return enabled ;
284+ }
285+
219286// NOLINTBEGIN
220287void BSL_LogEvent (int severity , const char * filename , int lineno , const char * funcname , const char * format , ...)
221288{
222- if ((severity < 0 ) || ( severity > LOG_DEBUG ))
289+ if (! BSL_LogIsEnabledFor (severity ))
223290 {
224291 return ;
225292 }
@@ -251,28 +318,25 @@ void BSL_LogEvent(int severity, const char *filename, int lineno, const char *fu
251318 va_end (val );
252319 }
253320
254- if (string_empty_p (event .message ))
321+ // ignore empty messages
322+ if (!string_empty_p (event .message ))
255323 {
256- // ignore empty messages
257- BSL_LogEvent_event_deinit (& event );
258- return ;
259- }
260-
261- if (atomic_load (& thr_valid ))
262- {
263- BSL_LogEvent_queue_push (event_queue , event );
264- }
265- else
266- {
267- BSL_LogEvent_event_t manual ;
268- BSL_LogEvent_event_init (& manual );
269- manual .severity = LOG_CRIT ;
270- string_set_str (manual .message , "BSL_LogEvent() called before BSL_openlog()" );
271- write_log (& manual );
272- BSL_LogEvent_event_deinit (& manual );
324+ if (atomic_load (& thr_valid ))
325+ {
326+ BSL_LogEvent_queue_push (event_queue , event );
327+ }
328+ else
329+ {
330+ BSL_LogEvent_event_t manual ;
331+ BSL_LogEvent_event_init (& manual );
332+ manual .severity = LOG_CRIT ;
333+ string_set_str (manual .message , "BSL_LogEvent() called before BSL_openlog()" );
334+ write_log (& manual );
335+ BSL_LogEvent_event_deinit (& manual );
273336
274- write_log (& event );
275- BSL_LogEvent_event_deinit ( & event );
337+ write_log (& event );
338+ }
276339 }
340+ BSL_LogEvent_event_deinit (& event );
277341}
278342// NOLINTEND
0 commit comments