From 4b1ffbe85b4fd120579b5ed1b67361b54b247e09 Mon Sep 17 00:00:00 2001
From: akwizgran <akwizgran@users.sourceforge.net>
Date: Fri, 7 Oct 2011 13:59:19 +0100
Subject: [PATCH] Logging for plugins.

---
 .../plugins/bluetooth/BluetoothListener.java  | 10 +++++---
 .../plugins/bluetooth/BluetoothPlugin.java    | 24 +++++++++++--------
 .../net/sf/briar/plugins/file/FilePlugin.java |  7 ++++++
 .../plugins/file/RemovableDrivePlugin.java    |  5 ++++
 .../sf/briar/plugins/socket/SocketPlugin.java | 18 ++++++++++----
 5 files changed, 46 insertions(+), 18 deletions(-)

diff --git a/components/net/sf/briar/plugins/bluetooth/BluetoothListener.java b/components/net/sf/briar/plugins/bluetooth/BluetoothListener.java
index 57723b2ab5..414abe7a61 100644
--- a/components/net/sf/briar/plugins/bluetooth/BluetoothListener.java
+++ b/components/net/sf/briar/plugins/bluetooth/BluetoothListener.java
@@ -4,6 +4,8 @@ import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.concurrent.atomic.AtomicInteger;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 import javax.bluetooth.BluetoothStateException;
 import javax.bluetooth.DeviceClass;
@@ -17,6 +19,9 @@ import net.sf.briar.api.ContactId;
 
 class BluetoothListener implements DiscoveryListener {
 
+	private static final Logger LOG =
+		Logger.getLogger(BluetoothListener.class.getName());
+
 	private static final int[] ATTRIBUTES = { 0x100 }; // Service name
 
 	private final AtomicInteger searches = new AtomicInteger(1);
@@ -48,11 +53,10 @@ class BluetoothListener implements DiscoveryListener {
 		// Try to discover the services associated with the UUID
 		try {
 			discoveryAgent.searchServices(ATTRIBUTES, uuids, device, this);
-			searches.incrementAndGet();
 		} catch(BluetoothStateException e) {
-			// FIXME: Logging
-			e.printStackTrace();
+			if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());
 		}
+		searches.incrementAndGet();
 	}
 
 	public void inquiryCompleted(int discoveryType) {
diff --git a/components/net/sf/briar/plugins/bluetooth/BluetoothPlugin.java b/components/net/sf/briar/plugins/bluetooth/BluetoothPlugin.java
index 1c600eb37f..ab5d18474a 100644
--- a/components/net/sf/briar/plugins/bluetooth/BluetoothPlugin.java
+++ b/components/net/sf/briar/plugins/bluetooth/BluetoothPlugin.java
@@ -9,6 +9,8 @@ import java.util.Map.Entry;
 import java.util.Random;
 import java.util.TreeMap;
 import java.util.concurrent.Executor;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 import javax.bluetooth.BluetoothStateException;
 import javax.bluetooth.DiscoveryAgent;
@@ -33,6 +35,8 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
 	public static final int TRANSPORT_ID = 2;
 
 	private static final TransportId id = new TransportId(TRANSPORT_ID);
+	private static final Logger LOG =
+		Logger.getLogger(BluetoothPlugin.class.getName());
 
 	private final long pollingInterval;
 
@@ -94,7 +98,7 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
 		try {
 			localDevice.setDiscoverable(DiscoveryAgent.GIAC);
 		} catch(BluetoothStateException e) {
-			// FIXME: Logging
+			if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());
 		}
 		// Bind the port
 		String url = "btspp://localhost:" + uuid + ";name=" + uuid;
@@ -102,7 +106,7 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
 		try {
 			scn = (StreamConnectionNotifier) Connector.open(url);
 		} catch(IOException e) {
-			// FIXME: Logging
+			if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());
 			return;
 		}
 		synchronized(this) {
@@ -110,7 +114,8 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
 				try {
 					scn.close();
 				} catch(IOException e) {
-					// FIXME: Logging
+					if(LOG.isLoggable(Level.WARNING))
+						LOG.warning(e.getMessage());
 				}
 				return;
 			}
@@ -152,7 +157,7 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
 			try {
 				s = scn.acceptAndOpen();
 			} catch(IOException e) {
-				// FIXME: Logging
+				if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());
 				return;
 			}
 			synchronized(this) {
@@ -160,7 +165,8 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
 					try {
 						s.close();
 					} catch(IOException e) {
-						// FIXME: Logging
+						if(LOG.isLoggable(Level.WARNING))
+							LOG.warning(e.getMessage());
 					}
 					return;
 				}
@@ -243,10 +249,8 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
 				listener.wait();
 			}
 		} catch(BluetoothStateException e) {
-			// FIXME: Logging
-		} catch(InterruptedException e) {
-			// FIXME: Logging
-		}
+			if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());
+		} catch(InterruptedException ignored) {}
 		return listener.getUrls();
 	}
 
@@ -260,7 +264,7 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
 			StreamConnection s = (StreamConnection) Connector.open(url);
 			return new BluetoothTransportConnection(s);
 		} catch(IOException e) {
-			// FIXME: Logging
+			if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());
 			return null;
 		}
 	}
