]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: Add `generate_unique_id()` helper
authorTim Duesterhus <tim@bastelstu.be>
Mon, 13 Apr 2026 17:37:27 +0000 (19:37 +0200)
committerWilly Tarreau <w@1wt.eu>
Mon, 13 Apr 2026 18:01:02 +0000 (20:01 +0200)
This new function will handle the actual generation of the unique ID according
to a format. The caller is responsible to check that no unique ID is stored
yet.

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

index 966bc94f103b75bfd6e3d5383e4b94b01505b1c5..98483eee0e7cf2be4834c8898d02f0af0ccd7d31 100644 (file)
@@ -196,6 +196,8 @@ char *update_log_hdr(const time_t time);
 char * get_format_pid_sep1(int format, size_t *len);
 char * get_format_pid_sep2(int format, size_t *len);
 
+void generate_unique_id(struct ist *dst, struct session *sess, struct stream *strm, struct lf_expr *format);
+
 /*
  * Builds a log line for the stream (must be valid).
  */
index ba2d9d1833ecfbaa5b847167e244e64acf4af97d..8d51f0c93e924e1a620d48ff36d6ad5904aac903 100644 (file)
--- a/src/log.c
+++ b/src/log.c
@@ -3879,6 +3879,30 @@ int lf_expr_dup(const struct lf_expr *orig, struct lf_expr *dest)
        return 0;
 }
 
+/* Generates a unique ID based on the given <format> and stores it
+ * in <dst>. <dst> must be IST_NULL when this function is called.
+ *
+ * If this function fails to allocate memory IST_NULL is stored.
+ */
+void generate_unique_id(struct ist *dst, struct session *sess, struct stream *strm, struct lf_expr *format)
+{
+       char *unique_id;
+
+       BUG_ON(isttest(*dst));
+
+       unique_id = pool_alloc(pool_head_uniqueid);
+       if (unique_id == NULL) {
+               *dst = IST_NULL;
+               return;
+       }
+
+       /* Initialize <dst> to an empty string to prevent infinite
+        * recursion when the <format> references %[unique-id] or %ID.
+        */
+       *dst = ist2(unique_id, 0);
+       dst->len = sess_build_logline(sess, strm, unique_id, UNIQUEID_LEN, format);
+}
+
 /* Builds a log line in <dst> based on <lf_expr>, and stops before reaching
  * <maxsize> characters. Returns the size of the output string in characters,
  * not counting the trailing zero which is always added if the resulting size
index eb13cf277605dbdb2c437d245344cc39d2c91a1c..9e026c23de59abb55f8102fa7f8f9cc299d040fa 100644 (file)
@@ -3094,16 +3094,7 @@ INITCALL0(STG_INIT, init_stream);
 struct ist stream_generate_unique_id(struct stream *strm, struct lf_expr *format)
 {
        if (!isttest(strm->unique_id)) {
-               char *unique_id;
-
-               if ((unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
-                       return IST_NULL;
-
-               /* Initialize ->unique_id to an empty string to prevent infinite
-                * recursion when the <format> references %[unique-id] or %ID.
-                */
-               strm->unique_id = ist2(unique_id, 0);
-               strm->unique_id.len = build_logline(strm, unique_id, UNIQUEID_LEN, format);
+               generate_unique_id(&strm->unique_id, strm_sess(strm), strm, format);
        }
 
        return strm->unique_id;