diff --git a/Properties/tinylog.properties b/Properties/tinylog.properties index 7570428..7ef3072 100644 --- a/Properties/tinylog.properties +++ b/Properties/tinylog.properties @@ -1,4 +1,11 @@ -exception=unpack, strip: jdk.internal -writer=console -writer.exception=drop cause -writer.format={date:dd-MM-yyyy HH:mm:ss} {class}.{method}() : {message} \ No newline at end of file +writerConsole=console +writerConsole.level=info +writerConsole.format={date:dd-MM-yyyy HH:mm:ss} {class}.{method}() :\n{message} + +writerFile=rolling file +writerFile.file=logs/{date:dd-MM-yyyy}.{count}.log +writerFile.level=info +writerFile.charset=UTF-8 +writerFile.append=true +writerFile.policies=daily +writerFile.format={date:dd-MM-yyyy HH:mm:ss}\t{class}.{method}() :\t{message} \ No newline at end of file diff --git a/WebContentt/public/assets/js/app.js b/WebContentt/public/assets/js/app.js index 5841b73..9de4577 100644 --- a/WebContentt/public/assets/js/app.js +++ b/WebContentt/public/assets/js/app.js @@ -167,4 +167,60 @@ function loginload(){ if (socket && socket.connected) { socket.disconnect(); } +} + +function logload(){ + console.log("Log loaded"); + if (socket && socket.connected) { + socket.disconnect(); + } + getLogFiles(); + $('#logfiles').change(function(){ + let logname = $('#logfiles').val(); + if (logname && logname.length>0){ + getLogData(logname); + } + }); +} + +function getLogFiles(){ + fetch('/logfiles', { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + } + }).then((response)=>{ + response.json().then((data)=>{ + let logList = $('#logfiles'); + logList.empty(); + logList.append(''); + data.forEach((log)=>{ + logList.append(''); + }); + }); + }); +} + +function getLogData(logname){ + fetch('/logdata', { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: logname + }).then((response)=>{ + response.json().then((data)=>{ + let tablebody = $('#tablebody'); + tablebody.empty(); + data.forEach((log)=>{ + let str = ''; + str += ''+log.Date+''; + str += ''+log.Time+''; + str += ''+log.Method+''; + str += ''+log.Message+''; + str += ''; + tablebody.append(str); + }); + }); + }); } \ No newline at end of file diff --git a/WebContentt/public/index.html b/WebContentt/public/index.html index b8651eb..7ece44a 100644 --- a/WebContentt/public/index.html +++ b/WebContentt/public/index.html @@ -27,6 +27,7 @@
@@ -85,8 +86,8 @@
-
-
+
+
diff --git a/WebContentt/public/log.html b/WebContentt/public/log.html new file mode 100644 index 0000000..01e1884 --- /dev/null +++ b/WebContentt/public/log.html @@ -0,0 +1,87 @@ + + + + + + + SIPIntercom + + + + + + + + + + + + + + +
+
+
+

Log File

