#!/usr/bin/env python
# ***************************************************************************
# Copyright 2020 VMware, Inc.  All rights reserved. VMware Confidential.
# ***************************************************************************
# $Id$
# $DateTime$
# $Change$
# $Author$:
# ***************************************************************************

import argparse
import sys
sys.path.append("/opt/vmware/nsx-common/python")
import nsx_logging


_LOG_LEVELS = {
    "debug": nsx_logging.DEBUG,
    "info": nsx_logging.INFO,
    "warning": nsx_logging.WARNING,
    "error": nsx_logging.ERROR,
    "critical": nsx_logging.CRITICAL
}


def main():
    arg_parser = argparse.ArgumentParser()
    arg_parser.add_argument('-n', '--name', type=str,
                            help=('logger name'))
    arg_parser.add_argument('-l', '--level', type=str,
                            choices=_LOG_LEVELS.keys(), help=('log level'))
    arg_parser.add_argument('msg', metavar='msg', type=str, nargs='+',
                            help='log message')
    arg_parser.add_argument('-m', '--msgid', type=str,
                            help=('message ID'))
    arg_parser.add_argument('-c', '--comp', type=str,
                            help=('value of the comp structured data field'))
    arg_parser.add_argument('-s', '--subcomp', type=str, required=True,
                            help=('value of the subcomp structured data field'))
    arg_parser.add_argument('-t', '--tag', type=str,
                            help=('comma separated list of tags in the form'
                                  ' of "key=value"'))
    args = arg_parser.parse_args()
    lg = nsx_logging.getLogger(args.name)
    nsx_logging.basicConfig(syslog=True, comp=args.comp, subcomp=args.subcomp,
        msgid=args.msgid or "-")
    lg.setLevel(nsx_logging.INFO)
    tags = {}
    if not args.level:
        args.level = "info"
    if args.tag:
        tags = dict(item.strip().split("=") for item in args.tag.split(","))
    lg.log(_LOG_LEVELS[args.level], " ".join(args.msg), **tags)


if __name__ == '__main__':
    main()
