Merge branch 'logs' of flavien/Projet_JAVA_P2P_STRI2A into master
commit
5a54663a7a
@ -0,0 +1,8 @@
|
||||
package tools;
|
||||
public enum LogLevel {
|
||||
Error,
|
||||
Info,
|
||||
Warning,
|
||||
Action,
|
||||
Debug
|
||||
}
|
@ -0,0 +1,121 @@
|
||||
package tools;
|
||||
import tools.LogLevel;
|
||||
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, LogLevel logLevel) {
|
||||
String msg = "[" + new Timestamp(System.currentTimeMillis()) + "] " + text + "\n";
|
||||
String level = null;
|
||||
switch (logLevel) {
|
||||
case Error:
|
||||
level = "[Error]";
|
||||
break;
|
||||
case Info:
|
||||
level = "[Info]";
|
||||
break;
|
||||
case Warning:
|
||||
level = "[Warning]";
|
||||
break;
|
||||
case Action:
|
||||
level = "[Action]";
|
||||
break;
|
||||
case Debug:
|
||||
level = "[Debug]";
|
||||
break;
|
||||
default:
|
||||
System.err.println("Error: incorrect logLevel");
|
||||
}
|
||||
if (level != null) {
|
||||
msg = level + " " + msg;
|
||||
}
|
||||
switch (logLevel) {
|
||||
case Error:
|
||||
case Info:
|
||||
case Warning:
|
||||
case Debug:
|
||||
default:
|
||||
System.err.println(text);
|
||||
break;
|
||||
case Action:
|
||||
break;
|
||||
}
|
||||
switch (logLevel) {
|
||||
case Debug:
|
||||
break;
|
||||
case Error:
|
||||
case Info:
|
||||
case Warning:
|
||||
default:
|
||||
try {
|
||||
Files.write(logFile, msg.getBytes(), StandardOpenOption.APPEND);
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error: cannot write in logfile");
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/** Appends log to filelog and print to stderr.
|
||||
* Adds [TCP] in log line.
|
||||
* @param text Text to log
|
||||
* @param logLevel Type of log
|
||||
*/
|
||||
public void writeTCP(String text, LogLevel logLevel) {
|
||||
write("[TCP] " + text, logLevel);
|
||||
}
|
||||
|
||||
/** Appends log to filelog and print to stderr.
|
||||
* Adds [TCP] in log line.
|
||||
* @param text Text to log
|
||||
* @param logLevel Type of log
|
||||
*/
|
||||
public void writeTCP(Exception e, LogLevel logLevel) {
|
||||
writeTCP(e.toString(), logLevel);
|
||||
}
|
||||
|
||||
/** Appends log to filelog and print to stderr.
|
||||
* Adds [UDP] in log line.
|
||||
* @param text Text to log
|
||||
* @param logLevel Type of log
|
||||
*/
|
||||
public void writeUDP(String text, LogLevel logLevel) {
|
||||
write("[UDP] " + text, logLevel);
|
||||
}
|
||||
|
||||
/** Appends log to filelog and print to stderr.
|
||||
* Adds [UDP] in log line.
|
||||
* @param text Text to log
|
||||
* @param logLevel Type of log
|
||||
*/
|
||||
public void writeUDP(Exception e, LogLevel logLevel) {
|
||||
writeUDP(e.toString(), logLevel);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue