aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/galacticgreg/auxiliary/LogHelper.java
blob: d4a3bb5dfcc8065bcddb6f1c2a31c17960f97fc7 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package galacticgreg.auxiliary;

import java.util.ArrayList;

import org.apache.logging.log4j.Level;

import cpw.mods.fml.common.FMLLog;

/**
 * Generic LogHelper to print stuff to the console
 *
 * @author Namikon
 */
public final class LogHelper {

    private ArrayList<String> _mReportedCategories = new ArrayList<>();
    private boolean doDebugLogs = false;
    private boolean doTraceLogs = false;
    private boolean quietMode = false;
    private String _mModID = "";

    private final static String STR_NOCAT = "ihaznocathegory";
    private final static String STR_TOKEN_ONETIMEMESSAGE = " OTM";

    public LogHelper(String pModID) {
        _mModID = pModID;
    }

    /**
     * If true, only error/fatal/warn messages will be printed
     *
     * @param pEnabled
     */
    public void setQuietMode(boolean pEnabled) {
        quietMode = pEnabled;
    }

    /**
     * Enable/Disable debug logs
     *
     * @param pEnabled
     */
    public void setDebugOutput(boolean pEnabled) {
        doDebugLogs = pEnabled;
    }

    /**
     * Enable/Disable trace logs
     *
     * @param pEnabled
     */
    public void setTraceOutput(boolean pEnabled) {
        doTraceLogs = pEnabled;
    }

    /**
     * Resets all One-Time categories, so they will be displayed again
     */
    public void ResetCategories() {
        _mReportedCategories = new ArrayList<>();
    }

    /**
     * Print a log-message with built-in String.format(x) support. This message will only appear once. usefull for
     * error/warnings within loops
     *
     * @param pCategory The category for this message. Used to identify the function, use an easy to memorize name. Will
     *                  never be displayed
     * @param pLogLevel The logLevel for this message
     * @param pMessage  The log message
     * @param args      Optional args, if you've used format-specifier in pMessage
     */
    public void log(String pCategory, Level pLogLevel, String pMessage, Object... args) {
        if (pLogLevel == Level.DEBUG && !doDebugLogs) return;

        if (pLogLevel == Level.TRACE && !doTraceLogs) return;

        if (pLogLevel != Level.ERROR && pLogLevel != Level.FATAL && pLogLevel != Level.WARN) if (quietMode) return;

        String tt = "";
        if (!pCategory.equals(STR_NOCAT)) {
            tt = STR_TOKEN_ONETIMEMESSAGE;
            if (_mReportedCategories.contains(pCategory)) return;
            else {
                _mReportedCategories.add(pCategory);
            }
        }

        FMLLog.log(_mModID.toUpperCase() + tt, pLogLevel, pMessage, args);
    }

    /**
     * Prints a one-time message with Category ALL
     *
     * @param pCategory The category for this message. Used to identify the function, use an easy to memorize name. Will
     *                  never be displayed
     * @param object    The log message
     * @param args      Optional args, if you've used format-specifier in pMessage
     */
    public void ot_all(String pCategory, String object, Object... args) {
        log(pCategory, Level.ALL, object, args);
    }

    /**
     * Prints a one-time message with Category DEBUG
     *
     * @param pCategory The category for this message. Used to identify the function, use an easy to memorize name. Will
     *                  never be displayed
     * @param object    The log message
     * @param args      Optional args, if you've used format-specifier in pMessage
     */
    public void ot_debug(String pCategory, String object, Object... args) {
        log(pCategory, Level.DEBUG, object, args);
    }

    /**
     * Prints a one-time message with Category ERROR
     *
     * @param pCategory The category for this message. Used to identify the function, use an easy to memorize name. Will
     *                  never be displayed
     * @param object    The log message
     * @param args      Optional args, if you've used format-specifier in pMessage
     */
    public void ot_error(String pCategory, String object, Object... args) {
        log(pCategory, Level.ERROR, object, args);
    }

