MEDIUM: logs: have global.log_send_hostname not contain the trailing space
This patch unifies global.log_send_hostname addition in the log header
processing.
diff --git a/src/cfgparse.c b/src/cfgparse.c
index f3a7aee..6958a1d 100644
--- a/src/cfgparse.c
+++ b/src/cfgparse.c
@@ -1647,7 +1647,6 @@
}
else if (!strcmp(args[0], "log-send-hostname")) { /* set the hostname in syslog header */
char *name;
- int len;
if (global.log_send_hostname != NULL) {
Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
@@ -1660,12 +1659,9 @@
else
name = hostname;
- len = strlen(name);
-
/* We'll add a space after the name to respect the log format */
free(global.log_send_hostname);
- global.log_send_hostname = malloc(len + 2);
- snprintf(global.log_send_hostname, len + 2, "%s ", name);
+ global.log_send_hostname = strdup(name);
}
else if (!strcmp(args[0], "server-state-base")) { /* path base where HAProxy can find server state files */
if (global.server_state_base != NULL) {
@@ -7897,23 +7893,19 @@
list_for_each_entry(tmplogsrv, &curproxy->logsrvs, list) {
char *hdr;
struct chunk *htp;
- char *htp_fmt;
char *host = global.log_send_hostname;
switch (tmplogsrv->format) {
case LOG_FORMAT_RFC3164:
hdr = logheader;
htp = &curproxy->log_htp;
- htp_fmt = default_host_tag_pid_log_format;
host = host ? host : "";
break;
case LOG_FORMAT_RFC5424:
hdr = logheader_rfc5424;
htp = &curproxy->log_htp_rfc5424;
- htp_fmt = rfc5424_host_tag_pid_log_format;
- if (!curproxy->conf.logformat_sd_string)
- curproxy->conf.logformat_sd_string = default_rfc5424_sd_log_format;
+ host = host ? host : hostname;
break;
default:
@@ -7923,19 +7915,10 @@
if (htp->str)
continue;
- if (!host) {
- int len = strlen(hostname);
- host = malloc(len + 2);
- snprintf(host, len + 2, "%s ", hostname);
- }
-
- htp->str = lf_host_tag_pid(hdr, htp_fmt, host,
+ htp->str = lf_host_tag_pid(hdr, tmplogsrv->format, host,
curproxy->log_tag ? curproxy->log_tag : global.log_tag,
pid, global.max_syslog_len);
- if ((host != global.log_send_hostname) && strlen(host))
- free(host);
-
if ((htp->str == NULL) ||
((htp->len = htp->str - hdr) >= global.max_syslog_len)) {
Alert("Proxy '%s': cannot write a syslog header string that contains "
diff --git a/src/log.c b/src/log.c
index 1829696..18a2006 100644
--- a/src/log.c
+++ b/src/log.c
@@ -158,8 +158,8 @@
/* Common printf format strings for hostname, log_tag and pid used in all
* outgoing syslog messages.
*/
-char default_host_tag_pid_log_format[] = "%s%s[%d]: ";
-char rfc5424_host_tag_pid_log_format[] = "%s%s %d - ";
+static char default_host_tag_pid_log_format[] = "%s%s%s[%d]: ";
+static char rfc5424_host_tag_pid_log_format[] = "%s%s%s %d - ";
/* Default string used for structured-data part in RFC5424 formatted
* syslog messages.
@@ -780,12 +780,26 @@
return ret;
}
-char *lf_host_tag_pid(char *dst, const char *format, const char *hostname, const char *log_tag, int pid, size_t size)
+char *lf_host_tag_pid(char *dst, int format, const char *hostname, const char *log_tag, int pid, size_t size)
{
char *ret = dst;
+ char *fmt;
int iret;
- iret = snprintf(dst, size, format, hostname, log_tag, pid);
+ switch (format) {
+ case LOG_FORMAT_RFC3164:
+ fmt = default_host_tag_pid_log_format;
+ break;
+
+ case LOG_FORMAT_RFC5424:
+ fmt = rfc5424_host_tag_pid_log_format;
+ break;
+
+ default:
+ return NULL;
+ }
+
+ iret = snprintf(dst, size, fmt, hostname, strlen(hostname) ? " " : "", log_tag, pid);
if (iret < 0 || iret > size)
return NULL;
ret += iret;