/* ================================================================
 * Delft FEWS 
 * ================================================================
 *
 * Project Info:  http://www.wldelft.nl/soft/fews/index.html
 * Project Lead:  Karel Heynert (karel.heynert@wldelft.nl)
 *
 * (C) Copyright 2003, by WL | Delft Hydraulics
 *                        P.O. Box 177
 *                        2600 MH  Delft
 *                        The Netherlands
 *                        http://www.wldelft.nl
 *
 * DELFT-FEWS is a sophisticated collection of modules designed 
 * for building a FEWS customised to the specific requirements 
 * of individual agencies. An open modelling approach allows users
 * to add their own modules in an efficient way.
 *
 * ----------------------------------------------------------------
 * PiDiagnosticsParser.java
 * ----------------------------------------------------------------
 * (C) Copyright 2003, by WL | Delft Hydraulics
 *
 * Original Author:  pelgrim
 * Contributor(s):   
 *
 * Changes:
 * --------
 * 27-Jun-14 : Version 1 ();
 * 
 *
 */

package nl.wldelft.timeseriesparsers;

import nl.wldelft.fews.pi.PiDiagnosticsReader;
import nl.wldelft.util.io.XmlParser;
import nl.wldelft.util.timeseries.TimeSeriesContentHandler;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

import javax.xml.stream.XMLStreamReader;

public class PiDiagnosticsParser implements XmlParser<TimeSeriesContentHandler> {
    private static final Logger log = Logger.getLogger(PiDiagnosticsParser.class);
    private static final String infoPrefix = "GA.Execution.Model.Info: In adapter model: ";
    private static final String warnPrefix = "GA.Execution.Model.Warn: In adapter model: ";
    private static final String errorPrefix = "GA.Execution.Model.Error: In adapter model: ";
    private static final String fatalPrefix = "GA.Execution.Model.Error: In adapter model: ";

    @Override
    public void parse(XMLStreamReader xmlReader, String virtualFileName, TimeSeriesContentHandler contentHandler) throws Exception {

        try (PiDiagnosticsReader piDiagnosticsReader = new PiDiagnosticsReader(xmlReader)) {

            while (piDiagnosticsReader.next()) {
                String description = piDiagnosticsReader.getMessage();
                String eventCode = piDiagnosticsReader.getEventCode();
                switch (piDiagnosticsReader.getLevel()) {
                    case FATAL:
                        logFatalError(virtualFileName, fatalPrefix, description, eventCode);
                        continue;
                    case ERROR:
                        logError(virtualFileName, errorPrefix, description, eventCode);
                        continue;
                    case WARN:
                        logWarning(warnPrefix, description, eventCode);
                        continue;
                    case INFO:
                        logInfo(infoPrefix, description, eventCode);
                        continue;
                    case DEBUG:
                        logDebug(description, eventCode);
                        continue;
                    case TRACE:
                        logTrace(description, eventCode);
                }
            }
        }
        if (log.isDebugEnabled()) log.debug("Stop writing logs of external module.");
    }

    private static void logTrace(String description, String eventCode) {
        if (log.isTraceEnabled()) {
            if (eventCode != null) {
                log.trace(eventCode + ": " + description);
                return;
            }
            log.trace(description);
        }
    }

    private static void logDebug(String description, String eventCode) {
        if (log.isDebugEnabled()) {
            if (eventCode != null) {
                log.debug(eventCode + ": " + description);
                return;
            }
            log.debug("GA.Execution.Model.Debug: " + description);
        }
    }

    private static void logInfo(String infoPrefix, String description, String eventCode) {
        if (log.isInfoEnabled()) {
            if (eventCode != null) {
                log.info(eventCode + ": " + description);
                return;
            }
            log.info(infoPrefix + ": " + description);
        }
    }

    private static void logWarning(String warnPrefix, String description, String eventCode) {
        if (log.isEnabledFor(Level.WARN)) {
            if (eventCode != null) {
                log.warn(eventCode + ": " + description);
                return;
            }
            log.warn(warnPrefix + description);
        }
    }

    private static void logError(String virtualFileName, String errorPrefix, String description, String eventCode) {
        if (log.isEnabledFor(Level.ERROR)) {
            if (eventCode != null) {
                log.error(eventCode + ": " + description);
                return;
            }
            log.error(errorPrefix + description + '\n' + virtualFileName);
        }
    }

    private static void logFatalError(String virtualFileName, String fatalPrefix, String description, String eventCode) {
        if (log.isEnabledFor(Level.ERROR)) {
            if (eventCode != null) {
                log.error(eventCode + ": " + description);
                return;
            }
            log.error(fatalPrefix + description + '\n' + virtualFileName);
        }
    }
}
  • No labels