    /**
     * Prints a one-time message with Category FATAL
     *
     * @param pCategory The category for this message. Used to identify the function, use an easy to memorize name. Will
     *                  never be displayed
     * @param object    The log message
     * @param args      Optional args, if you've used format-specifier in pMessage
     */
    public void ot_fatal(String pCategory, String object, Object... args) {
        log(pCategory, Level.FATAL, object, args);
    }

    /**
     * Prints a one-time message with Category INFO
     *
     * @param pCategory The category for this message. Used to identify the function, use an easy to memorize name. Will
     *                  never be displayed
     * @param object    The log message
     * @param args      Optional args, if you've used format-specifier in pMessage
     */
    public void ot_info(String pCategory, String object, Object... args) {
        log(pCategory, Level.INFO, object, args);
    }

    /**
     * Prints a one-time message with Category OFF
     *
     * @param pCategory The category for this message. Used to identify the function, use an easy to memorize name. Will
     *                  never be displayed
     * @param object    The log message
     * @param args      Optional args, if you've used format-specifier in pMessage
     */
    public void ot_off(String pCategory, String object, Object... args) {
        log(pCategory, Level.OFF, object, args);
    }

    /**
     * Prints a one-time message with Category TRACE
     *
     * @param pCategory The category for this message. Used to identify the function, use an easy to memorize name. Will
     *                  never be displayed
     * @param object    The log message
     * @param args      Optional args, if you've used format-specifier in pMessage
     */
    public void ot_trace(String pCategory, String object, Object... args) {
        log(pCategory, Level.TRACE, object, args);
    }

    /**
     * Prints a one-time message with Category WARN
     *
     * @param pCategory The category for this message. Used to identify the function, use an easy to memorize name. Will
     *                  never be displayed
     * @param object    The log message
     * @param args      Optional args, if you've used format-specifier in pMessage
     */
    public void ot_warn(String pCategory, String object, Object... args) {
        log(pCategory, Level.WARN, object, args);
    }

    /**
     * Prints a message with Category ALL
     *
     * @param object The log message
     * @param args   Optional args, if you've used format-specifier in pMessage
     */
    public void all(String object, Object... args) {
        log(STR_NOCAT, Level.ALL, object, args);
    }

    /**
     * Prints a message with Category DEBUG
     *
     * @param object The log message
     * @param args   Optional args, if you've used format-specifier in pMessage
     */
    public void debug(String object, Object... args) {
        log(STR_NOCAT, Level.DEBUG, object, args);
    }

    /**
     * Prints a message with Category ERROR
     *
     * @param object The log message
     * @param args   Optional args, if you've used format-specifier in pMessage
     */
    public void error(String object, Object... args) {
        log(STR_NOCAT, Level.ERROR, object, args);
    }

    /**
     * Prints a message with Category FATAL
     *
     * @param object The log message
     * @param args   Optional args, if you've used format-specifier in pMessage
     */
    public void fatal(String object, Object... args) {
        log(STR_NOCAT, Level.FATAL, object, args);
    }

    /**
     * Prints a message with Category INFO
     *
     * @param object The log message
     * @param args   Optional args, if you've used format-specifier in pMessage
     */
    public void info(String object, Object... args) {
        log(STR_NOCAT, Level.INFO, object, args);
    }

    /**
     * Prints a message with Category OFF
     *
     * @param object The log message
     * @param args   Optional args, if you've used format-specifier in pMessage
     */
    public void off(String object, Object... args) {
        log(STR_NOCAT, Level.OFF, object, args);
    }

    /**
     * Prints a message with Category TRACE
     *
     * @param object The log message
     * @param args   Optional args, if you've used format-specifier in pMessage
     */
    public void trace(String object, Object... args) {
        log(STR_NOCAT, Level.TRACE, object, args);
    }

    /**
     * Prints a message with Category WARN
     *
     * @param object The log message
     * @param args   Optional args, if you've used format-specifier in pMessage
     */
    public void warn(String object, Object... args) {
        log(STR_NOCAT, Level.WARN, object, args);
    }
}