#! /usr/bin/python

import sys
import subprocess
import os

ROOT_PATH = "/usr/bin/"

enable ="1"
disable ="0"

str_to_id_map = {}

def populate_string_to_id_map():
    p = subprocess.check_output(ROOT_PATH+"event_log_cli info", shell=True)
    for i in p.split("\n"):
        l = i.split()
        if len(l) > 0 and l[0] and "=" not in l[0] and l[0] != "Log" and l[0] != "Module":
            str_to_id_map[l[0]] = l[1]

def get_arg_str(arg_str):
    if arg_str.isdigit():
        return arg_str
    else:
        return str_to_id_map[arg_str]

def show_id():
    os.system(ROOT_PATH+"event_log_cli info")

def usage():
    print ""
    print "======================================================================================================================"
    print  "[show-id]                                                  - displays ids of modules, log-levels and sub-levels"
    print  "[show] [all] | [module-id] {log-level} {log-sub-level}     - displays current logging status for all/given "
    print  "                                                             sub-sytem/given module and log-level/given"
    print  "                                                             module, log-level and sub-log-level"
    print  "[enable] [all] | [module-id] {log-level } {log-sub-level}  - Enables logging status for all/given "
    print  "                                                             sub-sytem/given module and log-level/given "
    print  "                                                             module, log-level and sub-log-level"
    print  "[disable] [all] | [module-id] {log-level } {log-sub-level} - Disables logging status for all/given "
    print  "                                                             sub-sytem/given module and log-level/given "
    print  "                                                             module, log-level and sub-log-level\n"
    print  "NOTE :1. For enable and disable log-level and log-sub-level is optional when using module-id."
    print  "         If only module-id is given it will enable/disable all log-levels and log-sub-levels for that  "
    print  "         module-id, similarly if module-id and log-level is given, it will enable All log-sublevel for "
    print  "         the module-id and log-level\n"
    print  "      2. Instead of Module Ids now you can use the module name as string as well, for eg."
    print  "         sonic_logging_cli enable L3_SERVICES "
    print "======================================================================================================================="
    print ""
    sys.exit()


def arg_validation():
    if len(sys.argv) == 1:
        usage()
    if sys.argv[1] == "load":
        return;
    if sys.argv[1] != "show" and sys.argv[1] != "enable" and sys.argv[1] != "disable" and sys.argv[1] != "console" and sys.argv[1] != "show-id" :
        usage()
    if (sys.argv[1] == "show" or sys.argv[1] == "enable" or sys.argv[1] == "disable") and len(sys.argv) < 3:
        usage()


def parse_args():
    arg_str = ""
    if sys.argv[2] == "all":
        arg_str = "-1 -1 -1"
        return arg_str

    if len(sys.argv) ==3 :
        arg_str = get_arg_str(sys.argv[2]) +" -1 -1"
        return arg_str
    elif len(sys.argv) ==4 :
        arg_str = get_arg_str(sys.argv[2])+" "+get_arg_str(sys.argv[3])+" -1"
        return arg_str
    elif len(sys.argv) ==5 :
        arg_str = get_arg_str(sys.argv[2])+" "+get_arg_str(sys.argv[3])+" "+get_arg_str(sys.argv[4])
        return arg_str
    else:
        print ""
        print "Error :: Invalid system/log_level id/log sublevel id"
        show_id()

def logging_cli_handler():
    if sys.argv[1] == "load":
        os.system(ROOT_PATH+"event_log_cli load")
    if sys.argv[1] == "show":
        os.system(ROOT_PATH+"event_log_cli show "+parse_args())
    if sys.argv[1] == "enable":
        populate_string_to_id_map()
        os.system(ROOT_PATH+"event_log_cli enable "+parse_args())
    if sys.argv[1] == "disable":
        populate_string_to_id_map()
        os.system(ROOT_PATH+"event_log_cli disable "+parse_args())

    if sys.argv[1] == "show-id":
        show_id()

def main():
    arg_validation()
    logging_cli_handler()

if __name__ == '__main__':
    main()
