Skip to content

Commit

Permalink
Start logging work
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Lord authored and Sam Lord committed Jan 22, 2016
1 parent e74bbe6 commit e3605e0
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/main/java/chat/haver/server/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public String getName() {
public Object getToken() {return token;}

public void setName(final String name) {
if (Main.DEBUG && !(validName(name))) {System.err.println("Client:setName() : Invalid name");} // TODO: null check and fix error message
if (!(validName(name))) Logger.warning("Invalid name"); // TODO: null check and fix error message
this.name = name;
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/chat/haver/server/Location.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public static Location fromJSON(final JSONObject jsonObject) {

return new Location(latitude, longitude, accuracy);
} catch(Exception e) {
System.err.println("Location:fromJSON : " + e.getMessage());
Logger.warning(e.getMessage());
return null;
}
}
Expand All @@ -104,7 +104,7 @@ public double distanceBetween(final Location location) {
Math.sin(dLong/2) * Math.sin(dLong/2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double d = EARTH_RADIUS * c * 1000;
System.out.println("Locations are " + d + " meters apart.");
Logger.info("Locations are " + d + " meters apart.");
return d;
}

Expand Down
63 changes: 63 additions & 0 deletions src/main/java/chat/haver/server/Logger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package chat.haver.server;

import java.util.Date;

/**
* Created by azertify on 22/01/2016.
*/
public class Logger {

public static void info(String message) {
out(format(message, "info"));
}

public static void warning(String message) {
err(format(message, "warning"));
}

public static void sever(String message) {
err(format(message, "sever"));
}

private static String format(String message, String type) {
Date date = new Date();
StackTraceElement ste = Thread.currentThread().getStackTrace()[3];
String[] arr = ste.getClassName().split("\\.");
String classDetails = arr[arr.length] + ":" + ste.getMethodName();
if (type.equalsIgnoreCase("warning") || type.equalsIgnoreCase("sever")) {
return "[" + date.toString() + "] " + type.toUpperCase() + " (" + classDetails + ") " + message;
} else {
return "[" + date.toString() + "] " + type.toUpperCase() + " " + message;
}
}

private static void out(String message) {
switch(Main.ENVIRONMENT) {
case DEVELOPMENT:
System.out.println(message);
break;
case TESTING:
System.out.println(message);
break;
case PRODUCTION:
System.out.println(message); // TODO: Sort out production logging
break;
}
}

private static void err(String message) {
switch(Main.ENVIRONMENT) {
case DEVELOPMENT:
System.err.println(message);
break;
case TESTING:
System.out.println(message);
System.err.println(message);
break;
case PRODUCTION:
System.out.println(message);
System.err.println(message);
break;
}
}
}
10 changes: 7 additions & 3 deletions src/main/java/chat/haver/server/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ public String toString() {
}
}

private Main(){}
/**
* Empty private constructor to prevent creation of Main.
*/
private Main() {
}

/**
* Parses command line arguments then creates and runs a {@link Router server}.
Expand All @@ -39,9 +43,9 @@ public static void main(final String[] args) {
try {
Router router = new Router(host, port);
router.start();
System.out.println("Hosting new server on: " + router.getAddress());
Logger.info("Hosting new server on: " + router.getAddress());
} catch (UnknownHostException e) {
System.err.println("Invalid host: " + host + ':' + port);
Logger.sever("Invalid host: " + host + ':' + port);
}
}

Expand Down

0 comments on commit e3605e0

Please sign in to comment.