Crinit -- Configurable Rootfs Init
logio.h File Reference

(2023-08-02, commit: 9434b31)

Header related to debug/log output. More...

#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
Include dependency graph for logio.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define CRINIT_PRINT_PREFIX_MAX_LEN   32
 
#define CRINIT_PRINT_PREFIX   "[CRINIT] "
 
#define CRINIT_PRINT_EMPTY_LINE   "%s", ""
 
#define __FILE_BASENAME__   (__builtin_strrchr("/" __FILE__, '/') + 1)
 
#define crinitErrPrint(...)   crinitErrPrintFFL(__FILE_BASENAME__, __func__, __LINE__, __VA_ARGS__)
 
#define crinitErrnoPrint(...)   crinitErrnoPrintFFL(__FILE_BASENAME__, __func__, __LINE__, __VA_ARGS__)
 

Functions

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

Detailed Description

Header related to debug/log output.

Macro Definition Documentation

◆ __FILE_BASENAME__

#define __FILE_BASENAME__   (__builtin_strrchr("/" __FILE__, '/') + 1)

Constant to use instead of FILE which shows only the filename without the leading path.

For gcc from version 12 and clang from version 9, this already exists as a macro called FILE_NAME. If we detect a suitable compiler version, we'll use that, otherwise implement it manually by looking at the last slash in the path. Note, that this precludes us from using escaped slashes in file names (which we should not do anyway).

Using __builtin_strrchr() on a string constant should cause compile-time evaluation of the expressio.

◆ CRINIT_PRINT_EMPTY_LINE

#define CRINIT_PRINT_EMPTY_LINE   "%s", ""

Constant to use if one wishes to output an empty line using an output function, e.g. crinitInfoPrint(CRINIT_PRINT_EMPTY_LINE). The output will still contain everything the output function adds, e.g. prefix, line number, etc.

◆ CRINIT_PRINT_PREFIX

#define CRINIT_PRINT_PREFIX   "[CRINIT] "

Default prefix to put in front of log/info/error messages.

◆ CRINIT_PRINT_PREFIX_MAX_LEN

#define CRINIT_PRINT_PREFIX_MAX_LEN   32

Maximum size of prefix set with crinitSetPrintPrefix()

◆ crinitErrnoPrint

#define crinitErrnoPrint (   ...)    crinitErrnoPrintFFL(__FILE_BASENAME__, __func__, __LINE__, __VA_ARGS__)

Macro to print an error message using crinitErrnoPrintFFL() including the offending source file, function, and line, as well as the value of errno.

◆ crinitErrPrint

#define crinitErrPrint (   ...)    crinitErrPrintFFL(__FILE_BASENAME__, __func__, __LINE__, __VA_ARGS__)

Macro to print an error message including the offending source file, function, and line using crinitErrPrintFFL().

Function Documentation

◆ crinitDbgInfoPrint()

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

Print an info message if DEBUG global option is set.

Can be used like printf(). In contrast to printf(), this function adds the Crinit prefix at the start and a newline at the end. It uses the output stream specified by crinitSetInfoStream() or stdout if unset. There will be no output if the global option DEBUG is set to false using crinitGlobOptSetBoolean(). The function uses mutexes internally and is thread-safe.

If configured (USE_SYSLOG = true) and available (a task has provided syslog), the function will instead write to syslog.

◆ crinitErrnoPrintFFL()

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

Print an error message including a text representation of the current value of errno.

Can be used like printf(). In contrast to printf(), this function adds the Crinit prefix and the given file, func, and line parameters at the start as well as a text representation of errno and a newline at the end. It uses the output stream specified by crinitSetErrStream() or stderr if unset. The function uses mutexes internally and is thread-safe.

The macro crinitErrnoPrint() should be used to provide the function with the correct file, func, and line parameters.

If configured (USE_SYSLOG = true) and available (a task has provided syslog), the function will instead write to syslog.

◆ crinitErrPrintFFL()

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

Print an error message.

Can be used like printf(). In contrast to printf(), this function adds the Crinit prefix and the given file, func, and line parameters at the start as well as a newline at the end. It uses the output stream specified by crinitSetErrStream() or stderr if unset. The function uses mutexes internally and is thread-safe.

The macro crinitErrPrint() should be used to provide the function with the correct file, func, and line parameters.

If configured (USE_SYSLOG = true) and available (a task has provided syslog), the function will instead write to syslog.

◆ crinitInfoPrint()

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

Print an info message.

Can be used like printf(). In contrast to printf(), this function adds the Crinit prefix at the start and a newline at the end. It uses the output stream specified by crinitSetInfoStream() or stdout if unset. The function uses mutexes internally and is thread-safe.

◆ 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.