L.java revision 0cb9fbb96197af013f4f879ed6cddf2681b88fd6
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 javax.tools.Diagnostic;
22import javax.tools.Diagnostic.Kind;
23
24public class L {
25    private static boolean sEnableDebug = false;
26    private static final Client sSystemClient = new Client() {
27        @Override
28        public void printMessage(Kind kind, String message) {
29            if (kind == Kind.ERROR) {
30                System.err.println(message);
31            } else {
32                System.out.println(message);
33            }
34        }
35    };
36
37    private static Client sClient = sSystemClient;
38
39    public static void setClient(Client systemClient) {
40        L.sClient = systemClient;
41    }
42
43    public static void setDebugLog(boolean enabled) {
44        sEnableDebug = enabled;
45    }
46
47    public static void d(String msg, Object... args) {
48        if (sEnableDebug) {
49            printMessage(Diagnostic.Kind.NOTE, String.format(msg, args));
50        }
51    }
52
53    public static void d(Throwable t, String msg, Object... args) {
54        if (sEnableDebug) {
55            printMessage(Diagnostic.Kind.NOTE,
56                    String.format(msg, args) + " " + ExceptionUtils.getStackTrace(t));
57        }
58    }
59
60    public static void w(String msg, Object... args) {
61        printMessage(Kind.WARNING, String.format(msg, args));
62    }
63
64    public static void w(Throwable t, String msg, Object... args) {
65        printMessage(Kind.WARNING,
66                String.format(msg, args) + " " + ExceptionUtils.getStackTrace(t));
67    }
68
69    public static void e(String msg, Object... args) {
70        printMessage(Diagnostic.Kind.ERROR, String.format(msg, args));
71    }
72
73    public static void e(Throwable t, String msg, Object... args) {
74        printMessage(Diagnostic.Kind.ERROR,
75                String.format(msg, args) + " " + ExceptionUtils.getStackTrace(t));
76    }
77
78    private static void printMessage(Diagnostic.Kind kind, String message) {
79        sClient.printMessage(kind, message);
80        if (kind == Diagnostic.Kind.ERROR) {
81            throw new RuntimeException("failure, see logs for details.\n" + message);
82        }
83    }
84
85    public static interface Client {
86        public void printMessage(Diagnostic.Kind kind, String message);
87    }
88}
89