File: logconvert.py

package info (click to toggle)
python-can 4.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,372 kB
  • sloc: python: 25,840; makefile: 38; sh: 20
file content (65 lines) | stat: -rw-r--r-- 1,541 bytes parent folder | download
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
"""
Convert a log file from one format to another.
"""

import argparse
import errno
import sys

from can import Logger, LogReader, SizedRotatingLogger


class ArgumentParser(argparse.ArgumentParser):
    def error(self, message):
        self.print_help(sys.stderr)
        self.exit(errno.EINVAL, f"{self.prog}: error: {message}\n")


def main():
    parser = ArgumentParser(
        description="Convert a log file from one format to another.",
    )

    parser.add_argument(
        "-s",
        "--file_size",
        dest="file_size",
        type=int,
        help="Maximum file size in bytes. Rotate log file when size threshold is reached.",
        default=None,
    )

    parser.add_argument(
        "input",
        metavar="INFILE",
        type=str,
        help="Input filename. The type is dependent on the suffix, see can.LogReader.",
    )

    parser.add_argument(
        "output",
        metavar="OUTFILE",
        type=str,
        help="Output filename. The type is dependent on the suffix, see can.Logger.",
    )

    args = parser.parse_args()

    with LogReader(args.input) as reader:
        if args.file_size:
            logger = SizedRotatingLogger(
                base_filename=args.output, max_bytes=args.file_size
            )
        else:
            logger = Logger(filename=args.output)

        with logger:
            try:
                for m in reader:
                    logger(m)
            except KeyboardInterrupt:
                sys.exit(1)


if __name__ == "__main__":
    main()