Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 21 additions & 13 deletions adafruit_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,24 @@ def write(self, buf: str) -> int:

# The level module-global variables get created when loaded

CRITICAL: int = 50
FATAL = CRITICAL
ERROR: int = 40
WARNING: int = 30
WARN = WARNING
INFO: int = 20
DEBUG: int = 10
NOTSET: int = 0

LEVELS = [
(00, "NOTSET"),
(10, "DEBUG"),
(20, "INFO"),
(30, "WARNING"),
(40, "ERROR"),
(50, "CRITICAL"),
(NOTSET, "NOTSET"),
(DEBUG, "DEBUG"),
(INFO, "INFO"),
(WARNING, "WARNING"),
(ERROR, "ERROR"),
(CRITICAL, "CRITICAL"),
]

for __value, __name in LEVELS:
globals()[__name] = __value


def _level_for(value: int) -> str:
"""Convert a numeric level to the most appropriate name.
Expand Down Expand Up @@ -164,7 +170,7 @@ def __init__(
datefmt: Optional[str] = None,
style: str = "%",
validate: bool = True,
defaults: Dict = None,
defaults=None,
):
self.fmt = fmt
self.datefmt = datefmt
Expand Down Expand Up @@ -263,7 +269,7 @@ class StreamHandler(Handler):

terminator = "\n"

def __init__(self, stream: Optional[WriteableStream] = None) -> None:
def __init__(self, stream: WriteableStream | None = None) -> None:
super().__init__()
if stream is None:
stream = sys.stderr
Expand Down Expand Up @@ -393,7 +399,7 @@ def doRollover(self) -> None:
# Reopen the file.
self.stream = open(self._LogFileName, mode=self._WriteMode)

def GetLogSize(self) -> int:
def GetLogSize(self) -> int | None:
"""Check the size of the log file."""
try:
self.stream.flush() # We need to call this or the file size is always zero.
Expand All @@ -411,8 +417,10 @@ def emit(self, record: LogRecord) -> None:

:param record: The record (message object) to be logged
"""
logsize: int | None = self.GetLogSize()
if (
(self.GetLogSize() >= self._maxBytes)
(logsize is not None)
and (logsize >= self._maxBytes)
and (self._maxBytes > 0)
and (self._backupCount > 0)
):
Expand Down