From 22aff497470d9e26517f2bc1627b6ecd49556f3a Mon Sep 17 00:00:00 2001
From: zzz <zzz@mail.i2p>
Date: Fri, 27 Jun 2014 15:53:16 +0000
Subject: [PATCH] SAM: Log conditionals, javadocs

---
 .../src/net/i2p/sam/SAMMessageSession.java    |  34 +--
 .../java/src/net/i2p/sam/SAMRawSession.java   |   4 +-
 .../src/net/i2p/sam/SAMStreamSession.java     |  91 +++++---
 .../java/src/net/i2p/sam/SAMv1Handler.java    | 201 ++++++++++++------
 .../java/src/net/i2p/sam/SAMv2Handler.java    |  21 +-
 .../java/src/net/i2p/sam/SAMv3Handler.java    | 121 +++++++----
 .../java/src/net/i2p/sam/SAMv3RawSession.java |   6 +-
 .../src/net/i2p/sam/SAMv3StreamSession.java   |  23 +-
 8 files changed, 334 insertions(+), 167 deletions(-)

diff --git a/apps/sam/java/src/net/i2p/sam/SAMMessageSession.java b/apps/sam/java/src/net/i2p/sam/SAMMessageSession.java
index 3423a27353..452bbe0fb9 100644
--- a/apps/sam/java/src/net/i2p/sam/SAMMessageSession.java
+++ b/apps/sam/java/src/net/i2p/sam/SAMMessageSession.java
@@ -41,7 +41,7 @@ abstract class SAMMessageSession {
     /**
      * Initialize a new SAM message-based session.
      *
-     * @param dest Base64-encoded destination (private key)
+     * @param dest Base64-encoded destination and private keys (same format as PrivateKeyFile)
      * @param props Properties to setup the I2P session
      * @throws IOException
      * @throws DataFormatException
@@ -56,7 +56,7 @@ abstract class SAMMessageSession {
     /**
      * Initialize a new SAM message-based session.
      *
-     * @param destStream Input stream containing the destination keys
+     * @param destStream Input stream containing the destination and private keys (same format as PrivateKeyFile)
      * @param props Properties to setup the I2P session
      * @throws IOException
      * @throws DataFormatException
@@ -68,7 +68,8 @@ abstract class SAMMessageSession {
     }
 
     private void initSAMMessageSession (InputStream destStream, Properties props) throws IOException, DataFormatException, I2PSessionException {
-        _log.debug("Initializing SAM message-based session");
+        if (_log.shouldLog(Log.DEBUG))
+            _log.debug("Initializing SAM message-based session");
 
         handler = new SAMMessageSessionHandler(destStream, props);
 
@@ -168,14 +169,17 @@ abstract class SAMMessageSession {
 	 * @throws I2PSessionException 
          */
         public SAMMessageSessionHandler(InputStream destStream, Properties props) throws I2PSessionException {
-            _log.debug("Instantiating new SAM message-based session handler");
+            if (_log.shouldLog(Log.DEBUG))
+                _log.debug("Instantiating new SAM message-based session handler");
 
             I2PClient client = I2PClientFactory.createClient();
             session = client.createSession(destStream, props);
 
-            _log.debug("Connecting I2P session...");
+            if (_log.shouldLog(Log.DEBUG))
+                _log.debug("Connecting I2P session...");
             session.connect();
-            _log.debug("I2P session connected");
+            if (_log.shouldLog(Log.DEBUG))
+                _log.debug("I2P session connected");
 
             session.setSessionListener(this);
         }
@@ -193,7 +197,8 @@ abstract class SAMMessageSession {
 
         public void run() {
 
-            _log.debug("SAM message-based session handler running");
+            if (_log.shouldLog(Log.DEBUG))
+                _log.debug("SAM message-based session handler running");
 
             synchronized (runningLock) {
                 while (stillRunning) {
@@ -203,27 +208,32 @@ abstract class SAMMessageSession {
                 }
             }
 
-            _log.debug("Shutting down SAM message-based session handler");
+            if (_log.shouldLog(Log.DEBUG))
+                _log.debug("Shutting down SAM message-based session handler");
             
             shutDown();
             
             try {
-                _log.debug("Destroying I2P session...");
+                if (_log.shouldLog(Log.DEBUG))
+                    _log.debug("Destroying I2P session...");
                 session.destroySession();
-                _log.debug("I2P session destroyed");
+                if (_log.shouldLog(Log.DEBUG))
+                    _log.debug("I2P session destroyed");
             } catch (I2PSessionException e) {
                     _log.error("Error destroying I2P session", e);
             }
         }
         
         public void disconnected(I2PSession session) {
-            _log.debug("I2P session disconnected");
+            if (_log.shouldLog(Log.DEBUG))
+                _log.debug("I2P session disconnected");
             stopRunning();
         }
 
         public void errorOccurred(I2PSession session, String message,
                                   Throwable error) {
-            _log.debug("I2P error: " + message, error);
+            if (_log.shouldLog(Log.DEBUG))
+                _log.debug("I2P error: " + message, error);
             stopRunning();
         }
             
diff --git a/apps/sam/java/src/net/i2p/sam/SAMRawSession.java b/apps/sam/java/src/net/i2p/sam/SAMRawSession.java
index 4361af5e5c..08e98c2589 100644
--- a/apps/sam/java/src/net/i2p/sam/SAMRawSession.java
+++ b/apps/sam/java/src/net/i2p/sam/SAMRawSession.java
@@ -31,7 +31,7 @@ class SAMRawSession extends SAMMessageSession {
     /**
      * Create a new SAM RAW session.
      *
-     * @param dest Base64-encoded destination (private key)
+     * @param dest Base64-encoded destination and private keys (same format as PrivateKeyFile)
      * @param props Properties to setup the I2P session
      * @param recv Object that will receive incoming data
      * @throws IOException
@@ -48,7 +48,7 @@ class SAMRawSession extends SAMMessageSession {
     /**
      * Create a new SAM RAW session.
      *
-     * @param destStream Input stream containing the destination keys
+     * @param destStream Input stream containing the destination and private keys (same format as PrivateKeyFile)
      * @param props Properties to setup the I2P session
      * @param recv Object that will receive incoming data
      * @throws IOException
diff --git a/apps/sam/java/src/net/i2p/sam/SAMStreamSession.java b/apps/sam/java/src/net/i2p/sam/SAMStreamSession.java
index cfa8da66eb..db5d39d49a 100644
--- a/apps/sam/java/src/net/i2p/sam/SAMStreamSession.java
+++ b/apps/sam/java/src/net/i2p/sam/SAMStreamSession.java
@@ -80,7 +80,7 @@ class SAMStreamSession {
     /**
      * Create a new SAM STREAM session.
      *
-     * @param dest Base64-encoded destination (private key)
+     * @param dest Base64-encoded destination and private keys (same format as PrivateKeyFile)
      * @param dir Session direction ("RECEIVE", "CREATE" or "BOTH")
      * @param props Properties to setup the I2P session
      * @param recv Object that will receive incoming data
@@ -96,7 +96,7 @@ class SAMStreamSession {
     /**
      * Create a new SAM STREAM session.
      *
-     * @param destStream Input stream containing the destination keys
+     * @param destStream Input stream containing the destination and private keys (same format as PrivateKeyFile)
      * @param dir Session direction ("RECEIVE", "CREATE" or "BOTH")
      * @param props Properties to setup the I2P session
      * @param recv Object that will receive incoming data
@@ -108,7 +108,8 @@ class SAMStreamSession {
                             Properties props,  SAMStreamReceiver recv) throws IOException, DataFormatException, SAMException {
         this.recv = recv;
         _log = new Log(getClass());
-        _log.debug("SAM STREAM session instantiated");
+        if (_log.shouldLog(Log.DEBUG))
+	        _log.debug("SAM STREAM session instantiated");
 
         Properties allprops = (Properties) System.getProperties().clone();
         allprops.putAll(props);
@@ -122,7 +123,8 @@ class SAMStreamSession {
             throw new SAMException("Invalid I2CP port specified [" + port + "]");
         }
 
-        _log.debug("Creating I2PSocketManager...");
+        if (_log.shouldLog(Log.DEBUG))
+            _log.debug("Creating I2PSocketManager...");
         socketMgr = I2PSocketManagerFactory.createManager(destStream,
                                                           i2cpHost,
                                                           i2cpPort, 
@@ -193,12 +195,14 @@ class SAMStreamSession {
      */
     public boolean connect ( int id, String dest, Properties props ) throws I2PException, ConnectException, NoRouteToHostException, DataFormatException, InterruptedIOException, SAMInvalidDirectionException, IOException {
         if (!canCreate) {
-            _log.debug("Trying to create an outgoing connection using a receive-only session");
+            if (_log.shouldLog(Log.DEBUG))
+                _log.debug("Trying to create an outgoing connection using a receive-only session");
             throw new SAMInvalidDirectionException("Trying to create connections through a receive-only session");
         }
 
         if (checkSocketHandlerId(id)) {
-            _log.debug("The specified id (" + id + ") is already in use");
+            if (_log.shouldLog(Log.DEBUG))
+                _log.debug("The specified id (" + id + ") is already in use");
             return false;
         }
 
@@ -209,7 +213,8 @@ class SAMStreamSession {
         if (props.getProperty(I2PSocketOptions.PROP_CONNECT_TIMEOUT) == null)
             opts.setConnectTimeout(60 * 1000);
 
-        _log.debug("Connecting new I2PSocket...");
+        if (_log.shouldLog(Log.DEBUG))
+            _log.debug("Connecting new I2PSocket...");
 
 	// blocking connection (SAMv1)
 
@@ -271,7 +276,8 @@ class SAMStreamSession {
      */
     public boolean closeConnection(int id) {
         if (!checkSocketHandlerId(id)) {
-            _log.debug("The specified id (" + id + ") does not exist!");
+            if (_log.shouldLog(Log.DEBUG))
+                _log.debug("The specified id (" + id + ") does not exist!");
             return false;
         }
         removeSocketHandler(id);
@@ -368,7 +374,8 @@ class SAMStreamSession {
             reader.stopRunning();
         if (sender != null)
             sender.shutDownGracefully();
-        _log.debug("Removed SAM STREAM session socket handler (gracefully) " + id);
+        if (_log.shouldLog(Log.DEBUG))
+            _log.debug("Removed SAM STREAM session socket handler (gracefully) " + id);
     }
 
     /**
@@ -413,7 +420,8 @@ class SAMStreamSession {
          *
          */
         public SAMStreamSessionServer() {
-            _log.debug("Instantiating new SAM STREAM session server");
+            if (_log.shouldLog(Log.DEBUG))
+                _log.debug("Instantiating new SAM STREAM session server");
 
             serverSocket = socketMgr.getServerSocket();
         }
@@ -423,7 +431,8 @@ class SAMStreamSession {
          *
          */
         public void stopRunning() {
-            _log.debug("SAMStreamSessionServer.stopRunning() invoked");
+            if (_log.shouldLog(Log.DEBUG))
+                _log.debug("SAMStreamSessionServer.stopRunning() invoked");
             synchronized (runningLock) {
                 if (stillRunning) {
                     stillRunning = false;
@@ -437,7 +446,8 @@ class SAMStreamSession {
         }
 
         public void run() {
-            _log.debug("SAM STREAM session server running");
+            if (_log.shouldLog(Log.DEBUG))
+                _log.debug("SAM STREAM session server running");
             I2PSocket i2ps;
 
             while (stillRunning) {
@@ -446,7 +456,8 @@ class SAMStreamSession {
                     if (i2ps == null)
                         break;
 
-                    _log.debug("New incoming connection");
+                   if (_log.shouldLog(Log.DEBUG))
+                        _log.debug("New incoming connection");
 
                     int id = createSocketHandler(i2ps, 0);
                     if (id == 0) {
@@ -455,14 +466,17 @@ class SAMStreamSession {
                         continue;
                     }
 
-                    _log.debug("New connection id: " + id);
+                    if (_log.shouldLog(Log.DEBUG))
+                        _log.debug("New connection id: " + id);
 
                     recv.notifyStreamIncomingConnection ( id, i2ps.getPeerDestination() );
                 } catch (I2PException e) {
-                    _log.debug("Caught I2PException", e);
+                    if (_log.shouldLog(Log.DEBUG))
+                        _log.debug("Caught I2PException", e);
                     break;
                 } catch (IOException e) {
-                    _log.debug("Caught IOException", e);
+                    if (_log.shouldLog(Log.DEBUG))
+                        _log.debug("Caught IOException", e);
                     break;
                 }
             }
@@ -470,12 +484,14 @@ class SAMStreamSession {
             try {
                 serverSocket.close(); // In case it wasn't closed, yet
             } catch (I2PException e) {
-                _log.debug("Caught I2PException", e);
+                if (_log.shouldLog(Log.DEBUG))
+                    _log.debug("Caught I2PException", e);
             }
             
             close();
 
-            _log.debug("Shutting down SAM STREAM session server");
+            if (_log.shouldLog(Log.DEBUG))
+                _log.debug("Shutting down SAM STREAM session server");
         }
                 
     }
@@ -483,7 +499,8 @@ class SAMStreamSession {
 
     boolean setReceiveLimit ( int id, long limit, boolean nolimit )
     {
-        _log.debug ( "Protocol v1 does not support a receive limit for streams" );
+        if (_log.shouldLog(Log.DEBUG))
+            _log.debug ( "Protocol v1 does not support a receive limit for streams" );
 	return false ;
     }
 
@@ -540,7 +557,8 @@ class SAMStreamSession {
 
         public SAMv1StreamSessionSocketReader ( I2PSocket s, int id ) throws IOException {
             super(s, id);
-            _log.debug("Instantiating new SAM STREAM session socket reader");
+            if (_log.shouldLog(Log.DEBUG))
+                _log.debug("Instantiating new SAM STREAM session socket reader");
         }
 
         /**
@@ -549,7 +567,8 @@ class SAMStreamSession {
          */
         @Override
         public void stopRunning() {
-            _log.debug("stopRunning() invoked on socket reader " + id);
+            if (_log.shouldLog(Log.DEBUG))
+                _log.debug("stopRunning() invoked on socket reader " + id);
             synchronized (runningLock) {
                 if (stillRunning) {
                     stillRunning = false;
@@ -560,7 +579,8 @@ class SAMStreamSession {
 
         @Override
         public void run() {
-            _log.debug("run() called for socket reader " + id);
+            if (_log.shouldLog(Log.DEBUG))
+                _log.debug("run() called for socket reader " + id);
 
             int read = -1;
             ByteBuffer data = ByteBuffer.allocate(SOCKET_HANDLER_BUF_SIZE);
@@ -572,20 +592,23 @@ class SAMStreamSession {
                 	data.clear();
                     read = Channels.newChannel(in).read(data);
                     if (read == -1) {
-                        _log.debug("Handler " + id + ": connection closed");
+                        if (_log.shouldLog(Log.DEBUG))
+                            _log.debug("Handler " + id + ": connection closed");
                         break;
                     }
                     data.flip();
                     recv.receiveStreamBytes(id, data);
                 }
             } catch (IOException e) {
-                _log.debug("Caught IOException", e);
+                if (_log.shouldLog(Log.DEBUG))
+                    _log.debug("Caught IOException", e);
             }
             
             try {
                 i2pSocket.close();
             } catch (IOException e) {
-                _log.debug("Caught IOException", e);
+                if (_log.shouldLog(Log.DEBUG))
+                    _log.debug("Caught IOException", e);
             }
 
             if (stillRunning) {
@@ -594,12 +617,14 @@ class SAMStreamSession {
                 try {
                     recv.notifyStreamDisconnection(id, "OK", null);
                 } catch (IOException e) {
-                    _log.debug("Error sending disconnection notice for handler "
+                    if (_log.shouldLog(Log.DEBUG))
+                        _log.debug("Error sending disconnection notice for handler "
                                + id, e);
                 }
             }
 
-            _log.debug("Shutting down SAM STREAM session socket handler " +id);
+            if (_log.shouldLog(Log.DEBUG))
+                _log.debug("Shutting down SAM STREAM session socket handler " +id);
         }
     }
     
@@ -691,14 +716,16 @@ class SAMStreamSession {
          */
         @Override
         public void stopRunning() {
-            _log.debug("stopRunning() invoked on socket sender " + _id);
+            if (_log.shouldLog(Log.DEBUG))
+                _log.debug("stopRunning() invoked on socket sender " + _id);
             synchronized (runningLock) {
                 if (_stillRunning) {
                     _stillRunning = false;
                     try {
                         i2pSocket.close();
                     } catch (IOException e) {
-                        _log.debug("Caught IOException", e);
+                        if (_log.shouldLog(Log.DEBUG))
+                            _log.debug("Caught IOException", e);
                     }
                     synchronized (_data) {
                         _data.clear();
@@ -714,13 +741,15 @@ class SAMStreamSession {
          */
         @Override
         public void shutDownGracefully() {
-            _log.debug("shutDownGracefully() invoked on socket sender " + _id);
+            if (_log.shouldLog(Log.DEBUG))
+                _log.debug("shutDownGracefully() invoked on socket sender " + _id);
             _shuttingDownGracefully = true;
         }
 
         @Override
         public void run() {
-            _log.debug("run() called for socket sender " + _id);
+            if (_log.shouldLog(Log.DEBUG))
+                _log.debug("run() called for socket sender " + _id);
             ByteArray data = null;
             while (_stillRunning) {
                 data = null;
diff --git a/apps/sam/java/src/net/i2p/sam/SAMv1Handler.java b/apps/sam/java/src/net/i2p/sam/SAMv1Handler.java
index 581eef75cb..2549cedec9 100644
--- a/apps/sam/java/src/net/i2p/sam/SAMv1Handler.java
+++ b/apps/sam/java/src/net/i2p/sam/SAMv1Handler.java
@@ -77,7 +77,8 @@ class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatagramRece
     public SAMv1Handler(SocketChannel s, int verMajor, int verMinor, Properties i2cpProps) throws SAMException, IOException {
         super(s, verMajor, verMinor, i2cpProps);
         _id = __id.incrementAndGet();
-        _log.debug("SAM version 1 handler instantiated");
+        if (_log.shouldLog(Log.DEBUG))
+            _log.debug("SAM version 1 handler instantiated");
 
     if ( ! verifVersion() ) {
             throw new SAMException("BUG! Wrong protocol version!");
@@ -97,12 +98,14 @@ class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatagramRece
         Properties props;
 
         this.thread.setName("SAMv1Handler " + _id);
-        _log.debug("SAM handling started");
+        if (_log.shouldLog(Log.DEBUG))
+            _log.debug("SAM handling started");
 
         try {
             while (true) {
                 if (shouldStop()) {
-                    _log.debug("Stop request found");
+                    if (_log.shouldLog(Log.DEBUG))
+                        _log.debug("Stop request found");
                     break;
                 }
 
@@ -132,14 +135,16 @@ class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatagramRece
                 }
 
                 if(msg.equals("")) {
-                    _log.debug("Ignoring newline");
+                    if (_log.shouldLog(Log.DEBUG))
+                        _log.debug("Ignoring newline");
                     continue;
                 }
 
                 tok = new StringTokenizer(msg, " ");
                 if (tok.countTokens() < 2) {
                     // This is not a correct message, for sure
-                    _log.debug("Error in message format");
+                    if (_log.shouldLog(Log.DEBUG))
+                        _log.debug("Error in message format");
                     break;
                 }
                 domain = tok.nextToken();
@@ -165,7 +170,8 @@ class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatagramRece
                 } else if (domain.equals("NAMING")) {
                     canContinue = execNamingMessage(opcode, props);
                 } else {
-                    _log.debug("Unrecognized message domain: \""
+                    if (_log.shouldLog(Log.DEBUG))
+                        _log.debug("Unrecognized message domain: \""
                                + domain + "\"");
                     break;
                 }
@@ -175,12 +181,14 @@ class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatagramRece
                 }
             }
         } catch (IOException e) {
-            _log.debug("Caught IOException ("
+            if (_log.shouldLog(Log.DEBUG))
+                _log.debug("Caught IOException ("
                        + e.getMessage() + ") for message [" + msg + "]", e);
         } catch (Exception e) {
             _log.error("Unexpected exception for message [" + msg + "]", e);
         } finally {
-            _log.debug("Stopping handler");
+            if (_log.shouldLog(Log.DEBUG))
+                _log.debug("Stopping handler");
             try {
                 closeClientSocket();
             } catch (IOException e) {
@@ -207,17 +215,20 @@ class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatagramRece
             if (opcode.equals("CREATE")) {
                 if ((getRawSession() != null) || (getDatagramSession() != null)
                     || (getStreamSession() != null)) {
-                    _log.debug("Trying to create a session, but one still exists");
+                    if (_log.shouldLog(Log.DEBUG))
+                        _log.debug("Trying to create a session, but one still exists");
                     return writeString("SESSION STATUS RESULT=I2P_ERROR MESSAGE=\"Session already exists\"\n");
                 }
                 if (props == null) {
-                    _log.debug("No parameters specified in SESSION CREATE message");
+                    if (_log.shouldLog(Log.DEBUG))
+                        _log.debug("No parameters specified in SESSION CREATE message");
                     return writeString("SESSION STATUS RESULT=I2P_ERROR MESSAGE=\"No parameters for SESSION CREATE\"\n");
                 }
                 
                 dest = props.getProperty("DESTINATION");
                 if (dest == null) {
-                    _log.debug("SESSION DESTINATION parameter not specified");
+                    if (_log.shouldLog(Log.DEBUG))
+                        _log.debug("SESSION DESTINATION parameter not specified");
                     return writeString("SESSION STATUS RESULT=I2P_ERROR MESSAGE=\"DESTINATION not specified\"\n");
                 }
                 props.remove("DESTINATION");
@@ -247,7 +258,8 @@ class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatagramRece
                 
                 String style = props.getProperty("STYLE");
                 if (style == null) {
-                    _log.debug("SESSION STYLE parameter not specified");
+                    if (_log.shouldLog(Log.DEBUG))
+                        _log.debug("SESSION STYLE parameter not specified");
                     return writeString("SESSION STATUS RESULT=I2P_ERROR MESSAGE=\"No SESSION STYLE specified\"\n");
                 }
                 props.remove("STYLE");
@@ -264,33 +276,39 @@ class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatagramRece
                 } else if (style.equals("STREAM")) {
                     String dir = props.getProperty("DIRECTION");
                     if (dir == null) {
-                        _log.debug("No DIRECTION parameter in STREAM session, defaulting to BOTH");
+                        if (_log.shouldLog(Log.DEBUG))
+                            _log.debug("No DIRECTION parameter in STREAM session, defaulting to BOTH");
                         dir = "BOTH";
                     }
                     if (!dir.equals("CREATE") && !dir.equals("RECEIVE")
                         && !dir.equals("BOTH")) {
-                        _log.debug("Unknow DIRECTION parameter value: [" + dir + "]");
+                        if (_log.shouldLog(Log.DEBUG))
+                            _log.debug("Unknow DIRECTION parameter value: [" + dir + "]");
                         return writeString("SESSION STATUS RESULT=I2P_ERROR MESSAGE=\"Unknown DIRECTION parameter\"\n");
                     }
                     props.remove("DIRECTION");
                 
                     streamSession = newSAMStreamSession(destKeystream, dir,props);
                 } else {
-                    _log.debug("Unrecognized SESSION STYLE: \"" + style +"\"");
+                    if (_log.shouldLog(Log.DEBUG))
+                        _log.debug("Unrecognized SESSION STYLE: \"" + style +"\"");
                     return writeString("SESSION STATUS RESULT=I2P_ERROR MESSAGE=\"Unrecognized SESSION STYLE\"\n");
                 }
                 return writeString("SESSION STATUS RESULT=OK DESTINATION="
                                    + dest + "\n");
             } else {
-                _log.debug("Unrecognized SESSION message opcode: \""
+                if (_log.shouldLog(Log.DEBUG))
+                    _log.debug("Unrecognized SESSION message opcode: \""
                            + opcode + "\"");
                 return writeString("SESSION STATUS RESULT=I2P_ERROR MESSAGE=\"Unrecognized opcode\"\n");
             }
         } catch (DataFormatException e) {
-            _log.debug("Invalid destination specified");
+            if (_log.shouldLog(Log.DEBUG))
+                _log.debug("Invalid destination specified");
             return writeString("SESSION STATUS RESULT=INVALID_KEY DESTINATION=" + dest + " MESSAGE=\"" + e.getMessage() + "\"\n");
         } catch (I2PSessionException e) {
-            _log.debug("I2P error when instantiating session", e);
+            if (_log.shouldLog(Log.DEBUG))
+                _log.debug("I2P error when instantiating session", e);
             return writeString("SESSION STATUS RESULT=I2P_ERROR DESTINATION=" + dest + " MESSAGE=\"" + e.getMessage() + "\"\n");
         } catch (SAMException e) {
             _log.error("Unexpected SAM error", e);
@@ -313,7 +331,8 @@ class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatagramRece
 
         if (opcode.equals("GENERATE")) {
             if (!props.isEmpty()) {
-                _log.debug("Properties specified in DEST GENERATE message");
+                if (_log.shouldLog(Log.DEBUG))
+                    _log.debug("Properties specified in DEST GENERATE message");
                 return false;
             }
 
@@ -328,7 +347,8 @@ class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatagramRece
                                + Base64.encode(priv.toByteArray())
                                + "\n");
         } else {
-            _log.debug("Unrecognized DEST message opcode: \"" + opcode + "\"");
+            if (_log.shouldLog(Log.DEBUG))
+                _log.debug("Unrecognized DEST message opcode: \"" + opcode + "\"");
             return false;
         }
     }
@@ -343,7 +363,8 @@ class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatagramRece
             
             String name = props.getProperty("NAME");
             if (name == null) {
-                _log.debug("Name to resolve not specified in NAMING message");
+                if (_log.shouldLog(Log.DEBUG))
+                    _log.debug("Name to resolve not specified in NAMING message");
                 return false;
             }
 
@@ -356,7 +377,8 @@ class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatagramRece
                 } else if (getDatagramSession() != null) {
                     dest = getDatagramSession().getDestination();
                 } else {
-                    _log.debug("Lookup for SESSION destination, but session is null");
+                    if (_log.shouldLog(Log.DEBUG))
+                        _log.debug("Lookup for SESSION destination, but session is null");
                     return false;
                 }
             } else {
@@ -375,7 +397,8 @@ class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatagramRece
                                + dest.toBase64()
                                + "\n");
         } else {
-            _log.debug("Unrecognized NAMING message opcode: \""
+            if (_log.shouldLog(Log.DEBUG))
+                _log.debug("Unrecognized NAMING message opcode: \""
                        + opcode + "\"");
             return false;
         }
@@ -391,13 +414,15 @@ class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatagramRece
 
         if (opcode.equals("SEND")) {
             if (props == null) {
-                _log.debug("No parameters specified in DATAGRAM SEND message");
+                if (_log.shouldLog(Log.DEBUG))
+                    _log.debug("No parameters specified in DATAGRAM SEND message");
                 return false;
             }
             
             String dest = props.getProperty("DESTINATION");
             if (dest == null) {
-                _log.debug("Destination not specified in DATAGRAM SEND message");
+                if (_log.shouldLog(Log.DEBUG))
+                    _log.debug("Destination not specified in DATAGRAM SEND message");
                 return false;
             }
 
@@ -405,17 +430,20 @@ class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatagramRece
             {
                 String strsize = props.getProperty("SIZE");
                 if (strsize == null) {
-                    _log.debug("Size not specified in DATAGRAM SEND message");
+                    if (_log.shouldLog(Log.DEBUG))
+                        _log.debug("Size not specified in DATAGRAM SEND message");
                     return false;
                 }
                 try {
                     size = Integer.parseInt(strsize);
                 } catch (NumberFormatException e) {
-                    _log.debug("Invalid DATAGRAM SEND size specified: " + strsize);
+                    if (_log.shouldLog(Log.DEBUG))
+                        _log.debug("Invalid DATAGRAM SEND size specified: " + strsize);
                     return false;
                 }
                 if (!checkDatagramSize(size)) {
-                    _log.debug("Specified size (" + size
+                    if (_log.shouldLog(Log.DEBUG))
+                         _log.debug("Specified size (" + size
                                + ") is out of protocol limits");
                     return false;
                 }
@@ -434,20 +462,24 @@ class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatagramRece
 
                 return true;
             } catch (EOFException e) {
-                _log.debug("Too few bytes with DATAGRAM SEND message (expected: "
+                if (_log.shouldLog(Log.DEBUG))
+                    _log.debug("Too few bytes with DATAGRAM SEND message (expected: "
                            + size);
                 return false;
             } catch (IOException e) {
-                _log.debug("Caught IOException while parsing DATAGRAM SEND message",
+                if (_log.shouldLog(Log.DEBUG))
+                    _log.debug("Caught IOException while parsing DATAGRAM SEND message",
                            e);
                 return false;
             } catch (DataFormatException e) {
-                _log.debug("Invalid key specified with DATAGRAM SEND message",
+                if (_log.shouldLog(Log.DEBUG))
+                    _log.debug("Invalid key specified with DATAGRAM SEND message",
                            e);
                 return false;
             }
         } else {
-            _log.debug("Unrecognized DATAGRAM message opcode: \""
+            if (_log.shouldLog(Log.DEBUG))
+                _log.debug("Unrecognized DATAGRAM message opcode: \""
                        + opcode + "\"");
             return false;
         }
@@ -462,13 +494,15 @@ class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatagramRece
 
         if (opcode.equals("SEND")) {
             if (props == null) {
-                _log.debug("No parameters specified in RAW SEND message");
+                if (_log.shouldLog(Log.DEBUG))
+                    _log.debug("No parameters specified in RAW SEND message");
                 return false;
             }
             
             String dest = props.getProperty("DESTINATION");
             if (dest == null) {
-                _log.debug("Destination not specified in RAW SEND message");
+                if (_log.shouldLog(Log.DEBUG))
+                    _log.debug("Destination not specified in RAW SEND message");
                 return false;
             }
 
@@ -476,17 +510,20 @@ class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatagramRece
             {
                 String strsize = props.getProperty("SIZE");
                 if (strsize == null) {
-                    _log.debug("Size not specified in RAW SEND message");
+                    if (_log.shouldLog(Log.DEBUG))
+                        _log.debug("Size not specified in RAW SEND message");
                     return false;
                 }
                 try {
                     size = Integer.parseInt(strsize);
                 } catch (NumberFormatException e) {
-                    _log.debug("Invalid RAW SEND size specified: " + strsize);
+                    if (_log.shouldLog(Log.DEBUG))
+                        _log.debug("Invalid RAW SEND size specified: " + strsize);
                     return false;
                 }
                 if (!checkSize(size)) {
-                    _log.debug("Specified size (" + size
+                    if (_log.shouldLog(Log.DEBUG))
+                        _log.debug("Specified size (" + size
                                + ") is out of protocol limits");
                     return false;
                 }
@@ -505,20 +542,24 @@ class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatagramRece
 
                 return true;
             } catch (EOFException e) {
-                _log.debug("Too few bytes with RAW SEND message (expected: "
+                if (_log.shouldLog(Log.DEBUG))
+                    _log.debug("Too few bytes with RAW SEND message (expected: "
                            + size);
                 return false;
             } catch (IOException e) {
-                _log.debug("Caught IOException while parsing RAW SEND message",
+                if (_log.shouldLog(Log.DEBUG))
+                    _log.debug("Caught IOException while parsing RAW SEND message",
                            e);
                 return false;
             } catch (DataFormatException e) {
-                _log.debug("Invalid key specified with RAW SEND message",
+                if (_log.shouldLog(Log.DEBUG))
+                    _log.debug("Invalid key specified with RAW SEND message",
                            e);
                 return false;
             }
         } else {
-            _log.debug("Unrecognized RAW message opcode: \""
+            if (_log.shouldLog(Log.DEBUG))
+                _log.debug("Unrecognized RAW message opcode: \""
                        + opcode + "\"");
             return false;
         }
@@ -538,7 +579,8 @@ class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatagramRece
         } else if (opcode.equals("CLOSE")) {
             return execStreamClose(props);
         } else {
-            _log.debug("Unrecognized RAW message opcode: \""
+            if (_log.shouldLog(Log.DEBUG))
+                _log.debug("Unrecognized RAW message opcode: \""
                        + opcode + "\"");
             return false;
         }
@@ -546,7 +588,8 @@ class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatagramRece
             
   protected boolean execStreamSend(Properties props) {
         if (props == null) {
-            _log.debug("No parameters specified in STREAM SEND message");
+            if (_log.shouldLog(Log.DEBUG))
+                _log.debug("No parameters specified in STREAM SEND message");
             return false;
         }
 
@@ -554,13 +597,15 @@ class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatagramRece
         {
             String strid = props.getProperty("ID");
             if (strid == null) {
-                _log.debug("ID not specified in STREAM SEND message");
+                if (_log.shouldLog(Log.DEBUG))
+                    _log.debug("ID not specified in STREAM SEND message");
                 return false;
             }
             try {
                 id = Integer.parseInt(strid);
             } catch (NumberFormatException e) {
-                _log.debug("Invalid STREAM SEND ID specified: " + strid);
+                if (_log.shouldLog(Log.DEBUG))
+                    _log.debug("Invalid STREAM SEND ID specified: " + strid);
                 return false;
             }
         }
@@ -569,17 +614,20 @@ class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatagramRece
         {
             String strsize = props.getProperty("SIZE");
             if (strsize == null) {
-                _log.debug("Size not specified in STREAM SEND message");
+                if (_log.shouldLog(Log.DEBUG))
+                    _log.debug("Size not specified in STREAM SEND message");
                 return false;
             }
             try {
                 size = Integer.parseInt(strsize);
             } catch (NumberFormatException e) {
-                _log.debug("Invalid STREAM SEND size specified: "+strsize);
+                if (_log.shouldLog(Log.DEBUG))
+                    _log.debug("Invalid STREAM SEND size specified: "+strsize);
                 return false;
             }
             if (!checkSize(size)) {
-                _log.debug("Specified size (" + size
+                if (_log.shouldLog(Log.DEBUG))
+                    _log.debug("Specified size (" + size
                            + ") is out of protocol limits");
                 return false;
             }
@@ -596,11 +644,13 @@ class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatagramRece
 
             return true;
         } catch (EOFException e) {
-            _log.debug("Too few bytes with STREAM SEND message (expected: "
+            if (_log.shouldLog(Log.DEBUG))
+                _log.debug("Too few bytes with STREAM SEND message (expected: "
                        + size);
             return false;
         } catch (IOException e) {
-            _log.debug("Caught IOException while parsing STREAM SEND message",
+            if (_log.shouldLog(Log.DEBUG))
+                _log.debug("Caught IOException while parsing STREAM SEND message",
                        e);
             return false;
         }
@@ -608,7 +658,8 @@ class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatagramRece
 
   protected boolean execStreamConnect(Properties props) {
         if (props == null) {
-            _log.debug("No parameters specified in STREAM CONNECT message");
+            if (_log.shouldLog(Log.DEBUG))
+                _log.debug("No parameters specified in STREAM CONNECT message");
             return false;
         }
 
@@ -616,17 +667,20 @@ class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatagramRece
         {
             String strid = props.getProperty("ID");
             if (strid == null) {
-                _log.debug("ID not specified in STREAM SEND message");
+                if (_log.shouldLog(Log.DEBUG))
+                    _log.debug("ID not specified in STREAM SEND message");
                 return false;
             }
             try {
                 id = Integer.parseInt(strid);
             } catch (NumberFormatException e) {
-                _log.debug("Invalid STREAM CONNECT ID specified: " +strid);
+                if (_log.shouldLog(Log.DEBUG))
+                    _log.debug("Invalid STREAM CONNECT ID specified: " +strid);
                 return false;
             }
             if (id < 1) {
-                _log.debug("Invalid STREAM CONNECT ID specified: " +strid);
+                if (_log.shouldLog(Log.DEBUG))
+                    _log.debug("Invalid STREAM CONNECT ID specified: " +strid);
                 return false;
             }
             props.remove("ID");
@@ -642,26 +696,33 @@ class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatagramRece
         try {
             try {
                 if (!getStreamSession().connect(id, dest, props)) {
-                    _log.debug("STREAM connection failed");
+                    if (_log.shouldLog(Log.DEBUG))
+                        _log.debug("STREAM connection failed");
                     return false;
                 }
             } catch (DataFormatException e) {
-                _log.debug("Invalid destination in STREAM CONNECT message");
+                if (_log.shouldLog(Log.DEBUG))
+                    _log.debug("Invalid destination in STREAM CONNECT message");
                 notifyStreamOutgoingConnection ( id, "INVALID_KEY", null );
             } catch (SAMInvalidDirectionException e) {
-                _log.debug("STREAM CONNECT failed: " + e.getMessage());
+                if (_log.shouldLog(Log.DEBUG))
+                    _log.debug("STREAM CONNECT failed", e);
                 notifyStreamOutgoingConnection ( id, "INVALID_DIRECTION", null );
             } catch (ConnectException e) {
-                _log.debug("STREAM CONNECT failed: " + e.getMessage());
+                if (_log.shouldLog(Log.DEBUG))
+                    _log.debug("STREAM CONNECT failed", e);
                 notifyStreamOutgoingConnection ( id, "CONNECTION_REFUSED", null );
             } catch (NoRouteToHostException e) {
-                _log.debug("STREAM CONNECT failed: " + e.getMessage());
+                if (_log.shouldLog(Log.DEBUG))
+                    _log.debug("STREAM CONNECT failed", e);
                 notifyStreamOutgoingConnection ( id, "CANT_REACH_PEER", null );
             } catch (InterruptedIOException e) {
-                _log.debug("STREAM CONNECT failed: " + e.getMessage());
+                if (_log.shouldLog(Log.DEBUG))
+                    _log.debug("STREAM CONNECT failed", e);
                 notifyStreamOutgoingConnection ( id, "TIMEOUT", null );
             } catch (I2PException e) {
-                _log.debug("STREAM CONNECT failed: " + e.getMessage());
+                if (_log.shouldLog(Log.DEBUG))
+                    _log.debug("STREAM CONNECT failed", e);
                 notifyStreamOutgoingConnection ( id, "I2P_ERROR", null );
             }
         } catch (IOException e) {
@@ -673,7 +734,8 @@ class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatagramRece
     
   protected boolean execStreamClose(Properties props) {
         if (props == null) {
-            _log.debug("No parameters specified in STREAM CLOSE message");
+            if (_log.shouldLog(Log.DEBUG))
+                _log.debug("No parameters specified in STREAM CLOSE message");
             return false;
         }
 
@@ -681,13 +743,15 @@ class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatagramRece
         {
             String strid = props.getProperty("ID");
             if (strid == null) {
-                _log.debug("ID not specified in STREAM CLOSE message");
+                if (_log.shouldLog(Log.DEBUG))
+                    _log.debug("ID not specified in STREAM CLOSE message");
                 return false;
             }
             try {
                 id = Integer.parseInt(strid);
             } catch (NumberFormatException e) {
-                _log.debug("Invalid STREAM CLOSE ID specified: " +strid);
+                if (_log.shouldLog(Log.DEBUG))
+                    _log.debug("Invalid STREAM CLOSE ID specified: " +strid);
                 return false;
             }
         }
@@ -729,7 +793,8 @@ class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatagramRece
     }
 
     public void stopRawReceiving() {
-        _log.debug("stopRawReceiving() invoked");
+        if (_log.shouldLog(Log.DEBUG))
+            _log.debug("stopRawReceiving() invoked");
 
         if (getRawSession() == null) {
             _log.error("BUG! Got raw receiving stop, but session is null!");
@@ -765,7 +830,8 @@ class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatagramRece
     }
 
     public void stopDatagramReceiving() {
-        _log.debug("stopDatagramReceiving() invoked");
+        if (_log.shouldLog(Log.DEBUG))
+            _log.debug("stopDatagramReceiving() invoked");
 
         if (getDatagramSession() == null) {
             _log.error("BUG! Got datagram receiving stop, but session is null!");
@@ -885,7 +951,8 @@ class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatagramRece
     }
 
     public void stopStreamReceiving() {
-        _log.debug("stopStreamReceiving() invoked", new Exception("stopped"));
+        if (_log.shouldLog(Log.DEBUG))
+            _log.debug("stopStreamReceiving() invoked", new Exception("stopped"));
 
         if (getStreamSession() == null) {
             _log.error("BUG! Got stream receiving stop, but session is null!");
diff --git a/apps/sam/java/src/net/i2p/sam/SAMv2Handler.java b/apps/sam/java/src/net/i2p/sam/SAMv2Handler.java
index 54fda314ed..2a76b9da3a 100644
--- a/apps/sam/java/src/net/i2p/sam/SAMv2Handler.java
+++ b/apps/sam/java/src/net/i2p/sam/SAMv2Handler.java
@@ -94,7 +94,8 @@ class SAMv2Handler extends SAMv1Handler implements SAMRawReceiver, SAMDatagramRe
 			}
 			else
 			{
-				_log.debug ( "Unrecognized RAW message opcode: \""
+				if (_log.shouldLog(Log.DEBUG))
+					_log.debug ( "Unrecognized RAW message opcode: \""
 						+ opcode + "\"" );
 				return false;
 			}
@@ -108,7 +109,8 @@ class SAMv2Handler extends SAMv1Handler implements SAMRawReceiver, SAMDatagramRe
 		{
 			if ( props == null )
 			{
-				_log.debug ( "No parameters specified in STREAM RECEIVE message" );
+				if (_log.shouldLog(Log.DEBUG))
+					_log.debug ( "No parameters specified in STREAM RECEIVE message" );
 				return false;
 			}
 
@@ -119,7 +121,8 @@ class SAMv2Handler extends SAMv1Handler implements SAMRawReceiver, SAMDatagramRe
 
 				if ( strid == null )
 				{
-					_log.debug ( "ID not specified in STREAM RECEIVE message" );
+					if (_log.shouldLog(Log.DEBUG))
+						_log.debug ( "ID not specified in STREAM RECEIVE message" );
 					return false;
 				}
 
@@ -129,7 +132,8 @@ class SAMv2Handler extends SAMv1Handler implements SAMRawReceiver, SAMDatagramRe
 				}
 				catch ( NumberFormatException e )
 				{
-					_log.debug ( "Invalid STREAM RECEIVE ID specified: " + strid );
+					if (_log.shouldLog(Log.DEBUG))
+						_log.debug ( "Invalid STREAM RECEIVE ID specified: " + strid );
 					return false;
 				}
 			}
@@ -142,7 +146,8 @@ class SAMv2Handler extends SAMv1Handler implements SAMRawReceiver, SAMDatagramRe
 
 				if ( strsize == null )
 				{
-					_log.debug ( "Limit not specified in STREAM RECEIVE message" );
+					if (_log.shouldLog(Log.DEBUG))
+						_log.debug ( "Limit not specified in STREAM RECEIVE message" );
 					return false;
 				}
 
@@ -158,13 +163,15 @@ class SAMv2Handler extends SAMv1Handler implements SAMRawReceiver, SAMDatagramRe
 					}
 					catch ( NumberFormatException e )
 					{
-						_log.debug ( "Invalid STREAM RECEIVE size specified: " + strsize );
+						if (_log.shouldLog(Log.DEBUG))
+							_log.debug ( "Invalid STREAM RECEIVE size specified: " + strsize );
 						return false;
 					}
 
 					if ( limit < 0 )
 					{
-						_log.debug ( "Specified limit (" + limit
+						if (_log.shouldLog(Log.DEBUG))
+							_log.debug ( "Specified limit (" + limit
 								+ ") is out of protocol limits" );
 						return false;
 					}
diff --git a/apps/sam/java/src/net/i2p/sam/SAMv3Handler.java b/apps/sam/java/src/net/i2p/sam/SAMv3Handler.java
index 44ec91d18d..85c0955f56 100644
--- a/apps/sam/java/src/net/i2p/sam/SAMv3Handler.java
+++ b/apps/sam/java/src/net/i2p/sam/SAMv3Handler.java
@@ -45,6 +45,7 @@ class SAMv3Handler extends SAMv1Handler
 {
 	
 	private Session session;
+
 	
 	interface Session {
 		String getNick();
@@ -80,7 +81,8 @@ class SAMv3Handler extends SAMv1Handler
 	public SAMv3Handler ( SocketChannel s, int verMajor, int verMinor, Properties i2cpProps ) throws SAMException, IOException
 	{
 		super ( s, verMajor, verMinor, i2cpProps );
-		_log.debug("SAM version 3 handler instantiated");
+		if (_log.shouldLog(Log.DEBUG))
+			_log.debug("SAM version 3 handler instantiated");
 	}
 
 	public boolean verifVersion()
@@ -221,20 +223,24 @@ class SAMv3Handler extends SAMv1Handler
 		{
 			return m_dest;
 		}
+
 		synchronized public Properties getProps()
 		{
 			Properties p = new Properties();
 			p.putAll(m_props);
 			return m_props;
 		}
+
 		synchronized public SAMv3Handler getHandler()
 		{
 			return m_handler ;
 		}
+
 		synchronized public ThreadGroup getThreadGroup()
 		{
 			return m_threadgroup ;
 		}
+
 		synchronized public void createThreadGroup(String name)
 		{
 			if (m_threadgroup == null)
@@ -278,6 +284,7 @@ class SAMv3Handler extends SAMv1Handler
 			else
 				return false ;
 		}
+
 		synchronized public boolean del( String nick )
 		{
 			SessionRecord rec = map.get(nick);
@@ -293,6 +300,7 @@ class SAMv3Handler extends SAMv1Handler
 		{
 			return map.get(nick);
 		}
+
 		synchronized public boolean containsKey( String nick )
 		{
 			return map.containsKey(nick);
@@ -324,36 +332,42 @@ class SAMv3Handler extends SAMv1Handler
 		Properties props;
 
 		this.thread.setName("SAMv3Handler " + _id);
-		_log.debug("SAM handling started");
+		if (_log.shouldLog(Log.DEBUG))
+			_log.debug("SAM handling started");
 
 		try {
 			InputStream in = getClientSocket().socket().getInputStream();
 
 			while (true) {
 				if (shouldStop()) {
-					_log.debug("Stop request found");
+					if (_log.shouldLog(Log.DEBUG))
+						_log.debug("Stop request found");
 					break;
 				}
 				String line = DataHelper.readLine(in) ;
 				if (line==null) {
-					_log.debug("Connection closed by client (line read : null)");
+					if (_log.shouldLog(Log.DEBUG))
+						_log.debug("Connection closed by client (line read : null)");
 					break;
 				}
 				msg = line.trim();
 
 				if (_log.shouldLog(Log.DEBUG)) {
-					_log.debug("New message received: [" + msg + "]");
+					if (_log.shouldLog(Log.DEBUG))
+						_log.debug("New message received: [" + msg + "]");
 				}
 
 				if(msg.equals("")) {
-					_log.debug("Ignoring newline");
+					if (_log.shouldLog(Log.DEBUG))
+						_log.debug("Ignoring newline");
 					continue;
 				}
 
 				tok = new StringTokenizer(msg, " ");
 				if (tok.countTokens() < 2) {
 					// This is not a correct message, for sure
-					_log.debug("Error in message format");
+					if (_log.shouldLog(Log.DEBUG))
+						_log.debug("Error in message format");
 					break;
 				}
 				domain = tok.nextToken();
@@ -377,7 +391,8 @@ class SAMv3Handler extends SAMv1Handler
 				} else if (domain.equals("DATAGRAM")) {
 					canContinue = execDatagramMessage(opcode, props);
 				} else {
-					_log.debug("Unrecognized message domain: \""
+					if (_log.shouldLog(Log.DEBUG))
+						_log.debug("Unrecognized message domain: \""
 							+ domain + "\"");
 					break;
 				}
@@ -387,12 +402,14 @@ class SAMv3Handler extends SAMv1Handler
 				}
 			}
 		} catch (IOException e) {
-			_log.debug("Caught IOException ("
+			if (_log.shouldLog(Log.DEBUG))
+				_log.debug("Caught IOException ("
 					+ e.getMessage() + ") for message [" + msg + "]", e);
 		} catch (Exception e) {
 			_log.error("Unexpected exception for message [" + msg + "]", e);
 		} finally {
-			_log.debug("Stopping handler");
+			if (_log.shouldLog(Log.DEBUG))
+				_log.debug("Stopping handler");
 			
 			if (!this.stolenSocket)
 			{
@@ -451,30 +468,35 @@ class SAMv3Handler extends SAMv1Handler
 			if (opcode.equals("CREATE")) {
 				if ((this.getRawSession()!= null) || (this.getDatagramSession() != null)
 						|| (this.getStreamSession() != null)) {
-					_log.debug("Trying to create a session, but one still exists");
+					if (_log.shouldLog(Log.DEBUG))
+						_log.debug("Trying to create a session, but one still exists");
 					return writeString("SESSION STATUS RESULT=I2P_ERROR MESSAGE=\"Session already exists\"\n");
 				}
 				if (props == null) {
-					_log.debug("No parameters specified in SESSION CREATE message");
+					if (_log.shouldLog(Log.DEBUG))
+						_log.debug("No parameters specified in SESSION CREATE message");
 					return writeString("SESSION STATUS RESULT=I2P_ERROR MESSAGE=\"No parameters for SESSION CREATE\"\n");
 				}
 
 				dest = props.getProperty("DESTINATION");
 				if (dest == null) {
-					_log.debug("SESSION DESTINATION parameter not specified");
+					if (_log.shouldLog(Log.DEBUG))
+						_log.debug("SESSION DESTINATION parameter not specified");
 					return writeString("SESSION STATUS RESULT=I2P_ERROR MESSAGE=\"DESTINATION not specified\"\n");
 				}
 				props.remove("DESTINATION");
 
 
 				if (dest.equals("TRANSIENT")) {
-					_log.debug("TRANSIENT destination requested");
+					if (_log.shouldLog(Log.DEBUG))
+						_log.debug("TRANSIENT destination requested");
 					ByteArrayOutputStream priv = new ByteArrayOutputStream(640);
 					SAMUtils.genRandomKey(priv, null);
 
 					dest = Base64.encode(priv.toByteArray());
 				} else {
-					_log.debug("Custom destination specified [" + dest + "]");
+					if (_log.shouldLog(Log.DEBUG))
+						_log.debug("Custom destination specified [" + dest + "]");
 				}
 
 				try {
@@ -485,7 +507,8 @@ class SAMv3Handler extends SAMv1Handler
 
 				nick = props.getProperty("ID");
 				if (nick == null) {
-					_log.debug("SESSION ID parameter not specified");
+					if (_log.shouldLog(Log.DEBUG))
+						_log.debug("SESSION ID parameter not specified");
 					return writeString("SESSION STATUS RESULT=I2P_ERROR MESSAGE=\"ID not specified\"\n");
 				}
 				props.remove("ID");
@@ -493,7 +516,8 @@ class SAMv3Handler extends SAMv1Handler
 
 				String style = props.getProperty("STYLE");
 				if (style == null) {
-					_log.debug("SESSION STYLE parameter not specified");
+					if (_log.shouldLog(Log.DEBUG))
+						_log.debug("SESSION STYLE parameter not specified");
 					return writeString("SESSION STATUS RESULT=I2P_ERROR MESSAGE=\"No SESSION STYLE specified\"\n");
 				}
 				props.remove("STYLE");
@@ -512,7 +536,8 @@ class SAMv3Handler extends SAMv1Handler
 				try {
 					sSessionsHash.put( nick, new SessionRecord(dest, allProps, this) ) ;
 				} catch (SessionsDB.ExistingId e) {
-					_log.debug("SESSION ID parameter already in use");
+					if (_log.shouldLog(Log.DEBUG))
+						_log.debug("SESSION ID parameter already in use");
 					return writeString("SESSION STATUS RESULT=DUPLICATED_ID\n");
 				} catch (SessionsDB.ExistingDest e) {
 					return writeString("SESSION STATUS RESULT=DUPLICATED_DEST\n");
@@ -536,25 +561,30 @@ class SAMv3Handler extends SAMv1Handler
 					streamSession = v3;
 					this.session = v3;
 				} else {
-					_log.debug("Unrecognized SESSION STYLE: \"" + style +"\"");
+					if (_log.shouldLog(Log.DEBUG))
+						_log.debug("Unrecognized SESSION STYLE: \"" + style +"\"");
 					return writeString("SESSION STATUS RESULT=I2P_ERROR MESSAGE=\"Unrecognized SESSION STYLE\"\n");
 				}
 				ok = true ;
 				return writeString("SESSION STATUS RESULT=OK DESTINATION="
 						+ dest + "\n");
 			} else {
-				_log.debug("Unrecognized SESSION message opcode: \""
+				if (_log.shouldLog(Log.DEBUG))
+					_log.debug("Unrecognized SESSION message opcode: \""
 						+ opcode + "\"");
 				return writeString("SESSION STATUS RESULT=I2P_ERROR MESSAGE=\"Unrecognized opcode\"\n");
 			}
 		} catch (DataFormatException e) {
-			_log.debug("Invalid destination specified");
+			if (_log.shouldLog(Log.DEBUG))
+				_log.debug("Invalid destination specified");
 			return writeString("SESSION STATUS RESULT=INVALID_KEY DESTINATION=" + dest + " MESSAGE=\"" + e.getMessage() + "\"\n");
 		} catch (I2PSessionException e) {
-			_log.debug("I2P error when instantiating session", e);
+			if (_log.shouldLog(Log.DEBUG))
+				_log.debug("I2P error when instantiating session", e);
 			return writeString("SESSION STATUS RESULT=I2P_ERROR DESTINATION=" + dest + " MESSAGE=\"" + e.getMessage() + "\"\n");
 		} catch (SAMException e) {
-			_log.info("Funny SAM error", e);
+			if (_log.shouldLog(Log.INFO))
+				_log.info("Funny SAM error", e);
 			return writeString("SESSION STATUS RESULT=I2P_ERROR DESTINATION=" + dest + " MESSAGE=\"" + e.getMessage() + "\"\n");
 		} catch (IOException e) {
 			_log.error("Unexpected IOException", e);
@@ -607,7 +637,8 @@ class SAMv3Handler extends SAMv1Handler
 
 		nick = props.getProperty("ID");
 		if (nick == null) {
-			_log.debug("SESSION ID parameter not specified");
+			if (_log.shouldLog(Log.DEBUG))
+				_log.debug("SESSION ID parameter not specified");
 			try {
 				notifyStreamResult(true, "I2P_ERROR", "ID not specified");
 			} catch (IOException e) {}
@@ -618,7 +649,8 @@ class SAMv3Handler extends SAMv1Handler
 		rec = sSessionsHash.get(nick);
 
 		if ( rec==null ) {
-			_log.debug("STREAM SESSION ID does not exist");
+			if (_log.shouldLog(Log.DEBUG))
+				_log.debug("STREAM SESSION ID does not exist");
 			try {
 				notifyStreamResult(true, "INVALID_ID", "STREAM SESSION ID does not exist");
 			} catch (IOException e) {}
@@ -628,7 +660,8 @@ class SAMv3Handler extends SAMv1Handler
 		streamSession = rec.getHandler().streamSession ;
 		
 		if (streamSession==null) {
-			_log.debug("specified ID is not a stream session");
+			if (_log.shouldLog(Log.DEBUG))
+				_log.debug("specified ID is not a stream session");
 			try {
 				notifyStreamResult(true, "I2P_ERROR",  "specified ID is not a STREAM session");
 			} catch (IOException e) {}
@@ -649,7 +682,8 @@ class SAMv3Handler extends SAMv1Handler
 		}
 		else
 		{
-			_log.debug ( "Unrecognized RAW message opcode: \""
+			if (_log.shouldLog(Log.DEBUG))
+				_log.debug ( "Unrecognized RAW message opcode: \""
 					+ opcode + "\"" );
 			try {
 				notifyStreamResult(true, "I2P_ERROR",  "Unrecognized RAW message opcode: "+opcode );
@@ -663,7 +697,8 @@ class SAMv3Handler extends SAMv1Handler
 		try {
 			if (props == null) {
 				notifyStreamResult(true,"I2P_ERROR","No parameters specified in STREAM CONNECT message");
-				_log.debug("No parameters specified in STREAM CONNECT message");
+				if (_log.shouldLog(Log.DEBUG))
+					_log.debug("No parameters specified in STREAM CONNECT message");
 				return false;
 			}
 			boolean verbose = props.getProperty("SILENT","false").equals("false");
@@ -671,7 +706,8 @@ class SAMv3Handler extends SAMv1Handler
 			String dest = props.getProperty("DESTINATION");
 			if (dest == null) {
 				notifyStreamResult(verbose, "I2P_ERROR", "Destination not specified in RAW SEND message");
-				_log.debug("Destination not specified in RAW SEND message");
+				if (_log.shouldLog(Log.DEBUG))
+					_log.debug("Destination not specified in RAW SEND message");
 				return false;
 			}
 			props.remove("DESTINATION");
@@ -680,19 +716,24 @@ class SAMv3Handler extends SAMv1Handler
 				((SAMv3StreamSession)streamSession).connect( this, dest, props );
 				return true ;
 			} catch (DataFormatException e) {
-				_log.debug("Invalid destination in STREAM CONNECT message");
+				if (_log.shouldLog(Log.DEBUG))
+					_log.debug("Invalid destination in STREAM CONNECT message");
 				notifyStreamResult ( verbose, "INVALID_KEY", null );
 			} catch (ConnectException e) {
-				_log.debug("STREAM CONNECT failed: " + e.getMessage());
+				if (_log.shouldLog(Log.DEBUG))
+					_log.debug("STREAM CONNECT failed", e);
 				notifyStreamResult ( verbose, "CONNECTION_REFUSED", null );
 			} catch (NoRouteToHostException e) {
-				_log.debug("STREAM CONNECT failed: " + e.getMessage());
+				if (_log.shouldLog(Log.DEBUG))
+					_log.debug("STREAM CONNECT failed", e);
 				notifyStreamResult ( verbose, "CANT_REACH_PEER", null );
 			} catch (InterruptedIOException e) {
-				_log.debug("STREAM CONNECT failed: " + e.getMessage());
+				if (_log.shouldLog(Log.DEBUG))
+					_log.debug("STREAM CONNECT failed", e);
 				notifyStreamResult ( verbose, "TIMEOUT", null );
 			} catch (I2PException e) {
-				_log.debug("STREAM CONNECT failed: " + e.getMessage());
+				if (_log.shouldLog(Log.DEBUG))
+					_log.debug("STREAM CONNECT failed", e);
 				notifyStreamResult ( verbose, "I2P_ERROR", e.getMessage() );
 			}
 		} catch (IOException e) {
@@ -708,7 +749,8 @@ class SAMv3Handler extends SAMv1Handler
 				notifyStreamResult( true, "OK", null );
 				return true ;
 			} catch (SAMException e) {
-				_log.debug("Forwarding STREAM connections failed: " + e.getMessage());
+				if (_log.shouldLog(Log.DEBUG))
+					_log.debug("Forwarding STREAM connections failed", e);
 				notifyStreamResult ( true, "I2P_ERROR", "Forwarding failed : " + e.getMessage() );
 			}
 		} catch (IOException e) {
@@ -725,13 +767,16 @@ class SAMv3Handler extends SAMv1Handler
 				((SAMv3StreamSession)streamSession).accept(this, verbose);
 				return true ;
 			} catch (InterruptedIOException e) {
-				_log.debug("STREAM ACCEPT failed: " + e.getMessage());
+				if (_log.shouldLog(Log.DEBUG))
+					_log.debug("STREAM ACCEPT failed", e);
 				notifyStreamResult( verbose, "TIMEOUT", e.getMessage() );
 			} catch (I2PException e) {
-				_log.debug("STREAM ACCEPT failed: " + e.getMessage());
+				if (_log.shouldLog(Log.DEBUG))
+					_log.debug("STREAM ACCEPT failed", e);
 				notifyStreamResult ( verbose, "I2P_ERROR", e.getMessage() );
 			} catch (SAMException e) {
-				_log.debug("STREAM ACCEPT failed: " + e.getMessage());
+				if (_log.shouldLog(Log.DEBUG))
+					_log.debug("STREAM ACCEPT failed", e);
 				notifyStreamResult ( verbose, "ALREADY_ACCEPTING", null );
 			}
 		} catch (IOException e) {
diff --git a/apps/sam/java/src/net/i2p/sam/SAMv3RawSession.java b/apps/sam/java/src/net/i2p/sam/SAMv3RawSession.java
index 9d9da0aee4..90eae76f93 100644
--- a/apps/sam/java/src/net/i2p/sam/SAMv3RawSession.java
+++ b/apps/sam/java/src/net/i2p/sam/SAMv3RawSession.java
@@ -57,7 +57,8 @@ class SAMv3RawSession extends SAMRawSession  implements SAMv3Handler.Session, SA
         
     	String portStr = props.getProperty("PORT") ;
     	if ( portStr==null ) {
-    		_log.debug("receiver port not specified. Current socket will be used.");
+		if (_log.shouldLog(Log.DEBUG))
+    			_log.debug("receiver port not specified. Current socket will be used.");
     		this.clientAddress = null;
     	}
     	else {
@@ -67,7 +68,8 @@ class SAMv3RawSession extends SAMRawSession  implements SAMv3Handler.Session, SA
     		if ( host==null ) {
     			host = rec.getHandler().getClientIP();
 
-    			_log.debug("no host specified. Taken from the client socket : " + host +':'+port);
+			if (_log.shouldLog(Log.DEBUG))
+	    			_log.debug("no host specified. Taken from the client socket : " + host +':'+port);
     		}
 
     	
diff --git a/apps/sam/java/src/net/i2p/sam/SAMv3StreamSession.java b/apps/sam/java/src/net/i2p/sam/SAMv3StreamSession.java
index 18bb40cd0e..45002e4cd2 100644
--- a/apps/sam/java/src/net/i2p/sam/SAMv3StreamSession.java
+++ b/apps/sam/java/src/net/i2p/sam/SAMv3StreamSession.java
@@ -25,7 +25,7 @@ import net.i2p.util.Log;
 import java.nio.channels.Channels;
 import java.nio.channels.ReadableByteChannel;
 import java.nio.channels.WritableByteChannel;
-import java.nio.ByteBuffer ;
+import java.nio.ByteBuffer;
 import java.nio.channels.SocketChannel;
 
 /**
@@ -97,7 +97,8 @@ class SAMv3StreamSession  extends SAMStreamSession implements SAMv3Handler.Sessi
 	        if (props.getProperty(I2PSocketOptions.PROP_CONNECT_TIMEOUT) == null)
 	            opts.setConnectTimeout(60 * 1000);
 
-	        _log.debug("Connecting new I2PSocket...");
+	        if (_log.shouldLog(Log.DEBUG))
+	            _log.debug("Connecting new I2PSocket...");
 
 	        // blocking connection (SAMv3)
 
@@ -142,7 +143,8 @@ class SAMv3StreamSession  extends SAMStreamSession implements SAMv3Handler.Sessi
 	    	synchronized( this.socketServerLock )
 	    	{
 	    		if (this.socketServer!=null) {
-	    			_log.debug("a socket server is already defined for this destination");
+	                	if (_log.shouldLog(Log.DEBUG))
+	   				_log.debug("a socket server is already defined for this destination");
 	    			throw new SAMException("a socket server is already defined for this destination");
 	    		}
 	    		this.socketServer = this.socketMgr.getServerSocket();
@@ -183,7 +185,8 @@ class SAMv3StreamSession  extends SAMStreamSession implements SAMv3Handler.Sessi
 	        
 	    	String portStr = props.getProperty("PORT") ;
 	    	if ( portStr==null ) {
-	    		_log.debug("receiver port not specified");
+	                if (_log.shouldLog(Log.DEBUG))
+	    			_log.debug("receiver port not specified");
 	    		throw new SAMException("receiver port not specified");
 	    	}
 	    	int port = Integer.parseInt(portStr);
@@ -191,14 +194,16 @@ class SAMv3StreamSession  extends SAMStreamSession implements SAMv3Handler.Sessi
 	    	String host = props.getProperty("HOST");
 	    	if ( host==null ) {
 	    		host = rec.getHandler().getClientIP();
-	    		_log.debug("no host specified. Taken from the client socket : " + host +':'+port);
+	                if (_log.shouldLog(Log.DEBUG))
+		    		_log.debug("no host specified. Taken from the client socket : " + host +':'+port);
 	    	}
 
 	    	
 	    	synchronized( this.socketServerLock )
 	    	{
 	    		if (this.socketServer!=null) {
-	    			_log.debug("a socket server is already defined for this destination");
+		                if (_log.shouldLog(Log.DEBUG))
+		    			_log.debug("a socket server is already defined for this destination");
 	    			throw new SAMException("a socket server is already defined for this destination");
     			}
 	    		this.socketServer = this.socketMgr.getServerSocket();
@@ -337,12 +342,14 @@ class SAMv3StreamSession  extends SAMStreamSession implements SAMv3Handler.Sessi
 	    	synchronized( this.socketServerLock )
 	    	{
 	    		if (this.socketServer==null) {
-	    			_log.debug("no socket server is defined for this destination");
+		                if (_log.shouldLog(Log.DEBUG))
+		    			_log.debug("no socket server is defined for this destination");
 	    			throw new SAMException("no socket server is defined for this destination");
     			}
 	    		server = this.socketServer ;
 	    		this.socketServer = null ;
-	    		_log.debug("nulling socketServer in stopForwardingIncoming. Object " + this );
+	                if (_log.shouldLog(Log.DEBUG))
+		    		_log.debug("nulling socketServer in stopForwardingIncoming. Object " + this );
 	    	}
 	    	try {
 	    		server.close();
-- 
GitLab