Skip to content

Commit

Permalink
Merge pull request #8656 from opensourcerouting/xref-5424-prep-2
Browse files Browse the repository at this point in the history
lib: preparations for RFC5424 syslog support
  • Loading branch information
donaldsharp authored Jun 23, 2021
2 parents fa855f8 + ca846ff commit 2afd15f
Show file tree
Hide file tree
Showing 8 changed files with 227 additions and 54 deletions.
21 changes: 16 additions & 5 deletions doc/user/basic.rst
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,17 @@ Basic Config Commands
is used to start the daemon then this command is turned on by default
and cannot be turned off and the [no] form of the command is dissallowed.

.. clicmd:: log-filter WORD [DAEMON]
.. clicmd:: log filtered-file [FILENAME [LEVEL]]

Configure a destination file for filtered logs with the
:clicmd:`log filter-text WORD` command.

.. clicmd:: log filter-text WORD

This command forces logs to be filtered on a specific string. A log message
will only be printed if it matches on one of the filters in the log-filter
table. Can be daemon independent.
table. The filter only applies to file logging targets configured with
:clicmd:`log filtered-file [FILENAME [LEVEL]]`.

.. note::

Expand All @@ -193,10 +199,15 @@ Basic Config Commands
Log filters prevent this but you should still expect a small performance
hit due to filtering each of all those logs.

.. clicmd:: log-filter clear [DAEMON]
.. note::

This setting is not saved to ``frr.conf`` and not shown in
:clicmd:`show running-config`. It is intended for ephemeral debugging
purposes only.

.. clicmd:: clear log filter-text

This command clears all current filters in the log-filter table. Can be
daemon independent.
This command clears all current filters in the log-filter table.


.. clicmd:: log immediate-mode
Expand Down
4 changes: 4 additions & 0 deletions lib/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,11 @@ extern "C" {
#define PRIx64 "Lx"

#else /* !_FRR_ATTRIBUTE_PRINTFRR */
#ifdef __NetBSD__
#define PRINTFRR(a, b) __attribute__((format(gnu_syslog, a, b)))
#else
#define PRINTFRR(a, b) __attribute__((format(printf, a, b)))
#endif

/* frr-format plugin is C-only for now, so no point in doing these shenanigans
* for C++... (also they can break some C++ stuff...)
Expand Down
16 changes: 10 additions & 6 deletions lib/log_filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,14 @@ int zlog_filter_dump(char *buf, size_t max_size)
return len;
}

static int search_buf(const char *buf)
static int search_buf(const char *buf, size_t len)
{
char *found = NULL;

frr_with_mutex(&logfilterlock) {
for (int i = 0; i < zlog_filter_count; i++) {
found = strstr(buf, zlog_filters[i]);
found = memmem(buf, len, zlog_filters[i],
strlen(zlog_filters[i]));
if (found != NULL)
return 0;
}
Expand All @@ -131,12 +132,15 @@ static void zlog_filterfile_fd(struct zlog_target *zt, struct zlog_msg *msgs[],
{
struct zlog_msg *msgfilt[nmsgs];
size_t i, o = 0;
const char *text;
size_t text_len;

for (i = 0; i < nmsgs; i++) {
if (zlog_msg_prio(msgs[i]) >= LOG_DEBUG
&& search_buf(zlog_msg_text(msgs[i], NULL)) < 0)
continue;

if (zlog_msg_prio(msgs[i]) >= LOG_DEBUG) {
text = zlog_msg_text(msgs[i], &text_len);
if (search_buf(text, text_len) < 0)
continue;
}
msgfilt[o++] = msgs[i];
}

Expand Down
25 changes: 19 additions & 6 deletions lib/log_vty.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#define ZLOG_MAXLVL(a, b) MAX(a, b)

DEFINE_HOOK(zlog_rotate, (), ());
DEFINE_HOOK(zlog_cli_show, (struct vty * vty), (vty));

static const int log_default_lvl = LOG_DEBUG;

Expand All @@ -57,7 +58,7 @@ static struct zlog_cfg_filterfile zt_filterfile = {
},
};

static const char *zlog_progname;
const char *zlog_progname;
static const char *zlog_protoname;

static const struct facility_map {
Expand Down Expand Up @@ -94,7 +95,14 @@ static const char * const zlog_priority[] = {
"notifications", "informational", "debugging", NULL,
};

static const char *facility_name(int facility)
const char *zlog_priority_str(int priority)
{
if (priority > LOG_DEBUG)
return "???";
return zlog_priority[priority];
}

const char *facility_name(int facility)
{
const struct facility_map *fm;

Expand All @@ -104,7 +112,7 @@ static const char *facility_name(int facility)
return "";
}

static int facility_match(const char *str)
int facility_match(const char *str)
{
const struct facility_map *fm;

Expand Down Expand Up @@ -194,6 +202,8 @@ DEFUN_NOSH (show_logging,
vty_out(vty, "Record priority: %s\n",
(zt_file.record_priority ? "enabled" : "disabled"));
vty_out(vty, "Timestamp precision: %d\n", zt_file.ts_subsec);

hook_call(zlog_cli_show, vty);
return CMD_SUCCESS;
}

Expand Down Expand Up @@ -588,8 +598,9 @@ DEFUN (no_config_log_filterfile,

DEFPY (log_filter,
log_filter_cmd,
"[no] log-filter WORD$filter",
"[no] log filter-text WORD$filter",
NO_STR
"Logging control\n"
FILTER_LOG_STR
"String to filter by\n")
{
Expand All @@ -616,8 +627,9 @@ DEFPY (log_filter,
/* Clear all log filters */
DEFPY (log_filter_clear,
log_filter_clear_cmd,
"clear log-filter",
"clear log filter-text",
CLEAR_STR
"Logging control\n"
FILTER_LOG_STR)
{
zlog_filter_clear();
Expand All @@ -627,8 +639,9 @@ DEFPY (log_filter_clear,
/* Show log filter */
DEFPY (show_log_filter,
show_log_filter_cmd,
"show log-filter",
"show logging filter-text",
SHOW_STR
"Show current logging configuration\n"
FILTER_LOG_STR)
{
char log_filters[ZLOG_FILTERS_MAX * (ZLOG_FILTER_LENGTH_MAX + 3)] = "";
Expand Down
5 changes: 5 additions & 0 deletions lib/log_vty.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,14 @@ extern void log_config_write(struct vty *vty);
extern int log_level_match(const char *s);
extern void log_show_syslog(struct vty *vty);

extern int facility_match(const char *str);
extern const char *facility_name(int facility);

DECLARE_HOOK(zlog_rotate, (), ());
extern void zlog_rotate(void);

DECLARE_HOOK(zlog_cli_show, (struct vty * vty), (vty));

#ifdef __cplusplus
}
#endif
Expand Down
Loading

0 comments on commit 2afd15f

Please sign in to comment.