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.
This commit is contained in:
Aurelien DARRAGON 2024-09-26 10:09:09 +02:00
parent f6599cf5a6
commit e63c7da508
2 changed files with 58 additions and 0 deletions

View File

@ -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 (<s> 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.

View File

@ -5184,6 +5184,61 @@ int sess_build_logline_orig(struct session *sess, struct stream *s,
}
/*
* opportunistic log when at least the session is known to exist
* <s> 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.