40#define BSL_LOG_QUEUE_SIZE 100
43static const char *sev_names[] = {
61 struct timeval timestamp;
72 obj->
thread = pthread_self();
86#define M_OPL_BSL_LogEvent_event_t() (INIT(API_2(BSL_LogEvent_event_init)), CLEAR(API_2(BSL_LogEvent_event_deinit)))
98static atomic_bool
thr_valid = ATOMIC_VAR_INIT(false);
103 ASSERT_ARG_NONNULL(dstbuf);
104 ASSERT_ARG_NONNULL(srcbuf);
105 ASSERT_ARG_EXPR(dstlen > 0);
106 ASSERT_ARG_EXPR(srclen > 0);
108 memset(dstbuf, 0, dstlen);
109 const char hex_digits[] =
"0123456789ABCDEF";
110 for (
size_t i = 0; i < srclen && (((i * 2) + 1) < dstlen-1); i++)
112 dstbuf[(i * 2)] = (uint8_t)hex_digits[(srcbuf[i] >> 4) & 0x0F];
113 dstbuf[(i * 2) + 1] = (uint8_t)hex_digits[srcbuf[i] & 0x0F];
121 ASSERT_ARG_NONNULL(event);
124 const char *prioname = sev_names[
event->severity];
128 time_t nowtime =
event->timestamp.tv_sec;
130 gmtime_r(&nowtime, &nowtm);
133 size_t remain =
sizeof(tmbuf) - 1;
134 size_t len = strftime(curs, remain,
"%Y-%m-%dT%H:%M:%S", &nowtm);
137 snprintf(curs, remain,
".%06ld", event->
timestamp.tv_usec);
139 char thrbuf[2 *
sizeof(pthread_t) + 1];
141 const uint8_t *data = (
const void *)&(event->
thread);
143 for (
size_t ix = 0; ix <
sizeof(pthread_t); ++ix)
145 sprintf(out,
"%02X", *data);
151 fprintf(stderr,
"%s T:%s <%s> [%s] %s\n", tmbuf, thrbuf, prioname, string_get_cstr(event->
context),
152 string_get_cstr(event->
message));
163 if (string_empty_p(event.
message))
165 BSL_LogEvent_event_deinit(&event);
170 BSL_LogEvent_event_deinit(&event);
183 BSL_LogEvent_event_init(&manual);
185 string_set_str(manual.
message,
"BSL_openlog() failed");
187 BSL_LogEvent_event_deinit(&manual);
199 BSL_LogEvent_event_init(&event);
202 int res = pthread_join(
thr_sink, NULL);
207 BSL_LogEvent_event_init(&manual);
209 string_set_str(manual.
message,
"BSL_closelog() failed");
211 BSL_LogEvent_event_deinit(&manual);
220void BSL_LogEvent(
int severity,
const char *filename,
int lineno,
const char *funcname,
const char *format, ...)
222 if ((severity < 0) || (severity > LOG_DEBUG))
228 BSL_LogEvent_event_init(&event);
229 event.severity = severity;
233 static const char dirsep =
'/';
235 const char *pos = strrchr(filename, dirsep);
244 string_printf(event.
context,
"%s:%d:%s", pos, lineno, funcname);
249 va_start(val, format);
250 string_vprintf(event.
message, format, val);
254 if (string_empty_p(event.
message))
257 BSL_LogEvent_event_deinit(&event);
268 BSL_LogEvent_event_init(&manual);
270 string_set_str(manual.
message,
"BSL_LogEvent() called before BSL_openlog()");
272 BSL_LogEvent_event_deinit(&manual);
275 BSL_LogEvent_event_deinit(&event);
Single entry-point include file for all of the BPSec Lib (BSL) frontend API.
#define _U_
Mark an unused parameter Within a function definition.
static atomic_bool thr_valid
True if thr_sink is valid.
static void * work_sink(void *arg)
NOLINTEND.
#define BSL_LOG_QUEUE_SIZE
Number of events to buffer to I/O thread.
uint8_t * BSL_Log_DumpAsHexString(uint8_t *dstbuf, size_t dstlen, const uint8_t *srcbuf, size_t srclen)
NOLINTEND.
void BSL_LogEvent(int severity, const char *filename, int lineno, const char *funcname, const char *format,...)
Log an event.
void BSL_openlog(void)
Opens the event log.
static BSL_LogEvent_queue_t event_queue
NOLINTBEGIN.
static void write_log(const BSL_LogEvent_event_t *event)
NOLINTBEGIN.
void BSL_closelog(void)
Closes the event log.
static pthread_t thr_sink
Sink thread ID.
A single event for the log.
string_t message
Fully formatted message.
string_t context
File and function context.
pthread_t thread
Source thread ID.
struct timeval timestamp
Source event timestamp.
int severity
Event severity enumeration.