added logging module to server
This commit is contained in:
parent
0f64d99b36
commit
8dcccc86ba
4 changed files with 105 additions and 13 deletions
|
|
@ -29,7 +29,7 @@ import java.util.logging.Logger;
|
|||
* Which accepts incoming connections from client.
|
||||
*/
|
||||
public class JobServServer {
|
||||
private static final Logger logger = Logger.getLogger(JobServServer.class.getName());
|
||||
public static SimpleLogger logger = new SimpleLogger("JobServ-Server-");
|
||||
|
||||
private Server server;
|
||||
private ProcessManager manager;
|
||||
|
|
@ -54,12 +54,12 @@ public class JobServServer {
|
|||
private void start() throws IOException {
|
||||
// TODO: this should be passed in from a configuration manager
|
||||
server.start();
|
||||
logger.info("Server initialized!");
|
||||
logger.write("Server initialized!");
|
||||
|
||||
Runtime.getRuntime().addShutdownHook(new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
logger.info("Shutting down server");
|
||||
logger.write("Shutting down server");
|
||||
manager.shutdown();
|
||||
JobServServer.this.stop();
|
||||
}
|
||||
|
|
@ -125,7 +125,7 @@ public class JobServServer {
|
|||
return;
|
||||
}
|
||||
|
||||
System.out.println("JobServ Server Initialized! Connect anytime...");
|
||||
JobServServer.logger.write("Initialized JobServ Server");
|
||||
server.blockUntilShutdown();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -131,7 +131,7 @@ class ProcessController {
|
|||
*/
|
||||
public void kill() {
|
||||
if (this.killedManually) {
|
||||
System.err.println("[~] Tried to kill already killed process");
|
||||
JobServServer.logger.write("Tried to kill already killed process");
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -143,7 +143,8 @@ class ProcessController {
|
|||
this.process.destroy();
|
||||
this.killedManually = true;
|
||||
} catch (IOException e) {
|
||||
System.err.println("[-] Killing process failed: " + e.getMessage());
|
||||
JobServServer.logger.write("Killing process " +
|
||||
String.valueOf(this.pid) + " failed: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -77,7 +77,8 @@ class ProcessManager {
|
|||
return newProc.getPid();
|
||||
|
||||
} catch (IOException e) {
|
||||
System.err.println("Couldnt Spawn New Command: " + e.getMessage());
|
||||
JobServServer.logger.write("Couldnt Spawn New Command: (" +
|
||||
command + "): " + e.getMessage());
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
|
@ -99,7 +100,8 @@ class ProcessManager {
|
|||
|
||||
} catch (TimeoutException e) {
|
||||
// lock could not be grabbed before timeout
|
||||
System.err.println("Timeout getting process status: " + e.getMessage());
|
||||
JobServServer.logger.write("Timeout getting process " +
|
||||
String.valueOf(pid) + " status: " + e.getMessage());
|
||||
return 4;
|
||||
}
|
||||
|
||||
|
|
@ -125,7 +127,8 @@ class ProcessManager {
|
|||
}
|
||||
|
||||
} catch (TimeoutException e) {
|
||||
System.err.println("Timeout getting process return: " + e.getMessage());
|
||||
JobServServer.logger.write("Timeout getting process " +
|
||||
String.valueOf(pid) + " return: " + e.getMessage());
|
||||
return 259;
|
||||
}
|
||||
|
||||
|
|
@ -147,7 +150,8 @@ class ProcessManager {
|
|||
}
|
||||
|
||||
} catch (TimeoutException e) {
|
||||
System.err.println("Timeout getting process output: " + e.getMessage());
|
||||
JobServServer.logger.write("Timeout getting process " +
|
||||
String.valueOf(pid) + " output: " + e.getMessage());
|
||||
return "[-] SERVER: Timeout grabbing lock to access process information";
|
||||
}
|
||||
|
||||
|
|
@ -172,7 +176,8 @@ class ProcessManager {
|
|||
}
|
||||
|
||||
} catch (TimeoutException e) {
|
||||
System.err.println("Timeout killing process: " + e.getMessage());
|
||||
JobServServer.logger.write("Timeout killing process " +
|
||||
String.valueOf(pid) + ": " + e.getMessage());
|
||||
return 3;
|
||||
}
|
||||
|
||||
|
|
@ -213,12 +218,14 @@ class ProcessManager {
|
|||
future.get(this.LOCK_TIMEOUT, TimeUnit.SECONDS);
|
||||
|
||||
} catch (InterruptedException e) {
|
||||
System.err.println("[!] ERROR: " + e.getMessage());
|
||||
JobServServer.logger.write("[!] Couldnt get lock " +
|
||||
String.valueOf(pid) + ": "+ e.getMessage());
|
||||
future.cancel(true);
|
||||
return false;
|
||||
|
||||
} catch (ExecutionException e) {
|
||||
System.err.println("[!] ERROR: " + e.getMessage());
|
||||
JobServServer.logger.write("[!] Couldnt get lock " +
|
||||
String.valueOf(pid) + ": "+ e.getMessage());
|
||||
future.cancel(true);
|
||||
return false;
|
||||
|
||||
|
|
|
|||
84
src/main/java/JobServ/SimpleLogger.java
Normal file
84
src/main/java/JobServ/SimpleLogger.java
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
/*
|
||||
* SimpleLogger
|
||||
*
|
||||
* v1.0
|
||||
*
|
||||
* May 26, 2019
|
||||
*/
|
||||
|
||||
package JobServ;
|
||||
|
||||
import java.io.File;
|
||||
import java.sql.Timestamp;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
/*
|
||||
* SimpleLogger
|
||||
* Automatically manages the creation of and output to a log file
|
||||
* TODO: Log Levels, decorations for entries of different severity
|
||||
*/
|
||||
class SimpleLogger {
|
||||
private static final SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss");
|
||||
private Timestamp programStart;
|
||||
private FileWriter logWriter;
|
||||
private Boolean writable = true;
|
||||
|
||||
/*
|
||||
* Constructor
|
||||
* Initializes timestamp and opens new file for logging
|
||||
*/
|
||||
public SimpleLogger(String filePrefix) {
|
||||
this.programStart = new Timestamp(System.currentTimeMillis());
|
||||
File currentLog = new File(filePrefix + this.dateTimeFormat.format(this.programStart));
|
||||
|
||||
try{
|
||||
this.logWriter = new FileWriter(currentLog, true);
|
||||
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error creating LogWriter!");
|
||||
this.writable = false;
|
||||
}
|
||||
|
||||
this.write(this.programStart.toString() + ": JobServ Logging Started");
|
||||
}
|
||||
|
||||
/*
|
||||
* write
|
||||
* appends a line of information to the log
|
||||
*/
|
||||
public void write(String message) {
|
||||
Timestamp currentTime = new Timestamp(System.currentTimeMillis());
|
||||
message = currentTime.toString() + "> " + message;
|
||||
|
||||
if (this.writable) {
|
||||
try {
|
||||
this.logWriter.write(message);
|
||||
|
||||
} catch (IOException e) {
|
||||
System.out.println(e.getMessage());
|
||||
this.writable = false;
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println(message);
|
||||
}
|
||||
|
||||
/*
|
||||
* shutdown()
|
||||
* called on server exit, closes the FileWriter and frees its resources
|
||||
*/
|
||||
public void shutdown() {
|
||||
Timestamp exitTime = new Timestamp(System.currentTimeMillis());
|
||||
this.write(exitTime.toString() + ": JobServ Logging Stopped");
|
||||
|
||||
try {
|
||||
this.logWriter.close();
|
||||
|
||||
} catch (IOException e) {
|
||||
// not sure what would be appropriate to do here
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue