Skip to content

Commit 276696e

Browse files
committed
Fixed creation of temp file and permissions requirements for admin
1 parent d97a7fe commit 276696e

7 files changed

Lines changed: 54 additions & 24 deletions

File tree

HowToRun.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ The application is distributed as a **standalone EXE** built with `jpackage`.
44

55
### ✔ Requirements
66
Nothing except:
7-
- Windows 10/11
7+
- Windows 10/11 (Currently on win 10 System Logs are not working - TODO)
88
- Local user profile
99
- Windows PowerShell (built-in on Windows 10/11)
1010

src/main/java/com/project/system_log_analyzer/SystemLogAnalyzerApp.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.project.system_log_analyzer;
22

33
import com.project.system_log_analyzer.config.SpringConfig;
4-
import com.project.system_log_analyzer.system.WindowsElevationManager;
54
import javafx.application.Application;
65
import javafx.fxml.FXMLLoader;
76
import javafx.scene.Parent;
@@ -15,6 +14,8 @@
1514

1615
public class SystemLogAnalyzerApp extends Application {
1716

17+
private boolean app_Log = false;
18+
1819
private AnnotationConfigApplicationContext springContext;
1920

2021
private static Boolean elevatedFlag = false; // Admin permissions
@@ -23,15 +24,19 @@ public class SystemLogAnalyzerApp extends Application {
2324
public void init() {
2425
springContext = new AnnotationConfigApplicationContext(SpringConfig.class);
2526

26-
// debug
27-
PrintStream out = null;
28-
try {
29-
out = new PrintStream(new FileOutputStream("app.log", true), true);
30-
} catch (FileNotFoundException e) {
31-
throw new RuntimeException(e);
27+
if (app_Log) { // debug app.log in main directory
28+
PrintStream out = null;
29+
try {
30+
out = new PrintStream(new FileOutputStream("app.log", true), true);
31+
} catch (FileNotFoundException e) {
32+
throw new RuntimeException(e);
33+
}
34+
System.setOut(out);
35+
System.setErr(out);
3236
}
33-
System.setOut(out);
34-
System.setErr(out);
37+
38+
39+
3540
}
3641

3742
@Override

src/main/java/com/project/system_log_analyzer/config/SpringConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.project.system_log_analyzer.config;
22

33

4+
import com.project.system_log_analyzer.SystemLogAnalyzerApp;
5+
import org.springframework.context.annotation.Bean;
46
import org.springframework.context.annotation.ComponentScan;
57
import org.springframework.context.annotation.Configuration;
68

src/main/java/com/project/system_log_analyzer/config/appConfig.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ public class appConfig {
1111
private boolean csvSecurity;
1212
private boolean noLogs;
1313
private boolean saveInExeDir;
14+
private boolean relaunch;
15+
16+
public boolean isRelaunch() {
17+
return relaunch;
18+
}
19+
20+
public void setRelaunch(boolean relaunch) {
21+
this.relaunch = relaunch;
22+
}
1423

1524
public boolean isNoLogs() {
1625
return noLogs;

src/main/java/com/project/system_log_analyzer/controller/WelcomeViewFXController.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,18 @@ private void securityButtonON(ActionEvent event) throws IOException {
137137

138138
boolean relaunchStarted = WindowsElevationManager.relaunchAsAdmin("--elevated");
139139

140-
if (relaunchStarted) Platform.exit();
140+
if (relaunchStarted) {
141+
appConfig.setRelaunch(true);
142+
Platform.exit();
143+
} else {
144+
appConfig.setCsvSecurity(false);
145+
securityButton.setSelected(false);
146+
securityLabel.setText("(Requires Admin Permission)");
147+
}
141148

142149
} else {
143150
appConfig.setCsvSecurity(false);
151+
securityButton.setSelected(false);
144152
securityLabel.setText("(Requires Admin Permission)");
145153
}
146154
}
@@ -212,8 +220,8 @@ private void saveInAppDirectoryBoxOn(ActionEvent event) throws IOException {
212220
logFilesDirButton.setDisable(true);
213221
reportDirButton.setDisable(true);
214222

215-
new File(logs).mkdirs();
216-
new File(reports).mkdirs();
223+
//new File(logs).mkdirs();
224+
//new File(reports).mkdirs();
217225

218226
appConfig.setLogsDir(logs);
219227
appConfig.setReportDir(reports);

src/main/java/com/project/system_log_analyzer/io/WindowsEventExporter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class WindowsEventExporter {
2121
public enum LogType {
2222
APPLICATION("Application"),
2323
SYSTEM("System"),
24-
SECURITY("Security"); // TODO - Test in real environment (.exe/.jar)
24+
SECURITY("Security");
2525

2626
private final String logName;
2727
LogType(String logName) {
@@ -53,7 +53,7 @@ public Path exportToCsv(LogType type) { // Method responsible for exporting logs
5353
if (!exportedDir.exists()) exportedDir.mkdirs();
5454

5555
String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss"));
56-
Path outputFile = Path.of(exportedDir.getAbsolutePath(), "Security_" + timestamp + ".csv");
56+
Path outputFile = Path.of(exportedDir.getAbsolutePath(), type.getLogName() + "_" + timestamp + ".csv");
5757

5858
// Powershell command
5959
String command = String.format(

src/main/java/com/project/system_log_analyzer/system/AppShutdownHandler.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import org.springframework.context.event.EventListener;
1010
import org.springframework.stereotype.Component;
1111

12-
import java.io.File;
1312
import java.io.IOException;
1413
import java.nio.file.Files;
1514
import java.nio.file.Path;
@@ -22,6 +21,7 @@ public class AppShutdownHandler {
2221
private FileLoggerService fileLoggerService;
2322
private FileReportExporter reportExporter;
2423
private appConfig appConfig;
24+
private Boolean elevatedFlag;
2525

2626
@Autowired
2727
public AppShutdownHandler(FileLoggerService fileLoggerService, FileReportExporter reportExporter, appConfig config) {
@@ -48,18 +48,24 @@ public void onShutdown() {
4848
} catch (Exception e) {
4949
System.err.println("Failed to delete temp directory: " + e.getMessage());
5050
}
51-
5251
return;
5352
}
5453

55-
try {
56-
System.out.println("AppShutdownHandler - Application is shutting down! Flushing logs and exporting report...");
57-
fileLoggerService.flushLogToMainFile();
58-
System.out.println("AppShutdownHandler - Shutdown tasks completed successfully.");
59-
} catch (Exception e) {
60-
System.err.println("AppShutdownHandler - Error during shutdown tasks: " + e.getMessage());
61-
e.printStackTrace();
54+
if (appConfig.isRelaunch() || appConfig.getLogsDir() == null || appConfig.getLogsDir().isBlank()) {
55+
System.out.println("Skipping flush — elevated admin relaunch or before Spring Injection");
56+
return;
57+
} else {
58+
try {
59+
System.out.println("AppShutdownHandler - Application is shutting down! Flushing logs and exporting report...");
60+
fileLoggerService.flushLogToMainFile();
61+
System.out.println("AppShutdownHandler - Shutdown tasks completed successfully.");
62+
} catch (Exception e) {
63+
System.err.println("AppShutdownHandler - Error during shutdown tasks: " + e.getMessage());
64+
e.printStackTrace();
65+
}
6266
}
67+
68+
6369
}
6470
// Method for correct deletion of temp files when noLogs is selected
6571
private void deleteDirectoryRecursively(Path path) throws IOException {

0 commit comments

Comments
 (0)