diff --git a/components/net/sf/briar/plugins/file/FilePlugin.java b/components/net/sf/briar/plugins/file/FilePlugin.java
index 9cb3cd726d..e6210ba48c 100644
--- a/components/net/sf/briar/plugins/file/FilePlugin.java
+++ b/components/net/sf/briar/plugins/file/FilePlugin.java
@@ -7,6 +7,8 @@ import java.io.IOException;
 import java.io.OutputStream;
 import java.util.Map;
 import java.util.concurrent.Executor;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 import net.sf.briar.api.ContactId;
 import net.sf.briar.api.transport.InvalidConfigException;
@@ -23,6 +25,9 @@ import org.apache.commons.io.FileSystemUtils;
 abstract class FilePlugin extends AbstractPlugin
 implements BatchTransportPlugin {
 
+	private static final Logger LOG =
+		Logger.getLogger(FilePlugin.class.getName());
+
 	protected abstract File chooseOutputDirectory();
 	protected abstract void writerFinished(File f);
 	protected abstract void readerFinished(File f);
@@ -56,6 +61,7 @@ implements BatchTransportPlugin {
 			OutputStream out = new FileOutputStream(f);
 			return new FileTransportWriter(f, out, capacity, this);
 		} catch(IOException e) {
+			if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());
 			f.delete();
 			return null;
 		}
@@ -102,6 +108,7 @@ implements BatchTransportPlugin {
 					}
 				}
 			} catch(IOException e) {
+				if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());
 				return;
 			}
 		}
diff --git a/components/net/sf/briar/plugins/file/RemovableDrivePlugin.java b/components/net/sf/briar/plugins/file/RemovableDrivePlugin.java
index bbfd2bc29d..d24c084187 100644
--- a/components/net/sf/briar/plugins/file/RemovableDrivePlugin.java
+++ b/components/net/sf/briar/plugins/file/RemovableDrivePlugin.java
@@ -5,6 +5,8 @@ import java.io.IOException;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.Executor;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 import net.sf.briar.api.ContactId;
 import net.sf.briar.api.TransportId;
@@ -18,6 +20,8 @@ implements RemovableDriveMonitor.Callback {
 	public static final int TRANSPORT_ID = 0;
 
 	private static final TransportId id = new TransportId(TRANSPORT_ID);
+	private static final Logger LOG =
+		Logger.getLogger(RemovableDrivePlugin.class.getName());
 
 	private final RemovableDriveFinder finder;
 	private final RemovableDriveMonitor monitor;
@@ -73,6 +77,7 @@ implements RemovableDriveMonitor.Callback {
 			if(i == -1) return null;
 			return drives.get(i);
 		} catch(IOException e) {
+			if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());
 			return null;
 		}
 	}
diff --git a/components/net/sf/briar/plugins/socket/SocketPlugin.java b/components/net/sf/briar/plugins/socket/SocketPlugin.java
index d790210f92..2571c4eb61 100644
--- a/components/net/sf/briar/plugins/socket/SocketPlugin.java
+++ b/components/net/sf/briar/plugins/socket/SocketPlugin.java
@@ -6,6 +6,8 @@ import java.net.Socket;
 import java.net.SocketAddress;
 import java.util.Map;
 import java.util.concurrent.Executor;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 import net.sf.briar.api.ContactId;
 import net.sf.briar.api.transport.InvalidConfigException;
@@ -18,6 +20,9 @@ import net.sf.briar.plugins.AbstractPlugin;
 abstract class SocketPlugin extends AbstractPlugin
 implements StreamTransportPlugin {
 
+	private static final Logger LOG =
+		Logger.getLogger(SocketPlugin.class.getName());
+
 	// These fields should be accessed with this's lock held
 	protected StreamTransportCallback callback = null;
 	protected ServerSocket socket = null;
@@ -62,7 +67,7 @@ implements StreamTransportPlugin {
 			if(addr == null || ss == null) return;
 			ss.bind(addr);
 		} catch(IOException e) {
-			// FIXME: Logging
+			if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());
 			return;
 		}
 		synchronized(this) {
@@ -70,7 +75,8 @@ implements StreamTransportPlugin {
 				try {
 					ss.close();
 				} catch(IOException e) {
-					// FIXME: Logging
+					if(LOG.isLoggable(Level.WARNING))
+						LOG.warning(e.getMessage());
 				}
 				return;
 			}
@@ -101,7 +107,7 @@ implements StreamTransportPlugin {
 			try {
 				s = ss.accept();
 			} catch(IOException e) {
-				// FIXME: Logging
+				if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());
 				return;
 			}
 			synchronized(this) {
@@ -109,7 +115,8 @@ implements StreamTransportPlugin {
 					try {
 						s.close();
 					} catch(IOException e) {
-						// FIXME: Logging
+						if(LOG.isLoggable(Level.WARNING))
+							LOG.warning(e.getMessage());
 					}
 					return;
 				}
@@ -138,7 +145,8 @@ implements StreamTransportPlugin {
 				try {
 					socket.close();
 				} catch(IOException e) {
-					// FIXME: Logging
+					if(LOG.isLoggable(Level.WARNING))
+						LOG.warning(e.getMessage());
 				}
 				socket = null;
 				executor.execute(createBinder());
-- 
GitLab