001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    
020    import java.io.Closeable;
021    
022    /**
023     * @author Shuyang Zhou
024     */
025    public class LoggingTimer implements Closeable {
026    
027            public LoggingTimer() {
028                    this(_getInvokerName(null), System.currentTimeMillis());
029            }
030    
031            public LoggingTimer(String name) {
032                    this(_getInvokerName(name), System.currentTimeMillis());
033            }
034    
035            @Override
036            public void close() {
037                    if (_log.isInfoEnabled()) {
038                            _log.info(
039                                    "Completed " + _name + " in " +
040                                            (System.currentTimeMillis() - _startTime) + " ms");
041                    }
042            }
043    
044            private static String _getInvokerName(String name) {
045                    Thread thread = Thread.currentThread();
046    
047                    StackTraceElement[] stackTraceElements = thread.getStackTrace();
048    
049                    StackTraceElement stackTraceElement = stackTraceElements[3];
050    
051                    StringBundler sb = new StringBundler(5);
052    
053                    sb.append(stackTraceElement.getClassName());
054                    sb.append(StringPool.POUND);
055                    sb.append(stackTraceElement.getMethodName());
056    
057                    if (name != null) {
058                            sb.append(StringPool.POUND);
059                            sb.append(name);
060                    }
061    
062                    return sb.toString();
063            }
064    
065            private LoggingTimer(String name, long startTime) {
066                    _name = name;
067                    _startTime = startTime;
068    
069                    if (_log.isInfoEnabled()) {
070                            _log.info("Starting " + name);
071                    }
072            }
073    
074            private static final Log _log = LogFactoryUtil.getLog(LoggingTimer.class);
075    
076            private final String _name;
077            private final long _startTime;
078    
079    }