# Copyright (c) 2017 VMware, Inc.
# All rights reserved. -- VMware Confidential
"""
Rsyslog patch module for converting older syslog levels to *.*
"""
import logging
import re
from l10n import msgMetadata as _T, localizedString as _

logger = logging.getLogger(__name__)
SYSLOG_RELAY_CONF = '/etc/vmware-syslog/syslog.conf'


def getChanges():
    return _(_T("rsyslog.patches.syslog_levels.summary",
                "Converts old syslog levels to *.*"))


def doPatching(ctx):
    logger.info("Rsyslog level conversion being executed {0}".format(ctx))
    LOG_LEVEL_REGEX = ('\*.info|\*.notice|\*.warn|\*.error|\*.crit|\*.alert|'
                       '\*.emerg')
    with open(SYSLOG_RELAY_CONF, 'r') as f:
        contents = f.read()
    output = re.sub(LOG_LEVEL_REGEX, '*.*', contents)
    with open(SYSLOG_RELAY_CONF, 'w') as f:
        f.write(output)