Skip to content

Commit

Permalink
Unify logging (#21)
Browse files Browse the repository at this point in the history
* Unify logging

Unifies logging to remove the dependency on com.neuronrobotics.sdk.common.Log and to use the console and file logger as default.

* Removed ThreadUtil dependency
  • Loading branch information
TheRisenPhoenix authored Sep 11, 2023
1 parent 38b0375 commit c225a62
Show file tree
Hide file tree
Showing 19 changed files with 142 additions and 113 deletions.
21 changes: 7 additions & 14 deletions src/main/java/inputOutput/OpenIGTLinkConnection.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package inputOutput;

import com.neuronrobotics.sdk.addons.kinematics.math.TransformNR;
import com.neuronrobotics.sdk.common.Log;

import Jama.Matrix;

Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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) {

Expand Down Expand Up @@ -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);
}
}

Expand Down
10 changes: 9 additions & 1 deletion src/main/java/mainMethod/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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));
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/org/medcare/igtl/messages/ImageMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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];
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/org/medcare/igtl/messages/OpenIGTMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,6 +33,7 @@
**/

public abstract class OpenIGTMessage {
static Logger logger = Logger.getLogger(OpenIGTMessage.class.getName());
// ------------------------------------------------------------------------
public String deviceName;
private byte[] body;
Expand Down Expand Up @@ -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;
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/org/medcare/igtl/messages/TransformMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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;

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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");
}
}
}
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/org/medcare/igtl/network/GenericIGTLinkClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<IOpenIgtPacketListener> listeners = new ArrayList<IOpenIgtPacketListener>();
Sender s = new Sender();

Expand All @@ -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();

}
Expand Down Expand Up @@ -158,7 +161,7 @@ public void stopClient() {
interrupt();
while (isConnected()) ;

Log.debug("Stopped IGTLink client");
logger.log(Level.FINE, "Stopped GenericIGTLinkClient");
}


Expand Down Expand Up @@ -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) {
Expand All @@ -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");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<IOpenIgtPacketListener> listeners = new ArrayList<IOpenIgtPacketListener>();

Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
Expand All @@ -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();
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
Loading

0 comments on commit c225a62

Please sign in to comment.