Skip to content

Commit

Permalink
Add file and line to every log
Browse files Browse the repository at this point in the history
  • Loading branch information
5pilow committed May 14, 2024
1 parent a05f241 commit 4a83f22
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
7 changes: 7 additions & 0 deletions src/main/java/leekscript/compiler/JavaWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ public void writeErrorFunction(IACompiler comp, String ai) {
mCode.append(", ");
}
mCode.append("};}\n\n");

mCode.append("protected int[] getErrorFilesID() { return new int[] {");
for (var f : mFilesList) {
mCode.append(f.getId());
mCode.append(", ");
}
mCode.append("};}\n\n");
}

public void addCounter(int count) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import leekscript.compiler.Token;
import leekscript.compiler.AnalyzeError;
import leekscript.compiler.IACompiler;
import leekscript.compiler.JavaWriter;
import leekscript.compiler.WordCompiler;
import leekscript.compiler.AnalyzeError.AnalyzeErrorLevel;
Expand Down
1 change: 0 additions & 1 deletion src/main/java/leekscript/compiler/bloc/MainLeekBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import leekscript.compiler.exceptions.LeekCompilerException;
import leekscript.compiler.expression.LeekExpressionException;
import leekscript.compiler.expression.LeekNumber;
import leekscript.compiler.expression.LeekVariable;
import leekscript.compiler.expression.LeekVariable.VariableType;
import leekscript.compiler.instruction.ClassDeclarationInstruction;
import leekscript.compiler.instruction.LeekGlobalDeclarationInstruction;
Expand Down
28 changes: 26 additions & 2 deletions src/main/java/leekscript/runner/AI.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.TreeMap;
Expand Down Expand Up @@ -476,11 +477,28 @@ public String getErrorMessage(StackTraceElement[] elements) {
return sb.toString();
}

public record LeekScriptPosition(int file, int line) {}

public LeekScriptPosition getCurrentLeekScriptPosition() {
for (StackTraceElement element : Thread.currentThread().getStackTrace()) {
if (element.getClassName().startsWith("AI_")) {
var mapping = getLineMapping(element.getLineNumber());
if (mapping != null) {
var files = getErrorFilesID();
var f = mapping.getAI();
int file = f < files.length ? files[f] : 0;
return new LeekScriptPosition(file, mapping.getLeekScriptLine());
}
}
}
return null;
}

public String getErrorMessage(Throwable e) {
return getErrorMessage(e.getStackTrace());
}

protected String getErrorLocalisation(int javaLine) {
protected LineMapping getLineMapping(int javaLine) {
if (mLinesMapping.isEmpty() && this.filesLines != null && this.filesLines.exists()) {
try (Stream<String> stream = Files.lines(this.filesLines.toPath())) {
stream.forEach(l -> {
Expand All @@ -490,7 +508,11 @@ protected String getErrorLocalisation(int javaLine) {
} catch (IOException e) {}
thisObject = getAIString();
}
var lineMapping = mLinesMapping.get(javaLine);
return mLinesMapping.get(javaLine);
}

protected String getErrorLocalisation(int javaLine) {
var lineMapping = getLineMapping(javaLine);
if (lineMapping != null) {
var files = getErrorFiles();
var f = lineMapping.getAI();
Expand Down Expand Up @@ -639,6 +661,8 @@ else if (type == AILog.STANDARD)

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

protected int[] getErrorFilesID() { return null; }

protected String getAIString() { return ""; }

public Object runIA() throws LeekRunException {
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/leekscript/runner/BasicAILog.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,14 @@ public void addSystemLog(AI ai, int type, String trace, int key, Object[] parame

public void addLog(int type, String message) {
message = message.replaceAll("\t", " ");
addLog(type, message, 0);
addLog(type, message, 0, 0, 0);
}

public void addLog(int type, String message, int color) {
addLog(type, message, color, 0, 0);
}

public void addLog(int type, String message, int color, int ai, int line) {

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

0 comments on commit 4a83f22

Please sign in to comment.