kvz / bash3boilerplate

Templates to write better Bash scripts
http://bash3boilerplate.sh
MIT License
2.12k stars 198 forks source link

Redeclaring colors in b3bp_log function #91

Closed morbeo closed 7 years ago

morbeo commented 7 years ago

I noticed that the logging colors are defined in the __b3bp_log function and that they are set every time the function is called. Highly unlikely it have a noticeable impact on performance, but debugging stuff is terrible with the overhead. Is it necessary to redeclare them on each call? Of course I can move them outside myself, but if there is a specific reasoning for this structure I would like to hear it.

zbeekman commented 7 years ago

By declaring them this way, they do not pollute the global variables. Sometimes b3bp may be sourced rather than called directly, so it can be nice to encapsulate this data within a function and provide some data hiding.

How are you debugging? Are you using set -o xtrace (AKA set -x) or are you sprinkling info calls etc throughout your code? Have you confirmed that it is indeed the setting of these variables that is causing the slowdown?

zbeekman commented 7 years ago

@morbeo if you are willing to share a profile of your script using https://github.com/F-Hauri/bashProfiler I'd be curious to see how bad it is.

morbeo commented 7 years ago

The colors do look kind of global. I mean, are you going to change them? :) There is no slowdown at all, I am sorry if I implied there is. Yes, it is about xtrace. I am just running the example LOG_LEVEL=7 ./boiler.sh -f /tmp/x -d Basically any output is from that function includes the color variables declaration and that annoys me a lot :) I guess I will rewrite it to my liking.