Skip to content

Commit

Permalink
Use Logger to log messages
Browse files Browse the repository at this point in the history
  • Loading branch information
domi-b committed Nov 20, 2024
1 parent 561dacd commit 807731f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 15 deletions.
8 changes: 3 additions & 5 deletions src/main/java/ch/geowerkstatt/lk2dxf/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,12 @@ private static boolean processFiles(LK2DxfOptions options) {

objects.forEach(o -> o.writeToDxf(dxfWriter));
} catch (Exception e) {
System.err.println("Failed to process file: " + xtfFile);
e.printStackTrace();
LOGGER.error("Failed to process file: {}", xtfFile, e);
return false;
}
}
} catch (Exception e) {
System.err.println("Failed to write DXF file: " + options.dxfFile());
e.printStackTrace();
LOGGER.error("Failed to write DXF file: {}", options.dxfFile(), e);
return false;
}

Expand Down Expand Up @@ -137,7 +135,7 @@ private static CommandLine parseCommandLine(Options options, String[] args) {
DefaultParser parser = new DefaultParser();
return parser.parse(options, args);
} catch (ParseException e) {
System.err.println("Error parsing command line arguments: " + e.getMessage());
LOGGER.error("Error parsing command line arguments: {}", e.getMessage());
printUsage(options);
return null;
}
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/ch/geowerkstatt/lk2dxf/MappedObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import ch.interlis.iox_j.jts.Iox2jtsext;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public record MappedObject(
String oid,
Expand All @@ -18,6 +20,7 @@ public record MappedObject(
String text,
LayerMapping layerMapping) {
private static final GeometryFactory GEOMETRY_FACTORY = new JtsextGeometryFactory();
private static final Logger LOGGER = LogManager.getLogger();

/**
* Creates a new {@link MappedObject} that contains all information to process the object further.
Expand Down Expand Up @@ -59,8 +62,7 @@ public void writeToDxf(DxfWriter dxfWriter) {
default -> throw new AssertionError("Unknown output type: " + layerMapping().output());
}
} catch (Exception e) {
System.err.println("Failed to write object: " + oid() + " to dxf.");
e.printStackTrace();
LOGGER.error("Failed to write object: {} to dxf.", oid(), e);
}
}
}
13 changes: 9 additions & 4 deletions src/main/java/ch/geowerkstatt/lk2dxf/XtfStreamReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import ch.interlis.iox.StartTransferEvent;
import ch.interlis.iox_j.logging.LogEventFactory;
import ch.interlis.iox_j.utility.ReaderFactory;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.File;
import java.util.Spliterator;
Expand All @@ -24,8 +26,10 @@
*/
public final class XtfStreamReader implements AutoCloseable {
private static final ReaderFactory READER_FACTORY = new ReaderFactory();
private static final Logger LOGGER = LogManager.getLogger();

private final IoxReader reader;
private final String filename;
private LKMapXtfReaderState state = null;

/**
Expand All @@ -37,6 +41,7 @@ public XtfStreamReader(File xtfFile) throws IoxException {
LogEventFactory logEventFactory = new LogEventFactory();
Settings settings = new Settings();
this.reader = READER_FACTORY.createReader(xtfFile, logEventFactory, settings);
this.filename = xtfFile.getName();
}

/**
Expand Down Expand Up @@ -82,14 +87,14 @@ public boolean tryAdvance(Consumer<? super IomObject> action) {
throw new IllegalStateException("Unexpected start transfer event in state: " + state);
}
state = LKMapXtfReaderState.TRANSFER;
System.out.println("Start transfer");
LOGGER.info("Start transfer of \"{}\"", filename);
}
case StartBasketEvent startBasketEvent -> {
if (state != LKMapXtfReaderState.TRANSFER) {
throw new IllegalStateException("Unexpected start basket event in state: " + state);
}
state = LKMapXtfReaderState.BASKET;
System.out.println("Start basket \"" + startBasketEvent.getBid() + "\"");
LOGGER.info("Start basket \"{}\"", startBasketEvent.getBid());
}
case ObjectEvent objectEvent -> {
if (state != LKMapXtfReaderState.BASKET) {
Expand All @@ -103,14 +108,14 @@ public boolean tryAdvance(Consumer<? super IomObject> action) {
throw new IllegalStateException("Unexpected end basket event in state: " + state);
}
state = LKMapXtfReaderState.TRANSFER;
System.out.println("End basket");
LOGGER.info("End basket");
}
case EndTransferEvent ignored -> {
if (state != LKMapXtfReaderState.TRANSFER) {
throw new IllegalStateException("Unexpected end transfer event in state: " + state);
}
state = LKMapXtfReaderState.COMPLETED;
System.out.println("End transfer");
LOGGER.info("End transfer of \"{}\"", filename);
return false;
}
default -> throw new IllegalStateException("Unexpected iox event: " + event);
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/ch/geowerkstatt/lk2dxf/mapping/ObjectMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import ch.interlis.iom.IomObject;
import ch.interlis.iom_j.Iom_jObject;
import ch.interlis.iox_j.validator.Value;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.File;
import java.io.IOException;
Expand All @@ -33,6 +35,8 @@

public final class ObjectMapper {
private static final String MODELS_RESOURCE = "/models";
private static final Logger LOGGER = LogManager.getLogger();

private final List<LayerMapping> layerMappings;
private final TransferDescription transferDescription;

Expand Down Expand Up @@ -332,7 +336,7 @@ private static TransferDescription getTransferDescription(List<LayerMapping> lay
}

try {
System.out.println("iliModelsPath: " + iliModelsPath);
LOGGER.info("iliModelsPath: {}", iliModelsPath);
var modelManager = new IliManager();
modelManager.setRepositories(new String[]{iliModelsPath});
var ili2cConfig = modelManager.getConfig(requiredModels, 0.0);
Expand Down Expand Up @@ -369,11 +373,11 @@ public Stream<MappedObject> mapObjects(Stream<IomObject> iomObjects) {
private Optional<MappedObject> mapObject(IomObject iomObject, Map<String, IomObject> objectCache, Set<IomObject> objectsWithUnresolvedRef, boolean unresolvedReferencesAllowed) {
var element = transferDescription.getElement(iomObject.getobjecttag());
if (element == null) {
System.out.println("No element found for object with id \"" + iomObject.getobjectoid() + "\".");
LOGGER.error("No element \"{}\" found for object with id \"{}\".", iomObject.getobjecttag(), iomObject.getobjectoid());
return Optional.empty();
}
if (!(element instanceof AbstractClassDef<?> classDef)) {
System.out.println("Element is not an AbstractClassDef for object with id \"" + iomObject.getobjectoid() + "\".");
LOGGER.error("Element \"{}\" is not an AbstractClassDef for object with id \"{}\".", iomObject.getobjecttag(), iomObject.getobjectoid());
return Optional.empty();
}

Expand Down Expand Up @@ -413,6 +417,8 @@ private Optional<MappedObject> mapObject(IomObject iomObject, Map<String, IomObj
default -> { } // MATCH, continue with next filter
}
}

LOGGER.trace("Mapped object of type \"{}\" with id \"{}\" to layer \"{}\".", iomObject.getobjecttag(), iomObject.getobjectoid(), mapper.mapping().layer());
return Optional.of(new MappedObject(
iomObject.getobjectoid(),
Optional.ofNullable(resolve(iomObject, mapper.geometry(), objectCache).getComplexObjects()).map(Collection::iterator).map(Iterator::next).orElse(null),
Expand All @@ -424,7 +430,7 @@ private Optional<MappedObject> mapObject(IomObject iomObject, Map<String, IomObj
}

// no match found
System.out.println("No match found for object with id \"" + iomObject.getobjectoid() + "\".");
LOGGER.warn("No match found for object of type \"{}\" with id \"{}\".", iomObject.getobjecttag(), iomObject.getobjectoid());
return Optional.empty();
}

Expand Down

0 comments on commit 807731f

Please sign in to comment.