Skip to content

Commit

Permalink
Merge e47526b into 17e0501
Browse files Browse the repository at this point in the history
  • Loading branch information
tallitman committed Apr 10, 2021
2 parents 17e0501 + e47526b commit 054d5a6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
15 changes: 14 additions & 1 deletion src/main/java/il/ac/bgu/cs/bp/bpjs/execution/jsproxy/BpLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import il.ac.bgu.cs.bp.bpjs.internal.ScriptableUtils;
import il.ac.bgu.cs.bp.bpjs.model.BProgram;

import java.io.PrintStream;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.Set;
Expand All @@ -36,12 +37,20 @@
* @author michael
*/
public class BpLog implements java.io.Serializable {

private PrintStream out;
public enum LogLevel {
Off, Warn, Info, Fine
}
LogLevel level = LogLevel.Info;

public BpLog( PrintStream aStream ){
out = aStream;
}

public BpLog() {
this( System.out );
}

public void warn(Object msg, Object ...args) {
log(LogLevel.Warn, msg, args);
}
Expand Down Expand Up @@ -80,5 +89,9 @@ private Object formatArg(Object arg) {
if ( PASS_THROUGH.contains(arg.getClass()) ) return arg;
return ScriptableUtils.stringify(arg);
}

public void setLoggerPrintStream(PrintStream printStream){
out = printStream;
}

}
15 changes: 14 additions & 1 deletion src/main/java/il/ac/bgu/cs/bp/bpjs/model/BProgram.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import il.ac.bgu.cs.bp.bpjs.model.eventselection.EventSelectionStrategy;
import il.ac.bgu.cs.bp.bpjs.model.eventselection.SimpleEventSelectionStrategy;

import java.io.*;
import java.util.*;
import java.util.concurrent.*;

Expand Down Expand Up @@ -94,6 +95,8 @@ public interface BProgramCallback {

private BpLog.LogLevel preSetLogLevel = null;

private PrintStream preSetPrintStream = null;

/**
* Objects that client code wishes to put in scope before the scope is
* initialized are collected here.
Expand Down Expand Up @@ -322,6 +325,9 @@ private void initProgramScope() {
if ( preSetLogLevel != null ) {
jsProxy.log.setLevel(preSetLogLevel.name());
}
if ( preSetPrintStream != null ) {
jsProxy.log.setLoggerPrintStream(preSetPrintStream);
}
programScope.put("bp", programScope, Context.javaToJS(jsProxy, programScope));

initialScopeValues.entrySet().forEach(e -> putInGlobalScope(e.getKey(), e.getValue()));
Expand Down Expand Up @@ -506,7 +512,14 @@ public void setLogLevel( BpLog.LogLevel aLevel ) {
preSetLogLevel = aLevel;
}
}

public void setLoggerOutputStreamer(PrintStream printStream){
if ( jsProxy != null ) {
jsProxy.log.setLoggerPrintStream(printStream);
} else {
preSetPrintStream = printStream;
}
}

public BpLog.LogLevel getLogLevel() {
return (jsProxy != null ) ? BpLog.LogLevel.valueOf(jsProxy.log.getLevel()) : null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public void testExternalSetLogLevel() throws UnsupportedEncodingException, IOExc
" bp.log.warn(\"msg\");\n" +
"});");
bprog.setLogLevel(BpLog.LogLevel.Fine);
bprog.setLoggerOutputStreamer(myOut);
new BProgramRunner(bprog).run();
myOut.flush();
}
Expand Down

0 comments on commit 054d5a6

Please sign in to comment.