Search in sources :

Example 1 with LogField

use of com.cosylab.logging.engine.log.LogField in project ACS by ACS-Community.

the class LoadSaveTest method testSaveLoadFields.

/**
	 * Save and Load the special logs then check their fields.
	 * <P>
	 * This test is performed comparing the fields of the special logs one by one
	 * instead of comparing the XMLs.
	 * The test does the following steps:
	 * <OL>
	 * 	<LI>build vector of logs to save from the XMLs in <code>specialLogs</code>
	 *  <LI>save the vector of logs with an <code>IOHelper</code> object
	 *  <LI>load the logs from file
	 *  <LI>compare each field of the logs read from the file with those in <code>specialLogs</code>
	 *  <LI>compare the additional data names and values
	 * </OL>
	 * <P>
	 * The test implicitly checks the conversion between XML and ILogEntry too. 
	 */
public void testSaveLoadFields() throws Exception {
    ACSLogParser parser = ACSLogParserFactory.getParser();
    assertNotNull(parser);
    //Build the logs from the XML
    Vector<ILogEntry> logsToCheck = new Vector<ILogEntry>();
    for (int t = 0; t < specialLogs.length; t++) {
        ILogEntry log = parser.parse(specialLogs[t]);
        assertNotNull(log);
        logsToCheck.add(log);
    }
    // Save the logs on disk
    IOHelper ioHelper = new IOHelper();
    assertNotNull(ioHelper);
    ioHelper.saveLogs(fileName, logsToCheck, this, false, false);
    assertEquals(logsToCheck.size(), numOfLogsWritten);
    // Load the logs from disk
    ioHelper.loadLogs(fileName, this, null, this, this, false);
    assertEquals(logsRead.size(), logsToCheck.size());
    // Iterate over the logs comparing each field
    for (int t = 0; t < logsToCheck.size(); t++) {
        ILogEntry originalLog = logsToCheck.elementAt(t);
        assertNotNull(originalLog);
        ILogEntry savedLog = logsRead.elementAt(t);
        assertNotNull(savedLog);
        // Check the fields
        for (LogField f : LogField.values()) {
            Object original = originalLog.getField(f);
            Object saved = savedLog.getField(f);
            assertEquals("Fields " + f + " differ", original, saved);
        }
        // Check additional data
        assertEquals(originalLog.hasDatas(), savedLog.hasDatas());
        if (originalLog.hasDatas()) {
            Vector<AdditionalData> originalData = originalLog.getAdditionalData();
            assertNotNull(originalData);
            Vector<AdditionalData> savedData = savedLog.getAdditionalData();
            assertNotNull(savedData);
            assertEquals(originalData.size(), savedData.size());
            for (int count = 0; count < originalData.size(); count++) {
                AdditionalData originalDataItem = originalData.elementAt(count);
                assertNotNull(originalDataItem);
                AdditionalData savedDataItem = savedData.elementAt(count);
                assertNotNull(savedDataItem);
                assertEquals("Data names differ", originalDataItem.name, savedDataItem.name);
                assertEquals("Data values differ", originalDataItem.value, savedDataItem.value);
            }
        }
    }
}
Also used : AdditionalData(com.cosylab.logging.engine.log.ILogEntry.AdditionalData) ILogEntry(com.cosylab.logging.engine.log.ILogEntry) IOHelper(alma.acs.logging.engine.io.IOHelper) ACSLogParser(alma.acs.logging.engine.parser.ACSLogParser) Vector(java.util.Vector) LogField(com.cosylab.logging.engine.log.LogField)

Example 2 with LogField

use of com.cosylab.logging.engine.log.LogField in project ACS by ACS-Community.

the class FiltersVector method loadFilters.

/**
	 * Load filters 
	 * In case of errors an exception is thrown
	 * 
	 * @param f The xml file to parse (java.io.File) 
	 * @param eraseFilters If true existing filters will be deleted before loading
	 * @param fileName Is the name of the file (it is usually null and the name is
	 *                 read from the parameter f. However when this method is called
	 *                 recursively we need to pass the name because the file we're reading
	 *                 is a temporary file (generated by converting the original file)
	 * @throws <code>Exception</code> In case of error loading the filters
	 */
