diff options
Diffstat (limited to 'src/org/traccar/MainEventHandler.java')
-rw-r--r-- | src/org/traccar/MainEventHandler.java | 68 |
1 files changed, 51 insertions, 17 deletions
diff --git a/src/org/traccar/MainEventHandler.java b/src/org/traccar/MainEventHandler.java index 3032646c1..d4e1ff860 100644 --- a/src/org/traccar/MainEventHandler.java +++ b/src/org/traccar/MainEventHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 - 2018 Anton Tananaev (anton@traccar.org) + * Copyright 2012 - 2019 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,23 +29,28 @@ import org.traccar.model.Position; import java.sql.SQLException; import java.util.Arrays; import java.util.HashSet; +import java.util.LinkedHashSet; import java.util.Set; public class MainEventHandler extends ChannelInboundHandlerAdapter { private static final Logger LOGGER = LoggerFactory.getLogger(GeocoderHandler.class); + private static final String DEFAULT_LOGGER_EVENTS = "time,position,speed,course,accuracy,result"; private final Set<String> connectionlessProtocols = new HashSet<>(); + private final Set<String> logEvents; public MainEventHandler() { String connectionlessProtocolList = Context.getConfig().getString("status.ignoreOffline"); if (connectionlessProtocolList != null) { connectionlessProtocols.addAll(Arrays.asList(connectionlessProtocolList.split(","))); } + logEvents = new LinkedHashSet<>(Arrays.asList( + Context.getConfig().getString("logger.events", DEFAULT_LOGGER_EVENTS).split(","))); } @Override - public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + public void channelRead(ChannelHandlerContext ctx, Object msg) { if (msg instanceof Position) { Position position = (Position) msg; @@ -61,19 +66,45 @@ public class MainEventHandler extends ChannelInboundHandlerAdapter { StringBuilder s = new StringBuilder(); s.append(formatChannel(ctx.channel())).append(" "); s.append("id: ").append(uniqueId); - s.append(", time: ").append(DateUtil.formatDate(position.getFixTime(), false)); - s.append(", lat: ").append(String.format("%.5f", position.getLatitude())); - s.append(", lon: ").append(String.format("%.5f", position.getLongitude())); - if (position.getSpeed() > 0) { - s.append(", speed: ").append(String.format("%.1f", position.getSpeed())); - } - s.append(", course: ").append(String.format("%.1f", position.getCourse())); - if (position.getAccuracy() > 0) { - s.append(", accuracy: ").append(String.format("%.1f", position.getAccuracy())); - } - Object cmdResult = position.getAttributes().get(Position.KEY_RESULT); - if (cmdResult != null) { - s.append(", result: ").append(cmdResult); + for (String event : logEvents) { + switch (event) { + case "time": + s.append(", time: ").append(DateUtil.formatDate(position.getFixTime(), false)); + break; + case "position": + s.append(", lat: ").append(String.format("%.5f", position.getLatitude())); + s.append(", lon: ").append(String.format("%.5f", position.getLongitude())); + break; + case "speed": + if (position.getSpeed() > 0) { + s.append(", speed: ").append(String.format("%.1f", position.getSpeed())); + } + break; + case "course": + s.append(", course: ").append(String.format("%.1f", position.getCourse())); + break; + case "accuracy": + if (position.getAccuracy() > 0) { + s.append(", accuracy: ").append(String.format("%.1f", position.getAccuracy())); + } + break; + case "outdated": + if (position.getOutdated()) { + s.append(", outdated"); + } + break; + case "invalid": + if (!position.getValid()) { + s.append(", invalid"); + } + break; + default: + Object value = position.getAttributes().get(event); + if (value != null) { + s.append(", ").append(event).append(": ").append(value); + } + break; + } } LOGGER.info(s.toString()); @@ -104,13 +135,16 @@ public class MainEventHandler extends ChannelInboundHandlerAdapter { } @Override - public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { + while (cause.getCause() != null && cause.getCause() != cause) { + cause = cause.getCause(); + } LOGGER.warn(formatChannel(ctx.channel()) + " error", cause); closeChannel(ctx.channel()); } @Override - public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { + public void userEventTriggered(ChannelHandlerContext ctx, Object evt) { if (evt instanceof IdleStateEvent) { LOGGER.info(formatChannel(ctx.channel()) + " timed out"); closeChannel(ctx.channel()); |