Commits

P_W999 committed a76025b

- rework on the excessive logging

  • Participants
  • Parent commits 5b455a4

Comments (0)

Files changed (11)

File src/be/pw/jexif/DemoApp.java

 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
-import java.util.Locale;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Properties;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import be.pw.jexif.enums.Errors;
 import be.pw.jexif.enums.tag.ExifIFD;
 import be.pw.jexif.exception.ExifError;
 import be.pw.jexif.exception.JExifException;
 import be.pw.jexif.internal.constants.ExecutionConstant;
-import be.pw.jexif.internal.util.Cal10nUtil;
-import ch.qos.cal10n.IMessageConveyor;
-import ch.qos.cal10n.MessageConveyor;
 
 import com.google.common.base.Stopwatch;
 import com.google.common.io.Files;
 		watch.stop();
 
 		try {
-			tool.getInfo(new File("c:\\does\\not\\exist"));
+			tool.getInfo(new File("c:\\does\\not\\exist.pdf"));
 		} catch (IOException e1) {
 			LOG.error(e1.getMessage());
 		}
 		gpsInfo.setGPSInfo("60 35 13.08", "S", "131 50 9.38", "W", "100", "1");
 		tool.stop();
 
-		System.out.println(Cal10nUtil.get(Errors.VALIDATION_RAT_FOR_INT));
+		/*System.out.println(Cal10nUtil.get(Errors.VALIDATION_RAT_FOR_INT));
 		Cal10nUtil.changeLocale(Locale.ENGLISH);
 		System.out.println(Cal10nUtil.get(Errors.VALIDATION_RAT_FOR_INT));
 		Cal10nUtil.changeLocale(new Locale("nl"));
-		System.out.println(Cal10nUtil.get(Errors.VALIDATION_RAT_FOR_INT));
+		System.out.println(Cal10nUtil.get(Errors.VALIDATION_RAT_FOR_INT));*/
 		
 		LOG.info("Executing took {} ms", watch.elapsedTime(TimeUnit.MILLISECONDS));
 		LOG.info("Finished main method");

File src/be/pw/jexif/JExifTool.java

 package be.pw.jexif;
 
 import java.io.File;
+import java.io.FileNotFoundException;
 import java.io.FileWriter;
 import java.io.IOException;
 import java.text.MessageFormat;
 	/**
 	 * Default constructor. <br />
 	 * This automatically starts the ExifTool process and registers the standard Tag-set that comes with this library.
+	 * @throws JExifException if failed to start ExifTool
 	 */
 	@Beta
