Crinit -- Configurable Rootfs Init
logio.c File Reference

(2023-08-30, commit: 737bc27)

Implementation of debug/log output. More...

#include "logio.h"
#include <locale.h>
#include <pthread.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include "globopt.h"
Include dependency graph for logio.c:

Macros

#define CRINIT_ERROR_WITH_FFL_FMT   "(%s:%s:%d) Error: "
 Format string error prefix with function, file and line. More...
 
#define CRINIT_ERRNO_FMT   " Errno: %s"
 Format string errno suffix, to use with strerror(). More...
 
#define CRINIT_SYSLOG_IDENT   "crinit"
 Identification string for crinit logging to syslog. More...
 

Functions

static char * crinitThreadSafeStrerror (int errnum)
 
void crinitSetPrintPrefix (const char *prefix)
 
void crinitSetInfoStream (FILE *stream)
 
void crinitSetErrStream (FILE *stream)
 
void crinitSetUseSyslog (bool sl)
 
void crinitDbgInfoPrint (const char *format,...)
 
void crinitInfoPrint (const char *format,...)
 
void crinitErrPrintFFL (const char *file, const char *func, int line, const char *format,...)
 
void crinitErrnoPrintFFL (const char *file, const char *func, int line, const char *format,...)
 

Variables

static char crinitPrintPrefix [CRINIT_PRINT_PREFIX_MAX_LEN] = CRINIT_PRINT_PREFIX
 
static FILE * crinitInfoStream = NULL
 holds the stream to use for info messages. More...
 
static FILE * crinitErrStream = NULL
 holds the stream to use for error messages. More...
 
static bool crinitUseSyslog = false
 specifies if we should use syslog calls instead of FILE streams. More...
 
static pthread_mutex_t crinitLogLock = PTHREAD_MUTEX_INITIALIZER
 

Detailed Description

Implementation of debug/log output.

Macro Definition Documentation

◆ CRINIT_ERRNO_FMT

#define CRINIT_ERRNO_FMT   " Errno: %s"

Format string errno suffix, to use with strerror().

◆ CRINIT_ERROR_WITH_FFL_FMT

#define CRINIT_ERROR_WITH_FFL_FMT   "(%s:%s:%d) Error: "

Format string error prefix with function, file and line.

◆ CRINIT_SYSLOG_IDENT

#define CRINIT_SYSLOG_IDENT   "crinit"

Identification string for crinit logging to syslog.

Function Documentation

◆ crinitDbgInfoPrint()

void crinitDbgInfoPrint ( const char *  format,
  ... 
)

◆ crinitErrnoPrintFFL()

void crinitErrnoPrintFFL ( const char *  file,
const char *  func,
int  line,
const char *  format,
  ... 
)

◆ crinitErrPrintFFL()

void crinitErrPrintFFL ( const char *  file,
const char *  func,
int  line,
const char *  format,
  ... 
)

◆ crinitInfoPrint()

void crinitInfoPrint ( const char *  format,
  ... 
)

◆ crinitSetErrStream()

void crinitSetErrStream ( FILE *  stream)

Set FILE stream to use for error messages.

Defaults to stderr. This can be used to divert the error messages into a log file. The stream can be the same as the one used for crinitSetInfoStream() if the log should contain both types of messages. The function uses mutexes internally and is thread-safe.

Currently, the config file option to make use of this functionality is not yet implemented.

Parameters
streamThe FILE* stream to use. If NULL, stream is set to stderr.

◆ crinitSetInfoStream()

void crinitSetInfoStream ( FILE *  stream)

Set FILE stream to use for info messages.

Defaults to stdout. This can be used to divert the info messages into a log file. The stream can be the same as the one used for crinitSetErrStream() if the log should contain both types of messages. The function uses mutexes internally and is thread-safe.

Currently, the config file option to make use of this functionality is not yet implemented.

Parameters
streamThe FILE* stream to use. If NULL, stream is set to stdout.

◆ crinitSetPrintPrefix()

void crinitSetPrintPrefix ( const char *  prefix)

Set prefix to put in front of error and info message lines.

Parameters
prefixThe prefix to use.

◆ crinitSetUseSyslog()

void crinitSetUseSyslog ( bool  sl)

Specify if syslog should be used..

By default, Crinit will always use the specified FILE streams. If this is set to true, however, Crinit will output to syslog instead. The log connection will be opened with LOG_CONS, so if the connection fails, output will be sent to the system console instead.

Parameters
sltrue if syslog should be used, false otherwise.

◆ crinitThreadSafeStrerror()

static char * crinitThreadSafeStrerror ( int  errnum)
static

Thread-safe implementation of strerror() using strerror_l().

Always uses the POSIX/C locale to format the readable output.

Parameters
errnumThe error number to explain.
Returns
Pointer to a thread-local static memory location with a fitting error message.

Variable Documentation

◆ crinitErrStream

FILE* crinitErrStream = NULL
static

holds the stream to use for error messages.

◆ crinitInfoStream

FILE* crinitInfoStream = NULL
static

holds the stream to use for info messages.

◆ crinitLogLock

pthread_mutex_t crinitLogLock = PTHREAD_MUTEX_INITIALIZER
static

Mutex synchronizing print output (so that print statements are atomic wrt. to each other).

◆ crinitPrintPrefix

char crinitPrintPrefix[CRINIT_PRINT_PREFIX_MAX_LEN] = CRINIT_PRINT_PREFIX
static

Holds the Prefix to put in front of every printed line, defaults to CRINIT_PRINT_PREFIX

◆ crinitUseSyslog

bool crinitUseSyslog = false
static

specifies if we should use syslog calls instead of FILE streams.