]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: log: add do_log() logging helper
authorAurelien DARRAGON <adarragon@haproxy.com>
Thu, 26 Sep 2024 08:09:09 +0000 (10:09 +0200)
committerAurelien DARRAGON <adarragon@haproxy.com>
Fri, 4 Oct 2024 19:38:02 +0000 (21:38 +0200)
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.

include/haproxy/log.h
src/log.c

index bb4d5f69476d358027298ad45a5b8a9c8989ba6c..7da9681f53b73b84263f09b82fa7f57917942c81 100644 (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.
index 0f35aa20984a975dada5cd5adf85b8798ad996a5..c4800db06aefafdd15330176269c913b83dc36ce 100644 (file)
--- a/src/log.c
+++ b/src/log.c
@@ -5184,6 +5184,61 @@ out:
 
 }
 
+/*
+ * 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.