Add global _did_earlybird to allow main_decorator to coexist.

This way you don't get duplicate handlers.
This commit is contained in:
voussoir 2021-11-08 23:35:00 -08:00
parent b1849ccefa
commit 5a33c9369e
No known key found for this signature in database
GPG key ID: 5F7554F8C26DACCB

View file

@ -38,6 +38,8 @@ root.setLevel(NOTSET)
LOUD = 1
SILENT = 99999999999
_did_earlybird = False
def add_loud(log):
'''
Add the `loud` method to the given logger.
@ -89,7 +91,9 @@ def earlybird_config():
However, this might have the downside of making your program more difficult
to debug because sys.argv is permanently altered.
'''
global _did_earlybird
sys.argv = basic_config_by_argv(sys.argv)
_did_earlybird = True
def get_level_by_argv(argv):
'''
@ -199,7 +203,12 @@ def main_decorator(main):
betterhelp.HELPTEXT_EPILOGUES.add(BETTERHELP_EPILOGUE)
@functools.wraps(main)
def wrapped(argv, *args, **kwargs):
(level, argv) = get_level_by_argv(argv)
add_root_handler(level)
# The reason we don't call basic_config is that another module may have
# attached a root handler for another purpose.
# However we do check _did_earlybird so that we don't get double
# handlers from this module.
if not _did_earlybird:
(level, argv) = get_level_by_argv(argv)
add_root_handler(level)
return main(argv, *args, **kwargs)
return wrapped