Skip to content

Commit

Permalink
introduce LoggingSupport
Browse files Browse the repository at this point in the history
to have a common way to log messages overall modules. Reduces
code-duplications as well, if deprecated methods are removed
in the future

Signed-off-by: Frank Gasdorf <[email protected]>
  • Loading branch information
fgdrf committed Nov 2, 2021
1 parent 3de01f2 commit 46ee20f
Show file tree
Hide file tree
Showing 12 changed files with 376 additions and 181 deletions.
8 changes: 6 additions & 2 deletions plugins/org.locationtech.udig.core.tests/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ Bundle-Version: 2.3.0.qualifier
Bundle-Vendor: udig.refractions.net
Eclipse-BuddyPolicy: ext
Require-Bundle: org.eclipse.core.runtime,
org.junit,
org.locationtech.udig.libs
org.junit;bundle-version="4.12.0",
org.locationtech.udig.libs,
org.mockito;bundle-version="2.23.0",
org.objenesis;bundle-version="2.6.0",
net.bytebuddy.byte-buddy;bundle-version="1.9.0",
net.bytebuddy.byte-buddy-agent;bundle-version="1.9.0"
Bundle-ActivationPolicy: lazy
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Fragment-Host: org.locationtech.udig.core
Expand Down
3 changes: 2 additions & 1 deletion plugins/org.locationtech.udig.core.tests/build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ bin.includes = META-INF/,\
.,\
bsd3-v10.html,\
epl-v10.html,\
about.html
about.html,\
src/mockito-extensions/
src.includes = LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mock-maker-inline
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* uDig - User Friendly Desktop Internet GIS client
* https://locationtech.org/projects/technology.udig
* (C) 2021, Eclipse Foundation
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution
* License v1.0 (http://www.eclipse.org/org/documents/edl-v10.html).
*/
package org.locationtech.udig.core.logging;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Plugin;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.osgi.framework.Bundle;


@RunWith(MockitoJUnitRunner.class)
public class LoggingSupportTest {

private static final String BUNDLE_NAME = "test.bundle";

@Mock
Plugin plugin;

@Mock
ILog log;

@Mock
Bundle bundle;

@Captor
ArgumentCaptor<IStatus> statusCapture;

@Before
public void setUp() {
when(plugin.getLog()).thenReturn(log);
when(plugin.getBundle()).thenReturn(bundle);
when(bundle.getSymbolicName()).thenReturn(BUNDLE_NAME);
}

@Test
public void logErrorSeverityWhileExceptionIsGiven() {
LoggingSupport.log(plugin, "whatever", new Exception("expect error test"));

verify(log).log(statusCapture.capture());
assertEquals(IStatus.ERROR, statusCapture.getValue().getSeverity());
}

@Test
public void logWarningSeverityWhileThrowableIsGiven() {
String errorMessage = "ErrorMessage 1";
LoggingSupport.log(plugin, errorMessage, new Throwable("expect error test"));

verify(log).log(statusCapture.capture());
assertStatus(IStatus.WARNING, errorMessage, BUNDLE_NAME, statusCapture.getValue());
}

@Test
public void logInfoSeverityWithNullThrowableIsGiven() {
String errorMessage = "ErrorMessage 1";
LoggingSupport.log(plugin, errorMessage, null);

verify(log).log(statusCapture.capture());
assertStatus(IStatus.INFO, errorMessage, BUNDLE_NAME, statusCapture.getValue());
}
@Test
public void logWithNullMessage() {
LoggingSupport.log(plugin, null, new Exception("expect error test"));

verify(log).log(statusCapture.capture());
assertStatus(IStatus.ERROR, "", "test.bundle", statusCapture.getValue());
}

private void assertStatus(int expectedSeverity, String expectedMessage, String expectedBundleName, IStatus givenStatus) {
assertEquals(expectedSeverity, givenStatus.getSeverity());
assertEquals(expectedMessage, givenStatus.getMessage());
assertEquals(expectedBundleName, givenStatus.getPlugin());
}

}
1 change: 1 addition & 0 deletions plugins/org.locationtech.udig.core/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Export-Package: org.locationtech.udig.core,
org.locationtech.udig.core.filter,
org.locationtech.udig.core.internal,
org.locationtech.udig.core.jts,
org.locationtech.udig.core.logging,
org.locationtech.udig.core.opengis
Require-Bundle: org.eclipse.core.runtime,
org.eclipse.ui,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Plugin;
import org.eclipse.core.runtime.Status;
import org.locationtech.udig.core.logging.LoggingSupport;
import org.osgi.framework.BundleContext;

/**
Expand Down Expand Up @@ -67,6 +66,7 @@ public CorePlugin() {
/**
* @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
*/
@Override
public void start( BundleContext context ) throws Exception {
super.start(context);
}
Expand All @@ -84,7 +84,7 @@ public static URL createSafeURL( String spec ) {
try {
return new URL(null, spec, RELAXED_HANDLER);
} catch (MalformedURLException e) {
throw (RuntimeException) new RuntimeException( e );
throw new RuntimeException( e );
}
}
/**
Expand All @@ -100,7 +100,7 @@ public static URI createSafeURI( String spec ){
try {
return new URI( spec );
} catch (URISyntaxException e) {
throw (RuntimeException) new RuntimeException( e );
throw new RuntimeException( e );
}
}

Expand Down Expand Up @@ -132,7 +132,7 @@ public static List<URL> stringsToURLs( String string ) {
* @return a List of URLs, in the same order as the array
*/
public static List<URL> stringsToURLs( String[] strings ) {
List<URL> urls = new ArrayList<URL>();
List<URL> urls = new ArrayList<>();

for( String string : strings ) {
try {
Expand All @@ -154,32 +154,29 @@ public static List<URL> stringsToURLs( String[] strings ) {
* <p>
* This should be used for user level messages.
* </p>
*
* @deprecated Use {@link LoggingSupport#log(Plugin, String, Throwable)} instead.
*/
public static void log( String message2, Throwable e ) {
String message=message2;
if (message == null)
message = ""; //$NON-NLS-1$
getDefault().getLog().log(new Status(IStatus.INFO, ID, 0, message, e));
public static void log(String message2, Throwable e) {
LoggingSupport.log(getDefault(), message2, e);
}

/**
* Messages that only engage if getDefault().isDebugging()
* <p>
* It is much prefered to do this:
* It is much preferred to do this:
*
* <pre><code>
* private static final String RENDERING = &quot;org.locationtech.udig.project/render/trace&quot;;
* if (ProjectUIPlugin.getDefault().isDebugging() &amp;&amp; &quot;true&quot;.equalsIgnoreCase(RENDERING)) {
* System.out.println(&quot;your message here&quot;);
* }
* <pre>
* <code> private static final String RENDERING =
* &quot;org.locationtech.udig.project/render/trace&quot;; if
* (ProjectUIPlugin.getDefault().isDebugging() &amp;&amp;
* &quot;true&quot;.equalsIgnoreCase(RENDERING)) { System.out.println(&quot;your message
* here&quot;); }
*
* @deprecated Use {@link LoggingSupport#trace(Plugin, String, Throwable)} instead.
*/
public static void trace( String message, Throwable e ) {
if (getDefault().isDebugging()) {
if (message != null)
System.out.println(message);
if (e != null)
e.printStackTrace();
}
public static void trace(String message, Throwable e) {
LoggingSupport.trace(getDefault(), message, e);
}
/**
* Performs the Platform.getDebugOption true check on the provided trace
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* uDig - User Friendly Desktop Internet GIS client
* https://locationtech.org/projects/technology.udig
* (C) 2021, Eclipse Foundation
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution
* License v1.0 (http://www.eclipse.org/org/documents/edl-v10.html).
*/
package org.locationtech.udig.core.logging;

import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Plugin;
import org.eclipse.core.runtime.Status;
import org.locationtech.udig.core.internal.CorePlugin;

public class LoggingSupport {

/**
* Writes an info log in the plugin's log.
* <p>
* This should be used for user level messages.
*
* @param plugin Plug-In to create log messages for
* @param logMessage message or just null, in case of Just logging throwable/exception
* @param e {@link Throwable} will be logged as {@link IStatus#WARNING}, {@link Exception} is
* logged as {@link IStatus#ERROR}, if <code>null</code> that just {@link IStatus#INFO}
*/
public static void log(Plugin plugin, String logMessage, Throwable e) {
String message = logMessage;

Plugin logPlugin = (plugin == null ? CorePlugin.getDefault() : plugin);

ILog pluginLog = logPlugin.getLog();

int status = (e == null ? IStatus.INFO
: (e instanceof Exception ? IStatus.ERROR : IStatus.WARNING));

pluginLog.log(new Status(status, logPlugin.getBundle().getSymbolicName(), message, e));
}

/**
* See {@link #log(Plugin, String, Throwable)} just without an explicit logMessage.
*/
public static void log(Plugin plugin, Throwable e) {
log(plugin, null, e);
}

/**
* See {@link #log(Plugin, String, Throwable)} just without an explicit {@link Throwable}
*/
public static void log(Plugin plugin, String logMessage) {
log(plugin, logMessage, null);
}

/**
* Messages that only engage if is debugging is enabled for the given Plug-In.
* <p>
* It is much preferred to do this:
*
* <pre>
* <code>
* private static final String RENDERING = &quot;org.locationtech.udig.project/render/trace&quot;;
* if (ProjectUIPlugin.getDefault().isDebugging() &amp;&amp; &quot;true&quot;.equalsIgnoreCase(RENDERING)) {
* System.out.println(&quot;your message here&quot;);
* }
* </code>
*/
public static void trace(Plugin plugin, String message, Throwable e) {
if (plugin.isDebugging()) {
if (message != null)
System.out.println(message);
if (e != null)
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
*/
package org.locationtech.udig.tool.info;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.locationtech.udig.core.logging.LoggingSupport;
import org.osgi.framework.BundleContext;

/**
Expand Down Expand Up @@ -45,18 +44,20 @@ public InfoPlugin() {
plugin = this;
}

@Override
public void start(BundleContext context) throws Exception {
super.start(context);
}

@Override
public void stop(BundleContext context) throws Exception {
plugin = null;
super.stop(context);
}

/**
* Access shared InfoPlugin instance.
*
*
* @return Shared Instance
*/
public static InfoPlugin getDefault() {
Expand All @@ -68,29 +69,22 @@ public static InfoPlugin getDefault() {
* <p>
* This should be used for user level messages.
* </p>
*
* @deprecated Use LoggerSupport
*/
public static void log(String message, Throwable e) {
getDefault().getLog().log(new Status(IStatus.INFO, ID, 0, message, e));
LoggingSupport.log(getDefault(), message, e);
}

/**
* Messages that only engage if getDefault().isDebugging()
* <p>
* It is much prefered to do this:
*
* <pre><code>
* private static final String RENDERING = "org.locationtech.udig.project/render/trace";
* if( ProjectUIPlugin.getDefault().isDebugging() && "true".equalsIgnoreCase( RENDERING ) ){
* System.out.println( "your message here" );
* }
*
* @deprecated Use
* {@link LoggingSupport#trace(org.eclipse.core.runtime.Plugin, String, Throwable)}
* instead.
*/
public static void trace(String message, Throwable e) {
if (getDefault().isDebugging()) {
if (message != null)
System.out.println(message);
if (e != null)
e.printStackTrace();
}
LoggingSupport.trace(getDefault(), message, e);
}

/**
Expand All @@ -101,11 +95,11 @@ public static void trace(String message, Throwable e) {
* <li>Trace.RENDER - trace rendering progress
* </ul>
* </p>
*
*
* @param trace currently only RENDER is defined
*/
public static boolean isDebugging(final String trace) {
return getDefault().isDebugging()
&& "true".equalsIgnoreCase(Platform.getDebugOption(trace)); //$NON-NLS-1$
&& "true".equalsIgnoreCase(Platform.getDebugOption(trace)); //$NON-NLS-1$
}
}
Loading

0 comments on commit 46ee20f

Please sign in to comment.