The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Functions | Variables
log.c File Reference

Support functions for logging in FreeRADIUS libraries. More...

#include <freeradius-devel/util/debug.h>
#include <freeradius-devel/util/log.h>
#include <freeradius-devel/util/print.h>
#include <freeradius-devel/util/sbuff.h>
#include <freeradius-devel/util/syserror.h>
#include <freeradius-devel/util/atexit.h>
#include <freeradius-devel/util/value.h>
#include <fcntl.h>
+ Include dependency graph for log.c:

Go to the source code of this file.

Functions

static int _fr_log_pool_free (void *arg)
 Cleanup the memory pool used by vlog_request. More...
 
static int _restore_std_legacy (UNUSED int sig)
 On fault, reset STDOUT and STDERR to something useful. More...
 
static ssize_t _syslog_write (UNUSED void *cookie, const char *buf, size_t size)
 Write complete lines to syslog. More...
 
void fr_canonicalize_error (TALLOC_CTX *ctx, char **sp, char **text, ssize_t slen, char const *fmt)
 Canonicalize error strings, removing tabs, and generate spaces for error marker. More...
 
void fr_log (fr_log_t const *log, fr_log_type_t type, char const *file, int line, char const *fmt,...)
 Send a server log message to its destination. More...
 
int fr_log_close (fr_log_t *log)
 Universal close function for all logging destinations. More...
 
void fr_log_fd_event (UNUSED fr_event_list_t *el, int fd, UNUSED int flags, void *uctx)
 Function to provide as the readable callback to the event loop. More...
 
void fr_log_global_free (void)
 Restores the original stdout and stderr FDs, closes the pipes and removes them from the event loop. More...
 
int fr_log_global_init (fr_event_list_t *el, bool daemonize)
 Manipulate stderr and stdout so that was capture all data send to it from libraries. More...
 
void fr_log_hex (fr_log_t const *log, fr_log_type_t type, char const *file, int line, uint8_t const *data, size_t data_len, char const *line_prefix_fmt,...)
 Print out hex block. More...
 
void fr_log_hex_marker (fr_log_t const *log, fr_log_type_t type, char const *file, int line, uint8_t const *data, size_t data_len, ssize_t marker_idx, char const *marker, char const *line_prefix_fmt,...)
 Print out hex block. More...
 
int fr_log_init_file (fr_log_t *log, char const *file)
 Initialise a file logging destination. More...
 
int fr_log_init_fp (fr_log_t *log, FILE *fp)
 Initialise a file logging destination to a FILE*. More...
 
int fr_log_init_func (fr_log_t *log, cookie_write_function_t write, cookie_close_function_t close, void *uctx)
 Initialise a function based logging destination. More...
 
int fr_log_init_legacy (fr_log_t *log, bool daemonize)
 Initialise file descriptors based on logging destination. More...
 
int fr_log_init_std (fr_log_t *log, fr_log_dst_t dst_type)
 Initialise log dst for stdout, stderr or /dev/null. More...
 
int fr_log_init_syslog (fr_log_t *log)
 Initialise a syslog logging destination. More...
 
void fr_log_marker (fr_log_t const *log, fr_log_type_t type, char const *file, int line, char const *str, size_t str_len, ssize_t marker_idx, char const *marker, char const *line_prefix_fmt,...)
 Print out an error marker. More...
 
void fr_log_perror (fr_log_t const *log, fr_log_type_t type, char const *file, int line, fr_log_perror_format_t const *rules, char const *fmt,...)
 Drain any outstanding messages from the fr_strerror buffers. More...
 
TALLOC_CTX * fr_log_pool_init (void)
 talloc ctx to use when composing log messages More...
 
void fr_vlog (fr_log_t const *log, fr_log_type_t type, char const *file, int line, char const *fmt, va_list ap)
 Send a server log message to its destination. More...
 
void fr_vlog_perror (fr_log_t const *log, fr_log_type_t type, char const *file, int line, fr_log_perror_format_t const *f_rules, char const *fmt, va_list ap)
 Drain any outstanding messages from the fr_strerror buffers. More...
 

Variables

static fr_table_num_ordered_t const colours []
 Maps log categories to VT100 style/colour escape sequences. More...
 
static size_t colours_len = NUM_ELEMENTS(colours)
 
fr_log_t default_log
 
static FILE * devnull
 File handle for /dev/null. More...
 
int fr_debug_lvl = 0
 
