Simplify log colour handling
This commit is contained in:
+13
-17
@@ -81,15 +81,16 @@ LVLP = "{levelname:8}"
|
|||||||
LVLC = "{levelname:17}"
|
LVLC = "{levelname:17}"
|
||||||
TEXT = "{message:}"
|
TEXT = "{message:}"
|
||||||
|
|
||||||
|
# Read Environment
|
||||||
|
FORCE_COLOR = bool(os.environ.get("FORCE_COLOR"))
|
||||||
|
NO_COLOR = bool(os.environ.get("NO_COLOR"))
|
||||||
|
|
||||||
|
|
||||||
def main(sysArgs: list | None = None) -> GuiMain | None:
|
def main(sysArgs: list | None = None) -> GuiMain | None:
|
||||||
"""Parse command line, set up logging, and launch main GUI."""
|
"""Parse command line, set up logging, and launch main GUI."""
|
||||||
if sysArgs is None:
|
if sysArgs is None:
|
||||||
sysArgs = sys.argv[1:]
|
sysArgs = sys.argv[1:]
|
||||||
|
|
||||||
fColor = os.environ.get("FORCE_COLOR")
|
|
||||||
nColor = os.environ.get("NO_COLOR")
|
|
||||||
|
|
||||||
# Valid Input Options
|
# Valid Input Options
|
||||||
shortOpt = "hvidc"
|
shortOpt = "hvidc"
|
||||||
longOpt = [
|
longOpt = [
|
||||||
@@ -135,7 +136,8 @@ def main(sysArgs: list | None = None) -> GuiMain | None:
|
|||||||
|
|
||||||
# Defaults
|
# Defaults
|
||||||
logLevel = logging.WARN
|
logLevel = logging.WARN
|
||||||
fmtFlags = 0b01 if fColor else 0b00
|
fmtColor = FORCE_COLOR
|
||||||
|
fmtLong = False
|
||||||
confPath = None
|
confPath = None
|
||||||
dataPath = None
|
dataPath = None
|
||||||
qtStyle = "Fusion"
|
qtStyle = "Fusion"
|
||||||
@@ -163,10 +165,10 @@ def main(sysArgs: list | None = None) -> GuiMain | None:
|
|||||||
logLevel = logging.INFO
|
logLevel = logging.INFO
|
||||||
elif inOpt in ("-d", "--debug"):
|
elif inOpt in ("-d", "--debug"):
|
||||||
CONFIG.isDebug = True
|
CONFIG.isDebug = True
|
||||||
fmtFlags = fmtFlags | 0b10
|
fmtLong = True
|
||||||
logLevel = logging.DEBUG
|
logLevel = logging.DEBUG
|
||||||
elif inOpt in ("-c", "--color") and not nColor:
|
elif inOpt in ("-c", "--color"):
|
||||||
fmtFlags = fmtFlags | 0b01
|
fmtColor = not NO_COLOR
|
||||||
elif inOpt == "--meminfo":
|
elif inOpt == "--meminfo":
|
||||||
CONFIG.memInfo = True
|
CONFIG.memInfo = True
|
||||||
elif inOpt == "--style":
|
elif inOpt == "--style":
|
||||||
@@ -176,7 +178,7 @@ def main(sysArgs: list | None = None) -> GuiMain | None:
|
|||||||
elif inOpt == "--data":
|
elif inOpt == "--data":
|
||||||
dataPath = inArg
|
dataPath = inArg
|
||||||
|
|
||||||
if fmtFlags & 0b01:
|
if fmtColor:
|
||||||
# This will overwrite the default level names, and also ensure that
|
# This will overwrite the default level names, and also ensure that
|
||||||
# they can be converted back to integer levels
|
# they can be converted back to integer levels
|
||||||
logging.addLevelName(logging.DEBUG, f"{BLUE}DEBUG{END}")
|
logging.addLevelName(logging.DEBUG, f"{BLUE}DEBUG{END}")
|
||||||
@@ -185,15 +187,9 @@ def main(sysArgs: list | None = None) -> GuiMain | None:
|
|||||||
logging.addLevelName(logging.ERROR, f"{RED}ERROR{END}")
|
logging.addLevelName(logging.ERROR, f"{RED}ERROR{END}")
|
||||||
logging.addLevelName(logging.CRITICAL, f"{RED}CRITICAL{END}")
|
logging.addLevelName(logging.CRITICAL, f"{RED}CRITICAL{END}")
|
||||||
|
|
||||||
# Determine Log Format
|
logTxt = f"{LVLC} {TEXT}" if fmtColor else f"{LVLP} {TEXT}"
|
||||||
if fmtFlags == 0b00:
|
logPos = f"{BLUE}{FILE}{END}:{WHITE}{LINE}{END}" if fmtColor else f"{FILE}:{LINE}"
|
||||||
logFmt = f"{LVLP} {TEXT}"
|
logFmt = f"{TIME} {logPos} {logTxt}" if fmtLong else logTxt
|
||||||
elif fmtFlags == 0b01:
|
|
||||||
logFmt = f"{LVLC} {TEXT}"
|
|
||||||
elif fmtFlags == 0b10:
|
|
||||||
logFmt = f"{TIME} {FILE}:{LINE} {LVLP} {TEXT}"
|
|
||||||
elif fmtFlags == 0b11:
|
|
||||||
logFmt = f"{TIME} {BLUE}{FILE}{END}:{WHITE}{LINE}{END} {LVLC} {TEXT}"
|
|
||||||
|
|
||||||
# Setup Logging
|
# Setup Logging
|
||||||
pkgLogger = logging.getLogger(__package__)
|
pkgLogger = logging.getLogger(__package__)
|
||||||
|
|||||||
Reference in New Issue
Block a user