]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MEDIUM: logs: have global.log_send_hostname not contain the trailing space
authorDragan Dosen <ddosen@haproxy.com>
Mon, 28 Sep 2015 11:28:21 +0000 (13:28 +0200)
committerWilly Tarreau <w@1wt.eu>
Mon, 28 Sep 2015 16:27:45 +0000 (18:27 +0200)
This patch unifies global.log_send_hostname addition in the log header
processing.

include/proto/log.h
src/cfgparse.c
src/log.c

index 62b39eab83d04e84cc28d16f0509871a724e73da..dcc5ae4b9e604af716eb22ba66ee22fe47d2331d 100644 (file)
@@ -40,9 +40,6 @@ extern char default_tcp_log_format[];
 extern char default_http_log_format[];
 extern char clf_http_log_format[];
 
-extern char default_host_tag_pid_log_format[];
-extern char rfc5424_host_tag_pid_log_format[];
-
 extern char default_rfc5424_sd_log_format[];
 
 extern char *logheader;
@@ -154,7 +151,7 @@ char *lf_port(char *dst, struct sockaddr *sockaddr, size_t size, struct logforma
 /*
  * Write hostname, log_tag and pid to the log string
  */
-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);
 
 
 #endif /* _PROTO_LOG_H */
index f3a7aee3a8d88bdd2255177a9041af159c8b46b7..6958a1d24ab5c22b4e4d692ae9047ac4383cd0aa 100644 (file)
@@ -1647,7 +1647,6 @@ int cfg_parse_global(const char *file, int linenum, char **args, int kwm)
        }
        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 @@ int cfg_parse_global(const char *file, int linenum, char **args, int kwm)
                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 @@ out_uri_auth_compat:
                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 @@ out_uri_auth_compat:
                        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 "
index 18296967a10d26d1b992ef8188e0de5548b0b8d1..18a2006f97900a60a33d74fbde2ccbab1439e617 100644 (file)
--- a/src/log.c
+++ b/src/log.c
@@ -158,8 +158,8 @@ char *log_format = NULL;
 /* 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 @@ char *lf_port(char *dst, struct sockaddr *sockaddr, size_t size, struct logforma
        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;