1d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar/*
2d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar * Copyright (C) 2015 The Android Open Source Project
3d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar *
4d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar * Licensed under the Apache License, Version 2.0 (the "License");
5d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar * you may not use this file except in compliance with the License.
6d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar * You may obtain a copy of the License at
7d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar *
8d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar *      http://www.apache.org/licenses/LICENSE-2.0
9d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar *
10d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar * Unless required by applicable law or agreed to in writing, software
11d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar * distributed under the License is distributed on an "AS IS" BASIS,
12d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar * See the License for the specific language governing permissions and
14d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar * limitations under the License.
15d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar */
16d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar
17fead9ca09b117136b35bc5bf137340a754f9edddGeorge Mountpackage android.databinding.tool.util;
1897d6ddf47f4ff1abb3ed5201ce5232163f5325b1Yigit Boyar
19731b74f7f44e67312a1fc4161c4e0aae221b2417Yigit Boyarimport android.databinding.tool.processing.ScopedException;
20731b74f7f44e67312a1fc4161c4e0aae221b2417Yigit Boyar
214ba16229a40e9758db86d4fb1df5119fdcb8aa2aDeepanshu Guptaimport java.io.PrintWriter;
224ba16229a40e9758db86d4fb1df5119fdcb8aa2aDeepanshu Guptaimport java.io.StringWriter;
234ba16229a40e9758db86d4fb1df5119fdcb8aa2aDeepanshu Gupta
24d3f2b9229472c9dae9bf4ae8b3e2d653b5653b01George Mountimport javax.lang.model.element.Element;
2597d6ddf47f4ff1abb3ed5201ce5232163f5325b1Yigit Boyarimport javax.tools.Diagnostic;
261331801c598a377a2c16e1aed8f975b728adc06eGeorge Mountimport javax.tools.Diagnostic.Kind;
2797d6ddf47f4ff1abb3ed5201ce5232163f5325b1Yigit Boyar
28d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyarpublic class L {
290cb9fbb96197af013f4f879ed6cddf2681b88fd6Yigit Boyar    private static boolean sEnableDebug = false;
300cb9fbb96197af013f4f879ed6cddf2681b88fd6Yigit Boyar    private static final Client sSystemClient = new Client() {
310cb9fbb96197af013f4f879ed6cddf2681b88fd6Yigit Boyar        @Override
32d3f2b9229472c9dae9bf4ae8b3e2d653b5653b01George Mount        public void printMessage(Kind kind, String message, Element element) {
330cb9fbb96197af013f4f879ed6cddf2681b88fd6Yigit Boyar            if (kind == Kind.ERROR) {
340cb9fbb96197af013f4f879ed6cddf2681b88fd6Yigit Boyar                System.err.println(message);
350cb9fbb96197af013f4f879ed6cddf2681b88fd6Yigit Boyar            } else {
360cb9fbb96197af013f4f879ed6cddf2681b88fd6Yigit Boyar                System.out.println(message);
370cb9fbb96197af013f4f879ed6cddf2681b88fd6Yigit Boyar            }
380cb9fbb96197af013f4f879ed6cddf2681b88fd6Yigit Boyar        }
390cb9fbb96197af013f4f879ed6cddf2681b88fd6Yigit Boyar    };
400cb9fbb96197af013f4f879ed6cddf2681b88fd6Yigit Boyar
410cb9fbb96197af013f4f879ed6cddf2681b88fd6Yigit Boyar    private static Client sClient = sSystemClient;
420cb9fbb96197af013f4f879ed6cddf2681b88fd6Yigit Boyar
430cb9fbb96197af013f4f879ed6cddf2681b88fd6Yigit Boyar    public static void setClient(Client systemClient) {
444bba5894f1291dde9fb5b06b8f7a33db0bc5b670Deepanshu Gupta        sClient = systemClient;
450cb9fbb96197af013f4f879ed6cddf2681b88fd6Yigit Boyar    }
460cb9fbb96197af013f4f879ed6cddf2681b88fd6Yigit Boyar
47b1356339eaa6c8e967e4fc1dc283b82909a1208dYigit Boyar    public static void setDebugLog(boolean enabled) {
48b1356339eaa6c8e967e4fc1dc283b82909a1208dYigit Boyar        sEnableDebug = enabled;
49b1356339eaa6c8e967e4fc1dc283b82909a1208dYigit Boyar    }
50b1356339eaa6c8e967e4fc1dc283b82909a1208dYigit Boyar
51d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar    public static void d(String msg, Object... args) {
52d911e414a989cad68befdeb554580d59ad81f04aYigit Boyar        if (sEnableDebug) {
53d3f2b9229472c9dae9bf4ae8b3e2d653b5653b01George Mount            printMessage(null, Diagnostic.Kind.NOTE, String.format(msg, args));
54d3f2b9229472c9dae9bf4ae8b3e2d653b5653b01George Mount        }
55d3f2b9229472c9dae9bf4ae8b3e2d653b5653b01George Mount    }
56d3f2b9229472c9dae9bf4ae8b3e2d653b5653b01George Mount
57d3f2b9229472c9dae9bf4ae8b3e2d653b5653b01George Mount    public static void d(Element element, String msg, Object... args) {
58d3f2b9229472c9dae9bf4ae8b3e2d653b5653b01George Mount        if (sEnableDebug) {
59d3f2b9229472c9dae9bf4ae8b3e2d653b5653b01George Mount            printMessage(element, Diagnostic.Kind.NOTE, String.format(msg, args));
60d911e414a989cad68befdeb554580d59ad81f04aYigit Boyar        }
6197d6ddf47f4ff1abb3ed5201ce5232163f5325b1Yigit Boyar    }
6297d6ddf47f4ff1abb3ed5201ce5232163f5325b1Yigit Boyar
6397d6ddf47f4ff1abb3ed5201ce5232163f5325b1Yigit Boyar    public static void d(Throwable t, String msg, Object... args) {
64d911e414a989cad68befdeb554580d59ad81f04aYigit Boyar        if (sEnableDebug) {
65d3f2b9229472c9dae9bf4ae8b3e2d653b5653b01George Mount            printMessage(null, Diagnostic.Kind.NOTE,
664ba16229a40e9758db86d4fb1df5119fdcb8aa2aDeepanshu Gupta                    String.format(msg, args) + " " + getStackTrace(t));
67d911e414a989cad68befdeb554580d59ad81f04aYigit Boyar        }
68d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar    }
69d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar
701331801c598a377a2c16e1aed8f975b728adc06eGeorge Mount    public static void w(String msg, Object... args) {
71d3f2b9229472c9dae9bf4ae8b3e2d653b5653b01George Mount        printMessage(null, Kind.WARNING, String.format(msg, args));
72d3f2b9229472c9dae9bf4ae8b3e2d653b5653b01George Mount    }
73d3f2b9229472c9dae9bf4ae8b3e2d653b5653b01George Mount
74d3f2b9229472c9dae9bf4ae8b3e2d653b5653b01George Mount    public static void w(Element element, String msg, Object... args) {
75d3f2b9229472c9dae9bf4ae8b3e2d653b5653b01George Mount        printMessage(element, Kind.WARNING, String.format(msg, args));
761331801c598a377a2c16e1aed8f975b728adc06eGeorge Mount    }
771331801c598a377a2c16e1aed8f975b728adc06eGeorge Mount
781331801c598a377a2c16e1aed8f975b728adc06eGeorge Mount    public static void w(Throwable t, String msg, Object... args) {
79d3f2b9229472c9dae9bf4ae8b3e2d653b5653b01George Mount        printMessage(null, Kind.WARNING,
804ba16229a40e9758db86d4fb1df5119fdcb8aa2aDeepanshu Gupta                String.format(msg, args) + " " + getStackTrace(t));
811331801c598a377a2c16e1aed8f975b728adc06eGeorge Mount    }
821331801c598a377a2c16e1aed8f975b728adc06eGeorge Mount
83731b74f7f44e67312a1fc4161c4e0aae221b2417Yigit Boyar    private static void tryToThrowScoped(Throwable t, String fullMessage) {
84731b74f7f44e67312a1fc4161c4e0aae221b2417Yigit Boyar        if (t instanceof ScopedException) {
85731b74f7f44e67312a1fc4161c4e0aae221b2417Yigit Boyar            ScopedException ex = (ScopedException) t;
86731b74f7f44e67312a1fc4161c4e0aae221b2417Yigit Boyar            if (ex.isValid()) {
87731b74f7f44e67312a1fc4161c4e0aae221b2417Yigit Boyar                throw ex;
88731b74f7f44e67312a1fc4161c4e0aae221b2417Yigit Boyar            }
89731b74f7f44e67312a1fc4161c4e0aae221b2417Yigit Boyar        }
90731b74f7f44e67312a1fc4161c4e0aae221b2417Yigit Boyar        ScopedException ex = new ScopedException(fullMessage);
91731b74f7f44e67312a1fc4161c4e0aae221b2417Yigit Boyar        if (ex.isValid()) {
92731b74f7f44e67312a1fc4161c4e0aae221b2417Yigit Boyar            throw ex;
93731b74f7f44e67312a1fc4161c4e0aae221b2417Yigit Boyar        }
94731b74f7f44e67312a1fc4161c4e0aae221b2417Yigit Boyar    }
95731b74f7f44e67312a1fc4161c4e0aae221b2417Yigit Boyar
96d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar    public static void e(String msg, Object... args) {
97731b74f7f44e67312a1fc4161c4e0aae221b2417Yigit Boyar        String fullMsg = String.format(msg, args);
98731b74f7f44e67312a1fc4161c4e0aae221b2417Yigit Boyar        tryToThrowScoped(null, fullMsg);
99d3f2b9229472c9dae9bf4ae8b3e2d653b5653b01George Mount        printMessage(null, Diagnostic.Kind.ERROR, fullMsg);
100d3f2b9229472c9dae9bf4ae8b3e2d653b5653b01George Mount    }
101d3f2b9229472c9dae9bf4ae8b3e2d653b5653b01George Mount
102d3f2b9229472c9dae9bf4ae8b3e2d653b5653b01George Mount    public static void e(Element element, String msg, Object... args) {
103d3f2b9229472c9dae9bf4ae8b3e2d653b5653b01George Mount        String fullMsg = String.format(msg, args);
104d3f2b9229472c9dae9bf4ae8b3e2d653b5653b01George Mount        tryToThrowScoped(null, fullMsg);
105d3f2b9229472c9dae9bf4ae8b3e2d653b5653b01George Mount        printMessage(element, Diagnostic.Kind.ERROR, fullMsg);
106d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar    }
10797d6ddf47f4ff1abb3ed5201ce5232163f5325b1Yigit Boyar
108d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar    public static void e(Throwable t, String msg, Object... args) {
109731b74f7f44e67312a1fc4161c4e0aae221b2417Yigit Boyar        String fullMsg = String.format(msg, args);
110731b74f7f44e67312a1fc4161c4e0aae221b2417Yigit Boyar        tryToThrowScoped(t, fullMsg);
111d3f2b9229472c9dae9bf4ae8b3e2d653b5653b01George Mount        printMessage(null, Diagnostic.Kind.ERROR,
1124ba16229a40e9758db86d4fb1df5119fdcb8aa2aDeepanshu Gupta                fullMsg + " " + getStackTrace(t));
113d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar    }
11497d6ddf47f4ff1abb3ed5201ce5232163f5325b1Yigit Boyar
115d3f2b9229472c9dae9bf4ae8b3e2d653b5653b01George Mount    private static void printMessage(Element element, Diagnostic.Kind kind, String message) {
116d3f2b9229472c9dae9bf4ae8b3e2d653b5653b01George Mount        sClient.printMessage(kind, message, element);
1170cb9fbb96197af013f4f879ed6cddf2681b88fd6Yigit Boyar        if (kind == Diagnostic.Kind.ERROR) {
1180cb9fbb96197af013f4f879ed6cddf2681b88fd6Yigit Boyar            throw new RuntimeException("failure, see logs for details.\n" + message);
11997d6ddf47f4ff1abb3ed5201ce5232163f5325b1Yigit Boyar        }
12097d6ddf47f4ff1abb3ed5201ce5232163f5325b1Yigit Boyar    }
12197d6ddf47f4ff1abb3ed5201ce5232163f5325b1Yigit Boyar
122731b74f7f44e67312a1fc4161c4e0aae221b2417Yigit Boyar    public static boolean isDebugEnabled() {
123731b74f7f44e67312a1fc4161c4e0aae221b2417Yigit Boyar        return sEnableDebug;
124731b74f7f44e67312a1fc4161c4e0aae221b2417Yigit Boyar    }
125731b74f7f44e67312a1fc4161c4e0aae221b2417Yigit Boyar
126d3f2b9229472c9dae9bf4ae8b3e2d653b5653b01George Mount    public interface Client {
127d3f2b9229472c9dae9bf4ae8b3e2d653b5653b01George Mount        void printMessage(Diagnostic.Kind kind, String message, Element element);
1280cb9fbb96197af013f4f879ed6cddf2681b88fd6Yigit Boyar    }
1294ba16229a40e9758db86d4fb1df5119fdcb8aa2aDeepanshu Gupta
1304ba16229a40e9758db86d4fb1df5119fdcb8aa2aDeepanshu Gupta    private static String getStackTrace(Throwable t) {
1314bba5894f1291dde9fb5b06b8f7a33db0bc5b670Deepanshu Gupta        StringWriter sw = new StringWriter();
1324bba5894f1291dde9fb5b06b8f7a33db0bc5b670Deepanshu Gupta        PrintWriter pw = new PrintWriter(sw);
1334ba16229a40e9758db86d4fb1df5119fdcb8aa2aDeepanshu Gupta        try {
1344ba16229a40e9758db86d4fb1df5119fdcb8aa2aDeepanshu Gupta            t.printStackTrace(pw);
1354ba16229a40e9758db86d4fb1df5119fdcb8aa2aDeepanshu Gupta        } finally {
1364ba16229a40e9758db86d4fb1df5119fdcb8aa2aDeepanshu Gupta            pw.close();
1374ba16229a40e9758db86d4fb1df5119fdcb8aa2aDeepanshu Gupta        }
1384bba5894f1291dde9fb5b06b8f7a33db0bc5b670Deepanshu Gupta        return sw.toString();
1394ba16229a40e9758db86d4fb1df5119fdcb8aa2aDeepanshu Gupta    }
140d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar}
141