Search in sources :

Example 36 with ILogEntry

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

the class CacheTest method testMemoryCache.

/**
	 * It is deifficult to test LogCache...
	 * This is better then nothing
	 * 
	 * The test is done by reading all the cache sequentially.
	 * The first, middle and last logs acquired in the beginning are 
	 * compared with those retrieved with the sequential scan.
	 *
	 */
public void testMemoryCache() throws Exception {
    int first = 0;
    String firstMsg = (String) cache.getLog(first).getField(LogField.LOGMESSAGE);
    int last = cache.getSize() - 1;
    String lastMsg = (String) cache.getLog(last).getField(LogField.LOGMESSAGE);
    int pos = cache.getSize() / 2;
    String posMsg = (String) cache.getLog(pos).getField(LogField.LOGMESSAGE);
    // Scans the list
    for (int t = 0; t < last; t++) {
        cache.getLog(t);
        ILogEntry firstLog = cache.getLog(first);
        assertEquals("Error in mem cache pos " + first, firstMsg, firstLog.getField(LogField.LOGMESSAGE));
        ILogEntry lastLog = cache.getLog(last);
        assertEquals("Error in mem cache pos " + last, lastMsg, lastLog.getField(LogField.LOGMESSAGE));
        ILogEntry posLog = cache.getLog(pos);
        assertEquals("Error in mem cache pos " + pos, posMsg, posLog.getField(LogField.LOGMESSAGE));
    }
}
Also used : ILogEntry(com.cosylab.logging.engine.log.ILogEntry)

Example 37 with ILogEntry

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

the class CacheTest method testLogExceedingTimeFrame.

/**
	 * Check if the method returning the logs exceeding the time frame is
	 * working as expected.
	 * 
	 * @throws Exception
	 */
