7
7
from __future__ import absolute_import
8
8
9
9
import logging
10
+ import pathlib
11
+ import typing
12
+
13
+ import can .typechecking
10
14
11
15
from ..listener import Listener
12
16
from .generic import BaseIOHandler
22
26
except ImportError :
23
27
MF4Writer = None
24
28
25
- log = logging .getLogger ("can.io.logger" )
26
-
27
29
28
- class Logger (BaseIOHandler , Listener ):
30
+ class Logger (BaseIOHandler , Listener ): # pylint: disable=abstract-method
29
31
"""
30
32
Logs CAN messages to a file.
31
33
@@ -36,41 +38,45 @@ class Logger(BaseIOHandler, Listener):
36
38
* .db: :class:`can.SqliteWriter`
37
39
* .log :class:`can.CanutilsLogWriter`
38
40
* .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`.
40
44
41
45
The log files may be incomplete until `stop()` is called due to buffering.
42
46
43
47
.. 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
45
49
arguments are passed on to the returned instance.
46
50
"""
47
51
48
52
@staticmethod
49
- def __new__ (cls , filename , * args , ** kwargs ):
53
+ def __new__ (
54
+ cls , filename : typing .Optional [can .typechecking .StringPathLike ], * args , ** kwargs
55
+ ):
50
56
"""
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
56
61
"""
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
73
75
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