Commit 7fe0badc authored by Myriam Begel's avatar Myriam Begel

Logging with java.util.logging

parent aab780e4
package core.abilities;
import logging.Logging;
import java.util.logging.Logger;
/**
* Created by dupriez on 23/12/15.
*
* This class is there to contain the method "initialisePackage" (see its description)
*/
public class AbilityPackageInitialiser {
/**
* To be called at the beginning of the program. Make the JVM load some classes of the "ability" package, so that
* the initialisation of these classes is done as early as possible, because it may throw exceptions if there are
......@@ -15,12 +18,13 @@ public class AbilityPackageInitialiser {
* AbilityRoster.
*/
public static void initialisePackage() {
System.out.println("Initialising package abilities...");
Logger LOGGER = new Logging().getLogger();
LOGGER.info("Initialising package abilities...");
AbilityFactory.getInstance();
for (AbilityKey unregisteredAbilityKey : AbilityFactory.getInstance().checkAbilityKeyRegisteringExhaustivity()) {
System.out.println("* Warning: AbilityKey not registered in AbilityFactory: \"" + unregisteredAbilityKey.name() + "\"");
}
System.out.println("...package abilities initialised");
LOGGER.info("...package abilities initialised");
}
}
......@@ -4,9 +4,10 @@ import core.abilities.AbilityPackageInitialiser;
import core.abilities.effects.EffectPackageInitialiser;
import graphics.graphical_abstraction.GraphicsMasterAbstraction;
// Log system
import org.apache.log4j.Logger;
import org.apache.log4j.BasicConfigurator;
//Log system
import logging.Logging;
import java.util.logging.Logger;
/**
* Created by dupriez on 10/11/15.
......@@ -16,15 +17,13 @@ import org.apache.log4j.BasicConfigurator;
*/
public class Igniter_Main {
static Logger logger = Logger.getLogger(Igniter_Main.class);
static Logger logger = new Logging().getLogger();
public static void main(String[] args) {
logger.debug("msg de debogage");
logger.info("msg d'information");
logger.warn("msg d'avertissement");
logger.error("msg d'erreur");
logger.fatal("msg d'erreur fatale");
logger.info("msg d'information");
logger.warning("msg d'avertissement");
logger.severe("msg d'erreur");
/** Initialise some packages to detect instantly uncorrect declarations **/
AbilityPackageInitialiser.initialisePackage();
......
package logging;
import java.io.IOException;
import java.io.Serializable;
import java.util.logging.*;
/**
* Created by myriam on 01/01/16.
*/
public class Logging implements Serializable{
static final long serialVersionUID = 1L;
private static Logger LOG;
private Handler fh;
private Handler ch;
public Logging(){
LOG = Logger.getLogger("Donjon");
LOG.setLevel(Level.INFO);
try{
fh = new FileHandler("log/Logging.log",true);
fh.setFormatter(new SimpleFormatter());
LOG.addHandler(fh);
ch = new ConsoleHandler();
ch.setFormatter(new SimpleFormatter());
LOG.addHandler(ch);
} catch (SecurityException | IOException e){
LOG.severe("Impossible d'accéder au fichier de log");
}
}
public Logger getLogger(){
return LOG;
}
}
......@@ -31,11 +31,11 @@ public class ClientConnection extends NetworkConnection<NetworkObject>{
//catch (IOException e) {
// System.out.println("network: IO excception thrown in ClientConnection.close(), could also be a case of UnknownHostException");
//}
System.out.println("networks:ClientConnection initialized");
LOGGER.info("ClientConnection initialized");
}
catch (IOException e)
{
System.out.println("network: ClientConnection constructor - IOException detected");
LOGGER.warning(e.toString());
}
}
......@@ -49,7 +49,7 @@ public class ClientConnection extends NetworkConnection<NetworkObject>{
public synchronized void sendEvent(NetworkObject e)
{
if (e==null)
System.out.println("network: Please do not attempt to send null events!");
LOGGER.warning("Please do not attempt to send null events!");
else {
if (notSerializable(e))
throw new AssertionError("networks: Please don't try to use unserializable objects");
......@@ -57,8 +57,10 @@ public class ClientConnection extends NetworkConnection<NetworkObject>{
outputStream.writeObject(e);
} catch (IOException e1) {
if (e1 instanceof NotSerializableException)
System.out.println("NOTSERIALIZABLE!!!! network: NotSerializableObject!!!!!!!!!");
System.out.println("network: IO excception thrown in ClientConnection.sendEvent()" + e1);
LOGGER.severe("Not SerializableObject");
//System.out.println("NOTSERIALIZABLE!!!! network: NotSerializableObject!!!!!!!!!");
LOGGER.warning(e1.toString());
//System.out.println("network: IO excception thrown in ClientConnection.sendEvent()" + e1);
}
}
}
......@@ -74,7 +76,8 @@ public class ClientConnection extends NetworkConnection<NetworkObject>{
//we need to flush so that we ensure that the server receives the EndEvent
socket.getOutputStream().flush();
} catch (IOException e) {
e.printStackTrace();
LOGGER.warning(e.toString());
//e.printStackTrace();
}
// try {
// if (outputStream!=null)
......
package network;
import logging.Logging;
import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;
import java.net.SocketException;
import java.util.logging.Logger;
/**
* Thread which receive incoming data on the client.
......@@ -16,6 +19,7 @@ public class ClientInputThread extends Thread {
private Socket socket = null;
private NetworkConnection<?> parent;
private boolean serverside;
private static Logger LOGGER = new Logging().getLogger();
/**
* Initialization of the class.
......@@ -36,7 +40,7 @@ public class ClientInputThread extends Thread {
* Finally close the socket.
*/
@Override
public void run() {
public void run() {
try {
ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
......@@ -47,16 +51,16 @@ public class ClientInputThread extends Thread {
//need to push event into the Event queue, but only if it is not an EndEvent
if (command instanceof EndEvent) {
socket.close();
System.out.println("network:ClientInputThread ending its life naturally");
LOGGER.info("ClientInputThread ending its life naturally");
break;
}
parent.addIncomingEvent(command);
parent.addIncomingEvent(command);
}
} catch (SocketException e1) {
//this is here because of server threads, as of now the connection closure is asymmetric
System.out.println("network: A socket has been closed on the other end, corresponding thread is useless and ends its life");
LOGGER.info("A socket has been closed on the other end, corresponding thread is useless and ends its life");
//Append an end event! This will only have an effect on the client side, since on the server one if you receive an endevent
//the thread stops executing
......@@ -66,23 +70,23 @@ public class ClientInputThread extends Thread {
}
} catch (EOFException e2){
//this is here because of the client thread, see above
System.out.println("network: A socket has been closed on the other end, corresponding thread is useless and ends its life");
LOGGER.info("A socket has been closed on the other end, corresponding thread is useless and ends its life");
EndEvent endEvent=new EndEvent();
parent.addIncomingEvent(endEvent);
} catch(ClassNotFoundException e1){
//this is needed in order to correctly transmit objects through the network - both server and client must contain class definitions
System.out.println("network:Problem with package inclusions in package network:class ClientINThread");
LOGGER.warning("Problem with package inclusions");
} catch(Exception e2){
System.out.println("network:ClientInputThread grave exception " + e2);
LOGGER.severe(e2.toString());
}
finally
{
System.out.println("network: ClientInputThread dying...");
LOGGER.info("ClientInputThread dying");
try {
socket.close();
} catch (IOException e1) {
System.out.println("network:Socket cannot be closed in thread " + this.getId());
LOGGER.warning("Socket cannot be closed in thread "+ this.getId());
}
}
}
......
package network;
import logging.Logging;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.logging.Logger;
/**
* This is the thread for a unique client where events are send by the server.
......@@ -17,11 +20,12 @@ import java.util.concurrent.LinkedBlockingDeque;
public class ClientOutputThread extends Thread{
private BlockingDeque<NetworkObject> outgoingEvents=new LinkedBlockingDeque<>();
private Socket socket = null;
private Logger LOGGER = new Logging().getLogger();
//private ServerConnection parent;
/**
* @param serverConnection
*/
* @param serverConnection
*/
public ClientOutputThread(Socket socket, ServerConnection<?> serverConnection) {
this.socket = socket;
//parent=serverConnection;
......@@ -43,31 +47,31 @@ public class ClientOutputThread extends Thread{
* It takes the first event in the queue so the older and send it to the client via the socket.
*/
@Override
public void run() {
public void run() {
try {
ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
try {
while (true)
{
System.out.println("ClientOutputThread running!!!");
LOGGER.info("ClientOutputThread running");
//retrieves and removes first element from the outgoing queue, blocking if necessary
NetworkObject event=outgoingEvents.takeFirst();
outputStream.writeObject(event);
System.out.println("Object written!!!"+event);
LOGGER.info("Object written "+event);
}
} catch (InterruptedException e) {
System.out.println("Problem with interrupted exception in network:ClientOutputThreadClass");
LOGGER.severe("Problem with interrupted exception in network:ClientOutputThreadClass");
}
} catch (IOException e1) {
System.out.println("network:ClientInputThread - IOException");
LOGGER.warning("ClientInputThread - IOException");
} finally {
System.out.println("network:ClientOutputThread dying...");
LOGGER.info("ClientOutputThread dying");
try {
socket.close();
} catch (IOException e1) {
System.out.println("network:Socket cannot be closed in thread " + this.getId());
LOGGER.warning("Socket cannot be closed in thread " + this.getId());
}
}
}
......
......@@ -36,23 +36,23 @@ public class LocalConnection<E extends NetworkObject> extends NetworkConnection<
try {
outgoingEvents.putLast(endEvent);
} catch (InterruptedException e) {
e.printStackTrace();
LOGGER.warning("endEvent not send");
}
}
@Override
public void sendEvent(E event) {
if (event == null)
System.out.println("networks:Please do not attempt to send null events!");
LOGGER.warning("Please do not attempt to send null events!");
else
{
if (notSerializable(event))
throw new AssertionError("networks: Please don't try to use unserializable objects");
try {
outgoingEvents.putLast(event);
outgoingEvents.putLast(event);
} catch (InterruptedException e) {
System.out.println("network: Interrupted exception in LocalConnection():sendEvent");
LOGGER.warning(e.toString());
}
}
}
......@@ -72,11 +72,11 @@ public class LocalConnection<E extends NetworkObject> extends NetworkConnection<
//this means that there are no events in the queue, we return null
return null;
}
}
}
try {
return (E) incomingEvents.takeFirst();
} catch (InterruptedException e) {
System.out.println("network : Exception in addIncomingEvent");
LOGGER.warning(e.toString());
return null;
}
}
......
package network;
import logging.Logging;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.NoSuchElementException;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Abstract class for all types of connection.
......@@ -15,7 +19,7 @@ import java.util.concurrent.LinkedBlockingDeque;
*/
public abstract class NetworkConnection<E extends NetworkObject> {
protected BlockingDeque<NetworkObject> incomingEvents=new LinkedBlockingDeque<>();
protected static Logger LOGGER = new Logging().getLogger();
/**
* Function for testing whether an object is Serializable or not
......@@ -28,7 +32,7 @@ public abstract class NetworkConnection<E extends NetworkObject> {
new ObjectOutputStream(new ByteArrayOutputStream()).writeObject(event);
} catch (IOException e) {
//Our Object is not Serializable!
System.out.println("networks: notSerializable method raised exception"+e);
LOGGER.severe(e.toString());
return true;
}
return false;
......@@ -58,11 +62,11 @@ public abstract class NetworkConnection<E extends NetworkObject> {
try {
incomingEvents.putLast(event);
} catch (InterruptedException e) {
System.out.println("Exception in addIncomingEvent");
LOGGER.warning(e.toString());
}
}
/**
/**LOGGER.info(e);
* Receive new event.
* Only read and return the first event of incomingEvents
* This is a blocking call, we can modify the blocking properties if necessary
......@@ -71,20 +75,20 @@ public abstract class NetworkConnection<E extends NetworkObject> {
*/
//making this method synchronized would be a grave error, since incoming events might induce a block inside this method
public E receiveEvent(boolean blocking) {
if (blocking==false) {
try {
return (E)incomingEvents.removeFirst();
} catch (NoSuchElementException e) {
//this means that there are no events in the queue, we return null
return null;
}
}
try {
return (E)incomingEvents.takeFirst();
} catch (InterruptedException e) {
System.out.println("Exception in addIncomingEvent");
return null;
}
if (blocking==false) {
try {
return (E)incomingEvents.removeFirst();
} catch (NoSuchElementException e) {
//this means that there are no events in the queue, we return null
return null;
}
}
try {
return (E)incomingEvents.takeFirst();
} catch (InterruptedException e) {
LOGGER.warning(e.toString());
return null;
}
}
}
package network;
import logging.Logging;
import java.io.Serializable;
import java.util.logging.Logger;
/**
* Created by bogdanbear on 11/12/2015.
*/
public interface NetworkObject extends Serializable {
Logger LOGGER = new Logging().getLogger();
}
......@@ -13,7 +13,7 @@ public class ServerConnection<E extends NetworkObject> extends NetworkConnection
static ServerSocket serverSocket=null;
private boolean stillRunning=true;
//this blocking deque stores all the events received from the clients, in a consistent order
//TODO uses the two following variable
/*
private BlockingDeque<NetworkObject> incomingEvents=new LinkedBlockingDeque<>();
......@@ -52,7 +52,7 @@ public class ServerConnection<E extends NetworkObject> extends NetworkConnection
@Override
public void run()
{
System.out.println("ServerConnection: LateJoining thread is online!");
LOGGER.info("LateJoining thread is online!");
while (isStillRunning())
{
Socket socket= null;
......@@ -65,10 +65,10 @@ public class ServerConnection<E extends NetworkObject> extends NetworkConnection
clientOutputThread.start();
clientInputThread.start();
} catch (IOException e) {
System.out.println("IO exception in LateJoining thread" + e);
LOGGER.warning(e.toString());
}
}
System.out.println("ServerConnection: LateJoining thread dies peacefully!");
LOGGER.info("LateJoining thread dies peacefully");
}
}
......@@ -84,7 +84,7 @@ public class ServerConnection<E extends NetworkObject> extends NetworkConnection
public ServerConnection(int PORT, int numberOfClients, boolean disableLateJoining){
try {
serverSocket=new ServerSocket(PORT);
System.out.println("Waiting for clients...");
LOGGER.info("Waiting for clients ...");
int nr=0;
while(nr<numberOfClients)
{
......@@ -96,7 +96,7 @@ public class ServerConnection<E extends NetworkObject> extends NetworkConnection
//create new thread and start it
clientOutputThread.start();
clientInputThread.start();
System.out.println("First client in!");
LOGGER.info(nr+" clients in");
nr++;
}
if (disableLateJoining==false)
......@@ -105,7 +105,7 @@ public class ServerConnection<E extends NetworkObject> extends NetworkConnection
serverListeningThread.start();
}
} catch (IOException e) {
System.out.println("IO exception when instantiating Server connection class"+e);
LOGGER.warning(e.toString());
}
}
......@@ -120,7 +120,7 @@ public class ServerConnection<E extends NetworkObject> extends NetworkConnection
@Override
public synchronized void sendEvent(E event) {
if (event==null)
System.out.println("networks:Please do not attempt to send null events!");
LOGGER.warning("Please do not attempt to send null events!");
else {
if (notSerializable(event))
throw new AssertionError("networks: Please don't try to use unserializable objects");
......@@ -128,7 +128,7 @@ public class ServerConnection<E extends NetworkObject> extends NetworkConnection
for (int i = 0; i < outputThreads.size(); i++)
outputThreads.get(i).getOutgoingQueue().putLast(event);
} catch (InterruptedException e) {
System.out.println("network: Interrupted exception in initiateConnectionEnd()");
LOGGER.warning(e.toString());
}
}
......@@ -148,7 +148,7 @@ public class ServerConnection<E extends NetworkObject> extends NetworkConnection
for (int i=0;i<outputThreads.size();i++)
outputThreads.get(i).getOutgoingQueue().putLast(endEvent);
} catch (InterruptedException e) {
System.out.println("network: Interrupted exception in initiateConnectionEnd()");
LOGGER.warning(e.toString());
}
}
......@@ -161,15 +161,15 @@ public class ServerConnection<E extends NetworkObject> extends NetworkConnection
public synchronized void sendEventToSpecificClient(NetworkObject event, int id)
{
if (event==null)
System.out.println("networks:Please do not attempt to send null events!");
LOGGER.warning("Please do not attempt to send null events!");
else {
if (id >= outputThreads.size())
System.out.println("networks: Bad Client Id - client does not exist");
LOGGER.severe("Bad Client Id "+id+" - client does not exist");
else
try {
outputThreads.get(id).getOutgoingQueue().putLast(event);
} catch (InterruptedException e) {
System.out.println("network: Interrupted exception in initiateConnectionEnd()");
LOGGER.warning(e.toString());
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment