Skip to content

Commit

Permalink
Unify config logging functions, and pass through file/line
Browse files Browse the repository at this point in the history
  • Loading branch information
arr2036 committed May 8, 2019
1 parent 08ba526 commit 5f75f60
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 132 deletions.
170 changes: 52 additions & 118 deletions src/lib/server/cf_util.c
Expand Up @@ -1773,25 +1773,32 @@ int cf_pair_in_table(int32_t *out, FR_NAME_NUMBER const *table, CONF_PAIR *cp)
* @param[in] fmt of the message.
* @param[in] ... Message args.
*/
void _cf_log_err(CONF_ITEM const *ci, char const *fmt, ...)
void _cf_log(fr_log_type_t type, CONF_ITEM const *ci, char const *file, int line, char const *fmt, ...)
{
va_list ap;
char *msg;

va_start(ap, fmt);
msg = fr_vasprintf(NULL, fmt, ap);
va_end(ap);
if (!ci || !ci->filename || !*ci->filename || (*ci->filename == '<') ||
(((type == L_DBG) || (type == L_INFO)) && !DEBUG_ENABLED4)) {
va_start(ap, fmt);
fr_vlog(LOG_DST, type, file, line, fmt, ap);
va_end(ap);
return;
}

{
char const *e, *p;
int len;
char *msg;

if (!ci || !ci->filename || !*ci->filename || (*ci->filename == '<')) {
ERROR("%s", msg);
} else {
char const *e, *p;
int len;
truncate_filename(&e, &p, &len, ci->filename);
ERROR("%s%.*s[%d]: %s", e, len, p, ci->lineno, msg);
}

talloc_free(msg);
va_start(ap, fmt);
msg = fr_vasprintf(NULL, fmt, ap);
va_end(ap);

fr_log(LOG_DST, type, file, line, "%s%.*s[%d]: %s", e, len, p, ci->lineno, msg);
talloc_free(msg);
}
}

/** Log an error message relating to a #CONF_ITEM
Expand All @@ -1802,141 +1809,68 @@ void _cf_log_err(CONF_ITEM const *ci, char const *fmt, ...)
* @param[in] fmt of the message.
* @param[in] ... Message args.
*/
void _cf_log_perr(CONF_ITEM const *ci, char const *fmt, ...)
void _cf_log_perr(fr_log_type_t type, CONF_ITEM const *ci, char const *file, int line, char const *fmt, ...)
{
va_list ap;
char *msg;

va_start(ap, fmt);
msg = fr_vasprintf(NULL, fmt, ap);
va_end(ap);

if (!ci || !ci->filename) {
PERROR("%s", msg);
} else {
char const *e, *p;
int len;
truncate_filename(&e, &p, &len, ci->filename);
PERROR("%s%.*s[%d]: %s", e, len, p, ci->lineno, msg);
va_start(ap, fmt);
fr_vlog_perror(LOG_DST, type, file, line, fmt, ap);
va_end(ap);
return;
}

talloc_free(msg);
}

