Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: preparations for RFC5424 syslog support #8457

Closed
wants to merge 12 commits into from
Prev Previous commit
Next Next commit
lib: include \n in zlog_msg_text()
Since the file targets append one anyway, save them some extra work.
syslog can use `%.*s` since it's "forced" printf by API anyway.

Signed-off-by: David Lamparter <equinox@diac24.net>
  • Loading branch information
eqvinox committed Apr 21, 2021
commit d785204f9fe7ddb170329ae78d0d3001adeb8abe
10 changes: 7 additions & 3 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,10 +132,13 @@ 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)
&& (text = zlog_msg_text(msgs[i], &text_len))
&& search_buf(text, text_len) < 0)
continue;

msgfilt[o++] = msgs[i];
Expand Down
4 changes: 2 additions & 2 deletions lib/zlog.c
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ const char *zlog_msg_text(struct zlog_msg *msg, size_t *textlen)
va_end(args);

msg->textlen = need;
need += bputch(&fb, '\0');
need += bputch(&fb, '\n');

if (need <= msg->stackbufsz)
msg->text = msg->stackbuf;
Expand All @@ -603,7 +603,7 @@ const char *zlog_msg_text(struct zlog_msg *msg, size_t *textlen)
vbprintfrr(&fb, msg->fmt, args);
va_end(args);

bputch(&fb, '\0');
bputch(&fb, '\n');
}

msg->n_argpos = fb.outpos_i;
Expand Down
8 changes: 7 additions & 1 deletion lib/zlog.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,13 @@ struct zlog_msg;
extern int zlog_msg_prio(struct zlog_msg *msg);
extern const struct xref_logmsg *zlog_msg_xref(struct zlog_msg *msg);

/* pass NULL as textlen if you don't need it. */
/* text is NOT \0 terminated; instead there is a \n after textlen since the
* logging targets would jump extra hoops otherwise for a single byte. (the
* \n is not included in textlen)
*
* calling this with NULL textlen is likely wrong.
* use "%.*s", (int)textlen, text when passing to printf-like functions
*/
extern const char *zlog_msg_text(struct zlog_msg *msg, size_t *textlen);

extern void zlog_msg_args(struct zlog_msg *msg, size_t *hdrlen,
Expand Down
20 changes: 8 additions & 12 deletions lib/zlog_targets.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,13 @@ void zlog_fd(struct zlog_target *zt, struct zlog_msg *msgs[], size_t nmsgs)
int prio = zlog_msg_prio(msg);

if (prio <= zt->prio_min) {
iov[iovpos].iov_base = ts_pos;
if (iovpos > 0)
*ts_pos++ = '\n';

struct fbuf fbuf = {
.buf = ts_buf,
.pos = ts_pos,
.len = sizeof(ts_buf),
};

iov[iovpos].iov_base = ts_pos;
zlog_msg_ts(msg, &fbuf,
ZLOG_TS_LEGACY | zte->ts_subsec);
ts_pos = fbuf.pos;
Expand All @@ -113,7 +111,7 @@ void zlog_fd(struct zlog_target *zt, struct zlog_msg *msgs[], size_t nmsgs)

iov[iovpos].iov_base =
(char *)zlog_msg_text(msg, &textlen);
iov[iovpos].iov_len = textlen;
iov[iovpos].iov_len = textlen + 1;

iovpos++;
}
Expand All @@ -126,11 +124,6 @@ void zlog_fd(struct zlog_target *zt, struct zlog_msg *msgs[], size_t nmsgs)
if (iovpos > 0 && (ts_buf + sizeof(ts_buf) - ts_pos < TS_LEN
|| i + 1 == nmsgs
|| array_size(iov) - iovpos < 5)) {
iov[iovpos].iov_base = (char *)"\n";
iov[iovpos].iov_len = 1;

iovpos++;

writev(fd, iov, iovpos);

iovpos = 0;
Expand Down Expand Up @@ -445,13 +438,16 @@ static void zlog_syslog(struct zlog_target *zt, struct zlog_msg *msgs[],
{
size_t i;
struct zlt_syslog *zte = container_of(zt, struct zlt_syslog, zt);
const char *text;
size_t text_len;

for (i = 0; i < nmsgs; i++) {
if (zlog_msg_prio(msgs[i]) > zt->prio_min)
continue;

syslog(zlog_msg_prio(msgs[i]) | zte->syslog_facility, "%s",
zlog_msg_text(msgs[i], NULL));
text = zlog_msg_text(msgs[i], &text_len);
syslog(zlog_msg_prio(msgs[i]) | zte->syslog_facility, "%.*s",
(int)text_len, text);
}
}

Expand Down