+
+
+
+
+
+ + + + + + + + + + + + + + + + + + + + + + + +
DateTimeMethodMessage
Cell 1Cell 2Cell 3Cell 4
Cell 3Cell 4Cell 3Cell 4
+
+
+
+ + + + + + + \ No newline at end of file diff --git a/WebContentt/public/setting.html b/WebContentt/public/setting.html index 7410540..2203445 100644 --- a/WebContentt/public/setting.html +++ b/WebContentt/public/setting.html @@ -27,6 +27,7 @@
@@ -49,7 +50,7 @@
-
@@ -76,7 +77,7 @@
-
diff --git a/src/Log/Log.java b/src/Log/Log.java new file mode 100644 index 0000000..621f34d --- /dev/null +++ b/src/Log/Log.java @@ -0,0 +1,64 @@ +package Log; + +import org.tinylog.Logger; + +import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class Log { + private Path logpath; + final String regex = "(\\d{2}-\\d{2}-\\d{4}) (\\d{2}:\\d{2}:\\d{2})\\s+(\\S+)\\s:\\s+(.*)"; + final Pattern pattern = Pattern.compile(regex, Pattern.UNIX_LINES); + + public Log(){ + String currentDirectory = System.getProperty("user.dir"); + logpath = Paths.get(currentDirectory,"logs"); + } + + /** + * Get the log files in the logs directory + * @return List of log files + */ + public List getLogFiles(){ + List logFiles = new ArrayList<>(); + File[] files = logpath.toFile().listFiles(); + for(File file : files){ + if(file.isFile()){ + String shortName = file.getName(); + if (shortName.endsWith(".log")){ + if (file.length()>0){ + logFiles.add(shortName); + } + } + } + } + return logFiles; + } + + public List ReadLog(String logFile){ + List logData = new ArrayList<>(); + try{ + Path ff = logpath.resolve(logFile); + byte[] bytes = Files.readAllBytes(ff); + String content = new String(bytes); + Matcher matcher = pattern.matcher(content); + while(matcher.find()){ + LogData log = new LogData(); + log.Date = matcher.group(1); + log.Time = matcher.group(2); + log.Method = matcher.group(3); + log.Message = matcher.group(4); + logData.add(log); + } + } catch (Exception e){ + Logger.error("Failed reading log {}, Message: {}", logFile, e.getMessage()); + } + return logData; + } +} diff --git a/src/Log/LogData.java b/src/Log/LogData.java new file mode 100644 index 0000000..e0b6681 --- /dev/null +++ b/src/Log/LogData.java @@ -0,0 +1,24 @@ +package Log; + +import com.google.gson.JsonObject; + +public class LogData { + public String Date; + public String Time; + public String Method; + public String Message; + + @Override + public String toString(){ + return Date + " " + Time + " " + Method + " " + Message; + } + + public static JsonObject toJson(LogData log){ + JsonObject jo = new JsonObject(); + jo.addProperty("Date", log.Date); + jo.addProperty("Time", log.Time); + jo.addProperty("Method", log.Method); + jo.addProperty("Message", log.Message); + return jo; + } +} diff --git a/src/Main.java b/src/Main.java index 42fbbd6..6ae52bd 100644 --- a/src/Main.java +++ b/src/Main.java @@ -17,6 +17,7 @@ import java.util.*; import static code.common.*; + public class Main { private static jSIPClient client; private static WebServer webserver; @@ -47,11 +48,10 @@ public class Main { public static Properties config; private static Thread sipThread; - public static void main(String[] args) { common.ExtractProperties(currentDir,"config.properties", false); config = common.LoadProperties(currentDir,"config.properties"); - common.ExtractProperties(currentDir,"tinylog.properties", false); + //common.ExtractProperties(currentDir,"tinylog.properties", false); // Timer Section timer = new Timer(); @@ -68,6 +68,7 @@ public class Main { // Start System monitoring init_system_monitoring(); + // Shutdown Hook Runtime.getRuntime().addShutdownHook(new Thread(() -> { Logger.info("Shutting down SIPIntercom"); @@ -404,7 +405,7 @@ public class Main { */ private static void pickupCall() { if (incomingRequest!=null || incomingResponse!=null){ - Logger.info("Pickup incoming call from {}",incomingRequest.From); + Logger.info("Pickup incoming call from {}",incomingRequest!=null ? incomingRequest.From:"Unknown"); client.AcceptIncomingCall(incomingRequest); callLight_OnCall(); Buzzer_OnCall(); diff --git a/src/Webpage/WebServer.java b/src/Webpage/WebServer.java index 0c02891..b92a785 100644 --- a/src/Webpage/WebServer.java +++ b/src/Webpage/WebServer.java @@ -1,5 +1,8 @@ package Webpage; +import Log.Log; +import Log.LogData; +import com.google.gson.JsonArray; import io.javalin.Javalin; import io.javalin.http.HttpCode; import io.javalin.http.staticfiles.Location; @@ -7,6 +10,7 @@ import lombok.Getter; import lombok.Setter; import org.tinylog.Logger; +import java.util.List; import java.util.Objects; import java.util.Properties; import java.util.function.Consumer; @@ -21,12 +25,15 @@ public class WebServer { private @Setter Consumer onLoginSettingChanged; private @Setter Consumer onSipSettingChanged; private @Getter int ProcessID; + private final Log logworker; public WebServer(Properties prop) { listenport = GetProperties_IntValue(prop,"WebListenPort", 8080); webusername = GetProperties_StringValue(prop,"WebUsername", "admin"); webpassword = GetProperties_StringValue(prop,"WebPassword", "admin"); + logworker = new Log(); + int PID = GetPID(listenport); if (PID!=0) killPID(PID); @@ -49,6 +56,11 @@ public class WebServer { ctx.redirect("/login.html"); } }); + app.before("/log.html", ctx -> { + if (!Objects.equals(ctx.sessionAttribute("username"), webusername)){ + ctx.redirect("/login.html"); + } + }); app.get("/logout", ctx -> { ctx.sessionAttribute("username", null); ctx.redirect("/login.html"); @@ -133,6 +145,27 @@ public class WebServer { } }); + + app.post("/logfiles", ctx -> { + List logfiles = logworker.getLogFiles(); + JsonArray jsonArray = new JsonArray(); + for (String logfile : logfiles) { + jsonArray.add(logfile); + } + ctx.result(jsonArray.toString()); + }); + + app.post("/logdata", ctx -> { + String logfile = ctx.body(); + if (ValidString(logfile)){ + List logdata = logworker.ReadLog(logfile); + JsonArray jsonArray = new JsonArray(); + for (LogData log : logdata) { + jsonArray.add(LogData.toJson(log)); + } + ctx.result(jsonArray.toString()); + } else ctx.result("Invalid Log File").status(HttpCode.BAD_REQUEST); + }); } /** diff --git a/src/code/common.java b/src/code/common.java index c7ce130..3cf2a96 100644 --- a/src/code/common.java +++ b/src/code/common.java @@ -39,7 +39,7 @@ public class common { Matcher matcher = pattern.matcher(line); if (matcher.find()) { PID = Integer.parseInt(matcher.group(1)); - System.out.println("PID: "+PID); + //System.out.println("PID: "+PID); break; } } @@ -123,7 +123,7 @@ public class common { try{ File dest = new File(directory,filename); if (dest.isFile() && !overwrite){ - Logger.info("Properties file already exists: {}",filename); + //Logger.info("Properties file already exists: {}",filename); return; } // delete existing file @@ -156,8 +156,8 @@ public class common { InputStream is = new FileInputStream(file); prop.load(is); is.close(); - Logger.info("Loaded properties file: {}",filename); - } else Logger.info("Properties file not found: {}",filename); + //Logger.info("Loaded properties file: {}",filename); + } else Logger.error("Properties file not found: {}",filename); } catch (Exception e) { Logger.error("Failed to load properties file: {}",filename);