39 public static class Logger { |
39 public static class Logger { |
40 private boolean verbose = false; |
40 private boolean verbose = false; |
41 private PrintWriter out = null; |
41 private PrintWriter out = null; |
42 private PrintWriter err = null; |
42 private PrintWriter err = null; |
43 |
43 |
44 public Logger(boolean v) { |
44 // verbose defaults to true unless environment variable JPACKAGE_DEBUG |
45 verbose = v; |
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"))); |
46 } |
50 } |
47 |
51 |
48 public void setVerbose(boolean v) { |
52 public void setVerbose() { |
49 verbose = v; |
53 verbose = true; |
50 } |
54 } |
51 |
55 |
52 public boolean isVerbose() { |
56 public boolean isVerbose() { |
53 return verbose; |
57 return verbose; |
54 } |
58 } |
83 System.err.println(msg); |
87 System.err.println(msg); |
84 } |
88 } |
85 } |
89 } |
86 |
90 |
87 public void verbose(Throwable t) { |
91 public void verbose(Throwable t) { |
88 if (out != null && (Log.debug || verbose)) { |
92 if (out != null && verbose) { |
89 t.printStackTrace(out); |
93 t.printStackTrace(out); |
90 } else if (Log.debug || verbose) { |
94 } else if (verbose) { |
91 t.printStackTrace(System.out); |
95 t.printStackTrace(System.out); |
92 } |
96 } |
93 } |
97 } |
94 |
98 |
95 public void verbose(String msg) { |
99 public void verbose(String msg) { |
96 if (out != null && (Log.debug || verbose)) { |
100 if (out != null && verbose) { |
97 out.println(msg); |
101 out.println(msg); |
98 } else if (Log.debug || verbose) { |
102 } else if (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); |
103 System.out.println(msg); |
108 } |
104 } |
109 } |
105 } |
110 } |
106 } |
111 |
107 |
112 private static Logger delegate = null; |
108 private static Logger delegate = null; |
113 private static boolean debug = |
|
114 "true".equals(System.getenv("JPACKAGE_DEBUG")); |
|
115 |
109 |
116 public static void setLogger(Logger l) { |
110 public static void setLogger(Logger logger) { |
117 delegate = l; |
111 delegate = (logger != null) ? logger : new Logger(); |
118 if (l == null) { |
|
119 delegate = new Logger(false); |
|
120 } |
|
121 } |
|
122 |
|
123 public static Logger getLogger() { |
|
124 return delegate; |
|
125 } |
112 } |
126 |
113 |
127 public static void flush() { |
114 public static void flush() { |
128 if (delegate != null) { |
115 if (delegate != null) { |
129 delegate.flush(); |
116 delegate.flush(); |
140 if (delegate != null) { |
127 if (delegate != null) { |
141 delegate.error(msg); |
128 delegate.error(msg); |
142 } |
129 } |
143 } |
130 } |
144 |
131 |
145 public static void setVerbose(boolean v) { |
132 public static void setVerbose() { |
146 if (delegate != null) { |
133 if (delegate != null) { |
147 delegate.setVerbose(v); |
134 delegate.setVerbose(); |
148 } |
135 } |
149 } |
136 } |
150 |
137 |
151 public static boolean isVerbose() { |
138 public static boolean isVerbose() { |
152 if (delegate != null) { |
139 return (delegate != null) ? delegate.isVerbose() : false; |
153 return delegate.isVerbose(); |
|
154 } |
|
155 |
|
156 return false; // Off by default |
|
157 } |
140 } |
158 |
141 |
159 public static void verbose(String msg) { |
142 public static void verbose(String msg) { |
160 if (delegate != null) { |
143 if (delegate != null) { |
161 delegate.verbose(msg); |
144 delegate.verbose(msg); |
165 public static void verbose(Throwable t) { |
148 public static void verbose(Throwable t) { |
166 if (delegate != null) { |
149 if (delegate != null) { |
167 delegate.verbose(t); |
150 delegate.verbose(t); |
168 } |
151 } |
169 } |
152 } |
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 } |
153 } |