fixes #18
parent
cb94c15039
commit
e947fc7c46
@ -0,0 +1,57 @@
|
||||
package tools;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.io.IOException;
|
||||
import java.sql.Timestamp;
|
||||
|
||||
|
||||
/** Helper to log.
|
||||
* @author Louis Royer
|
||||
* @author Flavien Haas
|
||||
* @author JS Auge
|
||||
* @version 1.0
|
||||
*/
|
||||
public class Logger {
|
||||
private Path logFile;
|
||||
|
||||
public Logger(String logFile) {
|
||||
assert logFile != null : "Logfile name is null";
|
||||
this.logFile = Paths.get(logFile);
|
||||
try {
|
||||
this.logFile.toFile().createNewFile();
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error: cannot initialize logfile");
|
||||
}
|
||||
}
|
||||
|
||||
/** Appends log to filelog and print to stderr.
|
||||
* @param text Text to log
|
||||
*/
|
||||
private void write(String text) {
|
||||
System.err.println(text);
|
||||
try {
|
||||
Files.write(logFile, ("[" + new Timestamp(System.currentTimeMillis()) + "] " + text + "\n").getBytes(), StandardOpenOption.APPEND);
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error: cannot write in logfile");
|
||||
}
|
||||
}
|
||||
|
||||
/** Appends log to filelog and print to stderr.
|
||||
* Adds [TCP] in log line.
|
||||
* @param text Text to log
|
||||
*/
|
||||
public void writeTCP(String text) {
|
||||
write("[TCP] " + text);
|
||||
}
|
||||
|
||||
/** Appends log to filelog and print to stderr.
|
||||
* Adds [UDP] in log line.
|
||||
* @param text Text to log
|
||||
*/
|
||||
public void writeUDP(String text) {
|
||||
write("[UDP] " + text);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue