| 1 | /* |
|---|---|
| 2 | * Copyright (C) 2007-2010 JĂșlio Vilmar Gesser. |
| 3 | * Copyright (C) 2011, 2013-2020 The JavaParser Team. |
| 4 | * |
| 5 | * This file is part of JavaParser. |
| 6 | * |
| 7 | * JavaParser can be used either under the terms of |
| 8 | * a) the GNU Lesser General Public License as published by |
| 9 | * the Free Software Foundation, either version 3 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * b) the terms of the Apache License |
| 12 | * |
| 13 | * You should have received a copy of both licenses in LICENCE.LGPL and |
| 14 | * LICENCE.APACHE. Please refer to those files for details. |
| 15 | * |
| 16 | * JavaParser is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU Lesser General Public License for more details. |
| 20 | */ |
| 21 | |
| 22 | package com.github.javaparser.utils; |
| 23 | |
| 24 | import java.io.IOException; |
| 25 | import java.io.PrintWriter; |
| 26 | import java.io.StringWriter; |
| 27 | import java.util.function.Supplier; |
| 28 | |
| 29 | import static com.github.javaparser.utils.CodeGenerationUtils.f; |
| 30 | |
| 31 | /** |
| 32 | * To avoid dependencies on logging frameworks, we have invented yet another logging framework :-) |
| 33 | * <p> |
| 34 | * See <a href="http://javaparser.org/javaparsers-logging-framework-in-one-file/">a blog about this</a> |
| 35 | */ |
| 36 | public class Log { |
| 37 | /** |
| 38 | * This adapter logs to standard out and standard error. |
| 39 | */ |
| 40 | public static class StandardOutStandardErrorAdapter implements Adapter { |
| 41 | @Override |
| 42 | public void info(Supplier<String> messageSupplier) { |
| 43 | System.out.println(messageSupplier.get()); |
| 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public void trace(Supplier<String> messageSupplier) { |
| 48 | System.out.println(messageSupplier.get()); |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public void error(Supplier<Throwable> throwableSupplier, Supplier<String> messageSupplier) { |
| 53 | Throwable throwable = throwableSupplier.get(); |
| 54 | String message = messageSupplier.get(); |
| 55 | if (message == null) { |
| 56 | System.err.println(throwable.getMessage()); |
| 57 | printStackTrace(throwable); |
| 58 | } else if (throwable == null) { |
| 59 | System.err.println(message); |
| 60 | } else { |
| 61 | System.err.println(message + ":" + throwable.getMessage()); |
| 62 | printStackTrace(throwable); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | private void printStackTrace(Throwable throwable) { |
| 67 | try (StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw)) { |
| 68 | throwable.printStackTrace(pw); |
| 69 | trace(sw::toString); |
| 70 | } catch (IOException e) { |
| 71 | throw new AssertionError("Error in logging library"); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * This adapter logs nothing. |
| 78 | */ |
| 79 | public static class SilentAdapter implements Adapter { |
| 80 | @Override |
| 81 | public void info(Supplier<String> messageSupplier) { |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public void trace(Supplier<String> messageSupplier) { |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public void error(Supplier<Throwable> throwableSupplier, Supplier<String> messageSupplier) { |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | public interface Adapter { |
| 94 | |
| 95 | void info(Supplier<String> message); |
| 96 | |
| 97 | void trace(Supplier<String> message); |
| 98 | |
| 99 | /** |
| 100 | * Both can supply a null. |
| 101 | */ |
| 102 | void error(Supplier<Throwable> throwableSupplier, Supplier<String> messageSupplier); |
| 103 | } |
| 104 | |
| 105 | private static Adapter CURRENT_ADAPTER = new SilentAdapter(); |
| 106 | |
| 107 | /** |
| 108 | * Change how logging is handled. You can set your own implementation that forwards to your logging library. |
| 109 | */ |
| 110 | public static void setAdapter(Adapter adapter) { |
| 111 | CURRENT_ADAPTER = adapter; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * For logging information that may help solving a problem. |
| 116 | */ |
| 117 | @SafeVarargs |
| 118 | public static void trace(String format, Supplier<Object>... args) { |
| 119 | CURRENT_ADAPTER.trace(makeFormattingSupplier(format, args)); |
| 120 | } |
| 121 | |
| 122 | private static Supplier<String> makeFormattingSupplier(String format, Supplier<Object>[] args) { |
| 123 | return () -> { |
| 124 | Object[] objects = new Object[args.length]; |
| 125 | for (int i = 0; i < args.length; i++) { |
| 126 | objects[i] = args[i].get(); |
| 127 | } |
| 128 | return f(format, objects); |
| 129 | }; |
| 130 | } |
| 131 | |
| 132 | |
| 133 | /** |
| 134 | * For logging things that are nice to see scrolling by. |
| 135 | */ |
| 136 | @SafeVarargs |
| 137 | public static void info(String format, Supplier<Object>... args) { |
| 138 | CURRENT_ADAPTER.info(makeFormattingSupplier(format, args)); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * For drawing attention to an error. |
| 143 | */ |
| 144 | public static void error(Throwable throwable) { |
| 145 | CURRENT_ADAPTER.error(() -> throwable, null); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * For drawing attention to an error that you don't have an exception for. |
| 150 | */ |
| 151 | @SafeVarargs |
| 152 | public static void error(Throwable throwable, String format, Supplier<Object>... args) { |
| 153 | CURRENT_ADAPTER.error(() -> throwable, makeFormattingSupplier(format, args)); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * For drawing attention to an error that you don't have an exception for. |
| 158 | */ |
| 159 | @SafeVarargs |
| 160 | public static void error(String format, Supplier<Object>... args) { |
| 161 | CURRENT_ADAPTER.error(() -> null, makeFormattingSupplier(format, args)); |
| 162 | } |
| 163 | } |
| 164 |
Members