From c1ba9c6bfaa1bc539029df76b3ed07d8a3f979e6 Mon Sep 17 00:00:00 2001 From: Soy Authors Date: Mon, 13 May 2024 14:36:08 -0700 Subject: [PATCH] DO NOT SUBMIT - this is just a prototye PiperOrigin-RevId: 633335474 --- .../template/soy/jbcsrc/api/SoySauce.java | 14 ++++++++ .../template/soy/jbcsrc/api/SoySauceImpl.java | 34 +++++++++++++++---- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/java/src/com/google/template/soy/jbcsrc/api/SoySauce.java b/java/src/com/google/template/soy/jbcsrc/api/SoySauce.java index 5e26851a73..27b1ba951c 100644 --- a/java/src/com/google/template/soy/jbcsrc/api/SoySauce.java +++ b/java/src/com/google/template/soy/jbcsrc/api/SoySauce.java @@ -23,6 +23,7 @@ import com.google.common.collect.ImmutableSet; import com.google.errorprone.annotations.CanIgnoreReturnValue; import com.google.errorprone.annotations.CheckReturnValue; +import com.google.template.soy.data.LoggingAdvisingAppendable; import com.google.template.soy.data.SanitizedContent; import com.google.template.soy.data.SanitizedContent.ContentKind; import com.google.template.soy.data.SoyInjector; @@ -225,6 +226,19 @@ default WriteContinuation renderHtml(Appendable out) throws IOException { return renderHtml(asAdvisingAppendable(out)); } + /** + * Renders the configured html template to the given appendable, returning a continuation. If + * captureLogs is true, logs are writtent to the {@link LoggingAdvisingAppendable}. + * + *

Verifies that the content type is {@link ContentKind.HTML} (corresponding to kind="html" + * in the template). + * + *

See {@link #renderHtml(AdvisingAppendable out)} for more details). + */ + @CheckReturnValue + WriteContinuation renderHtml(LoggingAdvisingAppendable out, boolean captureLogs) + throws IOException; + /** * Renders the configured html template to a {@link SanitizedContent}. Verifies that the content * type is {@link ContentKind.HTML} (corresponding to kind="html" in the template). diff --git a/java/src/com/google/template/soy/jbcsrc/api/SoySauceImpl.java b/java/src/com/google/template/soy/jbcsrc/api/SoySauceImpl.java index a897a6b6b2..c7e1534b31 100644 --- a/java/src/com/google/template/soy/jbcsrc/api/SoySauceImpl.java +++ b/java/src/com/google/template/soy/jbcsrc/api/SoySauceImpl.java @@ -28,6 +28,7 @@ import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSortedSet; import com.google.errorprone.annotations.CanIgnoreReturnValue; +import com.google.template.soy.data.LoggingAdvisingAppendable; import com.google.template.soy.data.RecordProperty; import com.google.template.soy.data.SanitizedContent; import com.google.template.soy.data.SanitizedContent.ContentKind; @@ -278,6 +279,12 @@ public WriteContinuation renderHtml(AdvisingAppendable out) throws IOException { return startRender(out, ContentKind.HTML); } + @Override + public WriteContinuation renderHtml(LoggingAdvisingAppendable out, boolean captureLogs) + throws IOException { + return startRender(out, ContentKind.HTML, captureLogs); + } + @Override public Continuation renderHtml() { return startRenderToSanitizedContent(ContentKind.HTML); @@ -359,11 +366,21 @@ Continuation startRenderToValue(ContentKind contentKind) { private WriteContinuation startRender(AdvisingAppendable out, ContentKind contentKind) throws IOException { + return startRender(out, contentKind, /* captureLogs= */ false); + } + + private WriteContinuation startRender( + AdvisingAppendable out, ContentKind contentKind, boolean captureLogs) throws IOException { enforceContentKind(contentKind); ParamStore params = data == null ? ParamStore.EMPTY_INSTANCE : data; RenderContext context = makeContext(); - OutputAppendable output = OutputAppendable.create(out, context.getLogger()); + LoggingAdvisingAppendable output; + if (captureLogs && out instanceof LoggingAdvisingAppendable) { + output = (LoggingAdvisingAppendable) out; + } else { + output = OutputAppendable.create(out, context.getLogger()); + } return doRender(template, params, output, context); } @@ -386,7 +403,10 @@ private void enforceContentKind(ContentKind expectedContentKind) { } private static WriteContinuation doRender( - CompiledTemplate template, ParamStore params, OutputAppendable output, RenderContext context) + CompiledTemplate template, + ParamStore params, + LoggingAdvisingAppendable output, + RenderContext context) throws IOException { RenderResult result; try { @@ -420,7 +440,7 @@ abstract static class ContinuationImpl { // ThreadSafety guaranteed by the guarded write on hasContinueBeenCalled final CompiledTemplate template; final ParamStore params; - final OutputAppendable output; + final LoggingAdvisingAppendable output; final RenderContext context; boolean hasContinueBeenCalled; @@ -433,7 +453,7 @@ public RenderResult result() { RenderResult result, CompiledTemplate template, ParamStore params, - OutputAppendable output, + LoggingAdvisingAppendable output, RenderContext context) { checkArgument(!result.isDone()); this.result = checkNotNull(result); @@ -457,7 +477,7 @@ private static final class WriteContinuationImpl extends ContinuationImpl RenderResult result, CompiledTemplate template, ParamStore params, - OutputAppendable output, + LoggingAdvisingAppendable output, RenderContext context) { super(result, template, params, output, context); } @@ -475,7 +495,7 @@ Continuation doRenderToValue( StringBuilder underlying, CompiledTemplate template, ParamStore params, - OutputAppendable output, + LoggingAdvisingAppendable output, RenderContext context) { RenderResult result; try { @@ -519,7 +539,7 @@ private static final class ValueContinuationImpl< RenderResult result, CompiledTemplate template, ParamStore params, - OutputAppendable output, + LoggingAdvisingAppendable output, RenderContext context) { super(result, template, params, output, context); this.targetKind = checkNotNull(targetKind);