Search in sources :

Example 1 with LogParser

use of com.emc.storageos.systemservices.impl.logsvc.parse.LogParser in project coprhd-controller by CoprHD.

the class LogReader method readNextLogMessage.

/**
 * Read one log message from log file
 *
 * @return
 * @throws IOException
 */
@Override
public LogMessage readNextLogMessage() {
    // logger.info("readMessage()");
    try {
        if (reader == null) {
            return null;
        }
        while (true) {
            String line = reader.readLine();
            if (line == null) {
                close();
                if (currentLog != null && matchRegex(currentLog)) {
                    incrementLogCount(currentLog);
                    return currentLog;
                }
                return null;
            }
            fileLineNumber++;
            LogMessage nextLog = null;
            if (parser != null) {
                // already find the match parser
                nextLog = parser.parseLine(line, request);
            } else {
                // match parser
                for (LogParser parser : parserTable) {
                    nextLog = parser.parseLine(line, request);
                    if (!nextLog.isContinuation()) {
                        this.parser = parser;
                        break;
                    }
                }
                // this line does not match all parsers, skip it and write it into status
                if (nextLog == null || nextLog.isContinuation()) {
                    status.appendInfo(this.filePath, fileLineNumber);
                    continue;
                }
            }
            if (nextLog.isContinuation()) {
                if (currentLog != null) {
                    currentLog.appendMessage(LogUtil.stringToBytes(line));
                }
            } else if (nextLog.isRejected()) {
                // logs are only rejected in the first line
                if (currentLog != null) {
                    LogMessage returnedLog = currentLog;
                    currentLog = null;
                    if (matchRegex(returnedLog)) {
                        incrementLogCount(returnedLog);
                        return returnedLog;
                    }
                // else read the next line
                }
            } else if (nextLog.isRejectedLast()) {
                // log is too late
                if (currentLog != null) {
                    LogMessage returnedLog = currentLog;
                    currentLog = null;
                    if (matchRegex(returnedLog)) {
                        incrementLogCount(returnedLog);
                        return returnedLog;
                    }
                // else break from the loop
                }
                break;
            } else {
                // accepted as the first line of a new log message
                if (currentLog != null) {
                    LogMessage returnedLog = currentLog;
                    currentLog = nextLog;
                    // skip all but the last one
                    if (returnedLog.isHeader() && currentLog.isHeader()) {
                        continue;
                    }
                    if (matchRegex(returnedLog)) {
                        incrementLogCount(returnedLog);
                        return returnedLog;
                    }
                // else read the next line
                } else {
                    currentLog = nextLog;
                }
            }
        }
    } catch (IOException e) {
        status.appendErrFileName(filePath);
    }
    return null;
}
Also used : LogMessage(com.emc.storageos.systemservices.impl.logsvc.LogMessage) LogParser(com.emc.storageos.systemservices.impl.logsvc.parse.LogParser) IOException(java.io.IOException)

Aggregations

LogMessage (com.emc.storageos.systemservices.impl.logsvc.LogMessage)1 LogParser (com.emc.storageos.systemservices.impl.logsvc.parse.LogParser)1 IOException (java.io.IOException)1