]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: flags: implement a macro used to dump enums inside masks
authorWilly Tarreau <w@1wt.eu>
Fri, 9 Sep 2022 14:05:10 +0000 (16:05 +0200)
committerWilly Tarreau <w@1wt.eu>
Fri, 9 Sep 2022 14:15:10 +0000 (16:15 +0200)
Some of our flags have enums inside a mask. The new macro __APPEND_ENUM
is able to deal with that by comparing the flag's value against an exact
one under the mask. One needs to take care of eliminating the zero value
though, otherwise delimiters will not always be properly placed (e.g. if
some flags were dumped before and what remains is exactly zero). The
bits of the mask are cleared only upon exact matches.

include/haproxy/show_flags-t.h

index 3c045944eda54104b25cf7c4d16245dffc9e6821..72176618b1b2ea9f00b551d737ee9d9feba8a91d 100644 (file)
  *       _(X_FLAG1, _(X_FLAG2, _(X_FLAG3)));
  *       _(~0);
  *    #undef _
+ *
+ * __APPEND_ENUM() works a bit differently in that it takes an additional mask
+ * to isolate bits to compare to the enum's value, and will remove the mask's
+ * bits at once in case of match.
  */
 #ifdef EOF
 
                }                                                               \
        } while (0)
 
+#define __APPEND_ENUM(_buf, _len, _del, _flg, _msk, _val, _nam, ...)   \
+       do {                                                            \
+               size_t _ret = 0;                                        \
+               do { __VA_ARGS__; } while (0);                          \
+               if (((_flg) & (_msk)) == (_val)) {                      \
+                       (_flg) &= ~(_msk);                              \
+                       _ret = snprintf(_buf, _len, _nam "%s",          \
+                                       (_flg) ? (_del) : "");          \
+               }                                                       \
+               if (_ret < _len) {                                      \
+                       _len -= _ret;                                   \
+                       _buf += _ret;                                   \
+               }                                                       \
+       } while (0)
+
 #else /* EOF not defined => no stdio, do nothing */
 
 #define __APPEND_FLAG(_buf, _len, _del, _flg, _val, _nam)   do { } while (0)
+#define __APPEND_ENUM(_buf, _len, _del, _flg, _msk, _val, _nam)   do { } while (0)
 
 #endif /* EOF */