public void testLogExceedingTimeFrame() throws Exception {
    ACSLogParser parser = ACSLogParserFactory.getParser();
    // Create some logs 
    // The important fields here are the times (we'll test against a time frame of 30 secs)
    // It is also important the message that is used to check which messages
    // are added in the collection and which not
    String logStr1 = "<Info TimeStamp=\"2005-11-29T15:33:55.000\" Routine=\"CacheTest::testGet\" Host=\"this\" Process=\"test\" Thread=\"main\" Context=\"\"><![CDATA[Test1]]></Info>";
    String logStr2 = "<Info TimeStamp=\"2005-11-29T15:33:20.000\" Routine=\"CacheTest::testGet\" Host=\"this\" Process=\"test\" Thread=\"main\" Context=\"\"><![CDATA[Test2]]></Info>";
    String logStr3 = "<Info TimeStamp=\"2005-11-29T15:33:10.000\" Routine=\"CacheTest::testGet\" Host=\"this\" Process=\"test\" Thread=\"main\" Context=\"\"><![CDATA[Test3]]></Info>";
    String logStr4 = "<Info TimeStamp=\"2005-11-29T15:34:15.000\" Routine=\"CacheTest::testGet\" Host=\"this\" Process=\"test\" Thread=\"main\" Context=\"\"><![CDATA[Test4]]></Info>";
    String logStr5 = "<Info TimeStamp=\"2005-11-29T15:33:12.000\" Routine=\"CacheTest::testGet\" Host=\"this\" Process=\"test\" Thread=\"main\" Context=\"\"><![CDATA[Test5]]></Info>";
    String logStr6 = "<Info TimeStamp=\"2005-11-29T15:34:05.000\" Routine=\"CacheTest::testGet\" Host=\"this\" Process=\"test\" Thread=\"main\" Context=\"\"><![CDATA[Test6]]></Info>";
    String logStr7 = "<Info TimeStamp=\"2005-11-29T15:34:15.000\" Routine=\"CacheTest::testGet\" Host=\"this\" Process=\"test\" Thread=\"main\" Context=\"\"><![CDATA[Test7]]></Info>";
    String logStr8 = "<Info TimeStamp=\"2005-11-29T15:34:10.000\" Routine=\"CacheTest::testGet\" Host=\"this\" Process=\"test\" Thread=\"main\" Context=\"\"><![CDATA[Test8]]></Info>";
    String logStr9 = "<Info TimeStamp=\"2005-11-29T15:33:25.000\" Routine=\"CacheTest::testGet\" Host=\"this\" Process=\"test\" Thread=\"main\" Context=\"\"><![CDATA[Test9]]></Info>";
    cache.clear();
    cache.add(parser.parse(logStr1));
    cache.add(parser.parse(logStr2));
    cache.add(parser.parse(logStr3));
    cache.add(parser.parse(logStr4));
    cache.add(parser.parse(logStr5));
    cache.add(parser.parse(logStr6));
    cache.add(parser.parse(logStr7));
    cache.add(parser.parse(logStr8));
    cache.add(parser.parse(logStr9));
    assertEquals("Wrong cache size", 9, cache.getSize());
    // An array of boolean: the items marked as true should appear in the Collection
    boolean[] b = { true, false, false, true, false, true, true, true, false };
    // Get the logs exceeding a time frame of 30 secs 
    Collection<Integer> logs = cache.getLogExceedingTimeFrame(30 * 1000);
    assertEquals("Wrong number of logs exceeding time frame", 5, logs.size());
    // Check if the logs returned by getLogExceedingTimeFrame are ok
    boolean[] returned = new boolean[9];
    for (int t = 0; t < returned.length; t++) {
        // Init all as false
        returned[t] = false;
    }
    // Mark as true the logs in the Collection
    for (Integer logN : logs) {
        ILogEntry log = cache.getLog(logN);
        String msg = (String) log.getField(LogField.LOGMESSAGE);
        msg = msg.replaceFirst("Test", "");
        Integer pos = Integer.parseInt(msg);
        returned[pos - 1] = true;
    }
    // Compare the expected array (b) with the returned values (returned)
    for (int t = 0; t < b.length; t++) {
        assertEquals("The log " + t + " is/isn't in the Collection of logs", b[t], returned[t]);
    }
}
Also used : ILogEntry(com.cosylab.logging.engine.log.ILogEntry) ACSLogParser(alma.acs.logging.engine.parser.ACSLogParser)

Example 38 with ILogEntry

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

the class LogCache method getLog.

/**
	 * Return the log with the given key.
	 * The method is synchronized because both the HashMap and
	 * the LinkedList must be synchronized if there is a chance
	 * to acces these objects from more then one thread in the same 
	 * time
	 * @see java.util.LinkedList
	 * @see java.util.HashMap
	 *  
	 * @param pos The key of the log
	 * @return The LogEntryXML or null in case of error
	 */
public synchronized ILogEntry getLog(Integer key) throws LogCacheException {
    ILogEntry log;
    log = cache.get(key);
    if (log != null) {
        // Hit! The log is in the cache
        return log;
    } else {
        // Oops we need to read a log from disk!
        return loadNewLog(key);
    }
}
Also used : ILogEntry(com.cosylab.logging.engine.log.ILogEntry)

Example 39 with ILogEntry

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

the class LogCache method loadNewLog.

/**
	 * Get a log from the cache on disk updating all the 
	 * internal lists
	 * 
	 * @param idx The position of the log
	 * @return The log read from the cache on disk
	 */
private synchronized ILogEntry loadNewLog(Integer idx) throws LogCacheException {
    // Read the new log from the cache on disk
    ILogEntry log = super.getLog(idx);
    // There is enough room in the lists?
    if (cache.size() == actualCacheSize) {
        // We need to create a room for the new element
        Integer itemToRemove = manager.removeFirst();
        cache.remove(itemToRemove);
    }
    // Add the log in the cache
    cache.put(idx, log);
    // Append the index in the manager list
    manager.addLast(idx);
    return log;
}
Also used : ILogEntry(com.cosylab.logging.engine.log.ILogEntry)

