• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.github.javaparser.utils;
2 
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.io.StringWriter;
6 import java.util.function.Supplier;
7 
8 import static com.github.javaparser.utils.CodeGenerationUtils.f;
9 
10 /**
11  * To avoid dependencies on logging frameworks, we have invented yet another logging framework :-)
12  * <p>
13  * See <a href="http://javaparser.org/javaparsers-logging-framework-in-one-file/">a blog about this</a>
14  */
15 public class Log {
16     /**
17      * This adapter logs to standard out and standard error.
18      */
19     public static class StandardOutStandardErrorAdapter implements Adapter {
20         @Override
info(Supplier<String> messageSupplier)21         public void info(Supplier<String> messageSupplier) {
22             System.out.println(messageSupplier.get());
23         }
24 
25         @Override
trace(Supplier<String> messageSupplier)26         public void trace(Supplier<String> messageSupplier) {
27             System.out.println(messageSupplier.get());
28         }
29 
30         @Override
error(Supplier<Throwable> throwableSupplier, Supplier<String> messageSupplier)31         public void error(Supplier<Throwable> throwableSupplier, Supplier<String> messageSupplier) {
32             Throwable throwable = throwableSupplier.get();
33             String message = messageSupplier.get();
34             if (message == null) {
35                 System.err.println(throwable.getMessage());
36                 printStackTrace(throwable);
37             } else if (throwable == null) {
38                 System.err.println(message);
39             } else {
40                 System.err.println(message + ":" + throwable.getMessage());
41                 printStackTrace(throwable);
42             }
43         }
44 
printStackTrace(Throwable throwable)45         private void printStackTrace(Throwable throwable) {
46             try (StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw)) {
47                 throwable.printStackTrace(pw);
48                 trace(sw::toString);
49             } catch (IOException e) {
50                 throw new AssertionError("Error in logging library");
51             }
52         }
53     }
54 
55     /**
56      * This adapter logs nothing.
57      */
58     public static class SilentAdapter implements Adapter {
59         @Override
info(Supplier<String> messageSupplier)60         public void info(Supplier<String> messageSupplier) {
61         }
62 
63         @Override
trace(Supplier<String> messageSupplier)64         public void trace(Supplier<String> messageSupplier) {
65         }
66 
67         @Override
error(Supplier<Throwable> throwableSupplier, Supplier<String> messageSupplier)68         public void error(Supplier<Throwable> throwableSupplier, Supplier<String> messageSupplier) {
69         }
70     }
71 
72     public interface Adapter {
73 
info(Supplier<String> message)74         void info(Supplier<String> message);
75 
trace(Supplier<String> message)76         void trace(Supplier<String> message);
77 
78         /**
79          * Both can supply a null.
80          */
error(Supplier<Throwable> throwableSupplier, Supplier<String> messageSupplier)81         void error(Supplier<Throwable> throwableSupplier, Supplier<String> messageSupplier);
82     }
83 
84     private static Adapter CURRENT_ADAPTER = new SilentAdapter();
85 
86     /**
87      * Change how logging is handled. You can set your own implementation that forwards to your logging library.
88      */
setAdapter(Adapter adapter)89     public static void setAdapter(Adapter adapter) {
90         CURRENT_ADAPTER = adapter;
91     }
92 
93     /**
94      * For logging information that may help solving a problem.
95      */
96     @SafeVarargs
trace(String format, Supplier<Object>... args)97     public static void trace(String format, Supplier<Object>... args) {
98         CURRENT_ADAPTER.trace(makeFormattingSupplier(format, args));
99     }
100 
makeFormattingSupplier(String format, Supplier<Object>[] args)101     private static Supplier<String> makeFormattingSupplier(String format, Supplier<Object>[] args) {
102         return () -> {
103             Object[] objects = new Object[args.length];
104             for (int i = 0; i < args.length; i++) {
105                 objects[i] = args[i].get();
106             }
107             return f(format, objects);
108         };
109     }
110 
111 
112     /**
113      * For logging things that are nice to see scrolling by.
114      */
115     @SafeVarargs
info(String format, Supplier<Object>... args)116     public static void info(String format, Supplier<Object>... args) {
117         CURRENT_ADAPTER.info(makeFormattingSupplier(format, args));
118     }
119 
120     /**
121      * For drawing attention to an error.
122      */
error(Throwable throwable)123     public static void error(Throwable throwable) {
124         CURRENT_ADAPTER.error(() -> throwable, null);
125     }
126 
127     /**
128      * For drawing attention to an error that you don't have an exception for.
129      */
130     @SafeVarargs
error(Throwable throwable, String format, Supplier<Object>... args)131     public static void error(Throwable throwable, String format, Supplier<Object>... args) {
132         CURRENT_ADAPTER.error(() -> throwable, makeFormattingSupplier(format, args));
133     }
134 
135     /**
136      * For drawing attention to an error that you don't have an exception for.
137      */
138     @SafeVarargs
error(String format, Supplier<Object>... args)139     public static void error(String format, Supplier<Object>... args) {
140         CURRENT_ADAPTER.error(() -> null, makeFormattingSupplier(format, args));
141     }
142 }
143