Search in sources :

Example 31 with LogEntry

use of org.osgi.service.log.LogEntry in project felix by apache.

the class LogListenerThread method run.

/**
 * The main method of the thread: waits for new messages to be receieved
 * and then delivers them to any registered log listeners.
 */
public void run() {
    while (!isInterrupted()) {
        List entriesToDeliver = new ArrayList();
        synchronized (m_entriesToDeliver) {
            if (m_entriesToDeliver.isEmpty()) {
                try {
                    m_entriesToDeliver.wait();
                } catch (InterruptedException e) {
                    // the interrupt-flag is cleared; so, let's play nice and
                    // interrupt this thread again to stop it...
                    interrupt();
                }
            } else {
                // Copy all current entries and deliver them in a single go...
                entriesToDeliver.addAll(m_entriesToDeliver);
                m_entriesToDeliver.clear();
            }
        }
        if (!entriesToDeliver.isEmpty()) {
            // Take a snapshot of all current listeners and deliver all
            // pending messages to them...
            List listeners = new ArrayList();
            synchronized (m_listeners) {
                listeners.addAll(m_listeners);
            }
            Iterator entriesIt = entriesToDeliver.iterator();
            while (entriesIt.hasNext()) {
                LogEntry entry = (LogEntry) entriesIt.next();
                Iterator listenerIt = listeners.iterator();
                while (listenerIt.hasNext()) {
                    LogListener listener = (LogListener) listenerIt.next();
                    try {
                        listener.logged(entry);
                    } catch (Throwable t) {
                    // catch and discard any exceptions thrown by the listener
                    }
                }
            }
        }
    }
}
Also used : LogListener(org.osgi.service.log.LogListener) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList) LogEntry(org.osgi.service.log.LogEntry)

Example 32 with LogEntry

use of org.osgi.service.log.LogEntry in project rt.equinox.framework by eclipse.

the class SystemBundleTests method testOverrideEquinoxConfigAreaProp.

public void testOverrideEquinoxConfigAreaProp() {
    // $NON-NLS-1$
    File config = OSGiTestsActivator.getContext().getDataFile(getName());
    Map configuration = new HashMap();
    configuration.put(Constants.FRAMEWORK_STORAGE, config.getAbsolutePath());
    configuration.put(EquinoxLocations.PROP_CONFIG_AREA, config.getAbsolutePath() + "-override");
    configuration.put(EquinoxConfiguration.PROP_LOG_HISTORY_MAX, "10");
    Equinox equinox = null;
    try {
        equinox = new Equinox(configuration);
        equinox.init();
        BundleContext bc = equinox.getBundleContext();
        LogReaderService logReader = bc.getService(bc.getServiceReference(LogReaderService.class));
        Enumeration<LogEntry> logs = logReader.getLog();
        assertTrue("No logs found.", logs.hasMoreElements());
        LogEntry entry = logs.nextElement();
        assertEquals("Wrong log level.", LogLevel.WARN, entry.getLogLevel());
        assertTrue("Wrong message found: " + entry.getMessage(), entry.getMessage().contains(EquinoxLocations.PROP_CONFIG_AREA));
    } catch (BundleException e) {
        fail("Failed init", e);
    } finally {
        try {
            if (equinox != null) {
                equinox.stop();
                equinox.waitForStop(1000);
            }
        } catch (BundleException e) {
            fail("Failed to stop framework.", e);
        } catch (InterruptedException e) {
            fail("Failed to stop framework.", e);
        }
    }
}
Also used : Equinox(org.eclipse.osgi.launch.Equinox) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LogReaderService(org.osgi.service.log.LogReaderService) ExtendedLogReaderService(org.eclipse.equinox.log.ExtendedLogReaderService) BundleException(org.osgi.framework.BundleException) File(java.io.File) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LogEntry(org.osgi.service.log.LogEntry) BundleContext(org.osgi.framework.BundleContext)

Example 33 with LogEntry

use of org.osgi.service.log.LogEntry in project vespa by vespa-engine.

the class ConsoleLogFormatterTestCase method requireThatMessageIsOptional.

@Test
public void requireThatMessageIsOptional() {
    LogEntry entry = new MyEntry(0, 0, null);
    assertEquals("0.000\t-\t-\t-\t-\tunknown\t", SIMPLE_FORMATTER.formatEntry(entry));
}
Also used : LogEntry(org.osgi.service.log.LogEntry) Test(org.junit.Test)

Example 34 with LogEntry

use of org.osgi.service.log.LogEntry in project vespa by vespa-engine.

the class ConsoleLogFormatterTestCase method requireThatBundleAndLoggerNameIsCombined.

@Test
public void requireThatBundleAndLoggerNameIsCombined() {
    LogEntry entry = new MyEntry(0, 0, null).setBundleSymbolicName("bundleName").putProperty("LOGGER_NAME", "loggerName");
    assertEquals("0.000\t-\t-\t-\tbundleName/loggerName\tunknown\t", SIMPLE_FORMATTER.formatEntry(entry));
}
Also used : LogEntry(org.osgi.service.log.LogEntry) Test(org.junit.Test)

Example 35 with LogEntry

use of org.osgi.service.log.LogEntry in project vespa by vespa-engine.

the class ConsoleLogFormatterTestCase method requireThatBundleNameIsIncluded.

@Test
public void requireThatBundleNameIsIncluded() {
    LogEntry entry = new MyEntry(0, 0, null).setBundleSymbolicName("bundleName");
    assertEquals("0.000\t-\t-\t-\tbundleName\tunknown\t", SIMPLE_FORMATTER.formatEntry(entry));
}
Also used : LogEntry(org.osgi.service.log.LogEntry) Test(org.junit.Test)

Aggregations

LogEntry (org.osgi.service.log.LogEntry)40 Test (org.junit.Test)18 ArrayList (java.util.ArrayList)6 LogReaderService (org.osgi.service.log.LogReaderService)5 BundleException (org.osgi.framework.BundleException)4 Enumeration (java.util.Enumeration)3 ServiceReference (org.osgi.framework.ServiceReference)3 LogListener (org.osgi.service.log.LogListener)3 SynchronousLogListener (org.eclipse.equinox.log.SynchronousLogListener)2 BundleContext (org.osgi.framework.BundleContext)2 TestDriver (com.yahoo.jdisc.test.TestDriver)1 File (java.io.File)1 Formatter (java.util.Formatter)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Vector (java.util.Vector)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1