| /******************************************************************************* |
| * Copyright (c) 2018 Agence spatiale canadienne / Canadian Space Agency |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * Pierre Allard, |
| * Regent L'Archeveque, |
| * Sebastien Gemme - initial API and implementation |
| * SPDX-License-Identifier: EPL-1.0 |
| * |
| *******************************************************************************/ |
| package org.eclipse.apogy.addons.ros.utilities; |
| |
| import org.eclipse.core.runtime.ILogListener; |
| import org.eclipse.core.runtime.IStatus; |
| import org.eclipse.core.runtime.Platform; |
| import org.ros.node.ConnectedNode; |
| import org.ros.node.topic.Publisher; |
| |
| public class FrameworkLogPublisher { |
| protected ILogListener logListener = null; |
| protected ConnectedNode connectedNode = null; |
| protected String topicName = null; |
| protected Publisher<org.eclipse.apogy.addons.ros.messages.ROSFrameworkLogEntry> frameworkLogEntryPublisher = null; |
| |
| public FrameworkLogPublisher(String topicName, final ConnectedNode connectedNode) { |
| setTopicName(topicName); |
| setConnectedNode(connectedNode); |
| |
| registerListener(); |
| } |
| |
| @Override |
| protected void finalize() throws Throwable { |
| unRegisterListener(); |
| super.finalize(); |
| } |
| |
| private void setTopicName(String topicName) { |
| this.topicName = topicName; |
| } |
| |
| private void setConnectedNode(final ConnectedNode connectedNode) { |
| if (this.frameworkLogEntryPublisher != null) { |
| this.frameworkLogEntryPublisher.shutdown(); |
| this.frameworkLogEntryPublisher = null; |
| } |
| |
| this.connectedNode = connectedNode; |
| |
| if (connectedNode != null) { |
| this.frameworkLogEntryPublisher = connectedNode.newPublisher(this.topicName, |
| org.eclipse.apogy.addons.ros.messages.ROSFrameworkLogEntry._TYPE); |
| } |
| } |
| |
| private void registerListener() { |
| Platform.addLogListener(getILogListener()); |
| } |
| |
| private void unRegisterListener() { |
| Platform.removeLogListener(getILogListener()); |
| } |
| |
| private ILogListener getILogListener() { |
| if (this.logListener == null) { |
| this.logListener = new ILogListener() { |
| @Override |
| public void logging(IStatus status, String plugin) { |
| try { |
| if (FrameworkLogPublisher.this.frameworkLogEntryPublisher != null) { |
| org.eclipse.apogy.addons.ros.messages.ROSFrameworkLogEntry entry = FrameworkLogUtils |
| .convertToROSFrameworkLogEntry(status, FrameworkLogPublisher.this.connectedNode); |
| FrameworkLogPublisher.this.frameworkLogEntryPublisher.publish(entry); |
| } |
| } catch (Throwable t) { |
| |
| } |
| } |
| }; |
| } |
| |
| return this.logListener; |
| } |
| } |