1 /*
   2  * Copyright (c) 2011, 2014, 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 com.oracle.tools.packager;
  27 
  28 import java.io.ByteArrayOutputStream;
  29 import java.io.IOException;
  30 import java.io.PrintStream;
  31 
  32 public class Log {
  33     public static class Logger {
  34         private boolean verbose = false;
  35 
  36         public Logger(boolean v) {
  37             verbose = v;
  38         }
  39 
  40         public void info(String msg) {
  41             System.out.println(msg);
  42         }
  43 
  44         public void verbose(Throwable t) {
  45             if (Log.debug || verbose) {
  46                 t.printStackTrace(System.out);
  47             }
  48         }
  49 
  50         public void verbose(String msg) {
  51             if (Log.debug || verbose) {
  52                 System.out.println(msg);
  53             }
  54         }
  55 
  56         public void debug(String msg) {
  57             if (Log.debug) {
  58                 System.out.println(msg);
  59             }
  60         }
  61     }
  62 
  63     private static Logger delegate = null;
  64     private static boolean debug =
  65             "true".equals(System.getenv("JAVAFX_ANT_DEBUG"));
  66 
  67     public static void setLogger(Logger l) {
  68         delegate = l;
  69         if (l == null) {
  70             delegate = new Logger(false);
  71         }
  72     }
  73 
  74 
  75     public static void info(String msg) {
  76         if (delegate != null) {
  77            delegate.info(msg);
  78         }
  79     }
  80 
  81     public static void verbose(String msg) {
  82         if (delegate != null) {
  83            delegate.verbose(msg);
  84         }
  85     }
  86 
  87     public static void verbose(Throwable t) {
  88         if (delegate != null) {
  89            delegate.verbose(t);
  90         }
  91     }
  92 
  93     public static void debug(String msg) {
  94         if (delegate != null) {
  95            delegate.debug(msg);
  96         }
  97     }
  98 
  99     public static void debug(RuntimeException re) {
 100         debug((Throwable) re);
 101     }
 102 
 103     public static void debug(Throwable t) {
 104         try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
 105             try (PrintStream ps = new PrintStream(baos)) {
 106                 t.printStackTrace(ps);
 107             }
 108             debug(baos.toString());
 109         } catch (IOException e) {
 110             e.printStackTrace();
 111         }
 112     }
 113 
 114     public static boolean isDebug() {
 115         return debug;
 116     }
 117 
 118     public static void setDebug(boolean debug) {
 119         Log.debug = debug;
 120     }
 121 }