FILE * fr_log_fp = NULL
 
fr_table_num_ordered_t const fr_log_levels []
 Maps log categories to message prefixes. More...
 
size_t fr_log_levels_len = NUM_ELEMENTS(fr_log_levels)
 
static _Thread_local TALLOC_CTX * fr_log_pool
 
bool fr_log_rate_limit = true
 Whether repeated log entries should be rate limited. More...
 
static uint32_t location_indent = 30
 
bool log_dates_utc = false
 
static fr_event_list_tlog_el
 Event loop we use for process logging data. More...
 
static _Thread_local fr_log_type_t log_msg_type
 The type of the last message logged. More...
 
static fr_log_fd_event_ctx_t stderr_ctx
 Logging ctx for stderr. More...
 
static int stderr_fd = -1
 The original unmolested stderr file descriptor. More...
 
static int stderr_pipe [2]
 Pipe we use to transport stderr data. More...
 
static fr_log_fd_event_ctx_t stdout_ctx
 Logging ctx for stdout. More...
 
static int stdout_fd = -1
 The original unmolested stdout file descriptor. More...
 
static int stdout_pipe [2]
 Pipe we use to transport stdout data. More...
 

VT100 escape sequences

These sequences may be written to VT100 terminals to change the colour and style of the text.

fprintf(stdout, VTC_RED "This text will be coloured red" VTC_RESET);
#define VTC_RED
Colour following text red.
Definition: log.c:268
#define VTC_RESET
Reset terminal text to default style/colour.
Definition: log.c:271
#define VTC_BOLD   "\x1b[1m"
 Embolden following text. More...
 
#define VTC_RED   "\x1b[31m"
 Colour following text red. More...
 
#define VTC_RESET   "\x1b[0m"
 Reset terminal text to default style/colour. More...
 
#define VTC_YELLOW   "\x1b[33m"
 Colour following text yellow. More...
 

Detailed Description

Support functions for logging in FreeRADIUS libraries.

Definition in file log.c.

Macro Definition Documentation

◆ VTC_BOLD

#define VTC_BOLD   "\x1b[1m"

Embolden following text.

Definition at line 270 of file log.c.

◆ VTC_RED

#define VTC_RED   "\x1b[31m"

Colour following text red.

Definition at line 268 of file log.c.

◆ VTC_RESET

#define VTC_RESET   "\x1b[0m"

Reset terminal text to default style/colour.

Definition at line 271 of file log.c.

◆ VTC_YELLOW

#define VTC_YELLOW   "\x1b[33m"

Colour following text yellow.

Definition at line 269 of file log.c.

Function Documentation

◆ _fr_log_pool_free()

static int _fr_log_pool_free ( void *  arg)
static

Cleanup the memory pool used by vlog_request.

Definition at line 301 of file log.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _restore_std_legacy()

static int _restore_std_legacy ( UNUSED int  sig)
static

On fault, reset STDOUT and STDERR to something useful.

Returns
0

Definition at line 904 of file log.c.

+ Here is the caller graph for this function:

◆ _syslog_write()

static ssize_t _syslog_write ( UNUSED void *  cookie,
const char *  buf,
size_t  size 
)
static

Write complete lines to syslog.

Definition at line 1114 of file log.c.

+ Here is the caller graph for this function:

◆ fr_canonicalize_error()

void fr_canonicalize_error ( TALLOC_CTX *  ctx,
char **  sp,
char **  text,
ssize_t  slen,
char const *  fmt 
)

Canonicalize error strings, removing tabs, and generate spaces for error marker.

Note
talloc_free must be called on the buffer returned in spaces and text

Used to produce error messages such as this:

 I'm a string with a parser # error
                            ^ Unexpected character in string

With code resembling this:

ERROR("%s", parsed_str);
ERROR("%s^ %s", space, text);
#define ERROR(fmt,...)
Definition: dhcpclient.c:41
Todo:
merge with above function (log_request_marker)
Parameters
spWhere to write a dynamically allocated buffer of spaces used to indent the error text.
textWhere to write the canonicalized version of fmt (the error text).
ctxto allocate the spaces and text buffers in.
slenof error marker. Expects negative integer value, as returned by parse functions.
fmtto canonicalize.

Definition at line 89 of file log.c.

+ Here is the caller graph for this function:

◆ fr_log()