public void loadFilters(File f, boolean eraseFilters, String fileName) throws Exception {
    Document doc;
    FileInputStream fStream;
    fStream = new FileInputStream(f);
    JAXPDOMAdapter adapter = new JAXPDOMAdapter();
    DOMBuilder builder = new DOMBuilder();
    doc = builder.build(adapter.getDocument(fStream, false));
    Element root = doc.getRootElement();
    if (root.getName().compareToIgnoreCase("FILTER_LIST") != 0 && root.getName().compareToIgnoreCase("FILTERS") != 0) {
        // We show a message to the user and abort
        throw new Exception("Wrong xml file: the root is " + root.getName() + " instead of FILTER_LIST");
    } else if (root.getName().compareToIgnoreCase("FILTER_LIST") == 0) {
        // The file is an old version (show a message to the user to explain what's
        // happening and how to avoid this message appears again in future
        System.err.println("The format of this filter file is deprecated. \nSave the filters to avoid this message appears in future.");
        // Convert the format of the file
        File newFormatFile = convertOldFilterFile(f);
        if (newFormatFile == null) {
            // Something went wrong while converting: abort
            throw new Exception("Error converting the file to the new format");
        } else {
            // Recursively call loadFilters but now the file has
            // the new format
            loadFilters(newFormatFile, eraseFilters, f.getAbsolutePath());
            return;
        }
    }
    Element filtersElement = root.getChild("FILTER_LIST");
    // No filters defined
    if (filtersElement == null)
        return;
    List children = filtersElement.getChildren("FILTER");
    // Check if the vector has elements
    if (children == null || children.size() == 0) {
        return;
    }
    if (eraseFilters) {
        // The user whish to substitute the existing filters
        // with those he's loading
        clear();
    }
    // Read all the filters from the file
    Iterator it = children.iterator();
    while (it.hasNext()) {
        // Temporary Strings to store values read from the file
        String lethalStr = null;
        String notStr = null;
        String minStr = null;
        String maxStr = null;
        String exactStr = null;
        String wcharStr = null;
        String fieldStr = null;
        Boolean enabled = null;
        Element element = (Element) it.next();
        String type = element.getAttributeValue("type");
        Constraint constraint = Constraint.fromName(type);
        Element lethalElement = element.getChild("LETHAL");
        if (lethalElement != null) {
            lethalStr = lethalElement.getText();
        }
        Element notElement = element.getChild("APPLYNOT");
        if (notElement != null) {
            notStr = notElement.getText();
        }
        Element minElement = element.getChild("MIN");
        String minType = null;
        if (minElement != null) {
            minStr = minElement.getText();
            minType = minElement.getAttributeValue("class");
        }
        Element maxElement = element.getChild("MAX");
        String maxType = null;
        if (maxElement != null) {
            maxStr = maxElement.getText();
            maxType = maxElement.getAttributeValue("class");
        }
        Element exactElement = element.getChild("EXACT");
        String exactType = null;
        if (exactElement != null) {
            exactStr = exactElement.getText();
            exactType = exactElement.getAttributeValue("class");
        }
        Element wcharElement = element.getChild("WILDCHAR");
        if (wcharElement != null) {
            wcharStr = wcharElement.getText();
        }
        Element fieldElement = element.getChild("FIELD");
        if (fieldElement != null) {
            fieldStr = fieldElement.getText();
        }
        // Build the Field.
        LogField field = LogField.fromName(fieldStr);
        if (field == null) {
            // Ooops the field has not been found
            // Check if this String contains an Integer representing the
            // position of this field in the enum LogField (it was in the old format)
            Integer i = Integer.parseInt(fieldStr);
            field = LogField.values()[i];
        }
        Element enabledElement = element.getChild("ENABLED");
        if (enabledElement != null) {
            enabled = new Boolean(enabledElement.getText());
        } else {
            // Tag not found: enable the filter per default
            enabled = Boolean.TRUE;
        }
        // Build the filter
        Filter filter = Filter.buildFilter(constraint, field, lethalStr, notStr, minStr, minType, maxStr, maxType, exactStr, exactType, wcharStr);
        // bulidFilter throws an exception but ner return a null filter
        if (filter == null) {
            throw new IllegalStateException("The filter should not be null");
        }
        addFilter(filter, enabled);
    }
}
Also used : Constraint(com.cosylab.logging.engine.Filter.Constraint) Element(org.jdom.Element) Document(org.jdom.Document) LogField(com.cosylab.logging.engine.log.LogField) FileInputStream(java.io.FileInputStream) DOMBuilder(org.jdom.input.DOMBuilder) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) JAXPDOMAdapter(org.jdom.adapters.JAXPDOMAdapter) Iterator(java.util.Iterator) List(java.util.List) File(java.io.File)

Example 3 with LogField

use of com.cosylab.logging.engine.log.LogField in project ACS by ACS-Community.

the class LogEntryTest method testGetFieldDesc.

