JavaParser Source Viewer

Home|JavaParser/com/github/javaparser/utils/Log.java
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
22package com.github.javaparser.utils;
23
24import java.io.IOException;
25import java.io.PrintWriter;
26import java.io.StringWriter;
27import java.util.function.Supplier;
28
29import 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 */
36public 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<StringmessageSupplier) {
43            System.out.println(messageSupplier.get());
44        }
45
46        @Override
47        public void trace(Supplier<StringmessageSupplier) {
48            System.out.println(messageSupplier.get());
49        }
50
51        @Override
52        public void error(Supplier<ThrowablethrowableSupplierSupplier<StringmessageSupplier) {
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<StringmessageSupplier) {
82        }
83
84        @Override
85        public void trace(Supplier<StringmessageSupplier) {
86        }
87
88        @Override
89        public void error(Supplier<ThrowablethrowableSupplierSupplier<StringmessageSupplier) {
90        }
91    }
92
93    public interface Adapter {
94
95        void info(Supplier<Stringmessage);
96
97        void trace(Supplier<Stringmessage);
98
99        /**
100         * Both can supply a null.
101         */
102        void error(Supplier<ThrowablethrowableSupplierSupplier<StringmessageSupplier);
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 formatSupplier<Object>... args) {
119        CURRENT_ADAPTER.trace(makeFormattingSupplier(formatargs));
120    }
121
122    private static Supplier<StringmakeFormattingSupplier(String formatSupplier<Object>[] args) {
123        return () -> {
124            Object[] objects = new Object[args.length];
125            for (int i = 0i < args.lengthi++) {
126                objects[i] = args[i].get();
127            }
128            return f(formatobjects);
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 formatSupplier<Object>... args) {
138        CURRENT_ADAPTER.info(makeFormattingSupplier(formatargs));
139    }
140
141    /**
142     * For drawing attention to an error.
143     */
144    public static void error(Throwable throwable) {
145        CURRENT_ADAPTER.error(() -> throwablenull);
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 throwableString formatSupplier<Object>... args) {
153        CURRENT_ADAPTER.error(() -> throwablemakeFormattingSupplier(formatargs));
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 formatSupplier<Object>... args) {
161        CURRENT_ADAPTER.error(() -> nullmakeFormattingSupplier(formatargs));
162    }
163}
164
MembersX
Log:makeFormattingSupplier:Block:Block:objects
Log:StandardOutStandardErrorAdapter:error
Log:StandardOutStandardErrorAdapter:error:Block:message
Log:setAdapter
Log:Adapter:error
Log:StandardOutStandardErrorAdapter:error:Block:throwable
Log:trace
Log:Adapter:trace
Log:Adapter:info
Log:info
Log:SilentAdapter:trace
Log:StandardOutStandardErrorAdapter:printStackTrace
Log:error
Log:StandardOutStandardErrorAdapter:trace
Log:makeFormattingSupplier
Log:SilentAdapter:error
Log:SilentAdapter:info
Log:CURRENT_ADAPTER
Log:StandardOutStandardErrorAdapter:info
Members
X