Example 40 with ILogEntry

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

the class LogReceiver method startCaptureLogs.

/**
     * Variant of {@link #startCaptureLogs(PrintWriter)} which takes an optional ThreadFactory
     * which will be used to create the thread that reads out the log queue.
     * This method could be used if <code>LogReceiver</code> is run inside a container 
     * or as part of a ComponentClientTestCase.
	 */
public void startCaptureLogs(final PrintWriter logWriter, ThreadFactory threadFactory) throws IOException {
    if (!isInitialized()) {
        throw new IllegalStateException("First call LogReceiver#initialize(ORB, Manager), then startCaptureLogs(PrintWriter)");
    }
    if (listenForLogs) {
        if (verbose) {
            System.out.println("Ignoring call to 'startCaptureLogs' while already capturing logs.");
        }
        return;
    }
    Runnable logRetriever = new Runnable() {

        public void run() {
            //            	System.out.println("logRetriever.run called...");
            try {
                BlockingQueue<DelayedLogEntry> logQueue = getLogQueue();
                listenForLogs = true;
                // loop until "queue poison" entry is found
                while (true) {
                    try {
                        // extract logs from queue
                        DelayedLogEntry delayedLogEntry = logQueue.take();
                        if (delayedLogEntry.isQueuePoison()) {
                            if (verbose) {
                                System.out.println("got queue poison, will terminate method 'startCaptureLogs'.");
                            }
                            break;
                        } else {
                            ILogEntry logEntry = delayedLogEntry.getLogEntry();
                            String xmlLog = logEntry.toXMLString();
                            //								System.out.println("Yeah, got a log record: " + xmlLog);
                            logWriter.println(xmlLog);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            } catch (Throwable thr) {
                System.out.println("Log receiver got exception " + thr);
                logWriter.println("Log receiver failed with exception " + thr.toString());
            } finally {
                logWriter.close();
                // we only call stop now, so that late arriving records could still be added 
                // to the queue during the whole waiting time.
                stop();
            }
        }
    };
    Thread t = null;
    if (threadFactory != null) {
        t = threadFactory.newThread(logRetriever);
    } else {
        t = new Thread(logRetriever);
        t.setDaemon(true);
    }
    t.start();
}
Also used : ILogEntry(com.cosylab.logging.engine.log.ILogEntry) LogEngineException(com.cosylab.logging.engine.LogEngineException) IOException(java.io.IOException)

Aggregations

ILogEntry (com.cosylab.logging.engine.log.ILogEntry)85 Vector (java.util.Vector)15 ACSLogParser (alma.acs.logging.engine.parser.ACSLogParser)11 Date (java.util.Date)11 IsoDateFormat (alma.acs.util.IsoDateFormat)9 LogCacheException (com.cosylab.logging.client.cache.LogCacheException)9 AdditionalData (com.cosylab.logging.engine.log.ILogEntry.AdditionalData)9 SimpleDateFormat (java.text.SimpleDateFormat)9 FieldPosition (java.text.FieldPosition)8 LogTypeHelper (com.cosylab.logging.engine.log.LogTypeHelper)7 LogEntry (com.cosylab.logging.engine.log.LogEntry)6 ParserTypes (alma.acs.logging.engine.parser.ACSLogParserFactory.ParserTypes)5 LogBufferedFileCache (com.cosylab.logging.client.cache.LogBufferedFileCache)5 LogFileCache (com.cosylab.logging.client.cache.LogFileCache)5 Random (java.util.Random)5 IOHelper (alma.acs.logging.engine.io.IOHelper)4 LogCache (com.cosylab.logging.client.cache.LogCache)4 IOException (java.io.IOException)4 ILogMap (com.cosylab.logging.client.cache.ILogMap)3 LogField (com.cosylab.logging.engine.log.LogField)3