-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Option to send Equinox Framework trace to the OSGi LogService
The trace messages in the framework are currently sent to the System.out. This commit adds an option to send the Framework trace messages to the OSGi LogService using the LogLevel of Trace. The Logger name used is the trace option key. To set Equinox trace options with LoggerAdmin the logger name EQUINOX.TRACE is set to LogLevel TRACE. When EQUINOX.TRACE logger is set to TRACE then all other logger names in the LoggerContext configuration set to TRACE are also assumed to be trace names to be enabled.
- Loading branch information
Showing
47 changed files
with
827 additions
and
601 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
187 changes: 187 additions & 0 deletions
187
bundles/org.eclipse.osgi.tests/src/org/eclipse/equinox/log/test/LogEquinoxTraceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2007, 2022 IBM Corporation and others. | ||
* | ||
* This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License 2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: IBM Corporation - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.equinox.log.test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertNotEquals; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import java.io.File; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.eclipse.equinox.log.SynchronousLogListener; | ||
import org.eclipse.osgi.internal.debug.Debug; | ||
import org.eclipse.osgi.launch.Equinox; | ||
import org.eclipse.osgi.service.debug.DebugOptions; | ||
import org.eclipse.osgi.tests.OSGiTestsActivator; | ||
import org.eclipse.osgi.tests.bundles.AbstractBundleTests; | ||
import org.junit.Test; | ||
import org.osgi.framework.Bundle; | ||
import org.osgi.framework.BundleContext; | ||
import org.osgi.framework.Constants; | ||
import org.osgi.framework.ServiceReference; | ||
import org.osgi.service.condition.Condition; | ||
import org.osgi.service.log.LogEntry; | ||
import org.osgi.service.log.LogLevel; | ||
import org.osgi.service.log.LogReaderService; | ||
import org.osgi.service.log.Logger; | ||
import org.osgi.service.log.admin.LoggerAdmin; | ||
import org.osgi.service.log.admin.LoggerContext; | ||
import org.osgi.service.startlevel.StartLevel; | ||
|
||
@SuppressWarnings("deprecation") // LogService | ||
public class LogEquinoxTraceTest extends AbstractBundleTests { | ||
|
||
static class TestListener implements SynchronousLogListener { | ||
List<LogEntry> logs = new ArrayList<>(); | ||
|
||
@Override | ||
public void logged(LogEntry entry) { | ||
synchronized (logs) { | ||
Bundle b = entry.getBundle(); | ||
if (b != null && b.getBundleId() == 0) { | ||
logs.add(entry); | ||
} | ||
} | ||
} | ||
|
||
List<LogEntry> getLogs() { | ||
synchronized (logs) { | ||
List<LogEntry> results = new ArrayList(logs); | ||
logs.clear(); | ||
return results; | ||
} | ||
} | ||
} | ||
|
||
private LoggerAdmin loggerAdmin = null; | ||
private TestListener testListener = null; | ||
private Equinox equinox = null; | ||
|
||
@Override | ||
public void setUp() throws Exception { | ||
File config = OSGiTestsActivator.getContext().getDataFile(getName()); | ||
Map<String, Object> configuration = new HashMap<>(); | ||
configuration.put(Constants.FRAMEWORK_STORAGE, config.getAbsolutePath()); | ||
equinox = new Equinox(configuration); | ||
equinox.start(); | ||
loggerAdmin = equinox.getBundleContext() | ||
.getService(equinox.getBundleContext().getServiceReference(LoggerAdmin.class)); | ||
LogReaderService logReader = equinox.getBundleContext() | ||
.getService(equinox.getBundleContext().getServiceReference(LogReaderService.class)); | ||
|
||
testListener = new TestListener(); | ||
logReader.addLogListener(testListener); | ||
} | ||
|
||
@Override | ||
public void tearDown() { | ||
if (equinox != null) { | ||
try { | ||
equinox.stop(); | ||
} catch (Exception e) { | ||
// ignore | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
public void testEnableTrace() { | ||
BundleContext systemContext = equinox.getBundleContext(); | ||
DebugOptions debugOptions = systemContext.getService(systemContext.getServiceReference(DebugOptions.class)); | ||
assertFalse("Expected debug options to be disabled.", debugOptions.isDebugEnabled()); | ||
assertEquals("Expected no debug Options.", 0, debugOptions.getOptions().size()); | ||
|
||
LoggerContext rootContext = loggerAdmin.getLoggerContext(null); | ||
Map<String, LogLevel> rootLogLevels = rootContext.getLogLevels(); | ||
|
||
// enable service trace | ||
rootLogLevels.put(Debug.EQUINOX_TRACE, LogLevel.TRACE); | ||
rootLogLevels.put(Debug.OPTION_DEBUG_SERVICES, LogLevel.TRACE); | ||
rootContext.setLogLevels(rootLogLevels); | ||
|
||
assertTrue("Expected debug to be enabled.", debugOptions.isDebugEnabled()); | ||
assertEquals("Expected 1 debug Option.", 1, debugOptions.getOptions().size()); | ||
assertTrue("Expected trace option to be enabled.", | ||
debugOptions.getBooleanOption(Debug.OPTION_DEBUG_SERVICES, false)); | ||
assertFalse("Expected trace option to be disabled.", | ||
debugOptions.getBooleanOption(Debug.OPTION_DEBUG_STARTLEVEL, false)); | ||
|
||
// get some service to generate trace | ||
ServiceReference<StartLevel> ref = equinox.getBundleContext().getServiceReference(StartLevel.class); | ||
List<LogEntry> traceLogs = testListener.getLogs(); | ||
assertNotEquals("Expected to have some trace logs.", 0, traceLogs.size()); | ||
for (LogEntry logEntry : traceLogs) { | ||
assertEquals("Wrong logger name", Debug.OPTION_DEBUG_SERVICES, | ||
logEntry.getLoggerName()); | ||
} | ||
|
||
// disable service trace, enable startlevel trace | ||
rootLogLevels.remove(Debug.OPTION_DEBUG_SERVICES); | ||
rootLogLevels.put(Debug.OPTION_DEBUG_STARTLEVEL, LogLevel.TRACE); | ||
rootContext.setLogLevels(rootLogLevels); | ||
|
||
assertTrue("Expected debug to be enabled.", debugOptions.isDebugEnabled()); | ||
assertEquals("Expected 1 debug Option.", 1, debugOptions.getOptions().size()); | ||
assertTrue("Expected trace option to be enabled.", | ||
debugOptions.getBooleanOption(Debug.OPTION_DEBUG_STARTLEVEL, false)); | ||
assertFalse("Expected trace option to be disabled.", | ||
debugOptions.getBooleanOption(Debug.OPTION_DEBUG_SERVICES, false)); | ||
|
||
// Get the StartLevel service to generate service logs (should be disabled) | ||
// Use the StartLevel service to generate startlevel logs (should be enabled) | ||
StartLevel startLevel = equinox.getBundleContext().getService(ref); | ||
startLevel.setStartLevel(20); | ||
|
||
traceLogs = testListener.getLogs(); | ||
assertNotEquals("Expected to have some trace logs.", 0, traceLogs.size()); | ||
for (LogEntry logEntry : traceLogs) { | ||
assertEquals("Wrong logger name", Debug.OPTION_DEBUG_STARTLEVEL, | ||
logEntry.getLoggerName()); | ||
} | ||
|
||
// remove all debug options which should disable debug trace | ||
rootLogLevels.clear(); | ||
rootLogLevels.put(Debug.EQUINOX_TRACE, LogLevel.TRACE); | ||
rootContext.setLogLevels(rootLogLevels); | ||
assertFalse("Expected debug to be disabled.", debugOptions.isDebugEnabled()); | ||
assertEquals("Expected no debug Options.", 0, debugOptions.getOptions().size()); | ||
} | ||
|
||
@Test | ||
public void testRootLoggerTrace() { | ||
LoggerContext rootContext = loggerAdmin.getLoggerContext(null); | ||
Map<String, LogLevel> rootLogLevels = rootContext.getLogLevels(); | ||
|
||
// make sure that enabling the root logger trace does not enable all Equinox | ||
// trace | ||
rootLogLevels.put(Logger.ROOT_LOGGER_NAME, LogLevel.TRACE); | ||
// enable some unused trace for equinox | ||
rootLogLevels.put(Debug.EQUINOX_TRACE, LogLevel.TRACE); | ||
rootLogLevels.put(Debug.OPTION_DEBUG_STARTLEVEL, LogLevel.TRACE); | ||
rootContext.setLogLevels(rootLogLevels); | ||
|
||
// get some service to generate trace | ||
equinox.getBundleContext().getServiceReference(Condition.class); | ||
List<LogEntry> traceLogs = testListener.getLogs(); | ||
assertEquals("Did not expect any trace logs.", 0, traceLogs.size()); | ||
} | ||
|
||
@Test | ||
public void testLoaderTrace() { | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.