-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathErrorLogger.java
59 lines (52 loc) · 1.92 KB
/
ErrorLogger.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
package Scanner;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.Date;
public class ErrorLogger{
public PrintWriter out;
private String fileName;
public ErrorLogger(String outputFileName){
try{
fileName = outputFileName;
out = new PrintWriter(fileName);
Date date = new Date();
Timestamp timestamp = new Timestamp(date.getTime());
out.write("Log opened at: "+timestamp);//clears file, leaving only this.
} catch (IOException e) {
System.out.println("ErrorLogger:: An error occurred creating file "+outputFileName+" in ErrorLogger constructor");
e.printStackTrace();
}
}
public void log(String stringPassed){
try {
FileWriter logger = new FileWriter(fileName, true);
logger.append(stringPassed);
logger.close();
} catch (IOException e) {
System.out.println("An error occurred while logging output file.");
e.printStackTrace();
}
}
public void customError(String errorString, CompilerScanner s) {
String fullErrorString;
if (s != null) {
fullErrorString = errorString + " at line "+s.line;
} else {
fullErrorString = errorString + "At Line Null (Error was in SymbolTable)";
}
System.out.println(fullErrorString);
log(fullErrorString);
System.out.println("\n\n\n\n\n\n\n ");
if (s != null)
s.symbolTable.print();
System.exit(0);//kill process after first error is logged
}
public void customError(String errorString, int line) {
String fullErrorString = errorString + " at line "+line;
System.out.println(fullErrorString);
log(fullErrorString);
System.exit(0);//kill process after first error is logged
}
}