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         // 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 }