void fr_log ( fr_log_t const *  log,
fr_log_type_t  type,
char const *  file,
int  line,
char const *  fmt,
  ... 
)

Send a server log message to its destination.

Parameters
logdestination.
typeof log message.
filewhere the log message originated
linewhere the log message originated
fmtwith printf style substitution tokens.
...Substitution arguments.

Definition at line 599 of file log.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_log_close()

int fr_log_close ( fr_log_t log)

Universal close function for all logging destinations.

Definition at line 1199 of file log.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_log_fd_event()

void fr_log_fd_event ( UNUSED fr_event_list_t el,
int  fd,
UNUSED int  flags,
void *  uctx 
)

Function to provide as the readable callback to the event loop.

Writes any data read from a file descriptor to the request log, tries very hard not to chop lines in the middle, but will split at 1024 byte boundaries if forced to.

Parameters
[in]elUNUSED
[in]fdUNUSED
[in]flagsUNUSED
[in]uctxPointer to a log_fd_event_ctx_t

Definition at line 178 of file log.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_log_global_free()

void fr_log_global_free ( void  )

Restores the original stdout and stderr FDs, closes the pipes and removes them from the event loop.

Definition at line 1354 of file log.c.

+ Here is the call graph for this function:

◆ fr_log_global_init()

int fr_log_global_init ( fr_event_list_t el,
bool  daemonize 
)

Manipulate stderr and stdout so that was capture all data send to it from libraries.

Parameters
[in]elThe event list we use to process logging data.
[in]daemonizeWhether the server is starting as a daemon.
Returns
  • 0 on success.
  • -1 on failure.

Definition at line 1235 of file log.c.

+ Here is the call graph for this function:

◆ fr_log_hex()

void fr_log_hex ( fr_log_t const *  log,
fr_log_type_t  type,
char const *  file,
int  line,
uint8_t const *  data,
size_t  data_len,
char const *  line_prefix_fmt,
  ... 
)

Print out hex block.

Parameters
[in]logdestination.
[in]typeof log message.
[in]filesrc file the log message was generated in.
[in]linenumber the log message was generated on.
[in]datato print.
[in]data_lenlength of data.
[in]line_prefix_fmtPrefix to add to the marker messages.
[in]...Arguments for line_prefix_fmt.

Definition at line 800 of file log.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_log_hex_marker()

void fr_log_hex_marker ( fr_log_t const *  log,
fr_log_type_t  type,
char const *  file,
int  line,
uint8_t const *  data,
size_t  data_len,
ssize_t  marker_idx,
char const *  marker,
char const *  line_prefix_fmt,
  ... 
)

Print out hex block.

Parameters
[in]logdestination.
[in]typeof log message.
[in]filesrc file the log message was generated in.
[in]linenumber the log message was generated on.
[in]datato print.
[in]data_lenlength of data.
[in]marker_idxWhere to place the marker. May be negative.
[in]markertext to print at marker_idx.
[in]line_prefix_fmtPrefix to add to the marker messages.
[in]...Arguments for line_prefix_fmt.

Definition at line 847 of file log.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_log_init_file()

int fr_log_init_file ( fr_log_t log,
char const *  file 
)

Initialise a file logging destination.

Parameters
[out]logDestination to initialise.
[in]fileto open handle for.
Returns
  • 0 on success.
  • -1 on failure.

Definition at line 1099 of file log.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_log_init_fp()

int fr_log_init_fp ( fr_log_t log,
FILE *  fp 
)

Initialise a file logging destination to a FILE*.

Parameters
[out]logDestination to initialise.
[in]fppre-existing handle
Returns
  • 0 on success.
  • -1 on failure.

Definition at line 1078 of file log.c.

+ Here is the caller graph for this function:

◆ fr_log_init_func()

int fr_log_init_func ( fr_log_t log,
cookie_write_function_t  write,
cookie_close_function_t  close,
void *  uctx 
)

Initialise a function based logging destination.

Note
Cookie functions receive the fr_log_t which contains the uctx, not the uctx directly.
Parameters
[out]logDestination to initialise.
[in]writeCalled when a complete log line is ready for writing.
[in]closeMay be NULL. Called when the logging destination has been closed.
[in]uctxfor the write and close functions.
Returns
  • 0 on success.
  • -1 on failure.

Definition at line 1175 of file log.c.

+ Here is the call graph for this function:

◆ fr_log_init_legacy()

int fr_log_init_legacy ( fr_log_t log,
bool  daemonize 
)

