mirror of
https://git.haproxy.org/git/haproxy.git/
synced 2025-08-06 23:27:04 +02:00
MINOR: log: Extract some code to send syslog messages.
This patch extracts the code of __send_log() responsible of sending a syslog message to a syslog destination represented as a logsrv struct to define __do_send_log() function. __send_log() calls __do_send_log() for each syslog destination of a proxy after having prepared some of its parameters.
This commit is contained in:
parent
333939c2ee
commit
0bad840b4d
129
src/log.c
129
src/log.c
@ -1310,13 +1310,16 @@ void send_log(struct proxy *p, int level, const char *format, ...)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This function sends a syslog message.
|
* This function sends a syslog message to <logsrv>.
|
||||||
* It doesn't care about errors nor does it report them.
|
* <pid_str> is the string to be used for the PID of the caller, <pid_size> is length.
|
||||||
|
* Same thing for <sd> and <sd_size> which are used for the structured-data part
|
||||||
|
* in RFC5424 formatted syslog messages, and <tag_str> and <tag_size> the syslog tag.
|
||||||
* It overrides the last byte of the message vector with an LF character.
|
* It overrides the last byte of the message vector with an LF character.
|
||||||
* The arguments <sd> and <sd_size> are used for the structured-data part
|
* Does not return any error,
|
||||||
* in RFC5424 formatted syslog messages.
|
|
||||||
*/
|
*/
|
||||||
void __send_log(struct proxy *p, int level, char *message, size_t size, char *sd, size_t sd_size)
|
static inline void __do_send_log(struct logsrv *logsrv, int nblogger, char *pid_str, size_t pid_size,
|
||||||
|
int level, char *message, size_t size, char *sd, size_t sd_size,
|
||||||
|
char *tag_str, size_t tag_size)
|
||||||
{
|
{
|
||||||
static THREAD_LOCAL struct iovec iovec[NB_MSG_IOVEC_ELEMENTS] = { };
|
static THREAD_LOCAL struct iovec iovec[NB_MSG_IOVEC_ELEMENTS] = { };
|
||||||
static THREAD_LOCAL struct msghdr msghdr = {
|
static THREAD_LOCAL struct msghdr msghdr = {
|
||||||
@ -1326,48 +1329,11 @@ void __send_log(struct proxy *p, int level, char *message, size_t size, char *sd
|
|||||||
static THREAD_LOCAL int logfdunix = -1; /* syslog to AF_UNIX socket */
|
static THREAD_LOCAL int logfdunix = -1; /* syslog to AF_UNIX socket */
|
||||||
static THREAD_LOCAL int logfdinet = -1; /* syslog to AF_INET socket */
|
static THREAD_LOCAL int logfdinet = -1; /* syslog to AF_INET socket */
|
||||||
static THREAD_LOCAL char *dataptr = NULL;
|
static THREAD_LOCAL char *dataptr = NULL;
|
||||||
int fac_level;
|
time_t time = date.tv_sec;
|
||||||
struct list *logsrvs = NULL;
|
|
||||||
struct logsrv *tmp = NULL;
|
|
||||||
int nblogger;
|
|
||||||
char *hdr, *hdr_ptr;
|
char *hdr, *hdr_ptr;
|
||||||
size_t hdr_size;
|
size_t hdr_size;
|
||||||
time_t time = date.tv_sec;
|
|
||||||
struct buffer *tag = &global.log_tag;
|
struct buffer *tag = &global.log_tag;
|
||||||
static THREAD_LOCAL int curr_pid;
|
int fac_level;
|
||||||
static THREAD_LOCAL char pidstr[100];
|
|
||||||
static THREAD_LOCAL struct buffer pid;
|
|
||||||
|
|
||||||
msghdr.msg_iov = iovec;
|
|
||||||
|
|
||||||
dataptr = message;
|
|
||||||
|
|
||||||
if (p == NULL) {
|
|
||||||
if (!LIST_ISEMPTY(&global.logsrvs)) {
|
|
||||||
logsrvs = &global.logsrvs;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (!LIST_ISEMPTY(&p->logsrvs)) {
|
|
||||||
logsrvs = &p->logsrvs;
|
|
||||||
}
|
|
||||||
if (p->log_tag.area) {
|
|
||||||
tag = &p->log_tag;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!logsrvs)
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (unlikely(curr_pid != getpid())) {
|
|
||||||
curr_pid = getpid();
|
|
||||||
ltoa_o(curr_pid, pidstr, sizeof(pidstr));
|
|
||||||
chunk_initstr(&pid, pidstr);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Send log messages to syslog server. */
|
|
||||||
nblogger = 0;
|
|
||||||
list_for_each_entry(tmp, logsrvs, list) {
|
|
||||||
const struct logsrv *logsrv = tmp;
|
|
||||||
int *plogfd;
|
int *plogfd;
|
||||||
char *pid_sep1 = "", *pid_sep2 = "";
|
char *pid_sep1 = "", *pid_sep2 = "";
|
||||||
char logheader_short[3];
|
char logheader_short[3];
|
||||||
@ -1376,16 +1342,13 @@ void __send_log(struct proxy *p, int level, char *message, size_t size, char *sd
|
|||||||
int hdr_max = 0;
|
int hdr_max = 0;
|
||||||
int tag_max = 0;
|
int tag_max = 0;
|
||||||
int pid_sep1_max = 0;
|
int pid_sep1_max = 0;
|
||||||
int pid_max = 0;
|
|
||||||
int pid_sep2_max = 0;
|
int pid_sep2_max = 0;
|
||||||
int sd_max = 0;
|
int sd_max = 0;
|
||||||
int max = 0;
|
int max = 0;
|
||||||
|
|
||||||
nblogger++;
|
msghdr.msg_iov = iovec;
|
||||||
|
|
||||||
/* we can filter the level of the messages that are sent to each logger */
|
dataptr = message;
|
||||||
if (level > logsrv->level)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (logsrv->addr.ss_family == AF_UNSPEC) {
|
if (logsrv->addr.ss_family == AF_UNSPEC) {
|
||||||
/* the socket's address is a file descriptor */
|
/* the socket's address is a file descriptor */
|
||||||
@ -1415,7 +1378,7 @@ void __send_log(struct proxy *p, int level, char *message, size_t size, char *sd
|
|||||||
ha_alert("socket() failed in logger #%d: %s (errno=%d)\n",
|
ha_alert("socket() failed in logger #%d: %s (errno=%d)\n",
|
||||||
nblogger, strerror(errno), errno);
|
nblogger, strerror(errno), errno);
|
||||||
}
|
}
|
||||||
continue;
|
return;
|
||||||
} else {
|
} else {
|
||||||
/* we don't want to receive anything on this socket */
|
/* we don't want to receive anything on this socket */
|
||||||
setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
|
setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
|
||||||
@ -1458,7 +1421,7 @@ void __send_log(struct proxy *p, int level, char *message, size_t size, char *sd
|
|||||||
goto send;
|
goto send;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
continue; /* must never happen */
|
return; /* must never happen */
|
||||||
}
|
}
|
||||||
|
|
||||||
hdr_size = hdr_ptr - hdr;
|
hdr_size = hdr_ptr - hdr;
|
||||||
@ -1512,14 +1475,13 @@ void __send_log(struct proxy *p, int level, char *message, size_t size, char *sd
|
|||||||
maxlen -= pid_sep1_max;
|
maxlen -= pid_sep1_max;
|
||||||
|
|
||||||
/* pid */
|
/* pid */
|
||||||
pid_max = pid.data;
|
if (unlikely(pid_size >= maxlen)) {
|
||||||
if (unlikely(pid_max >= maxlen)) {
|
pid_size = maxlen - 1;
|
||||||
pid_max = maxlen - 1;
|
|
||||||
sd_max = 0;
|
sd_max = 0;
|
||||||
goto send;
|
goto send;
|
||||||
}
|
}
|
||||||
|
|
||||||
maxlen -= pid_max;
|
maxlen -= pid_size;
|
||||||
|
|
||||||
/* second pid separator */
|
/* second pid separator */
|
||||||
pid_sep2_max = log_formats[logsrv->format].pid.sep2.data;
|
pid_sep2_max = log_formats[logsrv->format].pid.sep2.data;
|
||||||
@ -1542,12 +1504,12 @@ void __send_log(struct proxy *p, int level, char *message, size_t size, char *sd
|
|||||||
send:
|
send:
|
||||||
iovec[0].iov_base = hdr_ptr;
|
iovec[0].iov_base = hdr_ptr;
|
||||||
iovec[0].iov_len = hdr_max;
|
iovec[0].iov_len = hdr_max;
|
||||||
iovec[1].iov_base = tag->area;
|
iovec[1].iov_base = tag_str;
|
||||||
iovec[1].iov_len = tag_max;
|
iovec[1].iov_len = tag_size;
|
||||||
iovec[2].iov_base = pid_sep1;
|
iovec[2].iov_base = pid_sep1;
|
||||||
iovec[2].iov_len = pid_sep1_max;
|
iovec[2].iov_len = pid_sep1_max;
|
||||||
iovec[3].iov_base = pid.area;
|
iovec[3].iov_base = pid_str;
|
||||||
iovec[3].iov_len = pid_max;
|
iovec[3].iov_len = pid_size;
|
||||||
iovec[4].iov_base = pid_sep2;
|
iovec[4].iov_base = pid_sep2;
|
||||||
iovec[4].iov_len = pid_sep2_max;
|
iovec[4].iov_len = pid_sep2_max;
|
||||||
iovec[5].iov_base = sd;
|
iovec[5].iov_base = sd;
|
||||||
@ -1580,6 +1542,55 @@ void __send_log(struct proxy *p, int level, char *message, size_t size, char *sd
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This function sends a syslog message.
|
||||||
|
* It doesn't care about errors nor does it report them.
|
||||||
|
* The arguments <sd> and <sd_size> are used for the structured-data part
|
||||||
|
* in RFC5424 formatted syslog messages.
|
||||||
|
*/
|
||||||
|
void __send_log(struct proxy *p, int level, char *message, size_t size, char *sd, size_t sd_size)
|
||||||
|
{
|
||||||
|
struct list *logsrvs = NULL;
|
||||||
|
struct logsrv *logsrv;
|
||||||
|
int nblogger;
|
||||||
|
static THREAD_LOCAL int curr_pid;
|
||||||
|
static THREAD_LOCAL char pidstr[100];
|
||||||
|
static THREAD_LOCAL struct buffer pid;
|
||||||
|
struct buffer *tag = &global.log_tag;
|
||||||
|
|
||||||
|
if (p == NULL) {
|
||||||
|
if (!LIST_ISEMPTY(&global.logsrvs)) {
|
||||||
|
logsrvs = &global.logsrvs;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (!LIST_ISEMPTY(&p->logsrvs)) {
|
||||||
|
logsrvs = &p->logsrvs;
|
||||||
|
}
|
||||||
|
if (p->log_tag.area) {
|
||||||
|
tag = &p->log_tag;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!logsrvs)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (unlikely(curr_pid != getpid())) {
|
||||||
|
curr_pid = getpid();
|
||||||
|
ltoa_o(curr_pid, pidstr, sizeof(pidstr));
|
||||||
|
chunk_initstr(&pid, pidstr);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Send log messages to syslog server. */
|
||||||
|
nblogger = 0;
|
||||||
|
list_for_each_entry(logsrv, logsrvs, list) {
|
||||||
|
/* we can filter the level of the messages that are sent to each logger */
|
||||||
|
if (level > logsrv->level)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
__do_send_log(logsrv, ++nblogger, pid.area, pid.data, level,
|
||||||
|
message, size, sd, sd_size, tag->area, tag->data);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extern fd_set hdr_encode_map[];
|
extern fd_set hdr_encode_map[];
|
||||||
|
Loading…
Reference in New Issue
Block a user