def copy_syslog()

in src/neuron-gatherinfo/neuron-gatherinfo.py [0:0]


def copy_syslog(*, outdir, include_flag=False, verbose):
    '''
        function to copy contents of the syslog to the output directory

        Args:
            outdir          : output directory location where the syslog's contents
                              are to be copied
            include_flag    : if True, include lines that do not match
            verbose : flag to indicate if verbose messages need to be displayed

        Output:
            copy of syslog's contents with just "Neuron-specific" lines

        Returns:
    '''

    # syslog looks like this:
    # 2019-11-21T19:32:50.347183+00:00 ink neuron-rtd[17977]: nrtd[17977]: <SNIP>
    # The first regex (regex1) is used to match lines that we want to see in our copy

    regex1 = re.compile(r'^(\S+)\s.*?({})'.format(SYSLOG_SEARCH_PATTERNS))
    regex2 = re.compile(r'^(\S+)\s')

    osver = get_os_version()
    if osver == 'Ubuntu':
        syslog = '/var/log/syslog'
    else:
        syslog = '/var/log/messages'

    try:
        with open(syslog) as fdin,\
            open(os.path.join(outdir, 'copy-of-syslog'), 'w') as fdout:
            for line in fdin:
                match = regex1.search(line)
                if match is not None:
                    fdout.write(line)
                else:
                    if include_flag:
                        match = regex2.match(line)
                        if match is not None:
                            # exclude the rest of the line
                            fdout.write(match.group(1) + ' XXX contents elided XXX\n')
                        else:
                            print("Error in parsing this line: {}".format(line))
    except FileNotFoundError:
        print("Error, /var/log/syslog not found")