1 /* |
|
2 * Copyright (c) 2002-2016, the original author or authors. |
|
3 * |
|
4 * This software is distributable under the BSD license. See the terms of the |
|
5 * BSD license in the documentation provided with this software. |
|
6 * |
|
7 * http://www.opensource.org/licenses/bsd-license.php |
|
8 */ |
|
9 package jdk.internal.jline.internal; |
|
10 |
|
11 import java.io.ByteArrayOutputStream; |
|
12 import java.io.PrintStream; |
|
13 //import java.util.logging.LogRecord; |
|
14 //import java.util.logging.Logger; |
|
15 |
|
16 import static jdk.internal.jline.internal.Preconditions.checkNotNull; |
|
17 |
|
18 /** |
|
19 * Internal logger. |
|
20 * |
|
21 * @author <a href="mailto:jason@planet57.com">Jason Dillon</a> |
|
22 * @author <a href="mailto:gnodet@gmail.com">Guillaume Nodet</a> |
|
23 * @since 2.0 |
|
24 */ |
|
25 public final class Log |
|
26 { |
|
27 ///CLOVER:OFF |
|
28 |
|
29 public static enum Level |
|
30 { |
|
31 TRACE, |
|
32 DEBUG, |
|
33 INFO, |
|
34 WARN, |
|
35 ERROR |
|
36 } |
|
37 |
|
38 public static final boolean TRACE = Configuration.getBoolean(Log.class.getName() + ".trace"); |
|
39 |
|
40 public static final boolean DEBUG = TRACE || Configuration.getBoolean(Log.class.getName() + ".debug"); |
|
41 |
|
42 private static PrintStream output = System.err; |
|
43 |
|
44 private static boolean useJul = Configuration.getBoolean("jline.log.jul"); |
|
45 |
|
46 public static PrintStream getOutput() { |
|
47 return output; |
|
48 } |
|
49 |
|
50 public static void setOutput(final PrintStream out) { |
|
51 output = checkNotNull(out); |
|
52 } |
|
53 |
|
54 /** |
|
55 * Helper to support rendering messages. |
|
56 */ |
|
57 @TestAccessible |
|
58 static void render(final PrintStream out, final Object message) { |
|
59 if (message.getClass().isArray()) { |
|
60 Object[] array = (Object[]) message; |
|
61 |
|
62 out.print("["); |
|
63 for (int i = 0; i < array.length; i++) { |
|
64 out.print(array[i]); |
|
65 if (i + 1 < array.length) { |
|
66 out.print(","); |
|
67 } |
|
68 } |
|
69 out.print("]"); |
|
70 } |
|
71 else { |
|
72 out.print(message); |
|
73 } |
|
74 } |
|
75 |
|
76 @TestAccessible |
|
77 static void log(final Level level, final Object... messages) { |
|
78 if (useJul) { |
|
79 logWithJul(level, messages); |
|
80 return; |
|
81 } |
|
82 //noinspection SynchronizeOnNonFinalField |
|
83 synchronized (output) { |
|
84 output.format("[%s] ", level); |
|
85 |
|
86 for (int i=0; i<messages.length; i++) { |
|
87 // Special handling for the last message if its a throwable, render its stack on the next line |
|
88 if (i + 1 == messages.length && messages[i] instanceof Throwable) { |
|
89 output.println(); |
|
90 ((Throwable)messages[i]).printStackTrace(output); |
|
91 } |
|
92 else { |
|
93 render(output, messages[i]); |
|
94 } |
|
95 } |
|
96 |
|
97 output.println(); |
|
98 output.flush(); |
|
99 } |
|
100 } |
|
101 |
|
102 static void logWithJul(Level level, Object... messages) { |
|
103 // Logger logger = Logger.getLogger("jline"); |
|
104 // Throwable cause = null; |
|
105 // ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
|
106 // PrintStream ps = new PrintStream(baos); |
|
107 // for (int i = 0; i < messages.length; i++) { |
|
108 // // Special handling for the last message if its a throwable, render its stack on the next line |
|
109 // if (i + 1 == messages.length && messages[i] instanceof Throwable) { |
|
110 // cause = (Throwable) messages[i]; |
|
111 // } |
|
112 // else { |
|
113 // render(ps, messages[i]); |
|
114 // } |
|
115 // } |
|
116 // ps.close(); |
|
117 // LogRecord r = new LogRecord(toJulLevel(level), baos.toString()); |
|
118 // r.setThrown(cause); |
|
119 // logger.log(r); |
|
120 } |
|
121 |
|
122 // private static java.util.logging.Level toJulLevel(Level level) { |
|
123 // switch (level) { |
|
124 // case TRACE: |
|
125 // return java.util.logging.Level.FINEST; |
|
126 // case DEBUG: |
|
127 // return java.util.logging.Level.FINE; |
|
128 // case INFO: |
|
129 // return java.util.logging.Level.INFO; |
|
130 // case WARN: |
|
131 // return java.util.logging.Level.WARNING; |
|
132 // case ERROR: |
|
133 // return java.util.logging.Level.SEVERE; |
|
134 // default: |
|
135 // throw new IllegalArgumentException(); |
|
136 // } |
|
137 // } |
|
138 |
|
139 public static void trace(final Object... messages) { |
|
140 if (TRACE) { |
|
141 log(Level.TRACE, messages); |
|
142 } |
|
143 } |
|
144 |
|
145 public static void debug(final Object... messages) { |
|
146 if (TRACE || DEBUG) { |
|
147 log(Level.DEBUG, messages); |
|
148 } |
|
149 } |
|
150 |
|
151 /** |
|
152 * @since 2.7 |
|
153 */ |
|
154 public static void info(final Object... messages) { |
|
155 log(Level.INFO, messages); |
|
156 } |
|
157 |
|
158 public static void warn(final Object... messages) { |
|
159 log(Level.WARN, messages); |
|
160 } |
|
161 |
|
162 public static void error(final Object... messages) { |
|
163 log(Level.ERROR, messages); |
|
164 } |
|
165 } |
|