/** Log a warning message relating to a #CONF_ITEM
*
* @param[in] ci #CONF_ITEM to print file/lineno for.
* @param[in] fmt of the message.
* @param[in] ... Message args.
*/
void _cf_log_warn(CONF_ITEM const *ci, char const *fmt, ...)
{
va_list ap;
char *msg;

va_start(ap, fmt);
msg = fr_vasprintf(NULL, fmt, ap);
va_end(ap);
{
char const *e, *p;
int len;
char *msg;

if (!ci || !ci->filename) {
WARN("%s", msg);
} else {
char const *e, *p;
int len;
truncate_filename(&e, &p, &len, ci->filename);
WARN("%s%.*s[%d]: %s", e, len, p, ci->lineno, msg);
}

talloc_free(msg);
}

/** Log a info message relating to a #CONF_ITEM
*
* @param[in] ci #CONF_ITEM to print file/lineno for.
* @param[in] fmt of the message.
* @param[in] ... Message args.
*/
void _cf_log_info(CONF_ITEM const *ci, char const *fmt, ...)
{
va_list ap;
char *msg;

va_start(ap, fmt);
msg = fr_vasprintf(NULL, fmt, ap);
va_end(ap);
va_start(ap, fmt);
msg = fr_vasprintf(NULL, fmt, ap);
va_end(ap);

if (!ci || !ci->filename || !DEBUG_ENABLED4) {
INFO("%s", msg);
} else {
char const *e, *p;
int len;
truncate_filename(&e, &p, &len, ci->filename);
INFO("%s%.*s[%d]: %s", e, len, p, ci->lineno, msg);
fr_log_perror(LOG_DST, type, file, line, "%s%.*s[%d]: %s", e, len, p, ci->lineno, msg);
talloc_free(msg);
}

talloc_free(msg);
}

/** Log a debug message relating to a #CONF_ITEM
*
* @note File and line number are only emitted at debug level 4.
* Always emits a filename/lineno prefix if available
*
* @param[in] ci #CONF_ITEM to print file/lineno for.
* @param[in] fmt of the message.
* @param[in] ... Message args.
*/
void _cf_log_debug(CONF_ITEM const *ci, char const *fmt, ...)
void _cf_log_debug_prefix(fr_log_type_t type, CONF_ITEM const *ci, char const *file, int line, char const *fmt, ...)
{
va_list ap;
char *msg;

if (!DEBUG_ENABLED) return;

va_start(ap, fmt);
msg = fr_vasprintf(NULL, fmt, ap);
va_end(ap);
if ((type == L_DBG) && !DEBUG_ENABLED) return;

if (!ci || !ci->filename || !DEBUG_ENABLED4) {
DEBUG("%s", msg);
} else {
char const *e, *p;
int len;
truncate_filename(&e, &p, &len, ci->filename);
DEBUG("%s%.*s[%d]: %s", e, len, p, ci->lineno, msg);
if (!ci || !ci->filename) {
va_start(ap, fmt);
fr_vlog(LOG_DST, type, file, line, fmt, ap);
va_end(ap);
return;
}

talloc_free(msg);
}

/** Log a debug message relating to a #CONF_ITEM
*
* Always emits a filename/lineno prefix is available
*
* @param[in] ci #CONF_ITEM to print file/lineno for.
* @param[in] fmt of the message.
* @param[in] ... Message args.
*/
void _cf_log_debug_prefix(CONF_ITEM const *ci, char const *fmt, ...)
{
va_list ap;
char *msg;
{
char const *e, *p;
int len;
char *msg;

if (!DEBUG_ENABLED) return;
truncate_filename(&e, &p, &len, ci->filename);

va_start(ap, fmt);
msg = fr_vasprintf(NULL, fmt, ap);
va_end(ap);
va_start(ap, fmt);
msg = fr_vasprintf(NULL, fmt, ap);
va_end(ap);

if (!ci || !ci->filename) {
DEBUG("%s", msg);
} else {
char const *e, *p;
int len;
truncate_filename(&e, &p, &len, ci->filename);
DEBUG("%s%.*s[%d]: %s", e, len, p, ci->lineno, msg);
fr_log(LOG_DST, type, file, line, "%s%.*s[%d]: %s", e, len, p, ci->lineno, msg);
talloc_free(msg);
}

talloc_free(msg);
}

/** Log an error message in the context of a child pair of the specified parent
Expand Down
24 changes: 10 additions & 14 deletions src/lib/server/cf_util.h
Expand Up @@ -47,6 +47,7 @@ typedef struct cf_data CONF_DATA; //!< #CONF_ITEM used to associate arbitrary da

#include <freeradius-devel/util/rbtree.h>
#include <freeradius-devel/util/token.h>
#include <freeradius-devel/util/log.h>

#define FR_TIMEVAL_TO_MS(_x) (((_x)->tv_usec / 1000) + ((_x)->tv_sec * (uint64_t)1000))
#define FR_TIMESPEC_TO_MS(_x) (((_x)->tv_usec / 1000000) + ((_x)->tv_sec * (uint64_t)1000))
Expand Down Expand Up @@ -205,23 +206,18 @@ int cf_pair_in_table(int32_t *out, FR_NAME_NUMBER const *table, CONF_PAIR *cp);
/*
* Error logging
*/
#define cf_log_err(_cf, _fmt, ...) _cf_log_err(CF_TO_ITEM(_cf), _fmt, ## __VA_ARGS__)
void _cf_log_err(CONF_ITEM const *ci, char const *fmt, ...) CC_HINT(format (printf, 2, 3));

#define cf_log_perr(_cf, _fmt, ...) _cf_log_perr(CF_TO_ITEM(_cf), _fmt, ## __VA_ARGS__)
void _cf_log_perr(CONF_ITEM const *ci, char const *fmt, ...) CC_HINT(format (printf, 2, 3));
#define cf_log_err(_cf, _fmt, ...) _cf_log(L_ERR, CF_TO_ITEM(_cf), __FILE__, __LINE__, _fmt, ## __VA_ARGS__)
#define cf_log_warn(_cf, _fmt, ...) _cf_log(L_WARN, CF_TO_ITEM(_cf), __FILE__, __LINE__, _fmt, ## __VA_ARGS__)
#define cf_log_info(_cf, _fmt, ...) _cf_log(L_INFO, CF_TO_ITEM(_cf), __FILE__, __LINE__, _fmt, ## __VA_ARGS__)
#define cf_log_debug(_cf, _fmt, ...) _cf_log(L_DBG, CF_TO_ITEM(_cf), __FILE__, __LINE__, _fmt, ## __VA_ARGS__)
void _cf_log(fr_log_type_t type, CONF_ITEM const *ci, char const *file, int line, char const *fmt, ...) CC_HINT(format (printf, 5, 6));

#define cf_log_warn(_cf, _fmt, ...) _cf_log_warn(CF_TO_ITEM(_cf), _fmt, ## __VA_ARGS__)
void _cf_log_warn(CONF_ITEM const *ci, char const *fmt, ...) CC_HINT(format (printf, 2, 3));
#define cf_log_perr(_cf, _fmt, ...) _cf_log_perr(L_ERR, CF_TO_ITEM(_cf), __FILE__, __LINE__, _fmt, ## __VA_ARGS__)
void _cf_log_perr(fr_log_type_t type, CONF_ITEM const *ci, char const *file, int line, char const *fmt, ...) CC_HINT(format (printf, 5, 6));

#define cf_log_info(_cf, _fmt, ...) _cf_log_info(CF_TO_ITEM(_cf), _fmt, ## __VA_ARGS__)
void _cf_log_info(CONF_ITEM const *ci, char const *fmt, ...) CC_HINT(format (printf, 2, 3));

#define cf_log_debug(_cf, _fmt, ...) _cf_log_debug(CF_TO_ITEM(_cf), _fmt, ## __VA_ARGS__)
void _cf_log_debug(CONF_ITEM const *ci, char const *fmt, ...) CC_HINT(format (printf, 2, 3));

#define cf_log_debug_prefix(_cf, _fmt, ...) _cf_log_debug_prefix(CF_TO_ITEM(_cf), _fmt, ## __VA_ARGS__)
void _cf_log_debug_prefix(CONF_ITEM const *ci, char const *fmt, ...) CC_HINT(format (printf, 2, 3));
#define cf_log_debug_prefix(_cf, _fmt, ...) _cf_log_debug_prefix(L_DBG, CF_TO_ITEM(_cf), __FILE__, __LINE__, _fmt, ## __VA_ARGS__)
void _cf_log_debug_prefix(fr_log_type_t type, CONF_ITEM const *ci, char const *file, int line, char const *fmt, ...) CC_HINT(format (printf, 5, 6));

void cf_log_err_by_name(CONF_SECTION const *parent,
char const *name, char const *fmt, ...) CC_HINT(format (printf, 3, 4));
Expand Down

0 comments on commit 5f75f60

Please sign in to comment.