contrib: Add script to colorize logs #26052

pull aureleoules wants to merge 1 commits into bitcoin:master from aureleoules:2022-09-colorize-output-script changing 2 files +61 −0
  1. aureleoules commented at 3:11 pm on September 9, 2022: member

    Proof of concept and an alternative to #26026. Adds a script to colorize the logs in order to make them more readable. This script removes the burden of maintaining code for colorizing in the actual logger.

    Can be used with ./src/bitcoind | ./contrib/devtools/colorize_logging.py

    image

  2. aureleoules commented at 3:11 pm on September 9, 2022: member
    @MarcoFalke friendly ping since it’s your idea.
  3. aureleoules force-pushed on Sep 9, 2022
  4. DrahtBot added the label Scripts and tools on Sep 9, 2022
  5. aureleoules force-pushed on Sep 9, 2022
  6. amovfx approved
  7. amovfx commented at 6:00 pm on September 9, 2022: none
    Man that is slick! This works over here.
  8. jarolrod commented at 6:11 pm on September 9, 2022: member

    Concept ACK

    Wouldn’t be controversial to have this in contrib while we have logging; better approach than essentially forcing it all the time with an environmental opt out in #26026. In terms of color compatibility with themes, some documentation can a) point out that this script exists and b) explain how one could change the colors to their liking

  9. unknown approved
  10. w0xlt approved
  11. w0xlt commented at 3:05 am on September 10, 2022: contributor

    ACK a9fd4016c1767b724d4b36d211e206fee27c3d36

    It worked as expected on Ubuntu 21.10. This approach is preferable to #26026.

  12. in contrib/devtools/colorize.py:12 in a9fd4016c1 outdated
     7+Usage: ./bitcoind | contrib/devtools/colorize.py
     8+'''
     9+import re
    10+
    11+colors = ("cyan", "red", "blue", "green", "magenta")
    12+color_codes = ("\033[36m", "\033[31m", "\033[34m", "\033[32m", "\033[35m")
    


    w0xlt commented at 3:09 am on September 10, 2022:

    Maybe a dict ?

    0colors = {"cyan": "\033[36m", "red": "\033[31m", "blue": "\033[34m", "green": "\033[32m", "magenta": "\033[35m"}
    
  13. in contrib/devtools/colorize.py:17 in a9fd4016c1 outdated
    12+color_codes = ("\033[36m", "\033[31m", "\033[34m", "\033[32m", "\033[35m")
    13+
    14+def colorize(text):
    15+    # Colorize groups of brackets
    16+    for i, match in enumerate(re.finditer(r"\[.*?\]", text)):
    17+        text = text.replace(match.group(0), color_codes[(i + 1) % len(colors)] + match.group(0) + "\033[0m")
    


    w0xlt commented at 3:18 am on September 10, 2022:

    Since the colors variable is being used only to calculate len(colors), it can be removed.

    0        text = text.replace(match.group(0), color_codes[(i + 1) % len(color_codes)] + match.group(0) + "\033[0m")
    
  14. kristapsk commented at 9:58 am on September 10, 2022: contributor
    Concept ACK. Clearly prefer this over #26026.
  15. luke-jr commented at 11:38 pm on September 10, 2022: member

    Concept ACK

    Does it need to set stdin/out to line-buffered to accept realtime piping to it?

  16. aureleoules force-pushed on Sep 12, 2022
  17. aureleoules commented at 7:38 am on September 12, 2022: member

    Addressed your suggestions @w0xlt, thanks.

    Does it need to set stdin/out to line-buffered to accept realtime piping to it?

    I’m unfamiliar with Python, not sure if there are better methods.

  18. in contrib/devtools/colorize.py:7 in d804390718 outdated
    0@@ -0,0 +1,32 @@
    1+#!/usr/bin/env python3
    2+# Copyright (c) 2022 The Bitcoin Core developers
    3+# Distributed under the MIT software license, see the accompanying
    4+# file COPYING or http://www.opensource.org/licenses/mit-license.php.
    5+'''
    6+Colorize log output
    7+Usage: ./bitcoind | contrib/devtools/colorize.py
    


    jonatack commented at 8:19 am on September 12, 2022:

    missing src/

    0Usage: ./src/bitcoind | ./contrib/devtools/colorize.py
    

    aureleoules commented at 8:23 am on September 12, 2022:
    pushed, thanks
  19. jonatack commented at 8:21 am on September 12, 2022: contributor
    Tested and works on macOS monterey 12.3
  20. aureleoules force-pushed on Sep 12, 2022
  21. in contrib/devtools/colorize.py:32 in f2282a6da9 outdated
    27+    try:
    28+        for line in sys.stdin:
    29+            print(colorize(line), end="")
    30+    except KeyboardInterrupt:
    31+        print()
    32+        sys.exit(0)
    


    jonatack commented at 8:27 am on September 12, 2022:

    Should the file be named colorize_logging.py (or colorize_logs.py)?

     0@@ -2,19 +2,23 @@
     1 # Copyright (c) 2022 The Bitcoin Core developers
     2 # Distributed under the MIT software license, see the accompanying
     3 # file COPYING or http://www.opensource.org/licenses/mit-license.php.
     4-'''
     5+"""
     6 Colorize log output
     7 Usage: ./bitcoind | contrib/devtools/colorize.py
     8-'''
     9+"""
    10 import re
    11 
    12 colors = ("cyan", "red", "blue", "green", "magenta")
    13 color_codes = ("\033[36m", "\033[31m", "\033[34m", "\033[32m", "\033[35m")
    14 
    15+
    16 def colorize(text):
    17     # Colorize groups of brackets
    18     for i, match in enumerate(re.finditer(r"\[.*?\]", text)):
    19-        text = text.replace(match.group(0), color_codes[(i + 1) % len(colors)] + match.group(0) + "\033[0m")
    20+        text = text.replace(
    21+            match.group(0),
    22+            color_codes[(i + 1) % len(colors)] + match.group(0) + "\033[0m",
    23+        )
    24 
    25     # Colorize date
    26     date = re.search(r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z", text)
    27@@ -23,8 +27,10 @@ def colorize(text):
    28 
    29     return text
    30 
    31+
    32 if __name__ == "__main__":
    33     import sys
    34+
    35     try:
    36         for line in sys.stdin:
    37             print(colorize(line), end="")
    

    aureleoules commented at 8:34 am on September 12, 2022:
    pushed thanks
  22. aureleoules force-pushed on Sep 12, 2022
  23. in contrib/devtools/colorize_logging.py:11 in a6c16288f0 outdated
     6+Colorize log output
     7+Usage: ./src/bitcoind | ./contrib/devtools/colorize_logging.py
     8+"""
     9+import re
    10+
    11+colors = {"cyan": "\033[36m", "red": "\033[31m", "blue": "\033[34m", "green": "\033[32m", "magenta": "\033[35m"}
    


    jonatack commented at 8:40 pm on September 14, 2022:

    naming nit, maybe COLORS for the constant

    black suggestion:

    0-colors = {"cyan": "\033[36m", "red": "\033[31m", "blue": "\033[34m", "green": "\033[32m", "magenta": "\033[35m"}
    1+colors = {
    2+    "cyan": "\033[36m",
    3+    "red": "\033[31m",
    4+    "blue": "\033[34m",
    5+    "green": "\033[32m",
    6+    "magenta": "\033[35m",
    7+}
    
  24. jonatack commented at 8:41 pm on September 14, 2022: contributor

    Seeing an issue with the latest push.

    0(a6c16288f0...):~/bitcoin/bitcoin$ ./src/bitcoind | ./contrib/devtools/colorize_logging.py
    1Traceback (most recent call last):
    2  File "/bitcoin/./contrib/devtools/colorize_logging.py", line 41, in <module>
    3    print(colorize(line), end="")
    4  File "/bitcoin/./contrib/devtools/colorize_logging.py", line 25, in colorize
    5    colors[(i + 1) % len(colors)] + match.group(0) + "\033[0m",
    6KeyError: 1
    
  25. aureleoules force-pushed on Sep 14, 2022
  26. aureleoules force-pushed on Sep 14, 2022
  27. aureleoules commented at 10:41 pm on September 14, 2022: member
    Fixed the script and addressed the comments, thanks @jonatack.
  28. jarolrod commented at 8:00 pm on September 19, 2022: member

    Example of how we could document it’s existence and hint towards user customization options so it can be compatible with everyone’s color scheme:

     0
     1diff --git a/contrib/devtools/README.md b/contrib/devtools/README.md
     2index 54b1a8558..63aa4a483 100644
     3--- a/contrib/devtools/README.md
     4+++ b/contrib/devtools/README.md
     5@@ -17,6 +17,22 @@ the script should be called from the git root folder as follows.
     6 git diff -U0 HEAD~1.. | ./contrib/devtools/clang-format-diff.py -p1 -i -v
     7+colorize_logging.py
     8+===================
     9+
    10+Colorizes the terminal log output of `bitcoind` or `bitcoin-qt`.
    11+Colors can be customized by changing the script's color definitions.
    12+
    13+Can be used by running the following command:
    14+
    15+```
    16+./src/bitcoind | ./contrib/devtools/colorize.py
    17+```
    18+
    19+```
    20+./src/qt/bitcoin-qt -printtoconsole | ./contrib/devtools/colorize_logging.py
    21+```
    22+
    23 copyright\_header.py
    24 ====================
    
  29. DrahtBot commented at 4:01 am on September 23, 2022: contributor

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    Conflicts

    No conflicts as of last run.

  30. contrib: Add script to colorize logs 904e3e454a
  31. aureleoules force-pushed on Oct 7, 2022
  32. aureleoules closed this on Oct 12, 2022

  33. aureleoules deleted the branch on Nov 2, 2022
  34. luke-jr commented at 10:21 pm on July 25, 2023: member
    Why was this deleted? :/
  35. bitcoin locked this on Jul 24, 2024

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2024-09-28 22:12 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me