diff --git a/src/main/java/inputOutput/OpenIGTLinkConnection.java b/src/main/java/inputOutput/OpenIGTLinkConnection.java index 70e4deb..77a2e25 100644 --- a/src/main/java/inputOutput/OpenIGTLinkConnection.java +++ b/src/main/java/inputOutput/OpenIGTLinkConnection.java @@ -1,7 +1,6 @@ package inputOutput; import com.neuronrobotics.sdk.addons.kinematics.math.TransformNR; -import com.neuronrobotics.sdk.common.Log; import Jama.Matrix; @@ -45,10 +44,7 @@ public class OpenIGTLinkConnection implements IOpenIgtPacketListener { */ public OpenIGTLinkConnection() { try { - Log.enableDebugPrint(false); - Log.enableSystemPrint(false); - - Log.debug("Starting client"); + logger.log(Level.INFO, "Starting OIGTL client"); this.client = new GenericIGTLinkClient(this.ip, this.port); this.client.addIOpenIgtOnPacket(this); @@ -70,10 +66,7 @@ public OpenIGTLinkConnection(String ip, int port) { this.setPort(port); try { - Log.enableDebugPrint(false); - Log.enableSystemPrint(false); - - Log.debug("Starting client"); + logger.log(Level.INFO, "Starting OIGTL client"); this.client = new GenericIGTLinkClient(this.ip, this.port); client.addIOpenIgtOnPacket(this); @@ -166,7 +159,7 @@ public boolean isConnected() { // from networkconnection @Override public void onRxTransform(String name, TransformNR t) { - Log.debug("Received Transform: " + t); + logger.log(Level.FINE, "Received Transform: " + t); if (this.exit && this.stop) { @@ -195,19 +188,19 @@ public void onRxTransform(String name, TransformNR t) { if (name.equals("RegistrationTransform") || name.equals("CALIBRATION")) { // System.err.println("Received Registration Transform"); - Log.debug("Setting fiducial registration matrix: " + t); + logger.log(Level.FINE, "Setting fiducial registration matrix: " + t); return; } else if (name.equals("TARGET")) { // System.err.println("Received RAS Transform: TARGET"); - Log.debug("Setting task space pose: " + t); + logger.log(Level.FINE, "Setting task space pose: " + t); } else if (name.equals("myTransform")) { // System.err.println("Received Transformation Matrix: myTransform"); - Log.debug("Setting task space pose: " + t); + logger.log(Level.FINE, "Setting task space pose: " + t); } else { // System.err.println("Received unidentified transform matrix"); - Log.debug("Setting task space pose: " + t); + logger.log(Level.FINE, "Setting task space pose: " + t); } } diff --git a/src/main/java/mainMethod/App.java b/src/main/java/mainMethod/App.java index d1040d0..710792d 100644 --- a/src/main/java/mainMethod/App.java +++ b/src/main/java/mainMethod/App.java @@ -2,6 +2,10 @@ import java.io.IOException; import java.util.ArrayList; +import java.util.logging.ConsoleHandler; +import java.util.logging.FileHandler; +import java.util.logging.Level; +import java.util.logging.Logger; import javafx.application.Application; import javafx.fxml.FXMLLoader; @@ -12,7 +16,7 @@ import util.CustomLogger; public class App extends Application { - + static Logger logger = Logger.getLogger(Application.class.getName()); @Override public void start(Stage primaryStage) throws IOException { @@ -23,6 +27,10 @@ public void start(Stage primaryStage) throws IOException { e.printStackTrace(); } + Thread.setDefaultUncaughtExceptionHandler((thread, throwable) -> { + logger.log(Level.SEVERE, "Uncaught exception in thread " + thread.getName(), throwable); + }); + String path = "/view/MainView.fxml"; FXMLLoader loader = new FXMLLoader(); loader.setLocation(getClass().getResource(path)); diff --git a/src/main/java/org/medcare/igtl/messages/ImageMessage.java b/src/main/java/org/medcare/igtl/messages/ImageMessage.java index 0846136..daa9296 100644 --- a/src/main/java/org/medcare/igtl/messages/ImageMessage.java +++ b/src/main/java/org/medcare/igtl/messages/ImageMessage.java @@ -16,11 +16,12 @@ package org.medcare.igtl.messages; -import com.neuronrobotics.sdk.common.Log; import org.medcare.igtl.util.BytesArray; import org.medcare.igtl.util.Header; import java.util.Arrays; +import java.util.logging.Level; +import java.util.logging.Logger; /** * ** This class create an Image object from bytes received or help to generate @@ -29,6 +30,7 @@ * @author Andre Charles Legendre */ public class ImageMessage extends OpenIGTMessage { + static Logger logger = Logger.getLogger(ImageMessage.class.getName()); public static final int TYPE_INT8 = 2; public static final int TYPE_UINT8 = 3; @@ -595,7 +597,7 @@ public double[][] getMatrix() { public void setMatrix(double[][] matrix) { this.matrix = matrix; - Log.debug(Arrays.toString(matrix)); + logger.log(Level.FINE, "Matrix: " + Arrays.toString(matrix)); norm_i = new double[3]; norm_j = new double[3]; norm_k = new double[3]; diff --git a/src/main/java/org/medcare/igtl/messages/OpenIGTMessage.java b/src/main/java/org/medcare/igtl/messages/OpenIGTMessage.java index 00eac20..7822281 100644 --- a/src/main/java/org/medcare/igtl/messages/OpenIGTMessage.java +++ b/src/main/java/org/medcare/igtl/messages/OpenIGTMessage.java @@ -16,10 +16,12 @@ package org.medcare.igtl.messages; -import com.neuronrobotics.sdk.common.Log; import org.medcare.igtl.util.BytesArray; import org.medcare.igtl.util.Header; +import java.util.logging.Level; +import java.util.logging.Logger; + //import org.medcare.igtl.util.CrcException; //import com.neuronrobotics.sdk.common.ByteList; @@ -31,6 +33,7 @@ **/ public abstract class OpenIGTMessage { + static Logger logger = Logger.getLogger(OpenIGTMessage.class.getName()); // ------------------------------------------------------------------------ public String deviceName; private byte[] body; @@ -77,7 +80,7 @@ public boolean Unpack() throws Exception { if (getBody().length > 0 //&& !isBodyUnpacked ) { - Log.debug("Unpacking message..."); + logger.log(Level.FINE, "Unpacking message..."); //if (header.getCrc() == bytesArray.crc64(body, body.length, 0L)) { isBodyUnpacked = unpackBody(); return isBodyUnpacked; diff --git a/src/main/java/org/medcare/igtl/messages/TransformMessage.java b/src/main/java/org/medcare/igtl/messages/TransformMessage.java index 8fd4265..a82bfb5 100644 --- a/src/main/java/org/medcare/igtl/messages/TransformMessage.java +++ b/src/main/java/org/medcare/igtl/messages/TransformMessage.java @@ -16,10 +16,12 @@ package org.medcare.igtl.messages; -import com.neuronrobotics.sdk.common.Log; import org.medcare.igtl.util.BytesArray; import org.medcare.igtl.util.Header; +import java.util.logging.Level; +import java.util.logging.Logger; + //import com.neuronrobotics.sdk.common.ByteList; /** @@ -29,6 +31,7 @@ * @author Andre Charles Legendre */ public class TransformMessage extends OpenIGTMessage { + static Logger logger = Logger.getLogger(TransformMessage.class.getName()); public static int IGTL_TRANSFORM_SIZE = 48; @@ -97,7 +100,8 @@ public TransformMessage(String deviceName, double[] positionAray, double[][] rot public boolean unpackBody() throws Exception { //Log.debug("Unpacking Transform Body.."); transformData = new byte[IGTL_TRANSFORM_SIZE]; - Log.debug("Body size: " + getBody().length + " date size: " + transformData.length); + + logger.log(Level.FINE,"Body size: " + getBody().length + " date size: " + transformData.length); System.arraycopy(getBody(), 0, transformData, 0, IGTL_TRANSFORM_SIZE); setTransformData(transformData); @@ -425,7 +429,6 @@ public void printDoubleDataArray(double[][] matrixArray) { for (int j = 0; j < matrixArray[0].length; j++) { System.out.print(matrixArray[i][j] + " "); } - Log.debug("\n"); } } } diff --git a/src/main/java/org/medcare/igtl/network/GenericIGTLinkClient.java b/src/main/java/org/medcare/igtl/network/GenericIGTLinkClient.java index 2c369f3..c24c554 100644 --- a/src/main/java/org/medcare/igtl/network/GenericIGTLinkClient.java +++ b/src/main/java/org/medcare/igtl/network/GenericIGTLinkClient.java @@ -11,9 +11,12 @@ import java.util.ArrayList; import java.util.LinkedList; import java.util.Queue; +import java.util.logging.Level; +import java.util.logging.Logger; public class GenericIGTLinkClient extends OpenIGTClient implements IOpenIgtPacketListener { + static Logger logger = Logger.getLogger(GenericIGTLinkClient.class.getName()); ArrayList listeners = new ArrayList(); Sender s = new Sender(); @@ -25,7 +28,7 @@ public void error(String message, Exception exception, int errorCode) { System.err.println(message); } }); - Log.debug("GenericIGTLinkClient started"); + logger.log(Level.FINE, "Starting GenericIGTLinkClient"); s.start(); } @@ -158,7 +161,7 @@ public void stopClient() { interrupt(); while (isConnected()) ; - Log.debug("Stopped IGTLink client"); + logger.log(Level.FINE, "Stopped GenericIGTLinkClient"); } @@ -251,7 +254,7 @@ public void run() { //take out a message from Queue and send it try { if (messageQueue.size() > 0) { - Log.debug("Number of messages in Queue = " + messageQueue.size()); + logger.log(Level.FINE, "Number of messages in Queue = " + messageQueue.size()); } OpenIGTMessage msg = messageQueue.poll(); if (msg != null) { @@ -263,14 +266,13 @@ public void run() { } catch (Exception e) { if (!messageQueue.isEmpty()) { messageQueue.clear(); //clear message queue if ws not able to send as that is a connection problem - Log.debug("Clearing Message Sender Queue ; Number of messages in Queue = " + messageQueue.size()); + logger.log(Level.FINE, "Clearing Message Sender Queue ; Number of messages in Queue = " + messageQueue.size()); } // TODO Auto-generated catch block e.printStackTrace(); } } - Log.debug("Stopped IGTLink client Sender"); - + logger.log(Level.FINE, "Stopped IGTLink client Sender"); } } } diff --git a/src/main/java/org/medcare/igtl/network/GenericIGTLinkServer.java b/src/main/java/org/medcare/igtl/network/GenericIGTLinkServer.java index ae156e4..3d6f9a2 100644 --- a/src/main/java/org/medcare/igtl/network/GenericIGTLinkServer.java +++ b/src/main/java/org/medcare/igtl/network/GenericIGTLinkServer.java @@ -10,9 +10,12 @@ import java.util.ArrayList; import java.util.LinkedList; import java.util.Queue; +import java.util.logging.Level; +import java.util.logging.Logger; public class GenericIGTLinkServer extends OpenIGTServer implements IOpenIgtPacketListener { + static Logger logger = Logger.getLogger(GenericIGTLinkServer.class.getName()); public Sender s = new Sender(); ArrayList listeners = new ArrayList(); @@ -236,7 +239,7 @@ public void run() { while (getServerThread() == null || getServerThread().isAlive() == false) { if (!messageQueue.isEmpty()) { messageQueue.clear(); //keep cleaning the message Queue till Clinet connects - Log.debug("Clearing Message Sender Queue ; Number of messages in Queue = " + messageQueue.size()); + logger.log(Level.FINE, "Clearing Message Sender Queue ; Number of messages in Queue = " + messageQueue.size()); } try { Thread.sleep(200); @@ -255,7 +258,7 @@ public void run() { //take out a message from Queue and send it try { if (messageQueue.size() > 0) { - Log.debug("Number of messages in Queue = " + messageQueue.size()); + logger.log(Level.FINE, "Number of messages in Queue = " + messageQueue.size()); } OpenIGTMessage msg = messageQueue.poll(); if (msg != null) { @@ -267,7 +270,7 @@ public void run() { } catch (Exception e) { if (!messageQueue.isEmpty()) { messageQueue.clear(); //clear message queue if ws not able to send as that is a connection problem - Log.debug("Clearing Message Sender Queue ; Number of messages in Queue = " + messageQueue.size()); + logger.log(Level.FINE, "Clearing Message Sender Queue ; Number of messages in Queue = " + messageQueue.size()); } // TODO Auto-generated catch block e.printStackTrace(); diff --git a/src/main/java/org/medcare/igtl/network/GenericMessageNodeHandler.java b/src/main/java/org/medcare/igtl/network/GenericMessageNodeHandler.java index c2e6337..7b1c1c4 100644 --- a/src/main/java/org/medcare/igtl/network/GenericMessageNodeHandler.java +++ b/src/main/java/org/medcare/igtl/network/GenericMessageNodeHandler.java @@ -2,12 +2,14 @@ import com.neuronrobotics.sdk.addons.kinematics.math.RotationNR; import com.neuronrobotics.sdk.addons.kinematics.math.TransformNR; -import com.neuronrobotics.sdk.common.Log; import org.medcare.igtl.messages.*; import org.medcare.igtl.util.Header; -public class GenericMessageNodeHandler { +import java.util.logging.Level; +import java.util.logging.Logger; +public class GenericMessageNodeHandler { + static Logger logger = Logger.getLogger(GenericMessageNodeHandler.class.getName()); public OpenIGTMessage openIGTMessage; public OpenIGTMessage perform(String messageType, Header head, byte[] body, IOpenIgtPacketListener node) throws Exception { @@ -16,7 +18,7 @@ public OpenIGTMessage perform(String messageType, Header head, byte[] body, IOpe //TODO - GSF: Need to add complete set of new IGTLInk commands for BRP robot //http://wiki.ncigt.org/index.php/P41:Prostate:BRP:MRI_New_BRP_OpenIGTLink_Protocol_2012_Mar //Should support both TRANSFORM and QTRANSFORM packets - Log.debug("Perform messageType : " + messageType); + logger.log(java.util.logging.Level.FINE, "perform messageType : " + messageType); if (messageType.equals("TRANSFORM")) { openIGTMessage = new TransformMessage(head, body); TransformMessage transform = (TransformMessage) openIGTMessage; @@ -33,7 +35,7 @@ public OpenIGTMessage perform(String messageType, Header head, byte[] body, IOpe // Position vector and rotation matrix from the received transform node.getTxTransform(openIGTMessage.getDeviceName()); } else if (messageType.equals("POSITION") || messageType.equals("MOVE_TO")) { - Log.debug("perform POSITION"); + logger.log(java.util.logging.Level.FINE, "perform POSITION"); openIGTMessage = new PositionMessage(head, body); PositionMessage transform = (PositionMessage) openIGTMessage; transform.Unpack(); @@ -50,7 +52,7 @@ public OpenIGTMessage perform(String messageType, Header head, byte[] body, IOpe node.onRxImage(openIGTMessage.getDeviceName(), imgMesg); } else if (messageType.equals("ARRAY")) { - Log.error("This method is not complete"); + logger.log(Level.SEVERE, "This method is not complete"); DataArrayMessage datMesg = new DataArrayMessage(head, body); openIGTMessage = datMesg; node.onRxDataArray(openIGTMessage.getDeviceName(), datMesg.getDataMatrix());// this is a non functional stub @@ -82,7 +84,7 @@ public OpenIGTMessage perform(String messageType, Header head, byte[] body, IOpe node.onRxNDArray(openIGTMessage.getDeviceName(), ndArrayMsg.get1DFloatData());// this is a non functional stub } else { - Log.debug("Message Type : " + messageType + " not implemented"); + logger.log(Level.WARNING, "Message Type : " + messageType + " not implemented"); return null; } return openIGTMessage; diff --git a/src/main/java/org/medcare/igtl/network/MessageQueueManager.java b/src/main/java/org/medcare/igtl/network/MessageQueueManager.java index e115e19..5278a7f 100644 --- a/src/main/java/org/medcare/igtl/network/MessageQueueManager.java +++ b/src/main/java/org/medcare/igtl/network/MessageQueueManager.java @@ -16,11 +16,12 @@ package org.medcare.igtl.network; -import com.neuronrobotics.sdk.common.Log; import org.medcare.igtl.util.CrcException; import org.medcare.igtl.util.ErrorManager; import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.logging.Level; +import java.util.logging.Logger; /** * One MessageQueueManager is created by each ServerThread to add MessageHandler @@ -31,6 +32,7 @@ * @author Andre Charles Legendre */ public class MessageQueueManager extends Thread { + static Logger logger = Logger.getLogger(MessageQueueManager.class.getName()); private static String VERSION = "0.1a"; private long sleep; @@ -74,7 +76,7 @@ public void run() { e.printStackTrace(); errorManager.error("PB messageHandler ", e, ErrorManager.MESSAGE_EXCEPTION); } finally { - Log.debug("MessageQueueManager messageHandler.performRequest OK"); + logger.log(Level.FINE, "MessageQueueManager messageHandler.performRequest OK"); } } else { diff --git a/src/main/java/org/medcare/igtl/network/OpenIGTClient.java b/src/main/java/org/medcare/igtl/network/OpenIGTClient.java index 4531a1b..3fd31e9 100644 --- a/src/main/java/org/medcare/igtl/network/OpenIGTClient.java +++ b/src/main/java/org/medcare/igtl/network/OpenIGTClient.java @@ -16,7 +16,6 @@ package org.medcare.igtl.network; -import com.neuronrobotics.sdk.common.Log; import org.medcare.igtl.messages.OpenIGTMessage; import org.medcare.igtl.util.ErrorManager; import org.medcare.igtl.util.Header; @@ -28,6 +27,8 @@ import java.io.OutputStream; import java.net.UnknownHostException; import java.util.Arrays; +import java.util.logging.Level; +import java.util.logging.Logger; /** *

@@ -41,6 +42,7 @@ */ public abstract class OpenIGTClient extends Thread { + static Logger logger = Logger.getLogger(OpenIGTClient.class.getName()); public ErrorManager errorManager; private SocketFactory socketFactory; private java.net.Socket socket = null; @@ -77,7 +79,7 @@ public OpenIGTClient(String host, int port, ErrorManager errorManager) throws Un try { this.socket = socketFactory.createSocket(host, port); } catch (Exception e) { - Log.debug("exception happened"); + logger.log(Level.SEVERE, "OpenIGTClient Exception while creating socket", e); e.printStackTrace(); return; } @@ -88,7 +90,7 @@ public OpenIGTClient(String host, int port, ErrorManager errorManager) throws Un this.outstr = socket.getOutputStream(); this.instr = socket.getInputStream(); this.start(); - Log.debug("Client connected and ready"); + logger.log(Level.FINE, "OpenIGTClient Client connected and ready"); } /*************************************************************************** @@ -97,14 +99,14 @@ public OpenIGTClient(String host, int port, ErrorManager errorManager) throws Un * **************************************************************************/ public void run() { - Log.debug("Starting client Thread"); + logger.log(Level.FINE, "OpenIGTClient Starting client Thread"); try { int ret_read = 0; byte[] headerBuff = new byte[Header.LENGTH]; // Log.debug("JE LIS"); do { ret_read = instr.read(headerBuff); - Log.debug("Size of Header ::" + ret_read + "\n"); + logger.log(Level.FINE, "OpenIGTClient Size of Header: " + ret_read); if (ret_read > 0) { // System.out.print(new String(buff, 0, ret_read)); @@ -113,11 +115,11 @@ public void run() { int size = (int) header.getBody_size(); byte[] bodyBuf = new byte[size]; - Log.debug("Size of Packet ::" + size); + logger.log(Level.FINE, "Size of Packet ::" + size); int currentPos = 0; while (true) { ret_read = instr.read(bodyBuf, currentPos, size); - Log.debug("Ret Read " + ret_read); + logger.log(Level.FINE, "OpenIGTClient Size of Body: " + ret_read); size = size - ret_read; currentPos = currentPos + ret_read; if (size == 0) @@ -129,7 +131,7 @@ public void run() { } } } while (isConnected() && ret_read >= 0); - Log.debug("Client thread exited! Connected state=" + isConnected() + " ret_read = " + ret_read); + logger.log(Level.FINE, "OpenIGTClient Client thread exited! Connected state=" + isConnected() + " ret_read = " + ret_read); } catch (UnknownHostException e) { errorManager.error("OpenIGTClient Don't know about host" + host, e, ErrorManager.OPENIGTCLIENT_UNKNOWNHOST_EXCEPTION); } catch (IOException e) { @@ -168,7 +170,7 @@ public void sendMessage(OpenIGTMessage message) throws Exception { public void sendMessage(Header header, byte[] body) throws Exception { sendBytes(header.getBytes()); sendBytes(body); - Log.debug("Client Sending Message: Header=" + logger.log(Level.FINE, "Client Sending Message: Header=" + header.toString() + " Body=" + Arrays.toString(body)); } diff --git a/src/main/java/org/medcare/igtl/network/OpenIGTServer.java b/src/main/java/org/medcare/igtl/network/OpenIGTServer.java index ab54547..0d05b06 100644 --- a/src/main/java/org/medcare/igtl/network/OpenIGTServer.java +++ b/src/main/java/org/medcare/igtl/network/OpenIGTServer.java @@ -19,7 +19,6 @@ Edited By Nirav Patel(napatel@wpi.edu) on Aug 10 2013 package org.medcare.igtl.network; -import com.neuronrobotics.sdk.common.Log; import com.neuronrobotics.sdk.util.ThreadUtil; import org.medcare.igtl.messages.OpenIGTMessage; import org.medcare.igtl.util.ErrorManager; @@ -28,6 +27,8 @@ Edited By Nirav Patel(napatel@wpi.edu) on Aug 10 2013 import javax.net.ServerSocketFactory; import java.io.IOException; import java.net.ServerSocket; +import java.util.logging.Level; +import java.util.logging.Logger; /** @@ -42,6 +43,7 @@ Edited By Nirav Patel(napatel@wpi.edu) on Aug 10 2013 */ public abstract class OpenIGTServer { + static Logger logger = Logger.getLogger(OpenIGTServer.class.getName()); public ErrorManager errorManager; ServerSocket socket = null; private ServerThread thread; @@ -73,12 +75,12 @@ public OpenIGTServer(int port, ErrorManager errorManager) throws Exception { } public void startServer(int port) throws IOException { - Log.debug("Starting IGTLink Server"); + logger.log(Level.FINE, "Starting IGTLink Server"); stopServer(); try { ServerSocketFactory serverSocketFactory = ServerSocketFactory.getDefault(); socket = serverSocketFactory.createServerSocket(this.port); - Log.debug("Server Socket created"); + logger.log(Level.FINE, "Server Socket created"); } catch (IOException e) { errorManager.error("OpenIGTServer Could not listen on port: " + this.port, e, ErrorManager.OPENIGTSERVER_IO_EXCEPTION); @@ -101,7 +103,7 @@ public void startListening(int port) { } public void stopServer() { - Log.debug("Stopping the IGTLink Server"); + logger.log(Level.FINE, "Stopping IGTLink Server"); if (getServerThread() != null) getServerThread().interrupt(); if (socket != null) { @@ -109,7 +111,7 @@ public void stopServer() { socket.close(); currentStatus = ServerStatus.STOPPED; socket = null; - Log.debug("IGTLink Server stopped"); + logger.log(Level.FINE, "IGTLink Server stopped"); } catch (IOException e) { e.printStackTrace(); } @@ -124,7 +126,7 @@ public void run() { while (getKeepAlive()) { try { while (socket == null || socket.isClosed()) { - Log.debug("IGTLink Server Died, restarting"); + logger.log(Level.FINE, "IGTLink Server Socket is null or closed, restarting"); try { startServer(port); } catch (IOException e) { @@ -136,12 +138,12 @@ public void run() { startIGT(); //ThreadUtil.wait(500); - Log.debug("Before waiting for another client, waiting for currnt client to get disconnected"); + logger.log(Level.FINE, "Before waiting for another client, waiting for current client to get disconnected"); while (getServerThread().getAlive() != false) { //wait here until client gets disconnected ThreadUtil.wait(500); } - Log.debug("IGTLink client Disconnected"); + logger.log(Level.FINE, "IGTLink Client disconnected."); //currentStatus = ServerStatus.DISCONNECTED; } catch (Exception e1) { // TODO Auto-generated catch block @@ -162,13 +164,13 @@ private void startIGT() throws IOException, Exception { try { currentStatus = ServerStatus.LISTENING; - Log.debug("IGTLink Server Waiting for connection"); + logger.log(Level.FINE, "IGTLink Server Waiting for connection on port " + socket.getLocalPort() + "..."); setServerThread(new ServerThread(socket.accept(), this)); getServerThread().start(); currentStatus = ServerStatus.CONNECTED; - Log.debug("IGTLink client connected"); + logger.log(Level.FINE, "IGTLink client connected"); } catch (Exception e) { - Log.error("Server died while waiting for client"); + logger.log(Level.SEVERE, "IGTLink Server Exception while waiting for client", e); e.printStackTrace(); } } @@ -192,7 +194,7 @@ public void sendMessage(OpenIGTMessage message) throws Exception { //Log.info("Pushing upstream IGTLink packet "+message); } else { - Log.debug("No clients connected"); + logger.log(Level.FINE, "IGTLink Server No clients connected"); } } diff --git a/src/main/java/org/medcare/igtl/network/RequestQueueManager.java b/src/main/java/org/medcare/igtl/network/RequestQueueManager.java index e9e1b2e..5fcb6d8 100644 --- a/src/main/java/org/medcare/igtl/network/RequestQueueManager.java +++ b/src/main/java/org/medcare/igtl/network/RequestQueueManager.java @@ -16,10 +16,11 @@ package org.medcare.igtl.network; -import com.neuronrobotics.sdk.common.Log; import org.medcare.igtl.util.ErrorManager; import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.logging.Level; +import java.util.logging.Logger; /** * RequestQueueManager create its own OpenIGTClient connected to one @@ -29,6 +30,7 @@ * @author Andre Charles Legendre */ public class RequestQueueManager extends Thread { + static Logger logger = Logger.getLogger(RequestQueueManager.class.getName()); private static String VERSION = "0.1a"; private long sleep; @@ -71,7 +73,7 @@ public void run() { } catch (Exception e) { errorManager.error("RequestQueueManager openIGTClient.sendBytes ", e, ErrorManager.REQUEST_EXCEPTION); } finally { - Log.debug("RequestQueueManager SendBytes OK"); + logger.log(Level.FINE, "RequestQueueManager SendBytes OK"); } } else { errorManager.error("PB openIGTClient openIGTRequest null", new Exception("sendBytes error"), ErrorManager.REQUEST_ERROR); diff --git a/src/main/java/org/medcare/igtl/network/ResponseQueueManager.java b/src/main/java/org/medcare/igtl/network/ResponseQueueManager.java index 9e5f09d..9af166e 100644 --- a/src/main/java/org/medcare/igtl/network/ResponseQueueManager.java +++ b/src/main/java/org/medcare/igtl/network/ResponseQueueManager.java @@ -16,11 +16,12 @@ package org.medcare.igtl.network; -import com.neuronrobotics.sdk.common.Log; import org.medcare.igtl.util.CrcException; import org.medcare.igtl.util.ErrorManager; import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.logging.Level; +import java.util.logging.Logger; /** * Each OpenIGTClient create its own ResponseQueueManager to queue response they @@ -33,6 +34,7 @@ * @author Andre Charles Legendre */ public class ResponseQueueManager extends Thread { + static Logger logger = Logger.getLogger(ResponseQueueManager.class.getName()); private static String VERSION = "0.1a"; private long sleep; @@ -72,7 +74,7 @@ public void run() { } catch (Exception e) { errorManager.error("PB responseHandler ", e, ErrorManager.RESPONSE_EXCEPTION); } finally { - Log.debug("ResponseQueueManager ResponseHandler.performResponse OK"); + logger.log(Level.FINE, "ResponseQueueManager ResponseHandler.performResponse OK"); } } else { errorManager.error("ResponseQueueManager responseHandler null", new Exception("performResponse error"), ErrorManager.RESPONSE_ERROR); diff --git a/src/main/java/org/medcare/igtl/network/ServerThread.java b/src/main/java/org/medcare/igtl/network/ServerThread.java index 97b7810..06552ab 100644 --- a/src/main/java/org/medcare/igtl/network/ServerThread.java +++ b/src/main/java/org/medcare/igtl/network/ServerThread.java @@ -16,7 +16,6 @@ package org.medcare.igtl.network; -import com.neuronrobotics.sdk.common.Log; import org.medcare.igtl.messages.OpenIGTMessage; import org.medcare.igtl.network.OpenIGTServer.ServerStatus; import org.medcare.igtl.util.ErrorManager; @@ -26,6 +25,8 @@ import java.io.InputStream; import java.io.OutputStream; import java.util.Arrays; +import java.util.logging.Level; +import java.util.logging.Logger; //import org.medcare.igtl.util.BytesArray; @@ -38,6 +39,7 @@ * @author Andre Charles Legendre */ public class ServerThread extends Thread { + static Logger logger = Logger.getLogger(ServerThread.class.getName()); private java.net.Socket socket = null; private OpenIGTServer openIGTServer; private OutputStream outstr; @@ -105,7 +107,7 @@ public void run() { outstr.close(); instr.close(); socket.close(); - Log.debug("IGTLink client got disconnected, will set alive=false to inform SocketServer"); + logger.log(Level.FINE, "IGTLink client got disconnected, will set alive=false to inform SocketServer"); } catch (IOException e) { e.printStackTrace(); errorManager.error("ServerThread IOException", e, ErrorManager.SERVERTHREAD_IO_EXCEPTION); @@ -122,7 +124,7 @@ public void sendMessage(OpenIGTMessage message) throws Exception { public void sendMessage(Header header, byte[] body) throws Exception { sendBytes(header.getBytes()); sendBytes(body); - Log.debug("Sending Message: Header=" + header.toString() + logger.log(Level.FINE, "Sending Message: Header=" + header.toString() + " Body=" + Arrays.toString(body)); } diff --git a/src/main/java/org/medcare/igtl/util/Header.java b/src/main/java/org/medcare/igtl/util/Header.java index e9983ed..7feedcd 100644 --- a/src/main/java/org/medcare/igtl/util/Header.java +++ b/src/main/java/org/medcare/igtl/util/Header.java @@ -16,7 +16,8 @@ package org.medcare.igtl.util; -import com.neuronrobotics.sdk.common.Log; +import java.util.logging.Level; +import java.util.logging.Logger; /** * ** For reading/writing OpenIGTLink headers @@ -25,6 +26,7 @@ **/ public class Header { + static Logger logger = Logger.getLogger(Header.class.getName()); public static int LENGTH = 58; int base = 1000000000; /* 10^9 */ long version; // unsigned int 16bits @@ -138,7 +140,7 @@ public Header(byte[] bytes) { bytesArray = new BytesArray(); bytesArray.putBytes(bytes); populate(); - Log.debug("New header: " + this); + logger.log(Level.FINE, "New header: " + this); } private void populate() { diff --git a/src/main/java/org/medcare/igtl/util/Tests.java b/src/main/java/org/medcare/igtl/util/Tests.java index c218cc2..31101d6 100644 --- a/src/main/java/org/medcare/igtl/util/Tests.java +++ b/src/main/java/org/medcare/igtl/util/Tests.java @@ -16,9 +16,10 @@ package org.medcare.igtl.util; -import com.neuronrobotics.sdk.common.Log; import java.math.BigInteger; +import java.util.logging.Level; +import java.util.logging.Logger; /** * ** Utility to test util library/BytesArray crc64 and timestamp utilities and @@ -28,7 +29,7 @@ **/ public class Tests { - + static Logger logger = Logger.getLogger(Tests.class.getName()); // ------------------------------------------------------------------------ /** @@ -47,19 +48,19 @@ public static void main(String[] args) { .println("Display the crc array calculated from Coefficient corresponding to ECMA-182 polynome"); BytesArray tool = new BytesArray(); double nombre = 15678.345 / 10000.0; - Log.debug("Tests: double [15678.345/10000] " + nombre); + logger.log(Level.FINE, "Tests: double [15678.345/10000] " + nombre); long entier = (long) nombre; long reste = (long) ((nombre - entier) * 1000); System.out .println("Tests: extract seconds and fraction from double [15678.345/10000] " + nombre + " seconds " + entier + " fraction " + reste); - Log.debug("Tests: encodeLong [1,224,false]"); + logger.log(Level.FINE, "Tests: encodeLong [1,224,false]"); byte[] bytes = tool.encodeLong(1, 224, false); - Log.debug("Tests: length [" + bytes.length + "]"); - Log.debug("Tests: decodeLong [" + logger.log(Level.FINE, "Tests: length [" + bytes.length + "]"); + logger.log(Level.FINE, "Tests: decodeLong [" + tool.decodeLong(bytes, 0, false) + "]"); - Log.debug("Tests: encodeLong [1,224,true]"); + logger.log(Level.FINE, "Tests: encodeLong [1,224,true]"); bytes = tool.encodeLong(1, 224, true); bytes = new byte[]{(byte) 0x80}; System.out @@ -89,14 +90,14 @@ public static void main(String[] args) { .toUpperCase()); long res = tool.decodeLong(bytes, 0, true); - Log.debug("Long res : \t" + res); - Log.debug("encodeTimeStamp now : "); + logger.log(Level.FINE, "Long res : \t" + res); + logger.log(Level.FINE, "encodeTimeStamp now:"); bytes = tool.encodeTimeStamp(System.currentTimeMillis()); bi = new BigInteger(bytes); - Log.debug("Tests: toString [" + bi.toString() + "]"); - Log.debug("Tests: longValue [" + bi.shortValue() + "]"); - Log.debug("Tests: bi.toString(2) [" + bi.toString(2) + "]"); + logger.log(Level.FINE, "Tests: toString [" + bi.toString() + "]"); + logger.log(Level.FINE, "Tests: longValue [" + bi.shortValue() + "]"); + logger.log(Level.FINE, "Tests: bi.toString(2) [" + bi.toString(2) + "]"); } } diff --git a/src/main/java/sample/ClientSample.java b/src/main/java/sample/ClientSample.java index ed36ad8..5ca02bc 100644 --- a/src/main/java/sample/ClientSample.java +++ b/src/main/java/sample/ClientSample.java @@ -2,13 +2,16 @@ import Jama.Matrix; import com.neuronrobotics.sdk.addons.kinematics.math.TransformNR; -import com.neuronrobotics.sdk.common.Log; import org.medcare.igtl.messages.ImageMessage; import org.medcare.igtl.network.GenericIGTLinkClient; import org.medcare.igtl.network.IOpenIgtPacketListener; import org.medcare.igtl.util.Status; +import java.util.logging.Level; +import java.util.logging.Logger; + public class ClientSample implements IOpenIgtPacketListener { + static Logger logger = Logger.getLogger(ClientSample.class.getName()); /** * @param args */ @@ -18,10 +21,7 @@ public static void main(String[] args) { ClientSample.parseXMLStringMessage(msg); GenericIGTLinkClient client; try { - Log.enableDebugPrint(true); - Log.enableSystemPrint(true); - - Log.debug("Starting client"); + logger.log(Level.FINE, "Connecting to server"); client = new GenericIGTLinkClient("127.0.0.1", 18944); client.addIOpenIgtOnPacket(new ClientSample()); @@ -29,7 +29,7 @@ public static void main(String[] args) { Thread.sleep(1000); } client.stopClient(); - Log.debug("Client disconnected"); + logger.log(Level.FINE, "Client disconnected"); System.exit(0); } catch (Exception e) { @@ -48,19 +48,19 @@ public void onRxTransform(String name, TransformNR t) { //Log.debug("Received Transform: "+t); if (name.equals("RegistrationTransform") || name.equals("CALIBRATION")) { System.err.println("Received Registration Transform"); - Log.debug("Setting fiducial registration matrix: " + t); + logger.log(Level.FINE, "Setting fiducial registration matrix: " + t); return; } else if (name.equals("TARGET")) { System.err.println("Received RAS Transform: TARGET"); - Log.debug("Setting task space pose: " + t); + logger.log(Level.FINE, "Setting task space pose: " + t); } else if (name.equals("myTransform")) { System.err.println("Received Transformation Matrix: myTransform"); - Log.debug("Setting task space pose: " + t); + logger.log(Level.FINE, "Setting task space pose: " + t); } else { System.err.println("Received unidentified transform matrix"); - Log.debug("Setting task space pose: " + t); + logger.log(Level.FINE, "Setting task space pose: " + t); } } @@ -113,9 +113,9 @@ public void onTxNDArray(String name) { @Override public void onRxNDArray(String name, float[] data) { // TODO Auto-generated method stub - Log.debug("Name" + name); + logger.log(Level.FINE, "Received RxNDArray: " + name); for (int i = 0; i < data.length; i++) { - Log.debug("Data[" + i + "]=" + data[i]); + logger.log(Level.FINE, "Data[" + i + "]=" + (double) data[i]); } } } diff --git a/src/main/java/sample/ServerSample.java b/src/main/java/sample/ServerSample.java index 64d0e27..e5f9034 100644 --- a/src/main/java/sample/ServerSample.java +++ b/src/main/java/sample/ServerSample.java @@ -2,7 +2,6 @@ import Jama.Matrix; import com.neuronrobotics.sdk.addons.kinematics.math.TransformNR; -import com.neuronrobotics.sdk.common.Log; import org.medcare.igtl.messages.ImageMessage; import org.medcare.igtl.messages.TransformMessage; import org.medcare.igtl.network.GenericIGTLinkServer; @@ -15,9 +14,11 @@ import javax.xml.parsers.DocumentBuilderFactory; import java.io.StringReader; import java.util.HashMap; +import java.util.logging.Level; +import java.util.logging.Logger; public class ServerSample implements IOpenIgtPacketListener { - + static Logger logger = Logger.getLogger(ServerSample.class.getName()); HashMap IGTData = null; public ServerSample() { @@ -33,8 +34,6 @@ public ServerSample() { public static void main(String[] args) { GenericIGTLinkServer server; - Log.enableDebugPrint(true); - Log.enableSystemPrint(true); try { //Set up server @@ -47,7 +46,7 @@ public static void main(String[] args) { Thread.sleep(100); } - Log.debug("Pushing packet"); + logger.log(Level.FINE, "Pushing packet"); //Create an identify matrix TransformNR t = new TransformNR(); @@ -68,7 +67,7 @@ public static void main(String[] args) { rotation[0][1] = Math.random(); server.sendMessage(new TransformMessage("TGT_001", position, rotation)); } else { - Log.debug("Wait"); + logger.log(Level.FINE, "Waiting..."); } } } catch (Exception e) { @@ -80,23 +79,23 @@ public static void main(String[] args) { @Override public void onRxTransform(String name, TransformNR t) { - Log.debug("Received Transform with name: " + name + "and transform:" + t); + logger.log(Level.FINE, "Received Transform with name: " + name + "and transform:" + t); if (name.equals("RegistrationTransform") || name.equals("CALIBRATION")) { System.err.println("Received Registration Transform"); - Log.debug("Setting fiducial registration matrix: " + t); + logger.log(Level.FINE, "Setting fiducial registration matrix: " + t); return; } else if (name.equals("TARGET")) { System.err.println("Received RAS Transform: TARGET"); - Log.debug("Setting task space pose: " + t); + logger.log(Level.FINE, "Setting task space pose: " + t); } else if (name.equals("myTransform")) { System.err.println("Received Transformation Matrix: myTransform"); - Log.debug("Setting task space pose: " + t); + logger.log(Level.FINE, "Setting task space pose: " + t); } else { System.err.println("Received unidentified transform matrix"); - Log.debug("Setting task space pose: " + t); + logger.log(Level.FINE, "Setting task space pose: " + t); } } @@ -192,9 +191,9 @@ public void onTxNDArray(String name) { @Override public void onRxNDArray(String name, float[] data) { // TODO Auto-generated method stub - Log.debug("Name" + name); + logger.log(Level.FINE, "Received NDArray with name: " + name); for (int i = 0; i < data.length; i++) { - Log.debug("Data[" + i + "]=" + (double) data[i]); + logger.log(Level.FINE, "Data[" + i + "]=" + (double) data[i]); } } diff --git a/src/main/java/util/CustomLogger.java b/src/main/java/util/CustomLogger.java index 6c3a694..962a54e 100644 --- a/src/main/java/util/CustomLogger.java +++ b/src/main/java/util/CustomLogger.java @@ -1,11 +1,7 @@ package util; import java.io.IOException; -import java.util.logging.ConsoleHandler; -import java.util.logging.FileHandler; -import java.util.logging.Handler; -import java.util.logging.Logger; -import java.util.logging.SimpleFormatter; +import java.util.logging.*; public class CustomLogger { private static FileHandler txtFile; @@ -18,10 +14,13 @@ public class CustomLogger { */ public static void setup() throws IOException { Logger log = Logger.getLogger(""); - txtFile = new FileHandler("logging.log", true); + txtFile = new FileHandler("logging.log",true); formatter = new SimpleFormatter(); txtFile.setFormatter(formatter); + log.addHandler(txtFile); + log.addHandler(new ConsoleHandler()); + log.setLevel(Level.FINE); } /**