L.java revision 731b74f7f44e67312a1fc4161c4e0aae221b2417
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 org.apache.commons.lang3.exception.ExceptionUtils;
20
21import android.databinding.tool.processing.ScopedException;
22
23import javax.tools.Diagnostic;
24import javax.tools.Diagnostic.Kind;
25
26public class L {
27    private static boolean sEnableDebug = false;
28    private static final Client sSystemClient = new Client() {
29        @Override
30        public void printMessage(Kind kind, String message) {
31            if (kind == Kind.ERROR) {
32                System.err.println(message);
33            } else {
34                System.out.println(message);
35            }
36        }
37    };
38
39    private static Client sClient = sSystemClient;
40
41    public static void setClient(Client systemClient) {
42        L.sClient = systemClient;
43    }
44
45    public static void setDebugLog(boolean enabled) {
46        sEnableDebug = enabled;
47    }
48
49    public static void d(String msg, Object... args) {
50        if (sEnableDebug) {
51            printMessage(Diagnostic.Kind.NOTE, String.format(msg, args));
52        }
53    }
54
55    public static void d(Throwable t, String msg, Object... args) {
56        if (sEnableDebug) {
57            printMessage(Diagnostic.Kind.NOTE,
58                    String.format(msg, args) + " " + ExceptionUtils.getStackTrace(t));
59        }
60    }
61
62    public static void w(String msg, Object... args) {
63        printMessage(Kind.WARNING, String.format(msg, args));
64    }
65
66    public static void w(Throwable t, String msg, Object... args) {
67        printMessage(Kind.WARNING,
68                String.format(msg, args) + " " + ExceptionUtils.getStackTrace(t));
69    }
70
71    private static void tryToThrowScoped(Throwable t, String fullMessage) {
72        if (t instanceof ScopedException) {
73            ScopedException ex = (ScopedException) t;
74            if (ex.isValid()) {
75                throw ex;
76            }
77        }
78        ScopedException ex = new ScopedException(fullMessage);
79        if (ex.isValid()) {
80            throw ex;
81        }
82    }
83
84    public static void e(String msg, Object... args) {
85        String fullMsg = String.format(msg, args);
86        tryToThrowScoped(null, fullMsg);
87        printMessage(Diagnostic.Kind.ERROR, fullMsg);
88    }
89
90    public static void e(Throwable t, String msg, Object... args) {
91        String fullMsg = String.format(msg, args);
92        tryToThrowScoped(t, fullMsg);
93        printMessage(Diagnostic.Kind.ERROR,
94                fullMsg + " " + ExceptionUtils.getStackTrace(t));
95    }
96
97    private static void printMessage(Diagnostic.Kind kind, String message) {
98        sClient.printMessage(kind, message);
99        if (kind == Diagnostic.Kind.ERROR) {
100            throw new RuntimeException("failure, see logs for details.\n" + message);
101        }
102    }
103
104    public static boolean isDebugEnabled() {
105        return sEnableDebug;
106    }
107
108    public static interface Client {
109        public void printMessage(Diagnostic.Kind kind, String message);
110    }
111}
112