Skip to content

Commit

Permalink
LDEV-5206 support writing executionLog straight to console, with opti…
Browse files Browse the repository at this point in the history
…onal code snippet
  • Loading branch information
zspitzer committed Dec 17, 2024
1 parent ab1ae3c commit 6db52d7
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions core/src/main/java/lucee/runtime/engine/ConsoleExecutionLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,39 +23,59 @@

import lucee.commons.io.log.Log;
import lucee.commons.io.log.LogUtil;
import lucee.commons.io.res.util.ResourceSnippet;
import lucee.commons.io.res.util.ResourceSnippetsMap;
import lucee.runtime.PageContext;
import lucee.runtime.PageContextImpl;
import lucee.runtime.functions.math.Int;
import lucee.runtime.op.Caster;

public class ConsoleExecutionLog extends ExecutionLogSupport {

private PrintWriter pw;
private PageContext pc;
private ResourceSnippetsMap snippetsMap = new ResourceSnippetsMap(1024, 128);
private boolean snippet = false;

@Override
protected void _init(PageContext pc, Map<String, String> arguments) {
this.pc = pc;

if (Caster.toBooleanValue(arguments.get("snippet"), false)) snippet = true;
if (pw == null) {
// stream type
String type = arguments.get("stream-type");
if (type != null && type.trim().equalsIgnoreCase("error")) pw = new PrintWriter(System.err);
else pw = new PrintWriter(System.out);

if (type == null) return;
if (type.trim().equalsIgnoreCase("error")) pw = new PrintWriter(System.err);
else if (type.trim().equalsIgnoreCase("out")) pw = new PrintWriter(System.out);
}
}

@Override
protected void _log(int startPos, int endPos, long startTime, long endTime) {

long diff = endTime - startTime;
LogUtil.log(pc, Log.LEVEL_TRACE, Controler.class.getName(),
pc.getId() + ":" + pc.getCurrentPageSource().getDisplayPath() + ":" + positons(startPos, endPos) + " > " + timeLongToString(diff));
String log = pc.getId() + ":" + pc.getCurrentPageSource().getDisplayPath() + ":";
if (snippet) {
ResourceSnippet snippet = snippetsMap.getSnippet(pc.getCurrentPageSource(), startPos, endPos, ((PageContextImpl) pc).getResourceCharset().name());
log += positions(snippet.getEndLine(), snippet.getEndLine()) + " > " + timeLongToString(diff) + " [" + snippet.getContent() + "]";
} else {
log += positions(startPos, endPos) + " > " + timeLongToString(diff);
}
if (pw != null){
pw.print(log + "\n");
pw.flush();
} else {
LogUtil.log(pc, Log.LEVEL_TRACE, Controler.class.getName(), log);
}
}

@Override
protected void _release() {
if (pw != null) pw.close();
snippetsMap = null;
}

private static String positons(int startPos, int endPos) {
private static String positions(int startPos, int endPos) {
if (startPos == endPos) return startPos + "";
return startPos + ":" + endPos;
}
Expand Down

0 comments on commit 6db52d7

Please sign in to comment.