Commit 688bc795 authored by Nico Mack's avatar Nico Mack

Initial commit of file

parent 57b3321b
/**
* Copyright Luxembourg Institute of Science and Technology, 2017. All rights reserved.
*
* This file is part of TULIP.
*
* TULIP is free software: you can redistribute it and/or modify it under the terms of the GNU
* Lesser General Public License as published by the Free Software Foundation, version 3 of the
* License.
*
* TULIP is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with TULIP. If
* not, see <http://www.gnu.org/licenses/lgpl-3.0.html>.
*/
package lu.list.itis.dkd.tui.logging;
import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.Layout;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.appender.AbstractAppender;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
import org.apache.logging.log4j.core.config.plugins.PluginElement;
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
import org.apache.logging.log4j.core.layout.PatternLayout;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* @author mack
* @since [major].[minor]
* @version [major].[minor].[micro]
*/
@Plugin(name = "TangibleLog", category = "Core", elementType = "appender", printObject = true)
public final class TangibleLogAppender extends AbstractAppender {
private Layout<? extends Serializable> layout;
private List<LogListener> logListeners;
private static HashMap<String, TangibleLogAppender> instances = new HashMap<>();
/**
* @param name
* @param filter
* @param layout
* @param size
*/
public TangibleLogAppender(String name, Filter filter, Layout<? extends Serializable> layout) {
super(name, filter, layout);
this.layout = layout;
logListeners = new ArrayList<>();
}
/** {@inheritDoc} */
@Override
public void append(LogEvent event) {
String entry = new String(this.layout.toByteArray(event));
notifyLogListeners(entry);
}
/**
* @param listener
*/
public void addLogListener(LogListener listener) {
logListeners.add(listener);
}
private void notifyLogListeners(String entry) {
logListeners.forEach(listener -> listener.messageLogged(entry));
}
// Your custom appender needs to declare a factory method
// annotated with `@PluginFactory`. Log4j will parse the configuration
// and call this factory method to construct an appender instance with
// the configured attributes.
/**
* @param name
* @param layout
* @param filter
* @return
*/
@PluginFactory
public static TangibleLogAppender createAppender(
@PluginAttribute("name") String name,
@PluginElement("Layout") Layout<? extends Serializable> layout,
@PluginElement("Filter") final Filter filter) {
if (name == null) {
LOGGER.error("No name provided for TangibleLogAppender"); //$NON-NLS-1$
return null;
}
Layout<? extends Serializable> loggerLayout = (layout == null) ? PatternLayout.createDefaultLayout() : layout;
instances.put(name, new TangibleLogAppender(name, filter, loggerLayout));
return instances.get(name);
}
/**
* @param name
* @return
*/
public static TangibleLogAppender getInstance(String name) {
return instances.get(name);
}
}
/**
* Copyright Luxembourg Institute of Science and Technology, 2017. All rights reserved.
*
* This file is part of TULIP.
*
* TULIP is free software: you can redistribute it and/or modify it under the terms of the GNU
* Lesser General Public License as published by the Free Software Foundation, version 3 of the
* License.
*
* TULIP is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with TULIP. If
* not, see <http://www.gnu.org/licenses/lgpl-3.0.html>.
*/
package lu.list.itis.dkd.tui.logging;
import lu.list.itis.dkd.tui.utility.Point;
import lu.list.itis.dkd.tui.widget.state.NuiState;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.text.DecimalFormat;
import java.util.HashMap;
/**
* A widget implementation which adds a {@link NuiState} to the widget keeping track of state
* transitions and the actual state.
*
* @author Nico Mack [nico.mack@list.lu]
* @author Eric Tobias [eric.tobias@list.lu]
* @since 1.0
* @version 2.5.0
*/
// ***************************************************************************
// * Class Definition and Members *
// ***************************************************************************
public class TangibleLogger {
private static DecimalFormat format = new DecimalFormat();
static {
format.setDecimalSeparatorAlwaysShown(false);
format.setMaximumFractionDigits(4);
format.setGroupingUsed(false);
}
// ***************************************************************************
// * Constants
// ***************************************************************************
/** */
public static final int UNKNOWN = 0;
/** */
public static final int DROPPED = 1;
/** */
public static final int TRANSLATED = 2;
/** */
public static final int ROTATED = 3;
/** */
public static final int STOPPED = 4;
/** */
public static final int LIFTED = 5;
private static HashMap<Integer, String> actionLookup = new HashMap<>();
static {
actionLookup.put(UNKNOWN, "UKN"); //$NON-NLS-1$
actionLookup.put(DROPPED, "DRP");//$NON-NLS-1$
actionLookup.put(TRANSLATED, "TRA");//$NON-NLS-1$
actionLookup.put(ROTATED, "ROT");//$NON-NLS-1$
actionLookup.put(STOPPED, "STP");//$NON-NLS-1$
actionLookup.put(LIFTED, "LFT");//$NON-NLS-1$
}
// private static final String WIDGET_TEMPLATE = "W[{}]:[{}]:[{}|{}|{}]"; //$NON-NLS-1$
private static final String WIDGET_TEMPLATE = "{\"widget\" : { \"id\":{}, \"event\":\"{}\", \"pos\": {\"x\":{}, \"y\":{}, \"a\":{}}}}"; //$NON-NLS-1$
private static final String CURSOR_TEMPLATE = "{\"cursor\" : { \"id\":{}, \"event\":\"{}\", \"pos\": {\"x\":{}, \"y\":{}}}}"; //$NON-NLS-1$
private static final Logger LOGGER = LoggerFactory.getLogger(TangibleLogger.class.getSimpleName());
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Constructor(s)
// ***************************************************************************
// ---------------------------------------------------------------------------
private TangibleLogger() {}
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Class Body
// ***************************************************************************
// ---------------------------------------------------------------------------
/**
* @param objectId
* @param position
* @param action
*/
// ---------------------------------------------------------------------------
public static void logWidget(int objectId, Point position, int action) {
if (LOGGER.isInfoEnabled()) {
if ((action < UNKNOWN) && (action > LIFTED)) {
LOGGER.error("Specified action {} is not defined!", action); //$NON-NLS-1$
return;
}
LOGGER.info(WIDGET_TEMPLATE,
objectId,
actionLookup.get(action),
format.format(position.getX()),
format.format(position.getY()),
format.format(position.getAngle()));
}
}
// ---------------------------------------------------------------------------
/**
* @param objectId
* @param position
* @param action
*/
// ---------------------------------------------------------------------------
public static void logCursor(int objectId, Point position, int action) {
if (LOGGER.isInfoEnabled()) {
if ((action < UNKNOWN) && (action > LIFTED)) {
LOGGER.error("Specified action {} is not defined!", action); //$NON-NLS-1$
return;
}
LOGGER.info(CURSOR_TEMPLATE,
objectId,
actionLookup.get(action),
format.format(position.getX()),
format.format(position.getY()));
}
}
// ---------------------------------------------------------------------------
// ***************************************************************************
// * End of Class
// ***************************************************************************
// ---------------------------------------------------------------------------
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment