import logging
from LCM.Publisher import LcmPublisher

logger = logging.getLogger("backseat")


class SyslogPublisher(LcmPublisher):
    """
    LRAUV logging priorities are:
        CRITICAL:
            * aborts the mission if the backseat component is configured as mission critical.
            * recycles power on the backseat.
            * message is sent to shore.
            * message is displayed on screen console.
        FAULT:
            * message is sent to shore.
            * message is displayed on screen console.
        IMPORTANT:
            * message is sent to shore.
            * message is displayed on screen console.
        ERROR:
        INFO:
            * message is displayed on screen console.
        DEBUG:
            * message NOT displayed on screen console.
    """

    def __init__(self, lcm_instance, source_id=0):
        LcmPublisher.__init__(self, lcm_instance, source_id)

        self.known_priorities = [
            'CRITICAL',
            'FAULT',
            'IMPORTANT',
            'ERROR',
            'INFO',
            'DEBUG'
        ]

    def publish_syslog(self, priority="INFO", msg="", channel_name='tethys_syslog'):
        """
        Publish syslog messages to LRAUV.

        :param priority: syslog entry logging priority.
        :param msg: syslog entry text.
        :param channel_name: LCM channel name (string)
        :return: publishes LCM message
        """
        if priority in self.known_priorities:
            self.add_variable(name=priority, val=msg)
            self.publish(channel_name)
            self.clear_msg()
            return True

        logger.error("Failed to publish syslog message with priority {}".format(priority))
        return False
