-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConsole.java
105 lines (94 loc) · 4.12 KB
/
Console.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
///////////////////
// Console: contains important output functions to the monitor or log file; also contains the error and crash functions
///////////////////
public class Console extends OS {
WriteFile fileWriter = new WriteFile();
//log - contains all important logic for logging to the monitor, log file, or both
public void log(String echoStatement, String route) {
if (config != null) {
if (config.logOption != null) {
if (config.logOption.equals("Log to File")) {
writeFileLog(echoStatement, route);
} else if (config.logOption.equals("Log to Monitor")) {
writeConsoleLog(echoStatement, route);
} else if (config.logOption.equals("Log to Both")) {
writeConsoleLog(echoStatement, route);
writeFileLog(echoStatement, route);
}
} else {
//since config file name hasn't loaded yet, just output to console
writeConsoleLog(echoStatement, route);
}
} else {
//since config hasn't loaded yet, just output to console
writeConsoleLog(echoStatement, route);
}
//after we write everything, if the log was fatal we should ask to crash
if (route.equals("fatal")) {
//if we allow fatal executions
if (allowFatalExecution) {
//prompt the user to continue
console.writeConsoleLog("Continue execution? (Y/N)", "log");
String name = System.console().readLine();
//if the user does not want to continue, then crash
if (!name.equals("y") && !name.equals("Y")) {
crash();
}
} else {
crash();
}
}
}
//writeConsoleLog - writes a log message to the monitor only
public void writeConsoleLog(String echoStatement, String route) {
if (route.equals("log")) {
System.out.println((char)27 + "[39m" + echoStatement);
} else if (route.equals("warn")) {
System.out.println((char)27 + "[39m");
System.out.println((char)27 + "[36m" + "➤ WARNING: " + echoStatement);
System.out.println((char)27 + "[39m");
} else if (route.equals("fatal")) {
System.out.println((char)27 + "[39m");
System.out.println((char)27 + "[31m" + "✖ FATAL ERROR: " + echoStatement);
System.out.println((char)27 + "[39m");
}
}
//writeFileLog - writes a log message to the log file only
public void writeFileLog(String echoStatement, String route) {
if (route.equals("log")) {
fileWriter.write(config.logFileName, true, echoStatement);
} else if (route.equals("warn")) {
fileWriter.write(config.logFileName, true, "➤ WARNING: " + echoStatement);
} else if (route.equals("fatal")) {
fileWriter.write(config.logFileName, true, "✖ FATAL ERROR: " + echoStatement);
}
}
//log - parameter override for single int printout
public void log(int echoStatement) {
log(Integer.toString(echoStatement), "log");
}
//log - parameter override for cleaner log statement outside class
public void log(String echoStatement) {
log(echoStatement, "log");
}
//warn - writes a log message with a warning color
public void warn(String echoStatement) {
log(echoStatement, "warn");
}
//error - writes a log message with isFatal set to true - will crash program
public void error(String echoStatement) {
log(echoStatement, "fatal");
}
//printDiv - prints a divider to the monitor or log file
public void printDiv() {
log("=======================================================", "log");
}
//printNewline - prints a new line to the monitor or log file
public void printNewline() {
log(" ", "log");
}
//crash - crashes the program prematurely so that the files can not be processed after a syntax error is found
private void crash() {
System.exit(0);
}
}