List of usage examples for java.lang StackTraceElement getFileName
public String getFileName()
From source file:org.sakaiproject.portal.charon.SkinnableCharonPortal.java
private void logXEntry() { Exception e = new Exception(); StackTraceElement se = e.getStackTrace()[1]; M_log.info("Log marker " + se.getMethodName() + ":" + se.getFileName() + ":" + se.getLineNumber()); }
From source file:com.simiacryptus.util.io.MarkdownNotebookOutput.java
@Override @SuppressWarnings("unchecked") public <T> T code(@javax.annotation.Nonnull final UncheckedSupplier<T> fn, final int maxLog, final int framesNo) { try {//from w w w .j av a 2s .c om final StackTraceElement callingFrame = Thread.currentThread().getStackTrace()[framesNo]; final String sourceCode = CodeUtil.getInnerText(callingFrame); @javax.annotation.Nonnull final SysOutInterceptor.LoggedResult<TimedResult<Object>> result = SysOutInterceptor.withOutput(() -> { long priorGcMs = ManagementFactory.getGarbageCollectorMXBeans().stream() .mapToLong(x -> x.getCollectionTime()).sum(); final long start = System.nanoTime(); try { @Nullable Object result1 = null; try { result1 = fn.get(); } catch (@javax.annotation.Nonnull final RuntimeException e) { throw e; } catch (@javax.annotation.Nonnull final Exception e) { throw new RuntimeException(e); } long gcTime = ManagementFactory.getGarbageCollectorMXBeans().stream() .mapToLong(x -> x.getCollectionTime()).sum() - priorGcMs; return new TimedResult<Object>(result1, System.nanoTime() - start, gcTime); } catch (@javax.annotation.Nonnull final Throwable e) { long gcTime = ManagementFactory.getGarbageCollectorMXBeans().stream() .mapToLong(x -> x.getCollectionTime()).sum() - priorGcMs; return new TimedResult<Object>(e, System.nanoTime() - start, gcTime); } }); out(anchor(anchorId()) + "Code from [%s:%s](%s#L%s) executed in %.2f seconds (%.3f gc): ", callingFrame.getFileName(), callingFrame.getLineNumber(), linkTo(CodeUtil.findFile(callingFrame)), callingFrame.getLineNumber(), result.obj.seconds(), result.obj.gc_seconds()); String text = sourceCode.replaceAll("\n", "\n "); out("```java"); out(" " + text); out("```"); if (!result.log.isEmpty()) { String summary = summarize(result.log, maxLog).replaceAll("\n", "\n ").replaceAll(" ~", ""); out(anchor(anchorId()) + "Logging: "); out("```"); out(" " + summary); out("```"); } out(""); final Object eval = result.obj.result; if (null != eval) { out(anchor(anchorId()) + "Returns: \n"); String str; boolean escape; if (eval instanceof Throwable) { @javax.annotation.Nonnull final ByteArrayOutputStream out = new ByteArrayOutputStream(); ((Throwable) eval).printStackTrace(new PrintStream(out)); str = new String(out.toByteArray(), "UTF-8"); escape = true;// } else if (eval instanceof Component) { str = image(Util.toImage((Component) eval), "Result"); escape = false; } else if (eval instanceof BufferedImage) { str = image((BufferedImage) eval, "Result"); escape = false; } else if (eval instanceof TableOutput) { str = ((TableOutput) eval).toTextTable(); escape = false; } else { str = eval.toString(); escape = true; } @javax.annotation.Nonnull String fmt = escape ? " " + summarize(str, maxLog).replaceAll("\n", "\n ").replaceAll(" ~", "") : str; if (escape) { out("```"); out(fmt); out("```"); } else { out(fmt); } out("\n\n"); if (eval instanceof RuntimeException) { throw ((RuntimeException) result.obj.result); } if (eval instanceof Throwable) { throw new RuntimeException((Throwable) result.obj.result); } } return (T) eval; } catch (@javax.annotation.Nonnull final IOException e) { throw new RuntimeException(e); } }
From source file:com.simiacryptus.util.io.HtmlNotebookOutput.java
@javax.annotation.Nonnull @SuppressWarnings("unchecked") @Override//from w w w . ja va 2 s .c o m public <T> T code(@javax.annotation.Nonnull final UncheckedSupplier<T> fn, final int maxLog, final int framesNo) { try { final StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace(); final StackTraceElement callingFrame = stackTrace[framesNo]; final String sourceCode = CodeUtil.getInnerText(callingFrame); @javax.annotation.Nonnull final SysOutInterceptor.LoggedResult<TimedResult<Object>> result = SysOutInterceptor.withOutput(() -> { long priorGcMs = ManagementFactory.getGarbageCollectorMXBeans().stream() .mapToLong(x -> x.getCollectionTime()).sum(); final long start = System.nanoTime(); try { @Nullable Object result1 = null; try { result1 = fn.get(); } catch (@javax.annotation.Nonnull final RuntimeException e) { throw e; } catch (@javax.annotation.Nonnull final Exception e) { throw new RuntimeException(e); } long gcTime = ManagementFactory.getGarbageCollectorMXBeans().stream() .mapToLong(x -> x.getCollectionTime()).sum() - priorGcMs; return new TimedResult<Object>(result1, System.nanoTime() - start, gcTime); } catch (@javax.annotation.Nonnull final Throwable e) { long gcTime = ManagementFactory.getGarbageCollectorMXBeans().stream() .mapToLong(x -> x.getCollectionTime()).sum() - priorGcMs; return new TimedResult<Object>(e, System.nanoTime() - start, gcTime); } }); try { @javax.annotation.Nonnull final URI resolved = URI.create(sourceRoot) .resolve(Util.pathTo(CodeUtil.projectRoot, CodeUtil.findFile(callingFrame))); out("<p>Code from <a href='%s#L%s'>%s:%s</a> executed in %.2f seconds: <br/>", resolved, callingFrame.getLineNumber(), callingFrame.getFileName(), callingFrame.getLineNumber(), result.obj.seconds()); } catch (@javax.annotation.Nonnull final Exception e) { out("<p>Code from %s:%s executed in %.2f seconds: <br/>", callingFrame.getFileName(), callingFrame.getLineNumber(), result.obj.seconds()); } out("<pre>"); out(sourceCode); out("</pre>"); if (!result.log.isEmpty()) { out("Logging: <br/>"); out("<pre>"); out(summarize(maxLog, result.log)); out("</pre>"); } out(""); final Object eval = result.obj.result; if (null != eval) { out("Returns: <br/>"); String str; boolean escape; if (eval instanceof Throwable) { @javax.annotation.Nonnull final ByteArrayOutputStream out = new ByteArrayOutputStream(); ((Throwable) eval).printStackTrace(new PrintStream(out)); str = new String(out.toByteArray(), "UTF-8"); escape = true;// } else if (eval instanceof Component) { str = image(Util.toImage((Component) eval), "Result"); escape = false; } else if (eval instanceof BufferedImage) { str = image((BufferedImage) eval, "Result"); escape = false; } else if (eval instanceof TableOutput) { str = ((TableOutput) eval).toHtmlTable(); escape = false; } else { str = eval.toString(); escape = true; } if (escape) { out("<pre>" + summarize(maxLog, str) + "</pre>"); } else { out(summarize(maxLog, str)); } if (escape) { } out("\n\n"); if (eval instanceof Throwable) { throw new RuntimeException((Throwable) result.obj.result); } } out("</p>"); return (T) eval; } catch (@javax.annotation.Nonnull final IOException e) { throw new RuntimeException(e); } }