forked from hardbyte/python-can
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.py
More file actions
80 lines (67 loc) · 2.39 KB
/
logger.py
File metadata and controls
80 lines (67 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"""
See the :class:`Logger` class.
"""
import pathlib
import typing
from pkg_resources import iter_entry_points
import can.typechecking
from ..listener import Listener
from .generic import BaseIOHandler
from .asc import ASCWriter
from .blf import BLFWriter
from .canutils import CanutilsLogWriter
from .csv import CSVWriter
from .sqlite import SqliteWriter
from .printer import Printer
class Logger(BaseIOHandler, Listener): # pylint: disable=abstract-method
"""
Logs CAN messages to a file.
The format is determined from the file format which can be one of:
* .asc: :class:`can.ASCWriter`
* .blf :class:`can.BLFWriter`
* .csv: :class:`can.CSVWriter`
* .db: :class:`can.SqliteWriter`
* .log :class:`can.CanutilsLogWriter`
* .txt :class:`can.Printer`
The **filename** may also be *None*, to fall back to :class:`can.Printer`.
The log files may be incomplete until `stop()` is called due to buffering.
.. note::
This class itself is just a dispatcher, and any positional and keyword
arguments are passed on to the returned instance.
"""
fetched_plugins = False
message_writers = {
".asc": ASCWriter,
".blf": BLFWriter,
".csv": CSVWriter,
".db": SqliteWriter,
".log": CanutilsLogWriter,
".txt": Printer,
}
@staticmethod
def __new__(
cls, filename: typing.Optional[can.typechecking.StringPathLike], *args, **kwargs
):
"""
:param filename: the filename/path of the file to write to,
may be a path-like object or None to
instantiate a :class:`~can.Printer`
:raises ValueError: if the filename's suffix is of an unknown file type
"""
if filename is None:
return Printer(*args, **kwargs)
if not Logger.fetched_plugins:
Logger.message_writers.update(
{
writer.name: writer.load()
for writer in iter_entry_points("can.io.message_writer")
}
)
Logger.fetched_plugins = True
suffix = pathlib.PurePath(filename).suffix
try:
return Logger.message_writers[suffix](filename, *args, **kwargs)
except KeyError:
raise ValueError(
f'No write support for this unknown log format "{suffix}"'
) from None