Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/tracing/PrintStreamProviderFactory.java
38829 views
/*1* Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.tracing;2627import java.lang.reflect.Method;28import java.io.PrintStream;29import java.util.HashMap;3031import com.sun.tracing.ProviderFactory;32import com.sun.tracing.Provider;33import com.sun.tracing.ProviderName;34import com.sun.tracing.Probe;35import com.sun.tracing.ProbeName;3637/**38* Factory class to create tracing Providers.39*40* This factory will create tracing instances that print to a PrintStream41* when activated.42*43* @since 1.744*/45public class PrintStreamProviderFactory extends ProviderFactory {4647private PrintStream stream;4849public PrintStreamProviderFactory(PrintStream stream) {50this.stream = stream;51}5253public <T extends Provider> T createProvider(Class<T> cls) {54PrintStreamProvider provider = new PrintStreamProvider(cls, stream);55provider.init();56return provider.newProxyInstance();57}58}5960class PrintStreamProvider extends ProviderSkeleton {6162private PrintStream stream;63private String providerName;6465protected ProbeSkeleton createProbe(Method m) {66String probeName = getAnnotationString(m, ProbeName.class, m.getName());67return new PrintStreamProbe(this, probeName, m.getParameterTypes());68}6970PrintStreamProvider(Class<? extends Provider> type, PrintStream stream) {71super(type);72this.stream = stream;73this.providerName = getProviderName();74}7576PrintStream getStream() {77return stream;78}7980String getName() {81return providerName;82}83}8485class PrintStreamProbe extends ProbeSkeleton {8687private PrintStreamProvider provider;88private String name;8990PrintStreamProbe(PrintStreamProvider p, String name, Class<?>[] params) {91super(params);92this.provider = p;93this.name = name;94}9596public boolean isEnabled() {97return true;98}99100public void uncheckedTrigger(Object[] args) {101StringBuffer sb = new StringBuffer();102sb.append(provider.getName());103sb.append(".");104sb.append(name);105sb.append("(");106boolean first = true;107for (Object o : args) {108if (first == false) {109sb.append(",");110} else {111first = false;112}113sb.append(o.toString());114}115sb.append(")");116provider.getStream().println(sb.toString());117}118}119120121122