1/* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.databinding.tool.util; 18 19import android.databinding.tool.processing.ScopedException; 20 21import java.io.PrintWriter; 22import java.io.StringWriter; 23 24import javax.lang.model.element.Element; 25import javax.tools.Diagnostic; 26import javax.tools.Diagnostic.Kind; 27 28public class L { 29 private static boolean sEnableDebug = false; 30 private static final Client sSystemClient = new Client() { 31 @Override 32 public void printMessage(Kind kind, String message, Element element) { 33 if (kind == Kind.ERROR) { 34 System.err.println(message); 35 } else { 36 System.out.println(message); 37 } 38 } 39 }; 40 41 private static Client sClient = sSystemClient; 42 43 public static void setClient(Client systemClient) { 44 sClient = systemClient; 45 } 46 47 public static void setDebugLog(boolean enabled) { 48 sEnableDebug = enabled; 49 } 50 51 public static void d(String msg, Object... args) { 52 if (sEnableDebug) { 53 printMessage(null, Diagnostic.Kind.NOTE, String.format(msg, args)); 54 } 55 } 56 57 public static void d(Element element, String msg, Object... args) { 58 if (sEnableDebug) { 59 printMessage(element, Diagnostic.Kind.NOTE, String.format(msg, args)); 60 } 61 } 62 63 public static void d(Throwable t, String msg, Object... args) { 64 if (sEnableDebug) { 65 printMessage(null, Diagnostic.Kind.NOTE, 66 String.format(msg, args) + " " + getStackTrace(t)); 67 } 68 } 69 70 public static void w(String msg, Object... args) { 71 printMessage(null, Kind.WARNING, String.format(msg, args)); 72 } 73 74 public static void w(Element element, String msg, Object... args) { 75 printMessage(element, Kind.WARNING, String.format(msg, args)); 76 } 77 78 public static void w(Throwable t, String msg, Object... args) { 79 printMessage(null, Kind.WARNING, 80 String.format(msg, args) + " " + getStackTrace(t)); 81 } 82 83 private static void tryToThrowScoped(Throwable t, String fullMessage) { 84 if (t instanceof ScopedException) { 85 ScopedException ex = (ScopedException) t; 86 if (ex.isValid()) { 87 throw ex; 88 } 89 } 90 ScopedException ex = new ScopedException(fullMessage); 91 if (ex.isValid()) { 92 throw ex; 93 } 94 } 95 96 public static void e(String msg, Object... args) { 97 String fullMsg = String.format(msg, args); 98 tryToThrowScoped(null, fullMsg); 99 printMessage(null, Diagnostic.Kind.ERROR, fullMsg); 100 } 101 102 public static void e(Element element, String msg, Object... args) { 103 String fullMsg = String.format(msg, args); 104 tryToThrowScoped(null, fullMsg); 105 printMessage(element, Diagnostic.Kind.ERROR, fullMsg); 106 } 107 108 public static void e(Throwable t, String msg, Object... args) { 109 String fullMsg = String.format(msg, args); 110 tryToThrowScoped(t, fullMsg); 111 printMessage(null, Diagnostic.Kind.ERROR, 112 fullMsg + " " + getStackTrace(t)); 113 } 114 115 private static void printMessage(Element element, Diagnostic.Kind kind, String message) { 116 sClient.printMessage(kind, message, element); 117 if (kind == Diagnostic.Kind.ERROR) { 118 throw new RuntimeException("failure, see logs for details.\n" + message); 119 } 120 } 121 122 public static boolean isDebugEnabled() { 123 return sEnableDebug; 124 } 125 126 public interface Client { 127 void printMessage(Diagnostic.Kind kind, String message, Element element); 128 } 129 130 private static String getStackTrace(Throwable t) { 131 StringWriter sw = new StringWriter(); 132 PrintWriter pw = new PrintWriter(sw); 133 try { 134 t.printStackTrace(pw); 135 } finally { 136 pw.close(); 137 } 138 return sw.toString(); 139 } 140} 141