Path: blob/master/src/jdk.compiler/share/classes/com/sun/tools/sjavac/Log.java
41175 views
/*1* Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.tools.sjavac;2627import java.io.PrintWriter;28import java.io.StringWriter;29import java.io.Writer;30import java.util.Locale;3132/**33* Utility class only for sjavac logging.34*35* Logging in sjavac has special requirements when running in server/client36* mode. Most of the log messages is generated server-side, but the server37* is typically spawned by the client in the background, so the user usually38* does not see the server stdout/stderr. For this reason log messages needs39* to relayed back to the client that performed the request that generated the40* log message. To support this use case this class maintains a per-thread log41* instance so that each connected client can have its own instance that42* relays messages back to the requesting client.43*44* On the client-side (or when running sjavac without server-mode) there will45* typically just be one Log instance.46*47* <p><b>This is NOT part of any supported API.48* If you write code that depends on this, you do so at your own risk.49* This code and its internal interfaces are subject to change or50* deletion without notice.</b>51*/52public class Log {5354public enum Level {55ERROR,56WARN,57INFO,58DEBUG,59TRACE;60}6162private static Log stdOutErr = new Log(new PrintWriter(System.out), new PrintWriter(System.err));63private static ThreadLocal<Log> loggers = new ThreadLocal<>();6465protected PrintWriter err; // Used for error and warning messages66protected PrintWriter out; // Used for other messages67protected Level level = Level.INFO;6869public Log(Writer out, Writer err) {70this.out = out == null ? null : new PrintWriter(out, true);71this.err = err == null ? null : new PrintWriter(err, true);72}7374public static void setLogForCurrentThread(Log log) {75loggers.set(log);76}7778public static void setLogLevel(String l) {79setLogLevel(Level.valueOf(l.toUpperCase(Locale.US)));80}8182public static void setLogLevel(Level l) {83get().level = l;84}8586public static void trace(String msg) {87log(Level.TRACE, msg);88}8990public static void debug(String msg) {91log(Level.DEBUG, msg);92}9394public static void info(String msg) {95log(Level.INFO, msg);96}9798public static void warn(String msg) {99log(Level.WARN, msg);100}101102public static void error(String msg) {103log(Level.ERROR, msg);104}105106public static void error(Throwable t) {107log(Level.ERROR, t);108}109110public static void log(Level l, String msg) {111get().printLogMsg(l, msg);112}113114public static void debug(Throwable t) {115log(Level.DEBUG, t);116}117118public static void log(Level l, Throwable t) {119StringWriter sw = new StringWriter();120t.printStackTrace(new PrintWriter(sw, true));121log(l, sw.toString());122}123124public static boolean isDebugging() {125return get().isLevelLogged(Level.DEBUG);126}127128protected boolean isLevelLogged(Level l) {129return l.ordinal() <= level.ordinal();130}131132public static Log get() {133Log log = loggers.get();134return log != null ? log : stdOutErr;135}136137protected void printLogMsg(Level msgLevel, String msg) {138if (isLevelLogged(msgLevel)) {139PrintWriter pw = msgLevel.ordinal() <= Level.WARN.ordinal() ? err : out;140pw.println(msg);141}142}143}144145146