From b5eac492d96626b9953ac4cfca6e66f4d4e986b1 Mon Sep 17 00:00:00 2001 From: Leonard Kupper <leonard.kupper@mailbox.tu-dresden.de> Date: Thu, 25 Jul 2019 18:54:03 +0200 Subject: [PATCH] Extend logging statements fpr multiple classes. --- .../de/tudresden/inf/mci/brailleplot/App.java | 7 +++++++ .../mci/brailleplot/configparser/Printer.java | 6 +++++- .../brailleplot/rendering/AbstractCanvas.java | 14 +++++++++++++ .../rendering/FunctionalRasterizer.java | 12 +++++++++-- .../rendering/FunctionalRenderingBase.java | 2 +- .../rendering/ImageRasterizer.java | 21 ++++++++++++++----- .../brailleplot/rendering/MasterRenderer.java | 14 ++++++------- .../brailleplot/rendering/RasterCanvas.java | 16 ++++++++++++++ .../mci/brailleplot/rendering/Rectangle.java | 7 +++++++ .../config/index_everest_d_v4.properties | 8 +++---- 10 files changed, 86 insertions(+), 21 deletions(-) diff --git a/src/main/java/de/tudresden/inf/mci/brailleplot/App.java b/src/main/java/de/tudresden/inf/mci/brailleplot/App.java index 7de2da75..cf1adf5a 100644 --- a/src/main/java/de/tudresden/inf/mci/brailleplot/App.java +++ b/src/main/java/de/tudresden/inf/mci/brailleplot/App.java @@ -1,5 +1,6 @@ package de.tudresden.inf.mci.brailleplot; +import ch.qos.logback.classic.Level; import de.tudresden.inf.mci.brailleplot.commandline.CommandLineParser; import de.tudresden.inf.mci.brailleplot.commandline.SettingType; import de.tudresden.inf.mci.brailleplot.commandline.SettingsReader; @@ -102,6 +103,11 @@ public final class App { System.exit(EXIT_ERROR); } + public static void setLoggingLevel(final ch.qos.logback.classic.Level level) { + ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) org.slf4j.LoggerFactory.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME); + root.setLevel(level); + } + /** * Main loop of the application. * @param args Command line parameters. @@ -117,6 +123,7 @@ public final class App { try { // Logging example mLogger.info("Application started"); + setLoggingLevel(Level.TRACE); // Parse command line parameters CommandLineParser cliParser = new CommandLineParser(); diff --git a/src/main/java/de/tudresden/inf/mci/brailleplot/configparser/Printer.java b/src/main/java/de/tudresden/inf/mci/brailleplot/configparser/Printer.java index d1e601ca..0f59f7c1 100644 --- a/src/main/java/de/tudresden/inf/mci/brailleplot/configparser/Printer.java +++ b/src/main/java/de/tudresden/inf/mci/brailleplot/configparser/Printer.java @@ -22,6 +22,10 @@ public final class Printer extends Configurable { @Override public String toString() { - return "printer configuration"; + if (getPropertyNames().contains("name")) { + return "printer configuration (" + getProperty("name") + ")"; + } else { + return "printer configuration"; + } } } diff --git a/src/main/java/de/tudresden/inf/mci/brailleplot/rendering/AbstractCanvas.java b/src/main/java/de/tudresden/inf/mci/brailleplot/rendering/AbstractCanvas.java index b2e63738..fa580925 100644 --- a/src/main/java/de/tudresden/inf/mci/brailleplot/rendering/AbstractCanvas.java +++ b/src/main/java/de/tudresden/inf/mci/brailleplot/rendering/AbstractCanvas.java @@ -3,6 +3,8 @@ package de.tudresden.inf.mci.brailleplot.rendering; import de.tudresden.inf.mci.brailleplot.configparser.Format; import de.tudresden.inf.mci.brailleplot.configparser.Printer; import de.tudresden.inf.mci.brailleplot.printabledata.PrintableData; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.ArrayList; import java.util.List; @@ -15,6 +17,8 @@ import java.util.ListIterator; */ public abstract class AbstractCanvas { + private final Logger mLogger = LoggerFactory.getLogger(this.getClass()); + Printer mPrinter; Format mFormat; @@ -23,21 +27,28 @@ public abstract class AbstractCanvas { List<PrintableData> mPageContainer; AbstractCanvas(final Printer printer, final Format format) throws InsufficientRenderingAreaException { + + mLogger.trace("Creating new canvas"); + mPrinter = printer; mFormat = format; mPageContainer = new ArrayList<>(); readConfig(); + } private void readConfig() throws InsufficientRenderingAreaException { + mLogger.trace("Reading general printer and format configuration for printing area calculation"); + // New approach using a box model: // Create a page box int pageWidth = mFormat.getProperty("page.width").toInt(); int pageHeight = mFormat.getProperty("page.height").toInt(); Rectangle pageBox = new Rectangle(0, 0, pageWidth, pageHeight); + mLogger.trace("Determined page box: {}", pageBox); // Create a margin box int marginTop = mFormat.getProperty("margin.top").toInt(); @@ -53,6 +64,7 @@ public abstract class AbstractCanvas { } catch (Rectangle.OutOfSpaceException e) { throw new InsufficientRenderingAreaException("The sum of the defined margins is bigger than the page size.", e); } + mLogger.trace("Determined margin box: {}", marginBox); // Create a constraint box double constraintTop = mPrinter.getProperty("constraint.top").toDouble(); @@ -69,8 +81,10 @@ public abstract class AbstractCanvas { constraintWidth = Integer.MAX_VALUE; } Rectangle constraintBox = new Rectangle(constraintLeft, constraintTop, constraintWidth, constraintHeight); + mLogger.trace("Determined constraint box: {}", constraintBox); mPrintableArea = calculatePrintingArea(marginBox, constraintBox); + mLogger.info("The calculated available printing area equals: {}", mPrintableArea); } diff --git a/src/main/java/de/tudresden/inf/mci/brailleplot/rendering/FunctionalRasterizer.java b/src/main/java/de/tudresden/inf/mci/brailleplot/rendering/FunctionalRasterizer.java index 121e78b9..d21e31cf 100644 --- a/src/main/java/de/tudresden/inf/mci/brailleplot/rendering/FunctionalRasterizer.java +++ b/src/main/java/de/tudresden/inf/mci/brailleplot/rendering/FunctionalRasterizer.java @@ -1,5 +1,8 @@ package de.tudresden.inf.mci.brailleplot.rendering; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * FunctionalRasterizer. This class implements a concrete rasterizer via a functional interface. * The rasterizing algorithm to be used is passed to the constructor as lambda function, method reference or rasterizer implementation. @@ -9,6 +12,8 @@ package de.tudresden.inf.mci.brailleplot.rendering; */ public class FunctionalRasterizer<T extends Renderable> implements Rasterizer { + private final Logger mLogger = LoggerFactory.getLogger(this.getClass()); + private Class<? extends T> mSupportedRenderableClass; private ThrowingBiConsumer<T, RasterCanvas, InsufficientRenderingAreaException> mRasterizingAlgorithm; @@ -21,6 +26,8 @@ public class FunctionalRasterizer<T extends Renderable> implements Rasterizer { public FunctionalRasterizer( final Class<T> supportedRenderableClass, final Rasterizer<T> rasterizer) { + mLogger.info("Creating new FunctionalRasterizer: Binding {} rasterizer {}.", + supportedRenderableClass.getSimpleName(), rasterizer); mSupportedRenderableClass = supportedRenderableClass; mRasterizingAlgorithm = rasterizer::rasterize; } @@ -28,8 +35,9 @@ public class FunctionalRasterizer<T extends Renderable> implements Rasterizer { @Override public void rasterize(final Renderable data, final RasterCanvas canvas) throws InsufficientRenderingAreaException { // invoke the given rasterizing algorithm - T diagram = safeCast(data); - mRasterizingAlgorithm.accept(diagram, canvas); + T safeData = safeCast(data); + mLogger.trace("Delegating task to bound rasterizing algorithm {}", mRasterizingAlgorithm); + mRasterizingAlgorithm.accept(safeData, canvas); } final Class<? extends T> getSupportedRenderableClass() { diff --git a/src/main/java/de/tudresden/inf/mci/brailleplot/rendering/FunctionalRenderingBase.java b/src/main/java/de/tudresden/inf/mci/brailleplot/rendering/FunctionalRenderingBase.java index 235fbaf0..c424df6c 100644 --- a/src/main/java/de/tudresden/inf/mci/brailleplot/rendering/FunctionalRenderingBase.java +++ b/src/main/java/de/tudresden/inf/mci/brailleplot/rendering/FunctionalRenderingBase.java @@ -85,7 +85,7 @@ public class FunctionalRenderingBase { */ public void setRasterCanvas(final RasterCanvas raster) { mRaster = Objects.requireNonNull(raster); - mLogger.info("RasterCanvas has been set to instance [{}]", raster.hashCode()); + mLogger.info("RasterCanvas has been set to instance {}", raster); } /** diff --git a/src/main/java/de/tudresden/inf/mci/brailleplot/rendering/ImageRasterizer.java b/src/main/java/de/tudresden/inf/mci/brailleplot/rendering/ImageRasterizer.java index 85031da3..497a714e 100644 --- a/src/main/java/de/tudresden/inf/mci/brailleplot/rendering/ImageRasterizer.java +++ b/src/main/java/de/tudresden/inf/mci/brailleplot/rendering/ImageRasterizer.java @@ -1,6 +1,8 @@ package de.tudresden.inf.mci.brailleplot.rendering; import de.tudresden.inf.mci.brailleplot.printabledata.MatrixData; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.awt.image.BufferedImage; @@ -20,6 +22,8 @@ public class ImageRasterizer implements Rasterizer<Image> { // it is technically also possible to implement a rasterizer as a method independent from a class, as long // as it takes a renderable and a canvas as parameters. + private final Logger mLogger = LoggerFactory.getLogger(this.getClass()); + // Switches private boolean mPreventOverStretch; private boolean mPreserveAspectRatio; @@ -27,16 +31,14 @@ public class ImageRasterizer implements Rasterizer<Image> { // Output threshold: A dot will be set for gray scale values below (darker than/equal) this threshold. private int mLowThreshold; - private final int mDefaultThreshold = 80; + private static final int DEFAULT_THRESHOLD = 80; /** * Constructor. Creates a new {@link Rasterizer} for instances of {@link Image} with default settings. */ public ImageRasterizer() { - mPreventOverStretch = true; - mPreserveAspectRatio = true; - mQuantifiedPositions = true; - mLowThreshold = mDefaultThreshold; + this(true, true, true, DEFAULT_THRESHOLD); + mLogger.trace("Created ImageRasterizer with default settings"); } /** @@ -60,6 +62,8 @@ public class ImageRasterizer implements Rasterizer<Image> { mPreserveAspectRatio = preserveAspectRatio; mQuantifiedPositions = useQuantifiedPositions; mLowThreshold = threshold; + mLogger.trace("Created ImageRasterizer. Settings: preventOverStretch={}, preserveAspectRatio={}, useQuantifiedPositions={}, lowThreshold={}", + mPreventOverStretch, mPreserveAspectRatio, mQuantifiedPositions, mLowThreshold); } /** @@ -83,6 +87,9 @@ public class ImageRasterizer implements Rasterizer<Image> { // the grey scale value of each pixel. // A more sophisticated implementation could utilize an edge finding algorithm. + mLogger.info("Rasterizing Image {}", imgData); + + mLogger.trace("Retrieving buffered image."); // First, a readable representation of the is retrieved. BufferedImage imgBuf = imgData.getBufferedImage(); @@ -98,6 +105,8 @@ public class ImageRasterizer implements Rasterizer<Image> { private void linearMapping(final BufferedImage imgBuf, final RasterCanvas canvas) { + mLogger.trace("Apply linear mapping algorithm."); + // A canvas is basically a wrapper for multiple representations of printable data, each representing a page. // These representations can be acquired by either requesting the current page or creating a new page. MatrixData<Boolean> data = canvas.getNewPage(); @@ -153,6 +162,8 @@ public class ImageRasterizer implements Rasterizer<Image> { private void quantifiedPositionMapping(final BufferedImage imgBuf, final RasterCanvas canvas) { + mLogger.trace("Apply quantified position algorithm."); + MatrixData<Boolean> data = canvas.getNewPage(); // Instead of using the dot rectangle a rectangle representing the target printing space in millimeters diff --git a/src/main/java/de/tudresden/inf/mci/brailleplot/rendering/MasterRenderer.java b/src/main/java/de/tudresden/inf/mci/brailleplot/rendering/MasterRenderer.java index 2b2b6964..e81fc226 100644 --- a/src/main/java/de/tudresden/inf/mci/brailleplot/rendering/MasterRenderer.java +++ b/src/main/java/de/tudresden/inf/mci/brailleplot/rendering/MasterRenderer.java @@ -5,8 +5,6 @@ import org.slf4j.LoggerFactory; import de.tudresden.inf.mci.brailleplot.configparser.Format; import de.tudresden.inf.mci.brailleplot.configparser.Printer; -import org.slf4j.Marker; -import org.slf4j.MarkerFactory; import java.util.Objects; @@ -54,13 +52,13 @@ public final class MasterRenderer { } public RasterCanvas rasterize(final Renderable data) throws InsufficientRenderingAreaException { - mLogger.info("Starting a new {} rasterizing on RenderingBase {}", - data.getClass().getSimpleName(), mRenderingBase.hashCode()); + mLogger.info("Preparing a new {} rasterizing on RenderingBase {}", + data.getClass().getSimpleName(), mRenderingBase); RasterCanvas canvas = createCompatibleRasterCanvas(); mRenderingBase.setRasterCanvas(canvas); mRenderingBase.rasterize(data); - mLogger.info("Rasterizing of {} on RenderingBase {} has finished", - data.getClass().getSimpleName(), mRenderingBase.hashCode()); + mLogger.info("Rasterizing of {} on RenderingBase {} has finished, result containing {} pages", + data.getClass().getSimpleName(), mRenderingBase, canvas.getPageCount()); return canvas; } @@ -89,7 +87,7 @@ public final class MasterRenderer { public void setPrinter(final Printer printer) { mPrinter = Objects.requireNonNull(printer); - mLogger.info("Rendering context: Printer was set to {}", mPrinter.getProperty("name")); // TODO: Printer.toString() should append printer name. + mLogger.info("Rendering context: Printer was set to {}", mPrinter); } public Printer getPrinter() { return mPrinter; @@ -105,7 +103,7 @@ public final class MasterRenderer { public void setRenderingBase(final FunctionalRenderingBase renderingBase) { mRenderingBase = Objects.requireNonNull(renderingBase); - mLogger.info("Rendering context: Set RenderingBase to instance [{}]", mRenderingBase.hashCode()); + mLogger.info("Rendering context: Set RenderingBase to instance {}", mRenderingBase); } public FunctionalRenderingBase getRenderingBase() { return mRenderingBase; diff --git a/src/main/java/de/tudresden/inf/mci/brailleplot/rendering/RasterCanvas.java b/src/main/java/de/tudresden/inf/mci/brailleplot/rendering/RasterCanvas.java index 72c272b9..ea45b77b 100644 --- a/src/main/java/de/tudresden/inf/mci/brailleplot/rendering/RasterCanvas.java +++ b/src/main/java/de/tudresden/inf/mci/brailleplot/rendering/RasterCanvas.java @@ -4,6 +4,8 @@ import de.tudresden.inf.mci.brailleplot.configparser.Format; import de.tudresden.inf.mci.brailleplot.configparser.Printer; import de.tudresden.inf.mci.brailleplot.printabledata.MatrixData; import de.tudresden.inf.mci.brailleplot.printabledata.SimpleMatrixDataImpl; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.ArrayList; @@ -19,6 +21,8 @@ import static java.lang.Math.floor; */ public class RasterCanvas extends AbstractCanvas { + private final Logger mLogger = LoggerFactory.getLogger(this.getClass()); + private ArrayList<Double> mXPositions; private ArrayList<Double> mYPositions; @@ -64,11 +68,13 @@ public class RasterCanvas extends AbstractCanvas { // Cell size in dots mCellWidth = cellWidth; mCellHeight = cellHeight; + mLogger.trace("Cell size set to {}x{}", mCellWidth, mCellHeight); readConfig(); calculateRasterSize(); calculateSpacing(); + mLogger.info("New RasterCanvas created from config: {}, {}", printer, format); } public final MatrixData<Boolean> getNewPage() { @@ -88,6 +94,8 @@ public class RasterCanvas extends AbstractCanvas { private void readConfig() { + mLogger.trace("Reading raster specific configuration"); + // What are the dot and cell distances in mm? mHorizontalDotDistance = mPrinter.getProperty("raster.dotDistance.horizontal").toDouble(); mVerticalDotDistance = mPrinter.getProperty("raster.dotDistance.vertical").toDouble(); @@ -105,6 +113,7 @@ public class RasterCanvas extends AbstractCanvas { // New approach using a box model: + mLogger.trace("Fitting raster into available printing area"); // Dividing the printable area into cells to create a cell raster box. int cellRasterX = (int) ceil(mPrintableArea.getX() / mCellHorizontalMM); int cellRasterY = (int) ceil(mPrintableArea.getY() / mCellVerticalMM); @@ -115,6 +124,7 @@ public class RasterCanvas extends AbstractCanvas { cellRasterR - cellRasterX, cellRasterB - cellRasterY ); + mLogger.trace("Determined cellRasterBox: {}", cellRasterBox); // The following properties impact the printing area, but are specific to rasterizing. (That's why they weren't read before in the AbstractCanvas) // The abstract parent class (AbstractCanvas) already calculated indentations based on millimeters, but it is @@ -136,8 +146,10 @@ public class RasterCanvas extends AbstractCanvas { } Rectangle rasterConstraintBox = new Rectangle(rasterConstraintLeft, rasterConstraintTop, rasterConstraintWidth, rasterConstraintHeight); + mLogger.trace("Determined rasterConstraintBox: {}", rasterConstraintBox); mPrintingAreaCells = calculatePrintingArea(cellRasterBox, rasterConstraintBox); + mLogger.trace("Determined printable raster: {}", mPrintingAreaCells); // The following values are set to keep track of the 'real' size of the internal data representation, because // the margins are created virtually by printing some empty cells at the pages top / left edge. @@ -150,13 +162,17 @@ public class RasterCanvas extends AbstractCanvas { // To how many dots does this raster size correspond? mColumnCount = mHorizontalCellCount * mCellWidth; mRowCount = mVerticalCellCount * mCellHeight; + mLogger.trace("Determined raster dimensions (dots): {} columns x {} rows", mColumnCount, mRowCount); } private void calculateSpacing() { + mLogger.trace("Pre calculating quantified raster positions."); mXPositions = calculateQuantizedPositions(mHorizontalDotDistance, mHorizontalCellDistance, mCellWidth, mHorizontalCellCount); + mLogger.trace("X coordinates: {}", mXPositions); mYPositions = calculateQuantizedPositions(mVerticalDotDistance, mVerticalCellDistance, mCellHeight, mVerticalCellCount); + mLogger.trace("Y coordinates: {}", mYPositions); } diff --git a/src/main/java/de/tudresden/inf/mci/brailleplot/rendering/Rectangle.java b/src/main/java/de/tudresden/inf/mci/brailleplot/rendering/Rectangle.java index b0289a10..c653368b 100644 --- a/src/main/java/de/tudresden/inf/mci/brailleplot/rendering/Rectangle.java +++ b/src/main/java/de/tudresden/inf/mci/brailleplot/rendering/Rectangle.java @@ -1,5 +1,8 @@ package de.tudresden.inf.mci.brailleplot.rendering; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.util.Objects; import static java.lang.Math.round; @@ -14,6 +17,8 @@ import static java.lang.Math.max; */ public class Rectangle { + private final Logger mLogger = LoggerFactory.getLogger(this.getClass()); + private double mX, mY, mW, mH; /** @@ -28,6 +33,7 @@ public class Rectangle { setY(y); setWidth(w); setHeight(h); + mLogger.trace("Created new rectangle: {}", this); } /** @@ -39,6 +45,7 @@ public class Rectangle { setY(rect.getY()); setWidth(rect.getWidth()); setHeight(rect.getHeight()); + mLogger.trace("Copied rectangle: {}", this); } /** diff --git a/src/main/resources/config/index_everest_d_v4.properties b/src/main/resources/config/index_everest_d_v4.properties index 3d6ad7a3..deaeeeb1 100644 --- a/src/main/resources/config/index_everest_d_v4.properties +++ b/src/main/resources/config/index_everest_d_v4.properties @@ -23,9 +23,9 @@ printer.floatingDot.resolution=0.05 # The following values represent the fixed indentation and maximum technical printing area of the embosser. # If the outputs don't fit on the page you might want to tweak this values. (Check the format too.) -printer.constraint.top=5.0 -printer.constraint.left=0 -#printer.constraint.width=? TODO: Check out manual, Conduct printing tests with bigger formats +printer.constraint.top=12 +printer.constraint.left=40 +printer.constraint.width=100 #printer.constraint.height=? # The second constraint in the printer.raster namespace helps to limit the available printing area in steps of # whole cells, for example if the printer enforces a maximum char per line limit or borders are activated. @@ -49,7 +49,7 @@ format.A4.page.width=210 format.A4.page.height=297 format.A4.margin.top=0 format.A4.margin.left=0 -format.A4.margin.bottom=0 +format.A4.margin.bottom=55 format.A4.margin.right=0 # A5 Format -- GitLab