Initialise file descriptors based on logging destination.

Parameters
logLogger to manipulate.
daemonizeWhether the server is starting as a daemon.
Returns
  • 0 on success.
  • -1 on failure.

Definition at line 923 of file log.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_log_init_std()

int fr_log_init_std ( fr_log_t log,
fr_log_dst_t  dst_type 
)

Initialise log dst for stdout, stderr or /dev/null.

Parameters
[out]logDestination to initialise.
[in]dst_typeThe specific type of log destination to initialise.
Returns
  • 0 on success.
  • -1 on failure.

Definition at line 1044 of file log.c.

+ Here is the caller graph for this function:

◆ fr_log_init_syslog()

int fr_log_init_syslog ( fr_log_t log)

Initialise a syslog logging destination.

Parameters
[out]logDestination to initialise.
Returns
  • 0 on success.
  • -1 on failure.

Definition at line 1145 of file log.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_log_marker()

void fr_log_marker ( fr_log_t const *  log,
fr_log_type_t  type,
char const *  file,
int  line,
char const *  str,
size_t  str_len,
ssize_t  marker_idx,
char const *  marker,
char const *  line_prefix_fmt,
  ... 
)

Print out an error marker.

Parameters
[in]logdestination.
[in]typeof log message.
[in]filesrc file the log message was generated in.
[in]linenumber the log message was generated on.
[in]strSubject string we're printing a marker for.
[in]str_lenSubject string length. Use SIZE_MAX for the length of the string.
[in]marker_idxWhere to place the marker. May be negative.
[in]markertext to print at marker_idx.
[in]line_prefix_fmtPrefix to add to the marker messages.
[in]...Arguments for line_prefix_fmt.

Definition at line 750 of file log.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_log_perror()

void fr_log_perror ( fr_log_t const *  log,
fr_log_type_t  type,
char const *  file,
int  line,
fr_log_perror_format_t const *  rules,
char const *  fmt,
  ... 
)

Drain any outstanding messages from the fr_strerror buffers.

This function drains any messages from fr_strerror buffer adding a prefix (fmt) to the first message.

Parameters
[in]logdestination.
[in]typeof log message.
[in]filesrc file the log message was generated in.
[in]linenumber the log message was generated on.
[in]rulesfor printing multiline errors.
[in]fmtwith printf style substitution tokens.
[in]...Substitution arguments.

Definition at line 725 of file log.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_log_pool_init()

TALLOC_CTX* fr_log_pool_init ( void  )

talloc ctx to use when composing log messages

Functions must ensure that they allocate a new ctx from the one returned here, and that this ctx is freed before the function returns.

Returns
talloc pool to use for scratch space.

Definition at line 315 of file log.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_vlog()

void fr_vlog ( fr_log_t const *  log,
fr_log_type_t  type,
char const *  file,
int  line,
char const *  fmt,
va_list  ap 
)

Send a server log message to its destination.

Parameters
[in]logdestination.
[in]typeof log message.
[in]filesrc file the log message was generated in.
[in]linenumber the log message was generated on.
[in]fmtwith printf style substitution tokens.
[in]apSubstitution arguments.

Definition at line 343 of file log.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_vlog_perror()

void fr_vlog_perror ( fr_log_t const *  log,
fr_log_type_t  type,
char const *  file,
int  line,
fr_log_perror_format_t const *  f_rules,
char const *  fmt,
va_list  ap 
)

Drain any outstanding messages from the fr_strerror buffers.

This function drains any messages from fr_strerror buffer prefixing the first message with fmt + args.

If a prefix is specified in rules, this is prepended to all lines logged. The prefix is useful for adding context, i.e. configuration file and line number information.

Parameters
[in]logdestination.
[in]typeof log message.
[in]filesrc file the log message was generated in.
[in]linenumber the log message was generated on.
[in]f_rulesfor printing multiline errors.
[in]fmtwith printf style substitution tokens.
[in]apSubstitution arguments.

Definition at line 630 of file log.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ colours

