Skip to content

Commit 22f3d64

Browse files
committed
re-add change to can.io.Logger; it somehow went lost while rebasing
1 parent e42126c commit 22f3d64

File tree

1 file changed

+36
-30
lines changed

1 file changed

+36
-30
lines changed

can/io/logger.py

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
from __future__ import absolute_import
88

99
import logging
10+
import pathlib
11+
import typing
12+
13+
import can.typechecking
1014

1115
from ..listener import Listener
1216
from .generic import BaseIOHandler
@@ -22,10 +26,8 @@
2226
except ImportError:
2327
MF4Writer = None
2428

25-
log = logging.getLogger("can.io.logger")
26-
2729

28-
class Logger(BaseIOHandler, Listener):
30+
class Logger(BaseIOHandler, Listener): # pylint: disable=abstract-method
2931
"""
3032
Logs CAN messages to a file.
3133
@@ -36,41 +38,45 @@ class Logger(BaseIOHandler, Listener):
3638
* .db: :class:`can.SqliteWriter`
3739
* .log :class:`can.CanutilsLogWriter`
3840
* .mf4 :class:`can.MF4Writer`
39-
* other: :class:`can.Printer`
41+
* .txt :class:`can.Printer`
42+
43+
The **filename** may also be *None*, to fall back to :class:`can.Printer`.
4044
4145
The log files may be incomplete until `stop()` is called due to buffering.
4246
4347
.. note::
44-
This class itself is just a dispatcher, and any positional an keyword
48+
This class itself is just a dispatcher, and any positional and keyword
4549
arguments are passed on to the returned instance.
4650
"""
4751

4852
@staticmethod
49-
def __new__(cls, filename, *args, **kwargs):
53+
def __new__(
54+
cls, filename: typing.Optional[can.typechecking.StringPathLike], *args, **kwargs
55+
):
5056
"""
51-
:type filename: str or None or path-like
52-
:param filename: the filename/path the file to write to,
53-
may be a path-like object if the target logger supports
54-
it, and may be None to instantiate a :class:`~can.Printer`
55-
57+
:param filename: the filename/path of the file to write to,
58+
may be a path-like object or None to
59+
instantiate a :class:`~can.Printer`
60+
:raises ValueError: if the filename's suffix is of an unknown file type
5661
"""
57-
if filename:
58-
if filename.endswith(".asc"):
59-
return ASCWriter(filename, *args, **kwargs)
60-
elif filename.endswith(".blf"):
61-
return BLFWriter(filename, *args, **kwargs)
62-
elif filename.endswith(".csv"):
63-
return CSVWriter(filename, *args, **kwargs)
64-
elif filename.endswith(".db"):
65-
return SqliteWriter(filename, *args, **kwargs)
66-
elif filename.endswith(".log"):
67-
return CanutilsLogWriter(filename, *args, **kwargs)
68-
elif filename.endswith(".mf4"):
69-
if MF4Writer is not None:
70-
return MF4Writer(filename, *args, **kwargs)
71-
else:
72-
log.info('Could not import MF4 logger, falling pack to can.Printer')
62+
if filename is None:
63+
return Printer(*args, **kwargs)
64+
65+
lookup = {
66+
".asc": ASCWriter,
67+
".blf": BLFWriter,
68+
".csv": CSVWriter,
69+
".db": SqliteWriter,
70+
".log": CanutilsLogWriter,
71+
".txt": Printer,
72+
}
73+
if MF4Writer is not None:
74+
lookup[".mf4"] = MF4Writer
7375

74-
# else:
75-
log.info('unknown file type "%s", falling pack to can.Printer', filename)
76-
return Printer(filename, *args, **kwargs)
76+
suffix = pathlib.PurePath(filename).suffix
77+
try:
78+
return lookup[suffix](filename, *args, **kwargs)
79+
except KeyError:
80+
raise ValueError(
81+
f'No write support for this unknown log format "{suffix}"'
82+
) from None

0 commit comments

Comments
 (0)