Skip to content

Commit 042c8d0

Browse files
author
NoteFox
committed
reversed last changes to Logger
1 parent d3bac4d commit 042c8d0

8 files changed

Lines changed: 327 additions & 182 deletions

File tree

src/java/Playground.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import Server.CustomObjects.LogType;
2+
import Server.FTPService.FTPCli.FtpClient;
3+
import Server.LogService.Logger;
4+
import org.apache.commons.net.ftp.FTP;
5+
import org.apache.commons.net.ftp.FTPFile;
6+
7+
import java.io.File;
8+
import java.io.IOException;
9+
import java.lang.reflect.Array;
10+
import java.text.SimpleDateFormat;
11+
import java.util.ArrayList;
12+
import java.util.Date;
13+
import java.util.List;
14+
15+
public class Playground {
16+
static String currentWritingDir;
17+
static String logDefaultDir;
18+
static SimpleDateFormat dirFormatter = new SimpleDateFormat("yyyy-MM-dd");
19+
static String currentWritingFile;
20+
static String baseFile;
21+
static SimpleDateFormat fileFormatter = new SimpleDateFormat("HH-mm-ss z");
22+
23+
24+
public static void main(String[] args) {
25+
logDefaultDir = "";
26+
baseFile = "LOG";
27+
28+
currentWritingDir = logDefaultDir + dirFormatter.format(System.currentTimeMillis() - 86400000 );
29+
currentWritingFile = currentWritingDir + "/" + baseFile.replace(".txt", "") + "["+ fileFormatter.format(System.currentTimeMillis() - 86400000) +"].txt";
30+
31+
swapToNextDayDirIfNecessary();
32+
}
33+
34+
private static void swapToNextDayDirIfNecessary() {
35+
Date date = new Date(System.currentTimeMillis());
36+
37+
if (!currentWritingDir.equals(logDefaultDir + dirFormatter.format(date))) {
38+
File nwd = new File(logDefaultDir + dirFormatter.format(date));
39+
40+
if (!nwd.exists())
41+
nwd.mkdir();
42+
43+
currentWritingDir = "./" + nwd.getName();
44+
45+
currentWritingFile = currentWritingDir + "/" + baseFile.replace(".txt", "") + "["+ fileFormatter.format(System.currentTimeMillis()) +"].txt";;
46+
47+
File file = new File(currentWritingFile);
48+
if (!file.exists()) {
49+
try {
50+
file.createNewFile();
51+
} catch (IOException e) {
52+
e.printStackTrace();
53+
}
54+
}
55+
}
56+
}
57+
}

src/java/Server/CustomObjects/QueryRequest.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
import java.io.*;
66
import java.util.List;
77

