Skip to content

Commit

Permalink
add code to log the messages with more description of the exception
Browse files Browse the repository at this point in the history
  • Loading branch information
Rohit Suthar committed Jul 20, 2019
1 parent 7732021 commit e55ea39
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 24 deletions.
9 changes: 6 additions & 3 deletions src/com/chatroom/Database/createdb.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package com.chatroom.Database;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import com.chatroom.configuration.Config;
import com.chatroom.others.LogFileWriter;
import com.chatroom.server.Server;


public class createdb {

static StringWriter errors = new StringWriter();
public static void main(String[] args) {
Connection connection = null;
java.sql.Statement statement= null;
Expand Down Expand Up @@ -42,8 +45,8 @@ public static void main(String[] args) {
try {
connection.close(); //close the database connection
} catch (SQLException e) {
// TODO Auto-generated catch block
LogFileWriter.Log(e.getMessage());
e.printStackTrace(new PrintWriter(errors));
LogFileWriter.Log(errors.toString());
}
}
}
Expand Down
27 changes: 19 additions & 8 deletions src/com/chatroom/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.Thread.State;
import java.net.Socket;
import java.util.Scanner;
Expand All @@ -14,6 +16,7 @@
import com.chatroom.others.Hash;
import com.chatroom.others.LogFileWriter;
import com.chatroom.others.Message;
import com.chatroom.server.Server;

public class Client {
private int clientID=-1;
Expand All @@ -29,11 +32,13 @@ public class Client {
private Request request = null;
private Response response = null;
private MessageListener messageListener;
private static StringWriter errors;

public Client(String host, int port) {
this.host = host;
this.port = port;
messageListener = new MessageListener();
errors = new StringWriter();

//create the log file if it is not present
String path = System.getProperty("user.home");
Expand All @@ -47,8 +52,8 @@ public Client(String host, int port) {
try {
f.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
e.printStackTrace(new PrintWriter(errors));
LogFileWriter.Log(errors.toString());
}
}
}
Expand All @@ -60,7 +65,8 @@ public void connect() {
objectInputStream = new ObjectInputStream(socket.getInputStream());
mainFunc();
} catch (IOException e) {
LogFileWriter.Log(e.getMessage());
e.printStackTrace(new PrintWriter(errors));
LogFileWriter.Log(errors.toString());
}
}

Expand Down Expand Up @@ -92,7 +98,8 @@ private void mainOptions() {
}
}
catch (Exception e) {
LogFileWriter.Log(e.getMessage());
e.printStackTrace(new PrintWriter(errors));
LogFileWriter.Log(errors.toString());
}
}

Expand Down Expand Up @@ -136,7 +143,8 @@ private void viewRooms() throws Exception{
}
}
catch(Exception e) {
LogFileWriter.Log(e.getMessage());
e.printStackTrace(new PrintWriter(errors));
LogFileWriter.Log(errors.toString());
}
}

Expand Down Expand Up @@ -238,10 +246,12 @@ else if(response.getId() == Response.Type.LOGOUT.ordinal()) {
}

} catch (ClassNotFoundException | IOException e) {
LogFileWriter.Log(e.getMessage());
e.printStackTrace(new PrintWriter(errors));
LogFileWriter.Log(errors.toString());
break;
} catch (InterruptedException e) {
LogFileWriter.Log(e.getMessage());
e.printStackTrace(new PrintWriter(errors));
LogFileWriter.Log(errors.toString());
}
}
}
Expand Down Expand Up @@ -307,7 +317,8 @@ else if(response.getId() == Request.Type.LOGIN.ordinal()) {

}
catch(Exception e) {
LogFileWriter.Log(e.getMessage());
e.printStackTrace(new PrintWriter(errors));
LogFileWriter.Log(errors.toString());
}
}
}
9 changes: 7 additions & 2 deletions src/com/chatroom/others/Hash.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package com.chatroom.others;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import com.chatroom.server.Server;

