Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've written something similar which I put as a public closure in a category I then mixin to services:</p> <pre><code>// TimingCategory.groovy /** * Provides common AOP timing functionality to Services which mixin this category. */ class TimingCategory { static Closure timer = { String label = "The call", Closure closure -&gt; Long start = System.currentTimeMillis() def result = closure.call() Long end = System.currentTimeMillis() Long duration = end - start log.warn "${label} took ${duration} ms" return result } } </code></pre> <p>In other classes, you just reference the <code>timer</code> closure as such:</p> <pre><code>@Mixin(TimingCategory) public class WhateverService { public String doSomeWork() { timer "Doing a lot of work", { 1000.times { doSomething() } someMethodWithAStringReturnValue() } } } </code></pre> <p>That will give you log output of <strong>"WARN: Doing a lot of work took nn ms"</strong> and return the value of the inner closure as the return value of the <code>doSomeWork</code> method. </p> <p>For your taglib instance, just wrap the <code>out &lt;&lt; ...</code> in the</p> <pre><code>timer "Writing an emoticon", { // your code } </code></pre> <p>code. </p> <p>If you don't care about passing through the internal return value, you could instead return the duration as the result of the closure invocation. </p> <p><strong>Update</strong>:</p> <p>I might have misread -- you're asking how to wrap the taglib execution without modifying the taglib code at all? What about creating a custom taglib which accepts the body and passes it to the other taglibs for execution?</p> <p>I haven't tried this, but something like:</p> <pre><code>class TimedTagLib { static namespace = "timer" def timedTag = { attrs, body -&gt; timer "Executing the timed tag", { out &lt;&lt; body() } } } </code></pre> <p>And invoking it like</p> <pre><code>&lt;timer:timedTag&gt;&lt;g:emoticon whatever="something"&gt;Text&lt;/g:emoticon&gt;&lt;/timer:timedTag&gt; </code></pre> <p><strong>Update 2:</strong></p> <p>Ok, so I tried it. Works fine. My final code (I added a second timer closure which returns the duration):</p> <pre><code>// TimedTagLib.groovy @Mixin(TimingCategory) class TimedTagLib { static namespace = "timer" def timedTag = { attrs, body -&gt; def duration = returnTimer "Printing the timed tag", { out &lt;&lt; body() } out &lt;&lt; "Took ${duration} ms to print" } } </code></pre> <p>And the view:</p> <pre><code>// someView.gsp &lt;timer:timedTag&gt; &lt;g:formatLocalDate date="${LocalDate.now()}" /&gt; &lt;/timer:timedTag&gt; </code></pre> <p>The resulting HTML is:</p> <pre><code>03/19/2013 Took 6 ms to print </code></pre> <p>And it also wrote to the log. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload