1 /* 2 * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package org.openjdk.jigsaw; 27 28 import java.security.AccessController; 29 import java.security.PrivilegedAction; 30 import java.util.List; 31 import java.util.ArrayList; 32 33 class Trace { 34 35 static int traceLevel = 0; 36 static boolean tracing = false; 37 38 static { 39 PrivilegedAction<String> pa = new PrivilegedAction<String>() { 40 public String run() { return System.getenv("JIGSAW_TRACE"); }}; 41 String v = AccessController.doPrivileged(pa); 42 if (v != null) { 43 traceLevel = Integer.parseInt(v); 44 tracing = traceLevel > 0; 45 } 46 } 47 48 // Trace.trace method may be called while the classes for tracing 49 // e.g. java.util.Formatter) are being loaded. Cache the traces 50 // to avoid infinite loop until the very first call completes. 51 static class Cache { 52 static List<String> formats = new ArrayList<String>(); 53 static List<Object[]> traceArgs = new ArrayList<Object[]>(); 54 static boolean caching = false; 55 56 /** 57 * Returns true if the trace message is added to the cache. 58 */ 59 static synchronized boolean add(String fmt, Object ... args) { 60 if (caching && formats != null) { 61 formats.add(fmt); 62 traceArgs.add(args); 63 return true; 64 } 65 caching = true; 66 return false; 67 } 68 69 static synchronized boolean isEmpty() { 70 return formats == null; 71 } 72 73 static synchronized void printAndClear() { 74 if (isEmpty()) 75 return; 76 77 for (int i=0; i < formats.size(); i++) { 78 System.out.format(formats.get(i), traceArgs.get(i)); 79 } 80 caching = false; 81 formats = null; 82 traceArgs = null; 83 } 84 } 85 86 static void trace(int level, int depth, String fmt, Object ... args) { 87 if (level >= traceLevel) 88 return; 89 90 StringBuilder sb = new StringBuilder(); 91 sb.append("| "); 92 for (int i = 0; i < level; i++) 93 sb.append(" "); 94 if (depth > 0) { 95 for (int i = 0; i < depth; i++) 96 sb.append("-"); 97 sb.append(" "); 98 } 99 sb.append(fmt); 100 sb.append("%n"); 101 102 // Cache the traces until the first call to the format method 103 // returns to avoid recursion while classes are being loaded 104 if (Cache.add(sb.toString(), args)) { 105 return; 106 } 107 108 System.out.format(sb.toString(), args); 109 110 if (!Cache.isEmpty()) { 111 // now classes needed by tracing are loaded and initialized 112 // print all cached traces 113 Cache.printAndClear(); 114 } 115 } 116 117 static void trace(int level, String fmt, Object ... args) { 118 trace(level, 0, fmt, args); 119 } 120 121 }