public class Hash {
static StringWriter errors = new StringWriter();
public static String getHash(String input) {
String hash;
try {
Expand All @@ -17,8 +22,8 @@ public static String getHash(String input) {
}
return hash;
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
e.printStackTrace(new PrintWriter(errors));
LogFileWriter.Log(errors.toString());
return null;
}

Expand Down
4 changes: 3 additions & 1 deletion src/com/chatroom/server/ClientThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.net.Socket;

import com.chatroom.models.Request;
Expand Down Expand Up @@ -39,7 +40,8 @@ public void run()
}

} catch (IOException | ClassNotFoundException e) {
LogFileWriter.Log(e.getMessage());
e.printStackTrace(new PrintWriter(Server.errors));
LogFileWriter.Log(Server.errors.toString());
if( e.getClass() == java.io.EOFException.class )
{
// if client exited the terminal itself
Expand Down
29 changes: 20 additions & 9 deletions src/com/chatroom/server/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.Connection;
Expand Down Expand Up @@ -242,7 +244,8 @@ public void run()
}
catch( Exception e)
{
LogFileWriter.Log(e.getMessage());
e.printStackTrace(new PrintWriter(Server.errors));
LogFileWriter.Log(Server.errors.toString());
}
}

Expand Down Expand Up @@ -290,7 +293,8 @@ public void run()
responseHolder.objectOutputStream.flush();
}
catch (Exception e) {
LogFileWriter.Log(e.getMessage());
e.printStackTrace(new PrintWriter(Server.errors));
LogFileWriter.Log(Server.errors.toString());
}
}
}
Expand Down Expand Up @@ -403,7 +407,8 @@ public void run()
}
catch( Exception e )
{
LogFileWriter.Log(e.getMessage());
e.printStackTrace(new PrintWriter(Server.errors));
LogFileWriter.Log(Server.errors.toString());
}
finally
{
Expand Down Expand Up @@ -431,12 +436,14 @@ public void run()

}
catch(Exception e) {
LogFileWriter.Log(e.getMessage());
e.printStackTrace(new PrintWriter(Server.errors));
LogFileWriter.Log(Server.errors.toString());
}
}
}
catch (Exception e) {
LogFileWriter.Log(e.getMessage());
e.printStackTrace(new PrintWriter(Server.errors));
LogFileWriter.Log(Server.errors.toString());
}
}
}
Expand Down Expand Up @@ -480,7 +487,7 @@ public class Server {
static HashMap<Integer, Integer> messagesTrackHashmap;
static ServerOperations serverOperations;
static int roomIdGenerator = 0;

static StringWriter errors;
public Server(int port)
{
this.port = port;
Expand All @@ -501,12 +508,14 @@ public Server(int port)
messagesTrackQueue = new PriorityQueue<MessageTrackObject>(100,new MessagesTrackComparator());
serverOperations = new ServerOperations();
serverOperations.start();
errors = new StringWriter();

try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(Config.DATABASE_URL+"/"+Config.DATABASE_NAME,Config.USER_NAME,Config.USER_PWD);
} catch (SQLException | ClassNotFoundException e) {
LogFileWriter.Log(e.getMessage());
e.printStackTrace(new PrintWriter(Server.errors));
LogFileWriter.Log(Server.errors.toString());
}
}

Expand Down Expand Up @@ -538,7 +547,8 @@ public static String getClientNameFromId(int id) {

} catch (SQLException e) {
// TODO Auto-generated catch block
LogFileWriter.Log(e.getMessage());
e.printStackTrace(new PrintWriter(Server.errors));
LogFileWriter.Log(Server.errors.toString());
return null;
}

Expand All @@ -563,7 +573,8 @@ public void connect() {

}
} catch (IOException e) {
LogFileWriter.Log(e.getMessage());
e.printStackTrace(new PrintWriter(Server.errors));
LogFileWriter.Log(Server.errors.toString());
}
}

Expand Down
1 change: 0 additions & 1 deletion src/com/chatroom/server/ServerOperations.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public void run() {
int input;
while(true) {
Message.println("1. For Messages logs");
//TODO : kitne user or rooms hai or queue ki size kitni hai
Message.println("2. Server Shutdown");
input = scanner.nextInt();
if(input == 1) {
Expand Down

0 comments on commit e55ea39

Please sign in to comment.