log: Colorize logs #26026

pull aureleoules wants to merge 1 commits into bitcoin:master from aureleoules:2022-09-add-log-colors-🌈 changing 5 files +167 −12
  1. aureleoules commented at 10:03 pm on September 6, 2022: member

    This PR adds a new class Color to logging.h which is used to wrap strings easily in ANSI color codes. I think this makes the logs much easier to read.

    I tested this on NixOS with the terminal Kitty and $TERM set to xterm-kitty. I don’t have a Windows lying around so I have not tested on Windows.

    image image

    can also be disabled, like before

    image

  2. DrahtBot added the label Utils/log/libs on Sep 6, 2022
  3. in src/logging.cpp:447 in 4f8349a429 outdated
    445     }
    446 
    447     if (m_log_threadnames && m_started_new_line) {
    448         const auto& threadname = util::ThreadGetInternalName();
    449-        str_prefixed.insert(0, "[" + (threadname.empty() ? "unknown" : threadname) + "] ");
    450+        static auto c{m_colorman({Color::Attribute::FgBlack, Color::Attribute::BgBlue})};
    


    jarolrod commented at 1:19 am on September 7, 2022:

    At the very least, we shouldn’t do any background coloring. This can easily degrade visibility of the information depending on what theme the user has set. This change shouldn’t make any information harder to see.

    master pr

    aureleoules commented at 7:44 am on September 7, 2022:

    Thanks for the feedback, I had not thought of this. I’ve just pushed a change, the thread name is now red without background.

    image

    I’m open to suggestions for the colors.

    Also, if there are issues with the colors, the user can disable them with the env variable shown in the PR description. Maybe adding a bitcoind parameter may be more intuitive for the user though.

  4. jarolrod commented at 1:21 am on September 7, 2022: member

    I like colors in my terminals, and in general i would be Concept ACK on this change.

    However, the glaring issue here is compatibility with users terminals and their color schemes. Right now, without colorizing, we assure that that the information is easy to read with human eyes. This change can cause problems for a lot of themes, and make the information harder to read. It would suck if core forces you to change your theme because you can no longer see some information. At the very least, not setting any background coloring should be an improvement over the current state of the PR.

  5. aureleoules force-pushed on Sep 7, 2022
  6. aureleoules force-pushed on Sep 7, 2022
  7. aureleoules force-pushed on Sep 7, 2022
  8. ghost commented at 3:38 pm on September 7, 2022: none
    Concept ACK 🎨
  9. log: Add colorization utils
    Adds a new class to the logging system, Color, which allows for
    easy colorization of strings. Also adds a new class, ColorMan, to
    the logger which is used to manage the colorization.
    019d06f73d
  10. in src/logging.h:160 in 192aaaa9b3 outdated
    155+    class ColorMan
    156+    {
    157+    public:
    158+        ColorMan()
    159+        {
    160+            m_enable_colors = getenv("TERM") != nullptr && getenv("TERM") != std::string("dumb") && (getenv("ANSI_COLORS_DISABLED") == nullptr || getenv("ANSI_COLORS_DISABLED") == std::string("0"));
    


    kristapsk commented at 5:19 pm on September 7, 2022:
    It might be ok to have colours on terminal by default, but not ok to have ANSI escape sequences written to debug.log by default. So at least isatty() check for stdout should be added. Or changed that colouring is always disabled by default and can be enabled manually.

    aureleoules commented at 6:09 pm on September 7, 2022:
    I just pushed a function that strips colors before writing to the debug file, is this right?
  11. aureleoules force-pushed on Sep 7, 2022
  12. fanquake commented at 3:39 pm on September 8, 2022: member

    Concept NACK - this seems out of scope for Bitcoin Core to maintain, and is never going to work perfectly everywhere, for everyone, leading to endless bikeshedding / “improvements”.

    The Bitcoin Core logging code almost warrants being it’s own externally maintained library at this point, and I don’t think we should be adding to it/expanding scope by introducing more nice-to-have functionality.

  13. amovfx commented at 0:57 am on September 9, 2022: none
    Concept ACK. Damn, I really like this but I can see fanquakes point. I ran the tests and confirmed that it works on macOS with zsh.
  14. hebasto commented at 5:11 am on September 9, 2022: member

    NACK for the following reasons:

    • “is never going to work perfectly everywhere, for everyone, leading to endless bikeshedding” as @fanquake pointed
    • changes into functional tests code

    The Bitcoin Core logging code almost warrants being it’s own externally maintained library at this point

    Sounds good.

  15. MarcoFalke commented at 5:38 am on September 9, 2022: member
    What about an external script that colors the logs? Something like ./src/bitcoind -printtoconsole=1 | ./color.py
  16. kristapsk commented at 5:56 am on September 9, 2022: contributor

    What about an external script that colors the logs? Something like ./src/bitcoind -printtoconsole=1 | ./color.py

    Agree this would be better approach.

  17. aureleoules commented at 9:14 am on September 9, 2022: member

    What about an external script that colors the logs? Something like ./src/bitcoind -printtoconsole=1 | ./color.py

    IMO that might be more difficult to achieve the same result as this PR because this require parsing the logs and identifying each part of the string (date, thread name, file, function, etc.), also they can be enabled/disabled with -logthreadnames -logsourcelocations which imo doesn’t help either.

    How about an opt-in bitcoind parameter instead? Another solution would be to allow users to define their own colors if the default ones don’t work. But this seems like too much work for what it is.

  18. aureleoules commented at 9:28 am on September 9, 2022: member

    The Bitcoin Core logging code almost warrants being it’s own externally maintained library at this point, and I don’t think we should be adding to it/expanding scope by introducing more nice-to-have functionality.

    In this case, should we discuss a logging library to use?

    If colors are not welcome, it would be nice to have at least indented logs to make logs more readable, such as:

    02022-09-09T09:24:18Z [init] [init/common.cpp:149]   [LogPackageVersion]  Bitcoin Core version v23.99.0-19585eeb778a-dirty
    12022-09-09T09:24:18Z [init] [kernel/context.cpp:21] [Context]            Using the 'sse4(1way),sse41(4way),avx2(8way)' SHA256 implementation
    22022-09-09T09:24:18Z [init] [random.cpp:100]        [ReportHardwareRand] Using RdSeed as an additional entropy source
    32022-09-09T09:24:18Z [init] [random.cpp:103]        [ReportHardwareRand] Using RdRand as an additional entropy source
    42022-09-09T09:24:18Z [init] [init/common.cpp:120]   [StartLogging]       Default data directory /home/aureleoules/.bitcoin
    52022-09-09T09:24:18Z [init] [init/common.cpp:121]   [StartLogging]       Using data directory /home/aureleoules/.bitcoin/testnet3
    

    instead of

    02022-09-09T09:24:18Z [init] [init/common.cpp:149] [LogPackageVersion] Bitcoin Core version v23.99.0-19585eeb778a-dirty (release build)
    12022-09-09T09:24:18Z [init] [kernel/context.cpp:21] [Context] Using the 'sse4(1way),sse41(4way),avx2(8way)' SHA256 implementation
    22022-09-09T09:24:18Z [init] [random.cpp:100] [ReportHardwareRand] Using RdSeed as an additional entropy source
    32022-09-09T09:24:18Z [init] [random.cpp:103] [ReportHardwareRand] Using RdRand as an additional entropy source
    42022-09-09T09:24:18Z [init] [init/common.cpp:120] [StartLogging] Default data directory /home/aureleoules/.bitcoin
    52022-09-09T09:24:18Z [init] [init/common.cpp:121] [StartLogging] Using data directory /home/aureleoules/.bitcoin/testnet3
    
  19. MarcoFalke commented at 9:33 am on September 9, 2022: member

    Is it important that each [...] type is coloured the same? You could just define 5 colors and then color each [...] in order, regardless of their type.

    Padding doesn’t work because you don’t know the pad size.

    I wish there was a function to get the color palette from the system (if there is one), so that there would be no need to bikeshed colors, as they are picked by the system user.

  20. fanquake commented at 9:36 am on September 9, 2022: member

    In this case, should we discuss a logging library to use?

    I’m definitely not suggesting we take on a new (external) dependency for logging. Adding colours to logs is not a problem we need to solve. Especially not when there is already an internal migration happening within our logging code, and finishing that would be a much better allocation of engineering time/resources, as opposed to bikeshedding log colouring / adding options to bitcoind to let people colour logs / starting a migration towards another dependency.

  21. hebasto commented at 9:40 am on September 9, 2022: member

    If colors are not welcome…

    Rather maintenance costs they come with.

  22. in src/logging.h:76 in 019d06f73d
    68@@ -68,6 +69,121 @@ namespace BCLog {
    69         BLOCKSTORE  = (1 << 26),
    70         ALL         = ~(uint32_t)0,
    71     };
    72+
    73+
    74+    constexpr auto escape_sequence{"\033["};
    75+
    76+    class Color
    


    jonatack commented at 10:00 am on September 9, 2022:

    logging.h is widely included, so it may be best to move as much new code into the logging.cpp implementation file instead.

    Also, there are refactorings to come in #25203 to de-duplicate and maybe speed up some of the logging.{h,cpp} code, along with benchmarks for it. Could be a good idea to test this rebased on that pull once it is updated and see how the new code performs in the benchmarks. (We are past feature freeze for v24, so this change would target v25 at the earliest.)


    aureleoules commented at 10:26 am on September 9, 2022:
    I will change this but considering there are some Concept NACKS on this PR I’m not sure I should work on this yet.

    jonatack commented at 10:37 am on September 9, 2022:
    Yes, I don’t know. Here is the pull that added colors to -getinfo, if the discussions there are helpful for you: #21832. Of course, -getinfo is much more isolated code than the logging.
  23. jonatack commented at 10:21 am on September 9, 2022: contributor

    Tested on macOS 12.3 (ARM M1 Max). The colorization seemed noisy to me at first, but I would probably end up using it. No strong opinion whether this should be done, or if it should be on or off by default.

    It might be useful to refer to the existing use of colorization in src/bitcoin-cli.cpp for -getinfo (and the -color={always, auto, never} config option) and how it was handled. There hasn’t been bikeshedding or maintainance since it was added IIRC; that experience may or may not transfer to logging.

  24. aureleoules commented at 10:23 am on September 9, 2022: member

    Is it important that each […] type is coloured the same? You could just define 5 colors and then color each […] in order, regardless of their type.

    I’ll try to draft a script.

    Adding colours to logs is not a problem we need to solve

    Sure, but it’s a means to make the logs more readable.

    If colors are not welcome…

    Rather maintenance costs they come with.

    Yes I understand, I meant this as well. (didn’t mean it in an aggressive way)

  25. aureleoules commented at 3:41 pm on September 9, 2022: member
    I’ve drafted an alternative with a python script in #26052.
  26. amovfx commented at 5:52 pm on September 10, 2022: none
    Maybe close this? The python script is awesome btw, Ill be using that even if it doesn’t get merged.
  27. aureleoules commented at 7:27 am on September 12, 2022: member
    Closing in favor of #26052.
  28. aureleoules closed this on Sep 12, 2022

  29. bitcoin locked this on Sep 14, 2023

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-11-21 09:12 UTC

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