Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Eclipse logging #419

Merged
merged 1 commit into from
Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
package org.eclipse.tm4e.languageconfiguration;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;
Expand Down Expand Up @@ -39,12 +40,22 @@ public static LanguageConfigurationPlugin getDefault() {
}

public static void log(final IStatus status) {
final var plugin = LanguageConfigurationPlugin.plugin;
if (plugin != null) {
plugin.getLog().log(status);
final var p = plugin;
if (p != null) {
p.getLog().log(status);
} else {
System.out.println(status);
}
}

public static void logError(final Exception ex) {
log(new Status(IStatus.ERROR, PLUGIN_ID, ex.getMessage(), ex));
}

public static void logError(final String message, @Nullable final Exception ex) {
log(new Status(IStatus.ERROR, PLUGIN_ID, message, ex));
}

@Override
public void start(@Nullable final BundleContext context) throws Exception {
super.start(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.tm4e.languageconfiguration.LanguageConfigurationPlugin;
import org.eclipse.tm4e.languageconfiguration.internal.registry.ILanguageConfigurationDefinition;
import org.eclipse.tm4e.languageconfiguration.internal.registry.ILanguageConfigurationRegistryManager;
import org.eclipse.tm4e.languageconfiguration.internal.registry.LanguageConfigurationRegistryManager;
Expand Down Expand Up @@ -279,8 +280,8 @@ public void setVisible(final boolean visible) {
public boolean performOk() {
try {
manager.save();
} catch (final BackingStoreException e) {
// TODO: Log
} catch (final BackingStoreException ex) {
LanguageConfigurationPlugin.logError(ex);
}
return super.performOk();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
import java.util.Collection;
import java.util.Objects;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.tm4e.languageconfiguration.LanguageConfigurationPlugin;
import org.eclipse.tm4e.languageconfiguration.internal.registry.ILanguageConfigurationDefinition;
Expand Down Expand Up @@ -44,9 +42,9 @@ public final class PreferenceHelper {
final var contentTypeId = object.get("contentTypeId").getAsString();
final var contentType = ContentTypeHelper.getContentTypeById(contentTypeId);
if (contentType == null) {
LanguageConfigurationPlugin.log(new Status(IStatus.ERROR, PreferenceHelper.class,
"Cannot load language configuration with unknown content type ID "
+ contentTypeId));
LanguageConfigurationPlugin.logError(
"Cannot load language configuration with unknown content type ID " + contentTypeId,
null);
return null;
}
return new LanguageConfigurationDefinition(contentType, // $NON-NLS-1$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,8 @@ public IContentType getContentType() {
public ILanguageConfiguration getLanguageConfiguration() {
try (var in = getInputStream()) {
return LanguageConfiguration.load(new InputStreamReader(in, Charset.defaultCharset()));
} catch (final IOException e) {
final var plugin = LanguageConfigurationPlugin.getDefault();
if (plugin != null) {
plugin.getLog().log(
new Status(IStatus.ERROR, LanguageConfigurationPlugin.PLUGIN_ID, e.getMessage(), e));
}
} catch (final IOException ex) {
LanguageConfigurationPlugin.logError(ex);
return null;
}
}
Expand Down
1 change: 0 additions & 1 deletion org.eclipse.tm4e.registry/.options

This file was deleted.

1 change: 0 additions & 1 deletion org.eclipse.tm4e.registry/build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ bin.includes = META-INF/,\
plugin.xml,\
plugin.properties,\
.,\
.options,\
about.html

# https://codeiseasy.wordpress.com/2013/03/08/tycho-and-jdt-null-analysis/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,45 +1,72 @@
/**
* Copyright (c) 2015-2017 Angelo ZERR.
* Copyright (c) 2015-2017 Angelo ZERR.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Angelo Zerr <angelo.zerr@gmail.com> - initial API and implementation
* Contributors:
* Angelo Zerr <angelo.zerr@gmail.com> - initial API and implementation
*/
package org.eclipse.tm4e.registry;

import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Plugin;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.tm4e.registry.internal.GrammarRegistryManager;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

/**
* OSGi Activator for TextMate Eclipse registry bundle.
*/
public class TMEclipseRegistryPlugin implements BundleActivator {
public class TMEclipseRegistryPlugin extends Plugin {

/** The plug-in ID */
public static final String PLUGIN_ID = "org.eclipse.tm4e.registry";

/** The shared instance */
@Nullable
private static BundleContext context;
private static volatile TMEclipseRegistryPlugin plugin;

/**
* Returns the shared instance
*
* @return the shared instance
*/
@Nullable
static BundleContext getContext() {
return context;
public static TMEclipseRegistryPlugin getDefault() {
return plugin;
}

public static void log(final IStatus status) {
final var p = plugin;
if (p != null) {
p.getLog().log(status);
} else {
System.out.println(status);
}
}

public static void logError(final Exception ex) {
log(new Status(IStatus.ERROR, PLUGIN_ID, ex.getMessage(), ex));
}

public static void logError(final String message, @Nullable final Exception ex) {
log(new Status(IStatus.ERROR, PLUGIN_ID, message, ex));
}

@Override
public void start(@Nullable final BundleContext bundleContext) throws Exception {
TMEclipseRegistryPlugin.context = bundleContext;
super.start(bundleContext);
plugin = this;
}

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

/**
Expand All @@ -50,16 +77,4 @@ public void stop(@Nullable final BundleContext bundleContext) throws Exception {
public static IGrammarRegistryManager getGrammarRegistryManager() {
return GrammarRegistryManager.getInstance();
}

/**
* Returns true if the debug option is enabled and false otherwise.
*
* @param option
* the option name
* @return true if the debug option is enabled and false otherwise.
*/
public static boolean isDebugOptionEnabled(final String option) {
final String enabled = Platform.getDebugOption(option);
return enabled != null && Boolean.parseBoolean(enabled);
}
}
2 changes: 0 additions & 2 deletions org.eclipse.tm4e.ui/.options
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
org.eclipse.tm4e.ui/debug/log/GenerateTest = false
org.eclipse.tm4e.ui/debug/log/TMPresentationReconciler = false
org.eclipse.tm4e.ui/debug/log/ThrowError = false
org.eclipse.tm4e.ui/debug/log/TMModelManager = false
org.eclipse.tm4e.ui/trace = false
Original file line number Diff line number Diff line change
Expand Up @@ -44,35 +44,42 @@ public class TMUIPlugin extends AbstractUIPlugin {
private static volatile TMUIPlugin plugin;

public static void log(final IStatus status) {
final var plugin = TMUIPlugin.plugin;
if (plugin != null) {
plugin.getLog().log(status);
final var p = plugin;
if (p != null) {
p.getLog().log(status);
} else {
System.out.println(status);
}
}

public static void trace(final String message) {
if (Boolean.parseBoolean(Platform.getDebugOption(TRACE_ID))) {
public static void logError(Exception ex) {
log(new Status(IStatus.ERROR, PLUGIN_ID, ex.getMessage(), ex));
}

public static void logTrace(final String message) {
if (isLogTraceEnabled()) {
log(new Status(IStatus.INFO, PLUGIN_ID, message));
}
}

public static boolean isLogTraceEnabled() {
return Boolean.parseBoolean(Platform.getDebugOption(TRACE_ID));
}

@Override
public void start(@Nullable final BundleContext context) throws Exception {
super.start(context);
plugin = this;
final boolean isDebugOn = Boolean.parseBoolean(Platform.getDebugOption(TRACE_ID));
if (isDebugOn) {
final Logger tm4eCoreLogger = Logger.getLogger("org.eclipse.tm4e");
if (isLogTraceEnabled()) {
// if the trace option is enabled publish all TM4E CORE JDK logging output to the Eclipse Error Log
final var tm4eCorePluginId = "org.eclipse.tm4e.core";
final var tm4eCoreLogger = Logger.getLogger(tm4eCorePluginId);
tm4eCoreLogger.setLevel(Level.FINEST);
tm4eCoreLogger.addHandler(new Handler() {

@Override
public void publish(@Nullable final LogRecord record) {
if (record != null) {
log(new Status(
toSeverity(record.getLevel()),
"org.eclipse.tm4e.core",
record.getMessage()));
public void publish(@Nullable final LogRecord entry) {
if (entry != null) {
log(new Status(toSeverity(entry.getLevel()), tm4eCorePluginId, entry.getMessage()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
*/
package org.eclipse.tm4e.ui.internal.model;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.DocumentEvent;
Expand Down Expand Up @@ -50,7 +48,7 @@ public void documentAboutToBeChanged(@Nullable final DocumentEvent event) {
default:
}
} catch (final BadLocationException ex) {
TMUIPlugin.log(new Status(IStatus.ERROR, TMUIPlugin.PLUGIN_ID, ex.getMessage(), ex));
TMUIPlugin.logError(ex);
}
}

Expand Down Expand Up @@ -86,7 +84,7 @@ public void documentChanged(@Nullable final DocumentEvent event) {
}
}
} catch (final BadLocationException ex) {
TMUIPlugin.log(new Status(IStatus.ERROR, TMUIPlugin.PLUGIN_ID, ex.getMessage(), ex));
TMUIPlugin.logError(ex);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

import java.io.File;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.layout.TableColumnLayout;
Expand Down Expand Up @@ -378,8 +376,8 @@ public boolean performOk() {
themeManager.save();
grammarRegistryManager.save();
return true;
} catch (final BackingStoreException e) {
TMUIPlugin.log(new Status(IStatus.ERROR, TMUIPlugin.PLUGIN_ID, e.getMessage(), e));
} catch (final BackingStoreException ex) {
TMUIPlugin.logError(ex);
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static void updateTextMarkers(final ModelTokensChangedEvent event) {
try {
updateTextMarkers(docModel, event.ranges.get(0).fromLineNumber);
} catch (final CoreException ex) {
TMUIPlugin.log(new Status(IStatus.ERROR, TMUIPlugin.PLUGIN_ID, ex.getMessage(), ex));
TMUIPlugin.logError(ex);
}
}
}
Expand Down Expand Up @@ -164,7 +164,7 @@ public static void updateTextMarkers(final TMDocumentModel docModel, final int s
res.createMarker(markerConfig.type, attrs);
}
} catch (final Exception ex) {
TMUIPlugin.log(new Status(IStatus.ERROR, TMUIPlugin.PLUGIN_ID, ex.getMessage(), ex));
TMUIPlugin.logError(ex);
}
}

Expand All @@ -184,7 +184,7 @@ private static Integer getLineNumber(final IMarker marker) {
if (lineNumberAttr instanceof final Integer lineNumber)
return lineNumber;
} catch (final CoreException ex) {
TMUIPlugin.log(new Status(IStatus.ERROR, TMUIPlugin.PLUGIN_ID, ex.getMessage(), ex));
TMUIPlugin.logError(ex);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/
package org.eclipse.tm4e.ui.internal.utils;

import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.jdt.annotation.Nullable;
Expand All @@ -23,7 +24,12 @@ public final class PreferenceUtils {
private static final String E4_CSS_PREFERENCE_NAME = "org.eclipse.e4.ui.css.swt.theme"; //$NON-NLS-1$
private static final String EDITORS_PREFERENCE_NAME = "org.eclipse.ui.editors"; //$NON-NLS-1$

private PreferenceUtils() {
public static boolean isDebugGenerateTest() {
return Boolean.parseBoolean(Platform.getDebugOption(TMUIPlugin.PLUGIN_ID + "/debug/log/GenerateTest"));
}

public static boolean isDebugThrowError() {
return Boolean.parseBoolean(Platform.getDebugOption(TMUIPlugin.PLUGIN_ID + "/debug/log/ThrowError"));
}

/**
Expand Down Expand Up @@ -67,4 +73,7 @@ public static IPreferenceStore getTM4EPreferencesStore() {
final var plugin = TMUIPlugin.getDefault();
return plugin == null ? null : plugin.getPreferenceStore();
}

private PreferenceUtils() {
}
}
Loading