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()
|