Skip to content

Commit b849f00

Browse files
authored
Add files via upload
1 parent 7cc8a3c commit b849f00

1 file changed

Lines changed: 82 additions & 18 deletions

File tree

pycatfile.py

Lines changed: 82 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -785,31 +785,95 @@ def _get(section_dict, key, default=None):
785785
scrcmd.wait()
786786

787787

788-
def VerbosePrintOut(dbgtxt, outtype="log", dbgenable=True, dgblevel=20):
789-
if(not dbgenable):
790-
return True
791-
log_functions = {
792-
"print": print,
793-
"log": logging.info,
794-
"warning": logging.warning,
795-
"error": logging.error,
796-
"critical": logging.critical,
797-
"exception": logging.exception,
798-
"logalt": lambda x: logging.log(dgblevel, x),
799-
"debug": logging.debug
800-
}
801-
log_function = log_functions.get(outtype)
802-
if(log_function):
803-
log_function(dbgtxt)
788+
# Use a module logger instead of the root logger
789+
_logger = logging.getLogger(__name__)
790+
791+
# Map common level names (case-insensitive) to numeric levels
792+
_LEVEL_BY_NAME = {
793+
"debug": logging.DEBUG,
794+
"info": logging.INFO,
795+
"warning": logging.WARNING,
796+
"error": logging.ERROR,
797+
"critical": logging.CRITICAL,
798+
}
799+
800+
def VerbosePrintOut(dbgtxt, outtype="log", dbgenable=True, dgblevel=20, **kwargs):
801+
"""
802+
Python 2/3-safe logging switchboard.
803+
804+
Args:
805+
dbgtxt: message to emit (any object; coerced to text).
806+
outtype: 'print', a level name (info/warning/error/critical/debug),
807+
an ALL-CAPS logging constant name ('INFO', 'WARNING', ...),
808+
an integer level, or 'log' to use dgblevel.
809+
dbgenable: if False, skip emitting and return False.
810+
dgblevel: numeric level used when outtype is 'log' or unmapped.
811+
**kwargs: passed to logging (e.g., exc_info=True, stacklevel=2, extra=...).
812+
813+
Returns:
814+
True if something was emitted; False otherwise.
815+
"""
816+
if not dbgenable:
817+
return False
818+
819+
logger = kwargs.pop("logger", None) or _logger
820+
msg = _to_text(dbgtxt)
821+
822+
# Normalize outtype
823+
lvl = None
824+
if isinstance(outtype, int):
825+
lvl = outtype
826+
route = "logging"
827+
else:
828+
name = (outtype or "log")
829+
if isinstance(name, string_types):
830+
name_l = name.lower()
831+
if name_l == "print":
832+
print(msg)
833+
return True
834+
if name_l in _LEVEL_BY_NAME:
835+
lvl = _LEVEL_BY_NAME[name_l]
836+
route = "logging"
837+
elif name.isupper() and hasattr(logging, name):
838+
# Accept 'INFO', 'WARNING', etc.
839+
lvl = getattr(logging, name)
840+
route = "logging"
841+
elif name_l in ("log", "logalt"):
842+
lvl = int(dgblevel)
843+
route = "logging"
844+
elif name_l == "exception":
845+
# Safer: only include exc_info if the caller asked for it
846+
lvl = logging.ERROR
847+
kwargs.setdefault("exc_info", True)
848+
route = "logging"
849+
else:
850+
# Unknown string → fall back to dgblevel
851+
lvl = int(dgblevel)
852+
route = "logging"
853+
else:
854+
# Unknown type → fallback
855+
lvl = int(dgblevel)
856+
route = "logging"
857+
858+
if route == "logging":
859+
if not logger.isEnabledFor(lvl):
860+
return False
861+
logger.log(lvl, msg, **kwargs)
804862
return True
863+
805864
return False
806865

807866

808-
def VerbosePrintOutReturn(dbgtxt, outtype="log", dbgenable=True, dgblevel=20):
809-
VerbosePrintOut(dbgtxt, outtype, dbgenable, dgblevel)
867+
def VerbosePrintOutReturn(dbgtxt, outtype="log", dbgenable=True, dgblevel=20, **kwargs):
868+
"""
869+
Log/print dbgtxt (per VerbosePrintOut) and return dbgtxt unchanged.
870+
Useful for tap-style debugging in pipelines.
871+
"""
872+
VerbosePrintOut(dbgtxt, outtype, dbgenable, dgblevel, **kwargs)
810873
return dbgtxt
811874

812875

876+
813877
def _split_posix(path_text):
814878
"""Split POSIX paths regardless of OS; return list of components."""
815879
# Normalize leading './'

0 commit comments

Comments
 (0)