From: Aurelien DARRAGON Date: Thu, 26 Sep 2024 08:09:09 +0000 (+0200) Subject: MINOR: log: add do_log() logging helper X-Git-Tag: v3.1-dev10~112 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e63c7da5085702c1e3782dc135655bcd1a442d9c;p=thirdparty%2Fhaproxy.git MINOR: log: add do_log() logging helper do_log() is quite similar to sess_log() or strm_log(), excepts that it may be called at any time during session handling in an opportunistic way as long as the session exists (the stream may or may not exist). Also, it will try to emit the log as INFO by default, unless set-log-level is used on the stream, or error origin flag is set. --- diff --git a/include/haproxy/log.h b/include/haproxy/log.h index bb4d5f6947..7da9681f53 100644 --- a/include/haproxy/log.h +++ b/include/haproxy/log.h @@ -105,6 +105,9 @@ static inline int sess_build_logline(struct session *sess, struct stream *s, cha log_orig(LOG_ORIG_UNSPEC, LOG_ORIG_FL_NONE)); } +/* opportunistic log when session already exists ( may be null) */ +void do_log(struct session *sess, struct stream *s, struct log_orig origin); + /* * send a log for the stream when we have enough info about it. * Will not log if the frontend has no log defined. diff --git a/src/log.c b/src/log.c index 0f35aa2098..c4800db06a 100644 --- a/src/log.c +++ b/src/log.c @@ -5184,6 +5184,61 @@ out: } +/* + * opportunistic log when at least the session is known to exist + * may be NULL + * + * Will not log if the frontend has no log defined. By default it will + * try to emit the log as INFO, unless the stream already exists and + * set-log-level was used. + */ +void do_log(struct session *sess, struct stream *s, struct log_orig origin) +{ + int size; + int sd_size = 0; + int level = -1; + + if (LIST_ISEMPTY(&sess->fe->loggers)) + return; + + if (s) { + if (s->logs.level) { /* loglevel was overridden */ + if (s->logs.level == -1) { + /* log disabled */ + return; + } + level = s->logs.level - 1; + } + /* if unique-id was not generated */ + if (!isttest(s->unique_id) && !lf_expr_isempty(&sess->fe->format_unique_id)) { + stream_generate_unique_id(s, &sess->fe->format_unique_id); + } + } + + if (level == -1) { + level = LOG_INFO; + if ((origin.flags & LOG_ORIG_FL_ERROR) && + (sess->fe->options2 & PR_O2_LOGERRORS)) + level = LOG_ERR; + } + + if (!lf_expr_isempty(&sess->fe->logformat_sd)) { + sd_size = sess_build_logline_orig(sess, s, logline_rfc5424, global.max_syslog_len, + &sess->fe->logformat_sd, origin); + } + + size = sess_build_logline_orig(sess, s, logline, global.max_syslog_len, &sess->fe->logformat, origin); + if (size > 0) { + struct process_send_log_ctx ctx; + + ctx.origin = origin; + ctx.sess = sess; + ctx.stream = s; + __send_log(&ctx, &sess->fe->loggers, &sess->fe->log_tag, level, + logline, size + 1, logline_rfc5424, sd_size); + } +} + /* * send a log for the stream when we have enough info about it. * Will not log if the frontend has no log defined.