Skip to content

Commit 4a83f22

Browse files
committed
Add file and line to every log
1 parent a05f241 commit 4a83f22

File tree

5 files changed

+38
-5
lines changed

5 files changed

+38
-5
lines changed

src/main/java/leekscript/compiler/JavaWriter.java

+7
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,13 @@ public void writeErrorFunction(IACompiler comp, String ai) {
108108
mCode.append(", ");
109109
}
110110
mCode.append("};}\n\n");
111+
112+
mCode.append("protected int[] getErrorFilesID() { return new int[] {");
113+
for (var f : mFilesList) {
114+
mCode.append(f.getId());
115+
mCode.append(", ");
116+
}
117+
mCode.append("};}\n\n");
111118
}
112119

113120
public void addCounter(int count) {

src/main/java/leekscript/compiler/bloc/AbstractLeekBlock.java

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import leekscript.compiler.Token;
77
import leekscript.compiler.AnalyzeError;
8-
import leekscript.compiler.IACompiler;
98
import leekscript.compiler.JavaWriter;
109
import leekscript.compiler.WordCompiler;
1110
import leekscript.compiler.AnalyzeError.AnalyzeErrorLevel;

src/main/java/leekscript/compiler/bloc/MainLeekBlock.java

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import leekscript.compiler.exceptions.LeekCompilerException;
2222
import leekscript.compiler.expression.LeekExpressionException;
2323
import leekscript.compiler.expression.LeekNumber;
24-
import leekscript.compiler.expression.LeekVariable;
2524
import leekscript.compiler.expression.LeekVariable.VariableType;
2625
import leekscript.compiler.instruction.ClassDeclarationInstruction;
2726
import leekscript.compiler.instruction.LeekGlobalDeclarationInstruction;

src/main/java/leekscript/runner/AI.java

+26-2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.util.Date;
3636
import java.util.HashSet;
3737
import java.util.Iterator;
38+
import java.util.Map;
3839
import java.util.Random;
3940
import java.util.Set;
4041
import java.util.TreeMap;
@@ -476,11 +477,28 @@ public String getErrorMessage(StackTraceElement[] elements) {
476477
return sb.toString();
477478
}
478479

480+
public record LeekScriptPosition(int file, int line) {}
481+
482+
public LeekScriptPosition getCurrentLeekScriptPosition() {
483+
for (StackTraceElement element : Thread.currentThread().getStackTrace()) {
484+
if (element.getClassName().startsWith("AI_")) {
485+
var mapping = getLineMapping(element.getLineNumber());
486+
if (mapping != null) {
487+
var files = getErrorFilesID();
488+
var f = mapping.getAI();
489+
int file = f < files.length ? files[f] : 0;
490+
return new LeekScriptPosition(file, mapping.getLeekScriptLine());
491+
}
492+
}
493+
}
494+
return null;
495+
}
496+
479497
public String getErrorMessage(Throwable e) {
480498
return getErrorMessage(e.getStackTrace());
481499
}
482500

483-
protected String getErrorLocalisation(int javaLine) {
501+
protected LineMapping getLineMapping(int javaLine) {
484502
if (mLinesMapping.isEmpty() && this.filesLines != null && this.filesLines.exists()) {
485503
try (Stream<String> stream = Files.lines(this.filesLines.toPath())) {
486504
stream.forEach(l -> {
@@ -490,7 +508,11 @@ protected String getErrorLocalisation(int javaLine) {
490508
} catch (IOException e) {}
491509
thisObject = getAIString();
492510
}
493-
var lineMapping = mLinesMapping.get(javaLine);
511+
return mLinesMapping.get(javaLine);
512+
}
513+
514+
protected String getErrorLocalisation(int javaLine) {
515+
var lineMapping = getLineMapping(javaLine);
494516
if (lineMapping != null) {
495517
var files = getErrorFiles();
496518
var f = lineMapping.getAI();
@@ -639,6 +661,8 @@ else if (type == AILog.STANDARD)
639661

640662
protected String[] getErrorFiles() { return null; }
641663

664+
protected int[] getErrorFilesID() { return null; }
665+
642666
protected String getAIString() { return ""; }
643667

644668
public Object runIA() throws LeekRunException {

src/main/java/leekscript/runner/BasicAILog.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,14 @@ public void addSystemLog(AI ai, int type, String trace, int key, Object[] parame
5252

5353
public void addLog(int type, String message) {
5454
message = message.replaceAll("\t", " ");
55-
addLog(type, message, 0);
55+
addLog(type, message, 0, 0, 0);
5656
}
5757

5858
public void addLog(int type, String message, int color) {
59+
addLog(type, message, color, 0, 0);
60+
}
61+
62+
public void addLog(int type, String message, int color, int ai, int line) {
5963

6064
if (message == null || !addSize(20 + message.length())) {
6165
return;

0 commit comments

Comments
 (0)