-	public JExifTool() {
+	public JExifTool() throws JExifException {
 		TagUtil.register(IFD0.class);
 		TagUtil.register(ExifIFD.class);
 		TagUtil.register(ExifGPS.class);
 	public JExifInfo getInfo(final File file) throws IOException {
 		Preconditions.checkNotNull(file);
 		if (!file.exists() || file.isDirectory()) {
-			throw new IOException(Cal10nUtil.get(Errors.IO_FILE_NOT_VALID, file.getName()));
+			throw new FileNotFoundException(Cal10nUtil.get(Errors.IO_FILE_NOT_VALID, file.getName()));
 		}
 		return new JExifInfo(this, file);
 	}
 
 	/**
 	 * Starts the ExifTool process using Apache Commons Exec This method is automatically called when a new instance of JExifTool is created.
+	 * @throws JExifException if failed to start thread.
 	 */
-	private synchronized void start() {
+	private synchronized void start() throws JExifException {
+		LOG.info("Starting ExifTool");
 		LOG.trace("Using exifToolPath in: " + exifToolPath.getAbsolutePath());
 		LOG.trace("Argsfile is stored in: " + argsFile.getAbsolutePath());
 		try {
 			String claFormat;
 			String cla;
 			if (System.getProperty("os.name").toLowerCase().contains("windows")) {
-				LOG.trace("Running windows: always putting CLA between \" \"");
 				claFormat = ExecutionConstant.WINDOWS_CLA;
 			} else {
 				if (argsFile.getCanonicalPath().contains(" ")) {
 			LOG.info("ExifTool was started");
 			Thread.sleep(500);	//wait a bit to ensure process has started
 		} catch (IOException e) {
-			LOG.error(Cal10nUtil.get(Errors.IO_ARGSFILE), e);
+			LOG.debug("Failed to start ExifTool", e);
+			throw new JExifException(Cal10nUtil.get(Errors.IO_ARGSFILE), e);
 		} catch (InterruptedException e) {
-			LOG.error(Cal10nUtil.get(Errors.INTERRUPTED_SLEEP), e);
+			LOG.debug("Failed to start ExifTool", e);
+			throw new JExifException(Cal10nUtil.get(Errors.INTERRUPTED_SLEEP), e);
 		}
 	}
 
 	/**
 	 * Starts the ExifTool process if needed
+	 * @throws JExifException if failed to start ExifTool.
 	 */
-	private synchronized void startIfNecessary() {
+	private synchronized void startIfNecessary() throws JExifException {
 		if (resultHandler != null) {
-			LOG.trace("Using apache commons-exec");
 			if (resultHandler.hasResult() || executor.getWatchdog().killedProcess()) {
-				LOG.info("ExifTool is not running anymore, restarting it");
+				LOG.info("Restarting ExifTool");
 				start();
 			}
 		} else {
-			LOG.info("ExifTool process was not running");
+			LOG.trace("ResultHandler was null");
 			start();
 		}
 	}
 			argsWriter.flush();	
 			executor.setWatchdog(new ExecuteWatchdog(1500));
 			while (!resultHandler.hasResult());	//wait for ExifTool to stop
-			LOG.trace("Process stopped");
+			LOG.info("ExifTool stopped");
 		} catch (IOException e) {
-			LOG.error(Cal10nUtil.get(Errors.IO_CLOSING), e);
+			LOG.debug("Failed to stop ExifTool", e);
+			throw new JExifException(Cal10nUtil.get(Errors.IO_CLOSING), e);
 		}
 	}
 
 	 */
 	String readTagInfo(final File file, final Tag tag, final boolean exact) throws JExifException, ExifError {
 		startIfNecessary();
+		LOG.trace("Starting readTagInfo");
 		int i = 0;
 		IAction action;
 		EventHandler handler;
 		try {
-			LOG.trace("Starting readTagInfo");
 			if (exact) {
 				action = ActionFactory.createExactReadAction(file, tag);
 			} else {
 			}
 			handler = new EventHandler();
 			bus.register(handler);
-			LOG.trace("Registered the EventHandler");
+			
 			String[] arguments = action.buildArguments();
 			for (String argument : arguments) {
 				argsWriter.append(argument).append("\r\n");
 			}
 			bus.unregister(handler);
 		} catch (IOException e) {
-			LOG.error(Cal10nUtil.get(Errors.IO_ARGSFILE), e);
-			throw new JExifException(e);
+			throw new JExifException(Cal10nUtil.get(Errors.IO_ARGSFILE), e);
 		} catch (InterruptedException e) {
-			LOG.error(Cal10nUtil.get(Errors.INTERRUPTED_SLEEP), e);
-			throw new JExifException(e);
+			throw new JExifException(Cal10nUtil.get(Errors.INTERRUPTED_SLEEP), e);
 		}
-		LOG.trace("Handler finished");
 		List<String> results = handler.getResultList();
 		List<String> errors = handler.getErrorList();
-		LOG.trace("Parsing results");
 		ResultHandler.run(action, results, errors);
 		return action.getResult().get(tag);
 	}
 	 */
 	void writeTagInfo(final File file, final Tag tag, final String value) throws ExifError, JExifException {
 		startIfNecessary();
+		LOG.trace("Starting readTagInfo");
 		int i = 0;
 		IAction action;
 		EventHandler handler;
 		try {
-			LOG.trace("Starting readTagInfo");
 			Map<Tag, String> valuesToWrite = new HashMap<>(1);
 			valuesToWrite.put(tag, value);
 			action = ActionFactory.createTagWriteAction(file, valuesToWrite);
 			handler = new EventHandler();
 			bus.register(handler);
-			LOG.trace("Registered the EventHandler");
+			
 			String[] arguments = action.buildArguments();
 			for (String argument : arguments) {
 				argsWriter.append(argument).append("\r\n");
 			}
 			bus.unregister(handler);
 		} catch (IOException e) {
-			LOG.error(Cal10nUtil.get(Errors.IO_ARGSFILE), e);
-			throw new JExifException(e);
+			throw new JExifException(Cal10nUtil.get(Errors.IO_ARGSFILE), e);
 		} catch (InterruptedException e) {
-			LOG.error(Cal10nUtil.get(Errors.INTERRUPTED_SLEEP), e);
-			throw new JExifException(e);
+			throw new JExifException(Cal10nUtil.get(Errors.INTERRUPTED_SLEEP), e);
 		}
-		LOG.trace("Handler finished");
 		List<String> results = handler.getResultList();
 		List<String> errors = handler.getErrorList();
-		LOG.trace("Parsing results");
 		ResultHandler.run(action, results, errors);
 	}
 
 	 */
 	void writeGPSTagInfo(final File file, final Map<Tag, String> valuesToWrite) throws ExifError, JExifException {
 		startIfNecessary();
+		LOG.trace("Starting readTagInfo");
 		int i = 0;
 		IAction action;
 		EventHandler handler;
 		try {
-			LOG.trace("Starting readTagInfo");
 			GPSUtil.validateGPSValues(valuesToWrite);
 			GPSUtil.formatGPSValues(valuesToWrite);
 			action = ActionFactory.createTagWriteAction(file, valuesToWrite);
 			String[] arguments = action.buildArguments();
 			handler = new EventHandler();
 			bus.register(handler);
-			LOG.trace("Registered the EventHandler");
+			
 			for (String argument : arguments) {
 				argsWriter.append(argument).append("\r\n");
 			}
 				i += 50;
 			}
 			if (!handler.isFinished()) {
-				LOG.error(Cal10nUtil.get(Errors.DEADLOCK, i));
 				throw new JExifException(Cal10nUtil.get(Errors.DEADLOCK, i));
 			}
 			bus.unregister(handler);
 		} catch (IOException e) {
-			LOG.error(Cal10nUtil.get(Errors.IO_ARGSFILE), e);
-			throw new JExifException(e);
+			throw new JExifException(Cal10nUtil.get(Errors.IO_ARGSFILE), e);
 		} catch (InterruptedException e) {
-			LOG.error(Cal10nUtil.get(Errors.INTERRUPTED_SLEEP), e);
-			throw new JExifException(e);
+			throw new JExifException(Cal10nUtil.get(Errors.INTERRUPTED_SLEEP), e);
 		}
-		LOG.trace("Handler finished");
 		List<String> results = handler.getResultList();
 		List<String> errors = handler.getErrorList();
-		LOG.trace("Parsing results");
 		ResultHandler.run(action, results, errors);
 	}
 
 
 	private Map<? extends Object, String> getAllTagInfo(final File file, final boolean exact, final boolean onlySupported) throws ExifError, JExifException {
 		startIfNecessary();
+		LOG.trace("Starting readTagInfo");
 		int i = 0;
 		IAction action;
 		EventHandler handler;
 		try {
-			LOG.trace("Starting readTagInfo");
 			if (exact) {
 				action = ActionFactory.createTagReadExactAllAction(file);
 			} else {
 			}
 			handler = new EventHandler();
 			bus.register(handler);
-			LOG.trace("Registered the EventHandler");
+			
 			String[] arguments = action.buildArguments();
 			for (String argument : arguments) {
 				argsWriter.append(argument).append("\r\n");
 			}
 			bus.unregister(handler);
 		} catch (IOException e) {
-			LOG.error(Cal10nUtil.get(Errors.IO_ARGSFILE), e);
-			throw new JExifException(e);
+			throw new JExifException(Cal10nUtil.get(Errors.IO_ARGSFILE), e);
 		} catch (InterruptedException e) {
-			LOG.error(Cal10nUtil.get(Errors.INTERRUPTED_SLEEP), e);
-			throw new JExifException(e);
+			throw new JExifException(Cal10nUtil.get(Errors.INTERRUPTED_SLEEP), e);
 		}
-		LOG.trace("Handler finished");
 		List<String> results = handler.getResultList();
 		List<String> errors = handler.getErrorList();
-		LOG.trace("Parsing results");
 		ResultHandler.run(action, results, errors);
 		if (onlySupported) {
 			return action.getResult();

File src/be/pw/jexif/internal/action/AbstractAction.java

 		this.results = new HashMap<>();
 		this.unsupportedTags = new HashMap<>();
 		this.id = UUID.randomUUID();
-		LOG.debug("New action created with UUID {}", id);
+		LOG.trace("New action created with UUID {}", id);
 	}
 
 	/**

File src/be/pw/jexif/internal/action/impl/TagReadAction.java

 package be.pw.jexif.internal.action.impl;
 
 import java.io.File;
+import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.util.HashSet;
 
 	void setParams(final File file, final Tag... tags) throws IOException {
 		Preconditions.checkNotNull(file);
 		if (!file.exists()) {
-			throw new IOException(Cal10nUtil.get(Errors.IO_FILE_NOT_VALID, file.getAbsolutePath()));
+			throw new FileNotFoundException(Cal10nUtil.get(Errors.IO_FILE_NOT_VALID, file.getAbsolutePath()));
 		}
 		Preconditions.checkNotNull(tags);
 		Preconditions.checkArgument(tags.length != 0, Cal10nUtil.get(Errors.VALIDATION_TAGLIST));
 			LOG.trace("Arguments are : " + ArrayUtil.toString(arguments));
 			return arguments;
 		} catch (IOException e) {
-			LOG.error(Cal10nUtil.get(Errors.IO_BUILD_ARGUMENTS), e);
-			throw new JExifException(e);
+			throw new JExifException(Cal10nUtil.get(Errors.IO_BUILD_ARGUMENTS), e);
 		}
 	}
 }

File src/be/pw/jexif/internal/action/impl/TagReadAllAction.java

 			LOG.trace("Arguments are : " + ArrayUtil.toString(arguments));
 			return arguments;
 		} catch (IOException e) {
-			LOG.error(Cal10nUtil.get(Errors.IO_BUILD_ARGUMENTS), e);
-			throw new JExifException(e);
+			throw new JExifException(Cal10nUtil.get(Errors.IO_BUILD_ARGUMENTS), e);
 		}
 	}
 

File src/be/pw/jexif/internal/action/impl/TagWriteAction.java

 package be.pw.jexif.internal.action.impl;
 
 import java.io.File;
+import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.util.Map;
 import java.util.Map.Entry;
 	void setParams(final File file, final Map<Tag, String> valuesToWrite) throws JExifValidationException, IOException {
 		Preconditions.checkNotNull(file);
 		if (!file.exists()) {
-			throw new IOException(Cal10nUtil.get(Errors.IO_FILE_NOT_VALID, file.getAbsolutePath()));
+			throw new FileNotFoundException(Cal10nUtil.get(Errors.IO_FILE_NOT_VALID, file.getAbsolutePath()));
 		}
 		Preconditions.checkNotNull(valuesToWrite);
 		Preconditions.checkArgument(valuesToWrite.size() != 0, Cal10nUtil.get(Errors.VALIDATION_VALUESLIST));
 			LOG.trace("Arguments are : " + ArrayUtil.toString(arguments));
 			return arguments;
 		} catch (IOException e) {
-			LOG.error(Cal10nUtil.get(Errors.IO_BUILD_ARGUMENTS), e);
-			throw new JExifException(e);
+			throw new JExifException(Cal10nUtil.get(Errors.IO_BUILD_ARGUMENTS), e);
 		}
 	}
 }

File src/be/pw/jexif/internal/result/ResultHandler.java

 				fixedResult.addAll(result);
 				parser.parse(action, uid, fixedResult);
 			} catch (JExifException e) {
-				LOG.error(Cal10nUtil.get(Errors.RETREIVE_PARSER_ERROR, uid), e);
-				LOG.warn("[{}] - There will be no results", uid);
+				LOG.debug(Cal10nUtil.get(Errors.RETREIVE_PARSER_ERROR, uid), e);
+				LOG.warn("[{}] - There will be no results due to an exception", uid);
 			}
 		} else {
 			LOG.warn("[{}] - No results were found", uid);

File src/be/pw/jexif/internal/result/impl/ParserFactory.java

  * @see be.pw.jexif.internal.result.IResultParser
  */
 public class ParserFactory {
-	/**
-	 * The logger for this class.
-	 */
-	private static final Logger LOG = LoggerFactory.getLogger(ParserFactory.class);
 
 	/**
 	 * Returns an instance of a parser for a given action instance.
 		if (action instanceof ITagReadExactAllAction) {
 			return new TagReadExactAllParser();
 		}
-		LOG.error(Cal10nUtil.get(Errors.PARSER_UNKNOWNTYPE, action.getClass().getName()));
 		throw new JExifException(Cal10nUtil.get(Errors.PARSER_UNKNOWNTYPE, action.getClass().getName()));
 	}
 }

File src/be/pw/jexif/internal/thread/RunnableReader.java

-/*******************************************************************************
- * Copyright 2012 P_W999
- * 
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- * 
- *   http://www.apache.org/licenses/LICENSE-2.0
- * 
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- ******************************************************************************/
-package be.pw.jexif.internal.thread;
-
-import java.io.BufferedReader;
-import java.io.IOException;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import be.pw.jexif.internal.thread.event.ErrorEvent;
-import be.pw.jexif.internal.thread.event.InputEvent;
-
-import com.google.common.eventbus.EventBus;
-
-/**
- * A Runnable implementation which can read, independently from the rest of the application, the info which is returned by a BufferedReader.
- * @author phillip
- */
-public class RunnableReader implements Runnable {
-	/**
-	 * The logger for this class.
-	 */
-	private static final Logger LOG = LoggerFactory.getLogger(RunnableReader.class);
-
-	/**
-	 * The BufferedReader which will read what ExifTool has outputed.
-	 */
-	private final BufferedReader reader;
-
-	/**
-	 * The Guava EventBus where this reader will post the lines read from the ExifTool output.
-	 */
-	private final EventBus bus;
-
-	/**
-	 * Indicates whether this reader is used to read the error-stream.
-	 */
-	private final boolean error;
-
-	/**
-	 * Creates a new instance of the reader runnable.
-	 * @param reader the read which shall be read
-	 * @param error whether the reader is going to spit out error messages (thus indicating that we must log it as an error)
-	 */
-	public RunnableReader(final BufferedReader reader, final EventBus bus, final boolean error) {
-		this.reader = reader;
-		this.bus = bus;
-		this.error = error;
-	}
-
-	/**
-	 * {@inheritDoc}
-	 */
-	@Override
-	public void run() {
-		try {
-			String line;
-			while ((line = reader.readLine()) != null) {
-				LOG.trace(line);
-				if (error) {
-					bus.post(new ErrorEvent(line));
-				} else {
-					bus.post(new InputEvent(line));
-				}
-			}
-			LOG.trace("Reader thread finished");
-		} catch (IOException e) {
-			LOG.error("Error in reader thread", e);
-		} finally {
-			try {
-				if (reader != null) {
-					reader.close();
-					LOG.trace("Reader closed");
-				}
-			} catch (IOException e) {
-				LOG.error("Failed to close reader", e);
-			}
-		}
-	}
-
-}

File src/be/pw/jexif/internal/thread/event/EventHandler.java

 		LOG.trace(line);
 		if (line.startsWith(ExecutionConstant.START)) {
 			currentUID = line.substring(ExecutionConstant.START.length());
-			LOG.debug("CurrentUID = {}", currentUID);
+			LOG.trace("CurrentUID = {}", currentUID);
 		} else if (line.startsWith(ExecutionConstant.STOP)) {
 			if (!line.substring(ExecutionConstant.STOP.length()).equals(currentUID)) {
 				throw new RuntimeException(MessageFormat.format("JExifOuput was desynchronized, expected {0} but got {1}", currentUID, line.substring(ExecutionConstant.STOP.length())));
 			finished = true;
 		} else {
 			if (error) {
-				LOG.debug("Adding {} to error list", line);
+				LOG.trace("Adding {} to error list", line);
 				errorForUID.add(line);
 			} else {
-				LOG.debug("Adding {} to output list", line);
+				LOG.trace("Adding {} to output list", line);
 				outputForUID.add(line);
 			}
 

File src/log4j.properties

 
 #log4j.logger.be.pw=DEBUG
 log4j.logger.be.pw.jexif.internal.thread.event.DebugHandler=FATAL
-log4j.logger.be.pw.jexif.DemoApp=INFO
-log4j.logger.be.pw.jexif.JExifTool=WARN
+log4j.logger.be.pw.jexif.DemoApp=ERROR
+log4j.logger.be.pw.jexif.JExifTool=INFO
 
 
 #------------------------------------------------------------------------------