public void testGetFieldDesc() {
    //String getFieldDescription(int fieldIndex) 
    String actualFieldDesc = null;
    Object fieldContent = null;
    String f = null;
    String expectedFieldDesc = "File";
    String expectedField = "maciHeartbeatController.cpp";
    for (LogField field : LogField.values()) {
        // all the fields 		
        fieldContent = log.getField(field);
        f = "" + fieldContent;
        if (f.equalsIgnoreCase(expectedField)) {
            // maciHeartbeatController.cpp
            actualFieldDesc = field.getName();
        }
    }
    assertEquals("The two logs are not equal.", expectedFieldDesc, actualFieldDesc);
}
Also used : LogField(com.cosylab.logging.engine.log.LogField)

Example 4 with LogField

use of com.cosylab.logging.engine.log.LogField in project ACS by ACS-Community.

the class CSVConverter method convert.

/**
	 * Convert a log in a CSV string 
	 * 
	 * @param log The log to convert
	 * @return The CSV string representing the log
	 */
public String convert(ILogEntry log) {
    if (log == null) {
        throw new IllegalArgumentException("Impossible to convert a null log");
    }
    StringBuilder str = new StringBuilder();
    SimpleDateFormat df = new IsoDateFormat();
    for (int t = 0; t < colIndex.length(); t++) {
        if (t > 0) {
            str.append(separator);
        }
        Character c = Character.toUpperCase(colIndex.charAt(t));
        if ((c >= '0' && c <= '9') || (c >= 'A' && c <= Character.toUpperCase(LogField.values()[LogField.values().length - 1].id))) {
            LogField field = LogField.fromID(c);
            Object obj = log.getField(field);
            if (obj == null) {
                appendField(null, str);
            } else if (field == LogField.TIMESTAMP) {
                // Write the date in the right format
                Date dt = new Date(((Long) obj).longValue());
                StringBuffer dateSB = new StringBuffer();
                java.text.FieldPosition pos = new java.text.FieldPosition(0);
                df.format(dt, dateSB, pos);
                appendField(dateSB.toString(), str);
            } else if (field == LogField.ENTRYTYPE) {
                appendField(LogTypeHelper.fromLogTypeDescription(obj.toString()).logEntryType, str);
            } else {
                appendField(obj.toString(), str);
            }
        } else {
            // DATA
            if (log.hasDatas()) {
                appendField(formatData(log.getAdditionalData()), str);
            } else {
                appendField(null, str);
            }
        }
    }
    str.append('\n');
    return str.toString();
}
Also used : IsoDateFormat(alma.acs.util.IsoDateFormat) LogField(com.cosylab.logging.engine.log.LogField) Date(java.util.Date) SimpleDateFormat(java.text.SimpleDateFormat)

Example 5 with LogField

use of com.cosylab.logging.engine.log.LogField in project ACS by ACS-Community.

the class LogConverter method setCols.

/**
	 * Set the columns to write in the converted string.
	 * 
	 * 
	 * @param columns A string of columns; if <code>null</code>
	 * 				or empty then colIndexs contain all
	 * 				the fields plus the additional data
	 * @see colIndex
	 */
public void setCols(String columns) {
    if (columns == null || columns.isEmpty()) {
        colIndex = "";
        for (LogField f : LogField.values()) {
            colIndex = colIndex + f.id;
        }
        colIndex = colIndex + ADDITIONAL_DATA_ID;
        colIndex = colIndex.toUpperCase();
        return;
    }
    // Check if all the chars represent a valid log field
    char[] ids = columns.toCharArray();
    for (char id : ids) {
        if (id == ADDITIONAL_DATA_ID) {
            continue;
        }
        LogField.fromID(id);
    }
    colIndex = columns.toUpperCase();
}
Also used : LogField(com.cosylab.logging.engine.log.LogField)

Aggregations

LogField (com.cosylab.logging.engine.log.LogField)13 ILogEntry (com.cosylab.logging.engine.log.ILogEntry)3 AdditionalData (com.cosylab.logging.engine.log.ILogEntry.AdditionalData)3 Date (java.util.Date)3 ACSLogParser (alma.acs.logging.engine.parser.ACSLogParser)2 IsoDateFormat (alma.acs.util.IsoDateFormat)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Vector (java.util.Vector)2 IOHelper (alma.acs.logging.engine.io.IOHelper)1 ParserTypes (alma.acs.logging.engine.parser.ACSLogParserFactory.ParserTypes)1 TextConverter (alma.acs.logging.tools.TextConverter)1 XMLConverter (alma.acs.logging.tools.XMLConverter)1 Constraint (com.cosylab.logging.engine.Filter.Constraint)1 LogTypeHelper (com.cosylab.logging.engine.log.LogTypeHelper)1 FieldChooserDialog (com.cosylab.logging.settings.FieldChooserDialog)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 Iterator (java.util.Iterator)1