The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
log.h
Go to the documentation of this file.
1 #pragma once
2 /*
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
16  */
17 /** libfreeradius logging functions
18  *
19  * @file src/lib/util/log.h
20  *
21  * @copyright 2016 The FreeRADIUS server project
22  */
23 RCSIDH(util_log_h, "$Id: 7ec1210e43a6a97e47e483d273c3d8d6a1a46abe $")
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 #include <freeradius-devel/build.h>
30 #include <freeradius-devel/missing.h>
31 #include <freeradius-devel/util/event.h>
32 #include <freeradius-devel/util/fopencookie.h>
33 #include <freeradius-devel/util/table.h>
34 #include <freeradius-devel/util/talloc.h>
35 
36 #include <stdbool.h>
37 #include <stdint.h>
38 #include <stdio.h>
39 #include <string.h>
40 
41 extern FILE *fr_log_fp;
42 
43 /*
44  * Error functions.
45  */
46 void fr_canonicalize_error(TALLOC_CTX *ctx, char **spaces, char **text, ssize_t slen, char const *fmt);
47 
48 extern int fr_debug_lvl; /* 0 = no debugging information */
49 extern bool log_dates_utc;
50 
52 extern size_t fr_log_levels_len;
53 
54 typedef enum {
55  L_INFO = 3, //!< Informational message.
56  L_ERR = 4, //!< Error message.
57  L_WARN = 5, //!< Warning.
58  L_AUTH = 6, //!< Authentication logs
59  L_DBG = 16, //!< Only displayed when debugging is enabled.
60  L_DBG_INFO = 17, //!< Info only displayed when debugging is enabled.
61  L_DBG_WARN = 18, //!< Warning only displayed when debugging is enabled.
62  L_DBG_ERR = 19, //!< Error only displayed when debugging is enabled.
63  L_DBG_WARN_REQ = 20, //!< Less severe warning only displayed when debugging is enabled.
64  L_DBG_ERR_REQ = 21 //!< Less severe error only displayed when debugging is enabled.
66 
67 typedef enum {
68  L_DBG_LVL_DISABLE = -1, //!< Don't print messages.
69  L_DBG_LVL_OFF = 0, //!< No debug messages.
70  L_DBG_LVL_1, //!< Highest priority debug messages (-x).
71  L_DBG_LVL_2, //!< 2nd highest priority debug messages (-xx | -X).
72  L_DBG_LVL_3, //!< 3rd highest priority debug messages (-xxx | -Xx).
73  L_DBG_LVL_4, //!< 4th highest priority debug messages (-xxxx | -Xxx).
74  L_DBG_LVL_MAX //!< Lowest priority debug messages (-xxxxx | -Xxxx).
76 
77 typedef enum {
78  L_DST_STDOUT = 0, //!< Log to stdout.
79  L_DST_FILES, //!< Log to a file on disk.
80  L_DST_SYSLOG, //!< Log to syslog.
81  L_DST_STDERR, //!< Log to stderr.
82  L_DST_FUNC, //!< Send log messages to a FILE*, via fopencookie()
83  L_DST_NULL, //!< Discard log messages.
86 
87 typedef enum {
88  L_TIMESTAMP_AUTO = 0, //!< Timestamp logging preference not specified. Do it based on
89  //!< debug level and destination.
90  L_TIMESTAMP_ON, //!< Always log timestamps.
91  L_TIMESTAMP_OFF //!< Never log timestamps.
93 
94 typedef struct fr_log_s fr_log_t;
95 
96 struct fr_log_s {
97  fr_log_dst_t dst; //!< Log destination.
98 
99  bool line_number; //!< Log src file and line number.
100 
101  bool colourise; //!< Prefix log messages with VT100 escape codes to change text
102  //!< colour.
103 
104  bool dates_utc; //!< Whether timestamps should be UTC or local timezone.
105 
106  bool print_level; //!< sometimes we don't want log levels printed
107 
108  bool suppress_secrets; //!< suppress secrets when printing to this destination
109 
110  fr_log_timestamp_t timestamp; //!< Prefix log messages with timestamps.
111 
112  int fd; //!< File descriptor to write messages to.
113  char const *file; //!< Path to log file.
114 
115  void *cookie; //!< for fopencookie()
116  FILE *handle; //!< Path to log file.
117 
118  ssize_t (*cookie_write)(void *, char const *, size_t); //!< write function
119  void *uctx; //!< User data associated with the fr_log_t.
120 
121  fr_log_t *parent; //!< Log destination this was cloned from.
122 };
123 
124 typedef struct {
125  char const *first_prefix; //!< Prefix for the first line printed.
126  char const *subsq_prefix; //!< Prefix for subsequent lines.
128 
129 /** Context structure for the log fd event function
130  *
131  * This enables a file descriptor to be inserted into an event loop
132  * and produce log output. It's useful for execd child processes
133  * and for capturing stdout/stderr from libraries.
134  */
135 typedef struct {
136  fr_log_t const *dst; //!< Where to log to.
137  fr_log_type_t type; //!< What type of log message it is.
138  fr_log_lvl_t lvl; //!< Priority of the message.
139  char const *prefix; //!< To add to log messages.
141 
142 extern fr_log_t default_log;
143 extern bool fr_log_rate_limit;
144 
145 /** Whether rate limiting is enabled
146  *
147  */
148 static inline bool fr_rate_limit_enabled(void)
149 {
150  if (fr_log_rate_limit || (fr_debug_lvl < 1)) return true;
151 
152  return false;
153 }
154 
155 int fr_log_init_legacy(fr_log_t *log, bool daemonize);
156 
157 void fr_log_fd_event(UNUSED fr_event_list_t *el, int fd, UNUSED int flags, void *uctx);
158 
159 void fr_vlog(fr_log_t const *log, fr_log_type_t lvl, char const *file, int line, char const *fmt, va_list ap)
160  CC_HINT(format (printf, 5, 0)) CC_HINT(nonnull (1,3));
161 
162 void fr_log(fr_log_t const *log, fr_log_type_t lvl, char const *file, int line, char const *fmt, ...)
163  CC_HINT(format (printf, 5, 6)) CC_HINT(nonnull (1,3));
164 
165 void fr_vlog_perror(fr_log_t const *log, fr_log_type_t type,
166  char const *file, int line, fr_log_perror_format_t const *rules, char const *fmt, va_list ap)
167  CC_HINT(format (printf, 6, 0)) CC_HINT(nonnull (1));
168 
169 void fr_log_perror(fr_log_t const *log, fr_log_type_t type,
170  char const *file, int line, fr_log_perror_format_t const *rules, char const *fmt, ...)
171  CC_HINT(format (printf, 6, 7)) CC_HINT(nonnull (1));
172 
173 void fr_log_marker(fr_log_t const *log, fr_log_type_t type, char const *file, int line,
174  char const *str, size_t str_len,
175  ssize_t marker_idx, char const *marker, char const *line_prefix_fmt, ...)
176  CC_HINT(format (printf, 9, 10)) CC_HINT(nonnull (1,3,5,8));
177 
178 void fr_log_hex(fr_log_t const *log, fr_log_type_t type, char const *file, int line,
179  uint8_t const *data, size_t data_len, char const *line_prefix_fmt, ...)
180  CC_HINT(format (printf, 7, 8)) CC_HINT(nonnull (1,3,5));
181 
182 void fr_log_hex_marker(fr_log_t const *log, fr_log_type_t type, char const *file, int line,
183  uint8_t const *data, size_t data_len,
184  ssize_t marker_idx, char const *marker, char const *line_prefix_fmt, ...)
185  CC_HINT(format (printf, 9, 10)) CC_HINT(nonnull (1, 3, 5, 8));
186 
187 int fr_log_init_std(fr_log_t *log, fr_log_dst_t dst_type) CC_HINT(nonnull);
188 
189 int fr_log_init_file(fr_log_t *log, char const *file) CC_HINT(nonnull);
190 
191 int fr_log_init_fp(fr_log_t *log, FILE *fp) CC_HINT(nonnull);
192 
193 int fr_log_init_syslog(fr_log_t *log) CC_HINT(nonnull);
194 
196  CC_HINT(nonnull(1,3));
197 
198 int fr_log_close(fr_log_t *log) CC_HINT(nonnull);
199 
200 TALLOC_CTX *fr_log_pool_init(void);
201 
202 int fr_log_global_init(fr_event_list_t *el, bool daemonize) CC_HINT(nonnull);
203 
204 void fr_log_global_free(void);
205 
206 #ifdef __cplusplus
207 }
208 #endif
int const char * file
Definition: acutest.h:702
static int const char * fmt
Definition: acutest.h:573
int const char int line
Definition: acutest.h:702
#define RCSIDH(h, id)
Definition: build.h:445
#define UNUSED
Definition: build.h:313
static char const * spaces
Definition: dependency.c:364
int(* cookie_close_function_t)(void *cookie)
Definition: fopencookie.h:49
ssize_t(* cookie_write_function_t)(void *cookie, const char *buf, size_t size)
Definition: fopencookie.h:45
Stores all information relating to an event list.
Definition: event.c:411
fr_table_num_ordered_t const fr_log_levels[]
Maps log categories to message prefixes.
Definition: log.c:244
int fr_log_init_legacy(fr_log_t *log, bool daemonize)
Initialise file descriptors based on logging destination.
Definition: log.c:923
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.
Definition: log.c:725
int fr_debug_lvl
Definition: log.c:42
int fr_log_init_syslog(fr_log_t *log)
Initialise a syslog logging destination.
Definition: log.c:1145
static bool fr_rate_limit_enabled(void)
Whether rate limiting is enabled.
Definition: log.h:148
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.
Definition: log.c:1235
TALLOC_CTX * fr_log_pool_init(void)
talloc ctx to use when composing log messages
Definition: log.c:315
fr_log_dst_t
Definition: log.h:77
@ L_DST_NULL
Discard log messages.
Definition: log.h:83
@ L_DST_STDERR
Log to stderr.
Definition: log.h:81
@ L_DST_FILES
Log to a file on disk.
Definition: log.h:79
@ L_DST_FUNC
Send log messages to a FILE*, via fopencookie()
Definition: log.h:82
@ L_DST_NUM_DEST
Definition: log.h:84
@ L_DST_STDOUT
Log to stdout.
Definition: log.h:78
@ L_DST_SYSLOG
Log to syslog.
Definition: log.h:80
void fr_log_global_free(void)
Restores the original stdout and stderr FDs, closes the pipes and removes them from the event loop.
Definition: log.c:1354
fr_log_timestamp_t
Definition: log.h:87
@ L_TIMESTAMP_ON
Always log timestamps.
Definition: log.h:90
@ L_TIMESTAMP_OFF
Never log timestamps.
Definition: log.h:91
@ L_TIMESTAMP_AUTO
Timestamp logging preference not specified.
Definition: log.h:88
void fr_log(fr_log_t const *log, fr_log_type_t lvl, char const *file, int line, char const *fmt,...))
Send a server log message to its destination.
Definition: log.c:599
fr_log_lvl_t
Definition: log.h:67
@ L_DBG_LVL_3
3rd highest priority debug messages (-xxx | -Xx).
Definition: log.h:72
@ L_DBG_LVL_1
Highest priority debug messages (-x).
Definition: log.h:70
@ L_DBG_LVL_DISABLE
Don't print messages.
Definition: log.h:68
@ L_DBG_LVL_2
2nd highest priority debug messages (-xx | -X).
Definition: log.h:71
@ L_DBG_LVL_OFF
No debug messages.
Definition: log.h:69
@ L_DBG_LVL_4
4th highest priority debug messages (-xxxx | -Xxx).
Definition: log.h:73
@ L_DBG_LVL_MAX
Lowest priority debug messages (-xxxxx | -Xxxx).
Definition: log.h:74
char const * prefix
To add to log messages.
Definition: log.h:139
int fr_log_init_std(fr_log_t *log, fr_log_dst_t dst_type)
Initialise log dst for stdout, stderr or /dev/null.
Definition: log.c:1044
char const * first_prefix
Prefix for the first line printed.
Definition: log.h:125
void fr_vlog(fr_log_t const *log, fr_log_type_t lvl, char const *file, int line, char const *fmt, va_list ap))
Send a server log message to its destination.
Definition: log.c:343
void fr_canonicalize_error(TALLOC_CTX *ctx, char **spaces, char **text, ssize_t slen, char const *fmt)
Canonicalize error strings, removing tabs, and generate spaces for error marker.
Definition: log.c:89
char const * subsq_prefix
Prefix for subsequent lines.
Definition: log.h:126
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.
Definition: log.c:800
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.
Definition: log.c:178
FILE * fr_log_fp
Definition: log.c:41
fr_log_t default_log
Definition: log.c:290
int fr_log_init_file(fr_log_t *log, char const *file)
Initialise a file logging destination.
Definition: log.c:1099
bool fr_log_rate_limit
Whether repeated log entries should be rate limited.
Definition: log.c:60
int fr_log_init_fp(fr_log_t *log, FILE *fp)
Initialise a file logging destination to a FILE*.
Definition: log.c:1078
fr_log_lvl_t lvl
Priority of the message.
Definition: log.h:138
bool log_dates_utc
Definition: log.c:288
size_t fr_log_levels_len
Definition: log.c:256
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.
Definition: log.c:1175
fr_log_type_t type
What type of log message it is.
Definition: log.h:137
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 *rules, char const *fmt, va_list ap))
Drain any outstanding messages from the fr_strerror buffers.
Definition: log.c:630
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.
Definition: log.c:847
int fr_log_close(fr_log_t *log)
Universal close function for all logging destinations.
Definition: log.c:1199
fr_log_t const * dst
Where to log to.
Definition: log.h:136
fr_log_type_t
Definition: log.h:54
@ L_DBG_INFO
Info only displayed when debugging is enabled.
Definition: log.h:60
@ 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_AUTH
Authentication logs.
Definition: log.h:58
@ L_INFO
Informational message.
Definition: log.h:55
@ L_DBG
Only displayed when debugging is enabled.
Definition: log.h:59
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.
Definition: log.c:750
Context structure for the log fd event function.
Definition: log.h:135
long int ssize_t
Definition: merged_model.c:24
unsigned char uint8_t
Definition: merged_model.c:30
unsigned long int size_t
Definition: merged_model.c:25
fr_aka_sim_id_type_t type
Definition: log.h:96
bool dates_utc
Whether timestamps should be UTC or local timezone.
Definition: log.h:104
fr_log_t * parent
Log destination this was cloned from.
Definition: log.h:121
void * uctx
User data associated with the fr_log_t.
Definition: log.h:119
bool colourise
Prefix log messages with VT100 escape codes to change text colour.
Definition: log.h:101
fr_log_dst_t dst
Log destination.
Definition: log.h:97
bool line_number
Log src file and line number.
Definition: log.h:99
int fd
File descriptor to write messages to.
Definition: log.h:112
ssize_t(* cookie_write)(void *, char const *, size_t)
write function
Definition: log.h:118
fr_log_timestamp_t timestamp
Prefix log messages with timestamps.
Definition: log.h:110
void * cookie
for fopencookie()
Definition: log.h:115
bool suppress_secrets
suppress secrets when printing to this destination
Definition: log.h:108
char const * file
Path to log file.
Definition: log.h:113
bool print_level
sometimes we don't want log levels printed
Definition: log.h:106
FILE * handle
Path to log file.
Definition: log.h:116
An element in an arbitrarily ordered array of name to num mappings.
Definition: table.h:53
close(uq->fd)
static fr_event_list_t * el
static fr_slen_t data
Definition: value.h:1259
int nonnull(2, 5))
int format(printf, 5, 0))