def _grep_log_for_errors()

in ccmlib/node.py [0:0]


def _grep_log_for_errors(log):
    except_re = re.compile(r'[Ee]xception|AssertionError')
    log_cat_re = re.compile(r'(\W|^)(INFO|DEBUG|WARN|ERROR)\W')

    def log_line_category(line):
        match = log_cat_re.search(line)
        return match.group(2) if match else None

    matches = []

    loglines = log.splitlines()

    for line_num, line in enumerate(loglines):
        found_exception = False
        line_category = log_line_category(line)
        if line_category == 'ERROR':
            matches.append([line])
            found_exception = True

        elif line_category == 'WARN':
            match = except_re.search(line)
            if match is not None:
                matches.append([line])
                found_exception = True

        if found_exception:
            for next_line_num in range(line_num + 1, len(loglines)):
                next_line = loglines[next_line_num]
                # if a log line can't be identified, assume continuation of an ERROR/WARN exception
                if log_line_category(next_line) is None:
                    matches[-1].append(next_line)
                else:
                    break

    return matches