logger: refactor the way output is written
Previously, output was written in exactly the same way in three different places. This is now combined into a single function. This hopefully makes it easier to adapt to changing output needs.
This commit is contained in:
parent
59d6ed3f01
commit
4be843064c
|
@ -332,6 +332,15 @@ rfc3164_current_time(void)
|
||||||
return time;
|
return time;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void write_output(const struct logger_ctl *ctl, const char *const buf,
|
||||||
|
const size_t len)
|
||||||
|
{
|
||||||
|
if (write_all(ctl->fd, buf, len) < 0)
|
||||||
|
warn(_("write failed"));
|
||||||
|
if (ctl->stderr_printout)
|
||||||
|
fprintf(stderr, "%s\n", buf);
|
||||||
|
}
|
||||||
|
|
||||||
static void syslog_rfc3164(const struct logger_ctl *ctl, const char *msg)
|
static void syslog_rfc3164(const struct logger_ctl *ctl, const char *msg)
|
||||||
{
|
{
|
||||||
char *buf, pid[30], *cp, *hostname, *dot;
|
char *buf, pid[30], *cp, *hostname, *dot;
|
||||||
|
@ -353,10 +362,7 @@ static void syslog_rfc3164(const struct logger_ctl *ctl, const char *msg)
|
||||||
len = xasprintf(&buf, "<%d>%.15s %s %.200s%s: %.400s",
|
len = xasprintf(&buf, "<%d>%.15s %s %.200s%s: %.400s",
|
||||||
ctl->pri, rfc3164_current_time(), hostname, cp, pid, msg);
|
ctl->pri, rfc3164_current_time(), hostname, cp, pid, msg);
|
||||||
|
|
||||||
if (write_all(ctl->fd, buf, len) < 0)
|
write_output(ctl, buf, len);
|
||||||
warn(_("write failed"));
|
|
||||||
if (ctl->stderr_printout)
|
|
||||||
fprintf(stderr, "%s\n", buf);
|
|
||||||
|
|
||||||
free(hostname);
|
free(hostname);
|
||||||
free(buf);
|
free(buf);
|
||||||
|
@ -427,11 +433,7 @@ static void syslog_rfc5424(const struct logger_ctl *ctl, const char *msg)
|
||||||
hostname ? hostname : "",
|
hostname ? hostname : "",
|
||||||
tag, pid, timeq, msg);
|
tag, pid, timeq, msg);
|
||||||
|
|
||||||
if (write_all(ctl->fd, buf, len) < 0)
|
write_output(ctl, buf, len);
|
||||||
warn(_("write failed"));
|
|
||||||
|
|
||||||
if (ctl->stderr_printout)
|
|
||||||
fprintf(stderr, "%s\n", buf);
|
|
||||||
|
|
||||||
free(hostname);
|
free(hostname);
|
||||||
free(buf);
|
free(buf);
|
||||||
|
@ -483,10 +485,7 @@ static void syslog_local(const struct logger_ctl *ctl, const char *msg)
|
||||||
|
|
||||||
len = xasprintf(&buf, "<%d>%s %s%s: %s", ctl->pri, rfc3164_current_time(),
|
len = xasprintf(&buf, "<%d>%s %s%s: %s", ctl->pri, rfc3164_current_time(),
|
||||||
tag, pid, msg);
|
tag, pid, msg);
|
||||||
if (write_all(ctl->fd, buf, len) < 0)
|
write_output(ctl, buf, len);
|
||||||
warn(_("write failed"));
|
|
||||||
if (ctl->stderr_printout)
|
|
||||||
fprintf(stderr, "%s\n", buf);
|
|
||||||
free(buf);
|
free(buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue