All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cat.inspiracio.util.Timber Maven / Gradle / Ivy

/*    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;

public class Timber {

	private static ThreadLocal local=new ThreadLocal(){
		@Override protected Timber initialValue(){return new Timber();}
	};

	private Mapstarts=new HashMap();
	private StringBuilder events=new StringBuilder();
	
	// public --------------------------------
	
	public static void start(String key){
		Timber t=local.get();
		t.starts.put(key, System.currentTimeMillis());
	}
	
	public static void stop(String key){
		Timber t=local.get();
		Long start=t.starts.remove(key);//may be null
		if(start==null)
			return;//No call to start(key) since last string()
		long delta=System.currentTimeMillis()-start;
		t.events.append(key).append('=').append(delta).append("\n");
	}
	
	/** @return Can be "" but never null. */
	public static String string(){
		Timber t=local.get();
		String s=t.events.toString();
		t.events=new StringBuilder();
		t.starts.clear();
		return s;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy