1 /*
   2  * Copyright (c) 2011, 2019, 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 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 }