-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
12 changed files
with
376 additions
and
181 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
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
1 change: 1 addition & 0 deletions
1
...ins/org.locationtech.udig.core.tests/src/mockito-extensions/org.mockito.plugins.MockMaker
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 @@ | ||
mock-maker-inline |
93 changes: 93 additions & 0 deletions
93
...cationtech.udig.core.tests/src/org/locationtech/udig/core/logging/LoggingSupportTest.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,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()); | ||
} | ||
|
||
} |
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
79 changes: 79 additions & 0 deletions
79
...ins/org.locationtech.udig.core/src/org/locationtech/udig/core/logging/LoggingSupport.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,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 = "org.locationtech.udig.project/render/trace"; | ||
* if (ProjectUIPlugin.getDefault().isDebugging() && "true".equalsIgnoreCase(RENDERING)) { | ||
* System.out.println("your message here"); | ||
* } | ||
* </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(); | ||
} | ||
} | ||
} |
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.