
cat.inspiracio.util.Timber Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of orange-servlet Show documentation
Show all versions of orange-servlet Show documentation
Orange Servlet provides HTML templating with server-side Java.
OrangeServlet is a servlet that is registered on *.html.
It reads the html file at the right location according to URL.
It renders the file as html, executing some scripting:
* data-if="E" attributes for server-side conditional,
* data-for="T x : E" attributes for server-side loops,
* data-import="C" attributes for server-side class imports,
* data-substitute="file.html" substitute an element with the processed contents of a file,
* ${expressions} in element bodies and attribute values.
The newest version!
/* Copyright 2019 Alexander Bunkenburg
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cat.inspiracio.util;
import java.util.HashMap;
import java.util.Map;
/** Useful for timing parts of request processing.
* Usage:
*
* Timber.start("hot-section");
* ...
* Timber.stop("hot-section"); // anywhere in same thread
*
* */
public class Timber {
/** Private construction */
private Timber(){}
/** Timber instance hooked to thread. */
private static final ThreadLocal local = new ThreadLocal(){
@Override protected Timber initialValue(){return new Timber();}
};
/** Started keys with their start time. */
private final Map started = new HashMap();
/** Putting together some output. */
private StringBuilder events = new StringBuilder();
// public --------------------------------
/** Start a new timing
* @param key identified by this key. */
public static void start(String key) {
Timber t = local.get();
t.started.put(key, System.currentTimeMillis());
}
/** Stop a timing
* @param key identified by this key.
* @return How long did it take, in ms? (If there was no such section, 0.) */
public static long stop(String key) {
Timber t = local.get();
Long start = t.started.remove(key); // may be null
if(start==null) return 0L; // No previous call to start(key).
long delta = System.currentTimeMillis()-start;
t.events.append(key).append('=').append(delta).append("\n");
return delta;
}
/** A description of all finished timed sections.
* @return Can be "" but never null. */
public static String string() {
Timber t = local.get();
String s = t.events.toString();
t.events = new StringBuilder();
t.started.clear();
return s;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy