BSL v1.0.0 - 16.g9d98179
AMMOS Bundle Protocol Security Library (BSL)
Loading...
Searching...
No Matches
log.c File Reference

Logging implementation for the Mock BPA. More...

#include "log.h"
#include <BPSecLib_Private.h>
#include <BSLConfig.h>
#include <pthread.h>
#include <stdarg.h>
#include <stdio.h>
#include <strings.h>
#include <syslog.h>
#include <sys/time.h>
#include <time.h>
#include <m-buffer.h>
#include <m-string.h>
#include <m-atomic.h>
+ Include dependency graph for log.c:

Data Structures

struct  mock_bpa_LogEvent_event_t
 A single event for the log. More...
 

Macros

#define MOCK_BPA_LOG_QUEUE_SIZE   100
 Number of events to buffer to I/O thread.
 
#define M_OPL_mock_bpa_LogEvent_event_t()
 OPLIST for mock_bpa_LogEvent_event_t.
 

Functions

static void mock_bpa_LogEvent_event_init (mock_bpa_LogEvent_event_t *obj)
 
static void mock_bpa_LogEvent_event_deinit (mock_bpa_LogEvent_event_t *obj)
 
static void mock_bpa_LogEvent_event_init_set (mock_bpa_LogEvent_event_t *obj, const mock_bpa_LogEvent_event_t *src)
 
static void mock_bpa_LogEvent_event_init_move (mock_bpa_LogEvent_event_t *obj, mock_bpa_LogEvent_event_t *src)
 
static void mock_bpa_LogEvent_event_set (mock_bpa_LogEvent_event_t *obj, const mock_bpa_LogEvent_event_t *src)
 
static void write_log (const mock_bpa_LogEvent_event_t *event)
 
static void * work_sink (void *arg)
 
void mock_bpa_LogOpen (void)
 Opens the event log.
 
void mock_bpa_LogClose (void)
 Closes the event log.
 
int mock_bpa_LogGetSeverity (int *severity, const char *name)
 Interpret a text name as a severity level.
 
void mock_bpa_LogSetLeastSeverity (int severity)
 Set the least severity enabled for logging.
 
bool mock_bpa_LogIsEnabledFor (int severity)
 Interface for BSL_HostDescriptors_t::log_is_enabled_for.
 
void mock_bpa_LogEvent (const struct timeval *timestamp, int severity, const char *filename, int lineno, const char *funcname, const char *format, va_list args)
 Interface for BSL_HostDescriptors_t::log_event.
 

Variables

static const char * sev_names []
 
static atomic_int least_severity = LOG_DEBUG
 Shared least severity.
 
static mock_bpa_LogEvent_queue_t event_queue
 Shared safe queue.
 
static pthread_t thr_sink
 Sink thread ID.
 
static atomic_bool thr_valid = ATOMIC_VAR_INIT(false)
 True if thr_sink is valid.
 

Detailed Description

Logging implementation for the Mock BPA.

This uses the stderr output stream in a work thread to ensure thread safety of event sources.

Macro Definition Documentation

◆ M_OPL_mock_bpa_LogEvent_event_t

#define M_OPL_mock_bpa_LogEvent_event_t ( )
Value:
static void mock_bpa_LogEvent_event_init(mock_bpa_LogEvent_event_t *obj)
Definition log.c:75
static void mock_bpa_LogEvent_event_deinit(mock_bpa_LogEvent_event_t *obj)
Definition log.c:84
static void mock_bpa_LogEvent_event_init_set(mock_bpa_LogEvent_event_t *obj, const mock_bpa_LogEvent_event_t *src)
Definition log.c:90
static void mock_bpa_LogEvent_event_init_move(mock_bpa_LogEvent_event_t *obj, mock_bpa_LogEvent_event_t *src)
Definition log.c:99
static void mock_bpa_LogEvent_event_set(mock_bpa_LogEvent_event_t *obj, const mock_bpa_LogEvent_event_t *src)
Definition log.c:108

OPLIST for mock_bpa_LogEvent_event_t.

◆ MOCK_BPA_LOG_QUEUE_SIZE

#define MOCK_BPA_LOG_QUEUE_SIZE   100

Number of events to buffer to I/O thread.

Function Documentation

◆ mock_bpa_LogClose()

void mock_bpa_LogClose ( void  )

◆ mock_bpa_LogEvent()

void mock_bpa_LogEvent ( const struct timeval *  timestamp,
int  severity,
const char *  filename,
int  lineno,
const char *  funcname,
const char *  format,
va_list  args 
)

◆ mock_bpa_LogEvent_event_deinit()

◆ mock_bpa_LogEvent_event_init()

◆ mock_bpa_LogEvent_event_init_move()

◆ mock_bpa_LogEvent_event_init_set()

◆ mock_bpa_LogEvent_event_set()

◆ mock_bpa_LogGetSeverity()

int mock_bpa_LogGetSeverity ( int *  severity,
const char *  name 
)

Interpret a text name as a severity level.

Parameters
[out]severityThe associated severity level.
[in]nameThe text name, which is case insensitive.
Returns
Zero if successful.

References BSL_CHKERR1, and sev_names.

◆ mock_bpa_LogIsEnabledFor()

bool mock_bpa_LogIsEnabledFor ( int  severity)

◆ mock_bpa_LogOpen()

void mock_bpa_LogOpen ( void  )

Opens the event log.

Note
This should be called once per process, not thread or library instance. At the end of the process there should be a call to mock_bpa_LogClose().

This is a mimic to POSIX openlog()

References event_queue, mock_bpa_LogEvent_event_t::message, MOCK_BPA_LOG_QUEUE_SIZE, mock_bpa_LogEvent_event_deinit(), mock_bpa_LogEvent_event_init(), mock_bpa_LogEvent_event_t::severity, thr_sink, thr_valid, work_sink(), and write_log().

Referenced by LLVMFuzzerInitialize(), main(), and suiteSetUp().

◆ mock_bpa_LogSetLeastSeverity()

void mock_bpa_LogSetLeastSeverity ( int  severity)

Set the least severity enabled for logging.

Other events will be dropped by the logging facility. This function is multi-thread safe.

Parameters
severityThe severity from a subset of the POSIX syslog values.

References least_severity.

Referenced by LLVMFuzzerInitialize(), and suiteSetUp().

◆ work_sink()

static void * work_sink ( void *  arg)
static

◆ write_log()

static void write_log ( const mock_bpa_LogEvent_event_t event)
static

Variable Documentation

◆ event_queue

mock_bpa_LogEvent_queue_t event_queue
static

Shared safe queue.

Referenced by mock_bpa_LogClose(), mock_bpa_LogEvent(), mock_bpa_LogOpen(), and work_sink().

◆ least_severity

atomic_int least_severity = LOG_DEBUG
static

Shared least severity.

Referenced by mock_bpa_LogIsEnabledFor(), and mock_bpa_LogSetLeastSeverity().

◆ sev_names

const char* sev_names[]
static
Initial value:
= {
NULL,
NULL,
"CRIT",
"ERROR",
"WARNING",
NULL,
"INFO",
"DEBUG",
}

Referenced by mock_bpa_LogGetSeverity(), and write_log().

◆ thr_sink

pthread_t thr_sink
static

Sink thread ID.

Referenced by mock_bpa_LogClose(), and mock_bpa_LogOpen().

◆ thr_valid

atomic_bool thr_valid = ATOMIC_VAR_INIT(false)
static

True if thr_sink is valid.

Referenced by mock_bpa_LogClose(), mock_bpa_LogEvent(), and mock_bpa_LogOpen().