0

我找到了几个关于如何从 System.out 和 System.err 传输和重定向消息的示例。决定使用 JavaFX Webview 和 Dukescript 开发应用程序后,我发现在一个地方显示所有消息很有用,即 Firebug Lite 控制台。

见下文。

PS这与此完全相反

4

1 回答 1

0

首先定义一个抽象类

public abstract class FirebugConsole extends OutputStream {

  abstract void log( String msg );

  StringBuilder sb = new StringBuilder();

  @Override
  public void write(int i) {
    sb.append((char)i);
  }

  @Override
  public void flush() {
    if( sb.length() >0 && !sb.toString().equals("\r\n"))
      log(sb.toString());
    sb = new StringBuilder();
  }  
}

然后使用将本机调用实现到 JavaScript 的方法对其进行扩展。例如,这里是如何编写日志消息的示例

public class FirebugConsoleInfo extends FirebugConsole{
  @net.java.html.js.JavaScriptBody(args = { "msg" }, body = ""
      + "Firebug.Console.log(msg);")
  public native void log( String msg );
}

最后,将 System.out 和 System.err 传递给这些对象

  public static void onPageLoad() throws Exception {
  ...
      System.setOut(new PrintStream(new FirebugConsoleInfo(), true));
      System.setErr(new PrintStream(new FirebugConsoleError(), true));
  ...
  }

注意:由于某些原因,通常console.log()对我不起作用,我知道如果控制台对象已经存在,Firebug 不会绑定控制台,所以我怀疑 WebFX webview 必须自己将 console.log 消息传递到 System.out第一名。

更新

当消息由浏览器以外的线程生成时,上述解决方案不起作用。这是基于BrwsrCtx.execute()的更新解决方案

  public abstract static class FirebugConsole extends OutputStream {
    protected final BrwsrCtx ctx;

    public FirebugConsole( BrwsrCtx ctx ){
      this.ctx = ctx;
    }
    abstract void logNative( String msg );

    void log(String msg) {
      ctx.execute(new Runnable(){
        @Override
        public void run() {
          logNative(msg);
        }
      });
    }

    StringBuilder sb = new StringBuilder();

    @Override
    public void write(int i) {
      sb.append((char)i);
    }

    @Override
    public void flush() {
      if( sb.length() >0 && !sb.toString().equals("\r\n"))
        log(sb.toString());
      sb = new StringBuilder();
    }  
  }

  public static class FirebugConsoleInfo extends FirebugConsole{
    public FirebugConsoleInfo(BrwsrCtx ctx) {
      super(ctx);
    }

    @net.java.html.js.JavaScriptBody(args = { "msg" }, body = ""
        + "Firebug.Console.log(msg);")
    public native void logNative( String msg );

  }

  public static class FirebugConsoleError extends FirebugConsole{
    public FirebugConsoleError(BrwsrCtx ctx) {
      super(ctx);
    }

    @net.java.html.js.JavaScriptBody(args = { "msg" }, body = ""
        + "Firebug.Console.error(msg);")
    public native void logNative( String msg );
    }
 }

  public static void onPageLoad() throws Exception {
      BrwsrCtx ctx = BrwsrCtx.findDefault(GoGPS_Fx.class);
      System.setOut(new PrintStream(new FirebugConsoleInfo(ctx), true));
      System.setErr(new PrintStream(new FirebugConsoleError(ctx), true));
  }

注意:大型日志的速度很慢,可能有更快的替代方案(StringWriter 就是其中之一)。但我怀疑瓶颈是消息从 Java 到 JavaScript 来回传递。

于 2015-06-15T21:42:41.380 回答