01: /*
02: * Copyright 2006 The Kuali Foundation.
03: *
04: * Licensed under the Educational Community License, Version 1.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.opensource.org/licenses/ecl1.php
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.kuali.core.util;
17:
18: import org.apache.commons.logging.Log;
19: import org.apache.commons.logging.LogFactory;
20:
21: public class Timer {
22: private static final Log LOG = LogFactory.getLog(Timer.class);
23:
24: private static int indent = 0;
25:
26: private long t0;
27: private long t1;
28: private String name;
29:
30: public Timer(String name) {
31: indent++;
32: if (LOG.isDebugEnabled()) {
33: LOG.debug(indent() + name + ": started");
34: }
35: this .name = name;
36: reset();
37: }
38:
39: public long getElapsed() {
40: t1 = System.currentTimeMillis();
41: return t1 - t0;
42: }
43:
44: public void reset() {
45: t0 = System.currentTimeMillis();
46: }
47:
48: public String indent() {
49: String whitespace = " ";
50: try {
51: return whitespace.substring(whitespace.length() - indent);
52: } catch (Exception e) {
53: return "";
54: }
55: }
56:
57: public void log() {
58: if (LOG.isDebugEnabled()) {
59: long elapsed = getElapsed();
60: LOG.debug(indent() + name + ": " + elapsed + "millisec");
61: indent--;
62: }
63: }
64:
65: }
|