aboutsummaryrefslogtreecommitdiff
path: root/javaparser-core/src/main/java/com/github/javaparser/utils/Log.java
blob: 12a9cdc85d77537e35e6ffece6f4f2eeacc3a000 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package com.github.javaparser.utils;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;

import static com.github.javaparser.utils.CodeGenerationUtils.f;

/**
 * To avoid dependencies on logging frameworks, we have invented yet another logging framework :-)
 */
public class Log {
    /**
     * This adapter logs to standard out and standard error.
     */
    public static class StandardOutStandardErrorAdapter implements Adapter {
        @Override
        public void info(String message) {
            System.out.println(message);
        }

        @Override
        public void trace(String message) {
            System.out.println(message);
        }

        @Override
        public void error(Throwable throwable, String message) {
            if (message == null) {
                System.err.println(throwable.getMessage());
                printStackTrace(throwable);
            } else if (throwable == null) {
                System.err.println(message);
            } else {
                System.err.println(message + ":" + throwable.getMessage());
                printStackTrace(throwable);
            }
        }

        private void printStackTrace(Throwable throwable) {
            try (StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw)) {
                throwable.printStackTrace(pw);
                trace(sw.toString());
            } catch (IOException e) {
                throw new AssertionError("Error in logging library");
            }
        }
    }

    /**
     * This adapter logs nothing.
     */
    public static class SilentAdapter implements Adapter {
        @Override
        public void info(String message) {
        }

        @Override
        public void trace(String message) {
        }

        @Override
        public void error(Throwable throwable, String f) {
        }
    }

    public interface Adapter {

        void info(String message);

        void trace(String message);

        /**
         * Both can be null.
         */
        void error(Throwable throwable, String f);
    }

    private static Adapter CURRENT_ADAPTER = new SilentAdapter();

    /**
     * Change how logging is handled. You can set your own implementation that forwards to your logging library.
     */
    public static void setAdapter(Adapter adapter) {
        CURRENT_ADAPTER = adapter;
    }

    /**
     * For logging information that may help solving a problem.
     */
    public static void trace(String format, Object... args) {
        CURRENT_ADAPTER.trace(f(format, args));
    }

    /**
     * For logging things that are nice to see scrolling by.
     */
    public static void info(String format, Object... args) {
        CURRENT_ADAPTER.info(f(format, args));
    }

    /**
     * For drawing attention to an error.
     */
    public static void error(Throwable throwable) {
        CURRENT_ADAPTER.error(throwable, null);
    }

    /**
     * For drawing attention to an error that you don't have an exception for.
     */
    public static void error(Throwable throwable, String format, Object... args) {
        CURRENT_ADAPTER.error(throwable, f(format, args));
    }

    /**
     * For drawing attention to an error that you don't have an exception for.
     */
    public static void error(String format, Object... args) {
        CURRENT_ADAPTER.error(null, f(format, args));
    }
}