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 jdk.jpackage.internal; 27 28 import java.io.ByteArrayOutputStream; 29 import java.io.IOException; 30 import java.io.PrintStream; 31 import java.io.PrintWriter; 32 33 /** 34 * Log 35 * 36 * General purpose logging mechanism. 37 */ 38 public class Log { 39 public static class Logger { 40 private boolean verbose = false; 41 private PrintWriter out = null; 42 private PrintWriter err = null; 43 44 public Logger(boolean v) { 45 verbose = v; 46 } 47 48 public void setVerbose(boolean v) { 49 verbose = v; 50 } 51 52 public boolean isVerbose() { 53 return verbose; 54 } 55 56 public void setPrintWriter(PrintWriter out, PrintWriter err) { 57 this.out = out; 58 this.err = err; 59 } 60 61 public void flush() { 62 if (out != null) { 63 out.flush(); 64 } 65 66 if (err != null) { 67 err.flush(); 68 } 69 } 70 71 public void info(String msg) { 72 if (out != null) { 73 out.println(msg); 74 } else { 75 System.out.println(msg); 76 } 77 } 78 79 public void error(String msg) { 80 if (err != null) { 81 err.println(msg); 82 } else { 83 System.err.println(msg); 84 } 85 } 86 87 public void verbose(Throwable t) { 88 if (out != null && (Log.debug || verbose)) { 89 t.printStackTrace(out); 90 } else if (Log.debug || verbose) { 91 t.printStackTrace(System.out); 92 } 93 } 94 95 public void verbose(String msg) { 96 if (out != null && (Log.debug || verbose)) { 97 out.println(msg); 98 } else if (Log.debug || verbose) { 99 System.out.println(msg); 100 } 101 } 102 103 public void debug(String msg) { 104 if (out != null && Log.debug) { 105 out.println(msg); 106 } else if (Log.debug) { 107 System.out.println(msg); 108 } 109 } 110 } 111 112 private static Logger delegate = null; 113 private static boolean debug = 114 "true".equals(System.getenv("JPACKAGE_DEBUG")); 115 116 public static void setLogger(Logger l) { 117 delegate = l; 118 if (l == null) { 119 delegate = new Logger(false); 120 } 121 } 122 123 public static Logger getLogger() { 124 return delegate; 125 } 126 127 public static void flush() { 128 if (delegate != null) { 129 delegate.flush(); 130 } 131 } 132 133 public static void info(String msg) { 134 if (delegate != null) { 135 delegate.info(msg); 136 } 137 } 138 139 public static void error(String msg) { 140 if (delegate != null) { 141 delegate.error(msg); 142 } 143 } 144 145 public static void setVerbose(boolean v) { 146 if (delegate != null) { 147 delegate.setVerbose(v); 148 } 149 } 150 151 public static boolean isVerbose() { 152 if (delegate != null) { 153 return delegate.isVerbose(); 154 } 155 156 return false; // Off by default 157 } 158 159 public static void verbose(String msg) { 160 if (delegate != null) { 161 delegate.verbose(msg); 162 } 163 } 164 165 public static void verbose(Throwable t) { 166 if (delegate != null) { 167 delegate.verbose(t); 168 } 169 } 170 171 public static void debug(String msg) { 172 if (delegate != null) { 173 delegate.debug(msg); 174 } 175 } 176 177 public static void debug(Throwable t) { 178 try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { 179 try (PrintStream ps = new PrintStream(baos)) { 180 t.printStackTrace(ps); 181 } 182 debug(baos.toString()); 183 } catch (IOException e) { 184 e.printStackTrace(); 185 } 186 } 187 188 public static boolean isDebug() { 189 return debug; 190 } 191 192 public static void setDebug(boolean debug) { 193 Log.debug = debug; 194 } 195 } | 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 jdk.incubator.jpackage.internal; 27 28 import java.io.ByteArrayOutputStream; 29 import java.io.IOException; 30 import java.io.PrintStream; 31 import java.io.PrintWriter; 32 33 /** 34 * Log 35 * 36 * General purpose logging mechanism. 37 */ 38 public class Log { 39 public static class Logger { 40 private boolean verbose = false; 41 private PrintWriter out = null; 42 private PrintWriter err = null; 43 44 // verbose defaults to true unless environment variable JPACKAGE_DEBUG 45 // is set to true. 46 // Then it is only set to true by using --verbose jpackage option 47 48 public Logger() { 49 verbose = ("true".equals(System.getenv("JPACKAGE_DEBUG"))); 50 } 51 52 public void setVerbose() { 53 verbose = true; 54 } 55 56 public boolean isVerbose() { 57 return verbose; 58 } 59 60 public void setPrintWriter(PrintWriter out, PrintWriter err) { 61 this.out = out; 62 this.err = err; 63 } 64 65 public void flush() { 66 if (out != null) { 67 out.flush(); 68 } 69 70 if (err != null) { 71 err.flush(); 72 } 73 } 74 75 public void info(String msg) { 76 if (out != null) { 77 out.println(msg); 78 } else { 79 System.out.println(msg); 80 } 81 } 82 83 public void error(String msg) { 84 if (err != null) { 85 err.println(msg); 86 } else { 87 System.err.println(msg); 88 } 89 } 90 91 public void verbose(Throwable t) { 92 if (out != null && verbose) { 93 t.printStackTrace(out); 94 } else if (verbose) { 95 t.printStackTrace(System.out); 96 } 97 } 98 99 public void verbose(String msg) { 100 if (out != null && verbose) { 101 out.println(msg); 102 } else if (verbose) { 103 System.out.println(msg); 104 } 105 } 106 } 107 108 private static Logger delegate = null; 109 110 public static void setLogger(Logger logger) { 111 delegate = (logger != null) ? logger : new Logger(); 112 } 113 114 public static void flush() { 115 if (delegate != null) { 116 delegate.flush(); 117 } 118 } 119 120 public static void info(String msg) { 121 if (delegate != null) { 122 delegate.info(msg); 123 } 124 } 125 126 public static void error(String msg) { 127 if (delegate != null) { 128 delegate.error(msg); 129 } 130 } 131 132 public static void setVerbose() { 133 if (delegate != null) { 134 delegate.setVerbose(); 135 } 136 } 137 138 public static boolean isVerbose() { 139 return (delegate != null) ? delegate.isVerbose() : false; 140 } 141 142 public static void verbose(String msg) { 143 if (delegate != null) { 144 delegate.verbose(msg); 145 } 146 } 147 148 public static void verbose(Throwable t) { 149 if (delegate != null) { 150 delegate.verbose(t); 151 } 152 } 153 } |