"""
@file wrtd-logging.py
@copyright: Copyright (c) 2019 CERN (home.cern)

SPDX-License-Identifier: LGPL-3.0-or-later
"""

import sys
import signal
import argparse

from PyWrtd import *

def signal_handler(sig, frame):
	sys.exit(0)

def print_logging(wrtd, count):
    n_read = 0

    while ((count == 0) or (n_read < count)):
        log_entry = wrtd.get_next_event_log_entry()
        if len(log_entry):
            print(log_entry)
            n_read += 1

def main():
    signal.signal(signal.SIGINT, signal_handler)

    parser = argparse.ArgumentParser(description='WRTD node log monitoring tool')
    parser.add_argument('-D', '--dev-id', dest='dev', required=True, type=int,
                        help='MockTurtle device ID (integer) to open')
    parser.add_argument('-c', '--count', dest='count', type=int, default=0,
                        help='Number of entries to read (0 = infinite)')

    args = parser.parse_args()

    dev = 'MT' + str(args.dev)

    wrtd = PyWrtd(dev)

    print_logging(wrtd, args.count)

if __name__ == "__main__":
    main()
