]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
DEV: flags/show-sess-to-flags: add support for color output
authorWilly Tarreau <w@1wt.eu>
Wed, 10 May 2023 15:48:00 +0000 (17:48 +0200)
committerWilly Tarreau <w@1wt.eu>
Wed, 10 May 2023 15:48:00 +0000 (17:48 +0200)
Highlighting a few fields helps spot them, but only if there are not too
many. What is done here is the following:
  - the first line of each stream is highlighted in white (helps find
    beginning/end in long dumps
  - fields in the form name=value where value starts with upper case
    letters are considered as a state dump (e.g. stconn state) and are
    also highlighted. This results in ~20 pairs. In this case the name
    and value use two different colors (cyan vs yellow) to further help
    find what is being looked for

This is only done when the output is a terminal or when --color=always
is passed. It's also possible to disable it with --color=never or
--no-color.

dev/flags/show-sess-to-flags.sh

index 73e5f9698bfaaca8e08059a70c4f9edfc94778e7..623d0c226ad693372d0ff97b5099f9e37681494a 100755 (executable)
 # out. It's mostly a matter of taste, they're equivalent.
 #
 # Usage: socat /path/to/socket - <<< "show sess all" | ./$0 > output
+#
+# options:
+#    --color=never, --no-color: never colorize output
+#    --color=always: always colorize output (default: only on terminal)
 
 # look for "flags in path then in dev/flags/flags then next to the script"
 FLAGS="${FLAGS:-$(command -v flags)}"
@@ -82,7 +86,15 @@ dump_and_reset() {
 
         line=0
         while [ $line -lt ${#out[@]} ]; do
-                echo "${out[$line]}"
+                if [ -n "$COLOR" ]; then
+                        # highlight name=value for values made of upper case letters
+                        echo "${out[$line]}" | \
+                                sed -e 's,\(^0x.*\),\x1b[1;37m\1\x1b[0m,g' \
+                                    -e 's,\([^ ,=]*\)=\([A-Z][^:, ]*\),\x1b[1;36m\1\x1b[0m=\x1b[1;33m\2\x1b[0m,g'
+
+                else
+                        echo "${out[$line]}"
+                fi
                 ((line++))
         done
 
@@ -102,6 +114,21 @@ dump_and_reset() {
 
 ### main entry point
 
+if [ -t 1 ]; then
+        # terminal on stdout, enable color by default
+        COLOR=1
+else
+        COLOR=
+fi
+
+if [ "$1" == "--no-color" -o "$1" == "--color=never" ]; then
+        shift
+        COLOR=
+elif [ "$1" == "--color=always" ]; then
+        shift
+        COLOR=1
+fi
+
 ctx=strm
 while read -r; do
         [ "$REPLY" != "EOF" ] || break  # for debugging