def extractErrorMessagesFromLogfile(logfile, targetDir):
    try:
        lineNumber = 0

        curBlock = []
        output = []

        def contains(msg):
            return " ERROR " in msg or msg in " CRITICAL "

        for line in open(logfile, "r"):
            lineNumber += 1

            if line[0] in (' ', '\t') or not curBlock:
                curBlock.append(line.rstrip())
            else:
                #found one - dump it
                if contains("\n".join(curBlock)):
                    output.append("\n".join(curBlock))

                curBlock = []

                curBlock.append(line.rstrip())


        if contains("\n".join(curBlock)):
            output.append("\n".join(curBlock))

        if output:
            fname = os.path.split(logfile)[1]
            finalFname = os.path.join(targetDir, fname + "_errors.txt")

            with open(finalFname, "w") as resultFile:
                for o in output:
                    print >> resultFile, o
            logging.info(
                "Checked %s lines of %s to produce %s lines of error messages in %s",
                lineNumber,
                logfile,
                len(output),
                finalFname
                )
        else:
            logging.info("Checked %s lines of %s, but found no error messages", lineNumber, logfile)
    except:
        logging.error("Failed to extract error messages from %s:\n%s",
                      logfile,
                      traceback.format_exc())
dataset/ETHPy150Open ufora/ufora/ufora/test/MultiMachineTestRunner.py/extractErrorMessagesFromLogfile/swapped operands of `in`