001/* 002 * SonarQube 003 * Copyright (C) 2009-2017 SonarSource SA 004 * mailto:info AT sonarsource DOT com 005 * 006 * This program is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 3 of the License, or (at your option) any later version. 010 * 011 * This program is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public License 017 * along with this program; if not, write to the Free Software Foundation, 018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 019 */ 020package org.sonar.api.utils; 021 022import org.slf4j.LoggerFactory; 023 024/** 025 * A very simple profiler to log the time elapsed performing some tasks. 026 * This implementation is not thread-safe. 027 * 028 * @deprecated since 5.1. Replaced by {@link org.sonar.api.utils.log.Profiler} 029 * @since 2.0 030 */ 031@Deprecated 032public class TimeProfiler { 033 034 private org.slf4j.Logger logger; 035 private long start = 0; 036 private String name; 037 private boolean debug = false; 038 039 public TimeProfiler(org.slf4j.Logger logger) { 040 this.logger = logger; 041 } 042 043 public TimeProfiler(Class clazz) { 044 this.logger = LoggerFactory.getLogger(clazz); 045 } 046 047 /** 048 * Use the default Sonar logger 049 */ 050 public TimeProfiler() { 051 this.logger = LoggerFactory.getLogger(getClass()); 052 } 053 054 public TimeProfiler start(String name) { 055 this.name = name; 056 this.start = System.currentTimeMillis(); 057 if (debug) { 058 logger.debug("%s ...", name); 059 } else { 060 logger.info(name + "..."); 061 } 062 return this; 063 } 064 065 public TimeProfiler setLogger(org.slf4j.Logger logger) { 066 this.logger = logger; 067 return this; 068 } 069 070 public org.slf4j.Logger getLogger() { 071 return logger; 072 } 073 074 /** 075 * @since 2.4 076 */ 077 public TimeProfiler setLevelToDebug() { 078 debug = true; 079 return this; 080 } 081 082 public TimeProfiler stop() { 083 if (start > 0) { 084 String format = "{} done: {} ms"; 085 if (debug) { 086 logger.debug(format, name, System.currentTimeMillis() - start); 087 } else { 088 logger.info(format, name, System.currentTimeMillis() - start); 089 } 090 } 091 start = 0; 092 return this; 093 } 094}