fr_table_num_ordered_t const colours[]
static
Initial value:
= {
{ L(VTC_BOLD), L_INFO },
{ L(VTC_RED), L_ERR },
}
#define L(_str)
Helper for initialising arrays of string literals.
Definition: build.h:207
#define VTC_YELLOW
Colour following text yellow.
Definition: log.c:269
#define VTC_BOLD
Embolden following text.
Definition: log.c:270
@ L_DBG_WARN_REQ
Less severe warning only displayed when debugging is enabled.
Definition: log.h:63
@ L_WARN
Warning.
Definition: log.h:57
@ L_ERR
Error message.
Definition: log.h:56
@ L_DBG_ERR
Error only displayed when debugging is enabled.
Definition: log.h:62
@ L_DBG_ERR_REQ
Less severe error only displayed when debugging is enabled.
Definition: log.h:64
@ L_DBG_WARN
Warning only displayed when debugging is enabled.
Definition: log.h:61
@ L_INFO
Informational message.
Definition: log.h:55

Maps log categories to VT100 style/colour escape sequences.

Definition at line 276 of file log.c.

◆ colours_len

size_t colours_len = NUM_ELEMENTS(colours)
static

Definition at line 285 of file log.c.

◆ default_log

fr_log_t default_log
Initial value:
= {
.colourise = false,
.fd = STDOUT_FILENO,
.dst = L_DST_STDOUT,
.file = NULL,
.timestamp = L_TIMESTAMP_AUTO
}
@ L_DST_STDOUT
Log to stdout.
Definition: log.h:78
@ L_TIMESTAMP_AUTO
Timestamp logging preference not specified.
Definition: log.h:88

Definition at line 290 of file log.c.

◆ devnull

FILE* devnull
static

File handle for /dev/null.

Definition at line 58 of file log.c.

◆ fr_debug_lvl

int fr_debug_lvl = 0

Definition at line 42 of file log.c.

◆ fr_log_fp

FILE* fr_log_fp = NULL

Definition at line 41 of file log.c.

◆ fr_log_levels

fr_table_num_ordered_t const fr_log_levels[]
Initial value:
= {
{ L("Debug : "), L_DBG },
{ L("Info : "), L_INFO },
{ L("Warn : "), L_WARN },
{ L("Error : "), L_ERR },
{ L("Auth : "), L_AUTH },
{ L("INFO : "), L_DBG_INFO },
{ L("WARN : "), L_DBG_WARN },
{ L("ERROR : "), L_DBG_ERR },
{ L("WARN : "), L_DBG_WARN_REQ },
{ L("ERROR : "), L_DBG_ERR_REQ }
}
@ L_DBG_INFO
Info only displayed when debugging is enabled.
Definition: log.h:60
@ L_AUTH
Authentication logs.
Definition: log.h:58
@ L_DBG
Only displayed when debugging is enabled.
Definition: log.h:59

Maps log categories to message prefixes.

Definition at line 244 of file log.c.

◆ fr_log_levels_len

size_t fr_log_levels_len = NUM_ELEMENTS(fr_log_levels)

Definition at line 256 of file log.c.

◆ fr_log_pool

_Thread_local TALLOC_CTX* fr_log_pool
static

Definition at line 44 of file log.c.

◆ fr_log_rate_limit

bool fr_log_rate_limit = true

Whether repeated log entries should be rate limited.

Definition at line 60 of file log.c.

◆ location_indent

uint32_t location_indent = 30
static

Definition at line 46 of file log.c.

◆ log_dates_utc

bool log_dates_utc = false

Definition at line 288 of file log.c.

◆ log_el

fr_event_list_t* log_el
static

Event loop we use for process logging data.

Definition at line 47 of file log.c.

◆ log_msg_type

_Thread_local fr_log_type_t log_msg_type
static

The type of the last message logged.

Mainly uses for syslog.

Definition at line 62 of file log.c.

◆ stderr_ctx

fr_log_fd_event_ctx_t stderr_ctx
static

Logging ctx for stderr.

Definition at line 53 of file log.c.

◆ stderr_fd

int stderr_fd = -1
static

The original unmolested stderr file descriptor.

Definition at line 49 of file log.c.

◆ stderr_pipe

int stderr_pipe[2]
static

Pipe we use to transport stderr data.

Definition at line 56 of file log.c.

◆ stdout_ctx

fr_log_fd_event_ctx_t stdout_ctx
static

Logging ctx for stdout.

Definition at line 52 of file log.c.

◆ stdout_fd

int stdout_fd = -1
static

The original unmolested stdout file descriptor.

Definition at line 50 of file log.c.

◆ stdout_pipe

int stdout_pipe[2]
static

Pipe we use to transport stdout data.

Definition at line 55 of file log.c.