8-
import static Server.StaticVariables.mapDir;
9-
import static Server.StaticVariables.osmDir;
10-
118
public class QueryRequest extends Thread {
129

1310
private List<Coords> coordinates;
@@ -21,7 +18,13 @@ public class QueryRequest extends Thread {
2118

2219
private String TAG;
2320

24-
public QueryRequest(List<Coords> coordinates, String date, String mapName, int id) {
21+
private String osmDir;
22+
private String mapDir;
23+
24+
public QueryRequest(List<Coords> coordinates, String date, String mapName, int id, String osmDir, String mapDir) {
25+
this.osmDir = osmDir;
26+
this.mapDir = mapDir;
27+
2528
this.coordinates = coordinates;
2629
this.date = date;
2730
this.mapName = mapName;

src/java/Server/FTPService/FTPService.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
import java.util.List;
2222
import java.util.Map;
2323

24-
import static Server.StaticVariables.*;
25-
2624
public class FTPService implements Runnable {
2725

2826
private FtpServer server;
@@ -33,8 +31,24 @@ public class FTPService implements Runnable {
3331
private UserManager um;
3432
private PropertiesUserManagerFactory userManagerFactory;
3533

34+
private int ftpPort;
35+
private String ftpServiceUserPropertiesFile;
36+
private String standardUserName;
37+
private String standardUserPassword;
38+
private String ftpServiceMapDir;
39+
private String ftpDefaultDir;
40+
3641
private String TAG = "FTPService-Thread";
3742

43+
public FTPService(int ftpPort, String ftpServiceUserPropertiesFile, String standardUserName, String standardUserPassword, String ftpServiceMapDir, String ftpDefaultDir) {
44+
this.ftpPort = ftpPort;
45+
this.ftpServiceUserPropertiesFile = ftpServiceUserPropertiesFile;
46+
this.standardUserName = standardUserName;
47+
this.standardUserPassword = standardUserPassword;
48+
this.ftpServiceMapDir = ftpServiceMapDir;
49+
this.ftpDefaultDir = ftpDefaultDir;
50+
}
51+
3852
@Override
3953
public void run() {
4054

src/java/Server/LogService/Logger.java

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
import java.util.Date;
99
import java.util.List;
1010

11-
import static Server.StaticVariables.*;
12-
1311
public class Logger extends Thread implements LoggerInt {
1412

1513
public static Logger instance;
@@ -27,7 +25,15 @@ public class Logger extends Thread implements LoggerInt {
2725
private Boolean isWaiting = false;
2826
public boolean isRunning = false;
2927

30-
public Logger() {
28+
private String logDefaultDir;
29+
private int maxLogFileSize;
30+
private boolean logTerminalOutput;
31+
32+
public Logger(String logDefaultDir, int maxLogFileSize, boolean logTerminalOutput) {
33+
34+
this.logDefaultDir = logDefaultDir;
35+
this.maxLogFileSize = maxLogFileSize;
36+
this.logTerminalOutput = logTerminalOutput;
3137

3238
this.baseFile = "LOG";
3339

@@ -60,7 +66,7 @@ private void swapToNextFileIfNecessary() {
6066
}
6167
}
6268

63-
private void swapToNextDayDirIfNecessary() {
69+
private void swapToNextDayDirIfNecessary() throws IOException {
6470
Date date = new Date(System.currentTimeMillis());
6571

6672
if (!currentWritingDir.equals(logDefaultDir + dirFormatter.format(date))) {
@@ -73,11 +79,10 @@ private void swapToNextDayDirIfNecessary() {
7379

7480
currentWritingFile = currentWritingDir + "/" + baseFile.replace(".txt", "") + "["+ fileFormatter.format(System.currentTimeMillis()) +"].txt";;
7581

76-
try {
77-
new File(currentWritingFile).createNewFile();
78-
} catch (IOException e) {
79-
e.printStackTrace();
80-
}
82+
File file = new File(currentWritingFile);
83+
84+
if (!file.exists())
85+
file.createNewFile();
8186
}
8287
}
8388

@@ -93,7 +98,11 @@ public synchronized void run() {
9398
}
9499

95100
while (true) {
96-
swapToNextDayDirIfNecessary();
101+
try {
102+
swapToNextDayDirIfNecessary();
103+
} catch (IOException e) {
104+
e.printStackTrace();
105+
}
97106
swapToNextFileIfNecessary();
98107
if (BUFFER_LIST.isEmpty()) {
99108
isWaiting = true;
@@ -114,6 +123,9 @@ private void writeEntry(LogEntry entry) throws IOException {
114123
String temp = "";
115124
File file = new File(currentWritingFile);
116125

126+
if (!file.exists())
127+
file.createNewFile();
128+
117129
Date date = new Date(System.currentTimeMillis());
118130
String input = entry.type + " | " + logFormatter.format(date) + " | " + entry.TAG + " : " + entry.message;
119131

src/java/Server/SpringClass.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@
1717

1818
import static Server.CustomObjects.LogType.ERROR;
1919
import static Server.CustomObjects.LogType.INFO;
20-
import static Server.StaticVariables.*;
2120

2221
@SpringBootApplication
2322
@RestController
2423
public class SpringClass {
2524

25+
private static String initFile = "init.txt";
26+
2627
private static ServiceNew serviceInstance;
2728
private static Thread serviceThread;
2829

@@ -32,16 +33,17 @@ public class SpringClass {
3233
private String TAG = "Spring-Thread";
3334

3435
public static void main(String[] args) throws IOException {
35-
3636
// just standard inits for the Variables used in this Project
3737
// most of them are read from the init.txt
3838
// but more of that in the OHDM wiki
39+
40+
new StaticVariables("init.txt");
3941
StaticVariables.init();
4042
StaticVariables.createStdFilesAndDirs();
4143

4244
// Logger is a singleton Class due to the availability need
4345
// the Logger is a System in itself
44-
new Logger();
46+
new Logger(StaticVariables.logDefaultDir, StaticVariables.maxLogFileSize, StaticVariables.logTerminalOutput);
4547
Logger.instance.start();
4648

4749
// the service instance, which just goes through a couple of lists and sets things to what they are
@@ -52,12 +54,12 @@ public static void main(String[] args) throws IOException {
5254
serviceThread.start();
5355

5456
// the ftp service, which allows the Android App to download the .map files
55-
ftpInstance = new FTPService();
57+
ftpInstance = new FTPService(StaticVariables.ftpPort, StaticVariables.ftpServiceUserPropertiesFile, StaticVariables.standardUserName, StaticVariables.standardUserPassword, StaticVariables.ftpServiceMapDir, StaticVariables.ftpDefaultDir);
5658
ftpThread = new Thread(ftpInstance);
5759
ftpThread.start();
5860

5961
// and here starts the Spring Application with the server port set to the
60-
// before given Port in StaticVariables
62+
// before given Port in Server.StaticVariables
6163
System.getProperties().put("server.port", StaticVariables.webPort);
6264
SpringApplication.run(SpringClass.class, args);
6365
}
@@ -120,7 +122,7 @@ public String webServiceStatus() {
120122
sb.append(" | ----------------------------------------------------------------------" +"<br>");
121123
sb.append(" | - Date : " + r.getDate() + "<br>");
122124
sb.append(" | - Coords : \n" + r.getPrintableCoordsString() + "<br>");
123-
sb.append(" | - Link to download .map file : <a href=\"ftp:"+ standardUserName + ":" +standardUserPassword + "@141.45.146.200:5000/" + r.getMapName()+ ".map\">direct link</a> <br>");
125+
sb.append(" | - Link to download .map file : <a href=\"ftp:"+ StaticVariables.standardUserName + ":" + StaticVariables.standardUserPassword + "@141.45.146.200:5000/" + r.getMapName()+ ".map\">direct link</a> <br>");
124126
sb.append(" | ---------------------------------------------------------------------</p>");
125127
i++;
126128
}
@@ -151,7 +153,7 @@ public String request(@RequestParam(value = "name", defaultValue = "testRequest"
151153
}
152154

153155
// TODO : add id system
154-
q = new QueryRequest(coordinates, date, mapname, 0000);
156+
q = new QueryRequest(coordinates, date, mapname,0000, StaticVariables.osmDir, StaticVariables.mapDir);
155157

156158
Logger.instance.addLogEntry(INFO, TAG,"given Data: " + mapname + " | Date: " + date + " | coords: \n" + q.getPrintableCoordsString());
157159

@@ -254,8 +256,8 @@ public String sanitize_mapName(String nme) {
254256
* @return true if existent, false if not
255257
*/
256258
public boolean map_exist(String nme) {
257-
File fOSM = new File(osmDir + nme);
258-
File fMAP = new File(mapDir + nme);
259+
File fOSM = new File(StaticVariables.osmDir + nme);
260+
File fMAP = new File(StaticVariables.mapDir + nme);
259261

260262
if (!(fOSM.exists() && !fOSM.isDirectory()))
261263
if (!(fMAP.exists() && !fOSM.isDirectory()))

0 commit comments

Comments
 (0)