Skip to content

Commit 25b04f2

Browse files
Fix null exception in tests
Add string formatting to logger Add tests for logger formatting
1 parent 820825e commit 25b04f2

File tree

6 files changed

+286
-28
lines changed

6 files changed

+286
-28
lines changed

extractor/src/main/java/org/schabi/newpipe/extractor/Extractor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,9 @@ public ContentCountry getExtractorContentCountry() {
156156
public TimeAgoParser getTimeAgoParser() {
157157
return getService().getTimeAgoParser(getExtractorLocalization());
158158
}
159+
160+
@Override
161+
public String toString() {
162+
return getClass().getSimpleName();
163+
}
159164
}

extractor/src/main/java/org/schabi/newpipe/extractor/NewPipe.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ public static void init(final Downloader d, final Localization l) {
5555
}
5656

5757
public static void init(final Downloader d, final Localization l, final ContentCountry c) {
58-
ExtractorLogger.d(TAG, "Initializing with downloader: "
59-
+ d.getClass().getSimpleName() + ", " + l + ", " + c);
58+
ExtractorLogger.d(TAG, "Initializing with downloader={}, localization={}, country={}",
59+
d, l, c);
6060
downloader = d;
6161
preferredLocalization = l;
6262
preferredContentCountry = c;

extractor/src/main/java/org/schabi/newpipe/extractor/downloader/Downloader.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,4 +243,9 @@ public Response postWithContentTypeJson(final String url,
243243
*/
244244
public abstract Response execute(@Nonnull Request request)
245245
throws IOException, ReCaptchaException;
246+
247+
@Override
248+
public String toString() {
249+
return getClass().getSimpleName();
250+
}
246251
}

extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfo.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public StreamInfo(final int serviceId,
6262
super(serviceId, id, url, originalUrl, name);
6363
this.streamType = streamType;
6464
this.ageLimit = ageLimit;
65-
ExtractorLogger.d(TAG, "Created " + this);
65+
ExtractorLogger.d(TAG, "Created {}", this);
6666

6767
}
6868

@@ -80,19 +80,19 @@ public String toString() {
8080
}
8181

8282
public static StreamInfo getInfo(final String url) throws IOException, ExtractionException {
83-
ExtractorLogger.d(TAG, "getInfo(" + url + ")");
83+
ExtractorLogger.d(TAG, "getInfo({url})", url);
8484
return getInfo(NewPipe.getServiceByUrl(url), url);
8585
}
8686

8787
public static StreamInfo getInfo(@Nonnull final StreamingService service,
8888
final String url) throws IOException, ExtractionException {
89-
ExtractorLogger.d(TAG, "getInfo(" + service.getClass().getSimpleName() + ", " + url + ")");
89+
ExtractorLogger.d(TAG, "getInfo({service},{url})", service, url);
9090
return getInfo(service.getStreamExtractor(url));
9191
}
9292

9393
public static StreamInfo getInfo(@Nonnull final StreamExtractor extractor)
9494
throws ExtractionException, IOException {
95-
ExtractorLogger.d(TAG, "getInfo(" + extractor.getClass().getSimpleName() + ")");
95+
ExtractorLogger.d(TAG, "getInfo({extractor)", extractor);
9696
extractor.fetchPage();
9797
final StreamInfo streamInfo;
9898
try {

extractor/src/main/java/org/schabi/newpipe/extractor/utils/ExtractorLogger.java

Lines changed: 126 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,72 +4,176 @@ public final class ExtractorLogger {
44

55
private ExtractorLogger() { }
66

7-
private static Logger logger = new EmptyLogger();
7+
private static final Logger EMPTY_LOGGER = new EmptyLogger();
8+
private static volatile Logger logger = EMPTY_LOGGER;
89

910
public static void setLogger(final Logger customLogger) {
10-
logger = customLogger;
11+
logger = customLogger != null ? customLogger : EMPTY_LOGGER;
1112
}
1213

14+
public enum Level { DEBUG, WARN, ERROR }
15+
16+
@SuppressWarnings("checkstyle:NeedBraces")
17+
private static void log(final Level level,
18+
final String tag,
19+
final String message,
20+
final Throwable t) {
21+
if (logger == EMPTY_LOGGER) return;
22+
switch (level) {
23+
case DEBUG:
24+
if (t == null) {
25+
logger.debug(tag, message);
26+
} else {
27+
logger.debug(tag, message, t);
28+
}
29+
break;
30+
case WARN:
31+
if (t == null) {
32+
logger.warn(tag, message);
33+
} else {
34+
logger.warn(tag, message, t);
35+
}
36+
break;
37+
case ERROR:
38+
if (t == null) {
39+
logger.error(tag, message);
40+
} else {
41+
logger.error(tag, message, t);
42+
}
43+
break;
44+
}
45+
}
46+
47+
@SuppressWarnings("checkstyle:NeedBraces")
48+
private static void logFormat(final Level level,
49+
final String tag,
50+
final Throwable t,
51+
final String template,
52+
final Object... args) {
53+
if (logger == EMPTY_LOGGER) return;
54+
log(level, tag, format(template, args), t);
55+
}
56+
57+
// DEBUG
1358
public static void d(final String tag, final String msg) {
14-
logger.debug(tag, msg);
59+
log(Level.DEBUG, tag, msg, null);
1560
}
1661

1762
public static void d(final String tag, final String msg, final Throwable t) {
18-
logger.debug(tag, msg, t);
63+
log(Level.DEBUG, tag, msg, t);
64+
}
65+
66+
public static void d(final String tag, final String template, final Object... args) {
67+
logFormat(Level.DEBUG, tag, null, template, args);
1968
}
2069

70+
public static void d(final String tag,
71+
final Throwable t,
72+
final String template,
73+
final Object... args) {
74+
logFormat(Level.DEBUG, tag, t, template, args);
75+
}
76+
77+
// WARN
2178
public static void w(final String tag, final String msg) {
22-
logger.warn(tag, msg);
79+
log(Level.WARN, tag, msg, null);
2380
}
2481

2582
public static void w(final String tag, final String msg, final Throwable t) {
26-
logger.warn(tag, msg, t);
83+
log(Level.WARN, tag, msg, t);
84+
}
85+
86+
public static void w(final String tag, final String template, final Object... args) {
87+
logFormat(Level.WARN, tag, null, template, args);
2788
}
2889

90+
public static void w(final String tag,
91+
final Throwable t,
92+
final String template,
93+
final Object... args) {
94+
logFormat(Level.WARN, tag, t, template, args);
95+
}
96+
97+
// ERROR
2998
public static void e(final String tag, final String msg) {
30-
logger.error(tag, msg);
99+
log(Level.ERROR, tag, msg, null);
31100
}
32101

33102
public static void e(final String tag, final String msg, final Throwable t) {
34-
logger.error(tag, msg, t);
103+
log(Level.ERROR, tag, msg, t);
104+
}
105+
106+
public static void e(final String tag, final String template, final Object... args) {
107+
logFormat(Level.ERROR, tag, null, template, args);
35108
}
36109

110+
public static void e(final String tag,
111+
final Throwable t,
112+
final String template,
113+
final Object... args) {
114+
logFormat(Level.ERROR, tag, t, template, args);
115+
}
116+
117+
/**
118+
* Simple string format method for easier logger in the form of
119+
* {@code ExtractorLogger.d("Hello my name {Name} {}", name, surname)}
120+
* @param template The template string to format
121+
* @param args Arguments to replace identifiers with in {@code template}
122+
* @return Formatted string with arguments replaced
123+
*/
124+
private static String format(final String template, final Object... args) {
125+
if (template == null || args == null || args.length == 0) {
126+
return template;
127+
}
128+
final var out = new StringBuilder(template.length() + Math.min(32, 16 * args.length));
129+
int cursorIndex = 0;
130+
int argIndex = 0;
131+
final int n = template.length();
132+
while (cursorIndex < n) {
133+
// Find first/next open brace
134+
final int openBraceIndex = template.indexOf('{', cursorIndex);
135+
if (openBraceIndex < 0) {
136+
// If none found then there's no more arguments to replace
137+
out.append(template, cursorIndex, n); break;
138+
}
139+
140+
// Find matching closing brace
141+
final int close = template.indexOf('}', openBraceIndex + 1);
142+
if (close < 0) {
143+
// If none found then there's no more arguments to replace
144+
out.append(template, cursorIndex, n); break;
145+
}
146+
out.append(template, cursorIndex, openBraceIndex);
147+
out.append(argIndex < args.length
148+
? String.valueOf(args[argIndex++])
149+
: template.substring(openBraceIndex, close + 1));
150+
cursorIndex = close + 1;
151+
}
152+
return out.toString();
153+
}
37154

38155
private static final class EmptyLogger implements Logger {
39156
public void debug(final String tag, final String msg) { }
40-
41-
@Override
42157
public void debug(final String tag, final String msg, final Throwable throwable) { }
43-
44158
public void warn(final String tag, final String msg) { }
45-
46-
@Override
47159
public void warn(final String tag, final String msg, final Throwable t) { }
48-
49160
public void error(final String tag, final String msg) { }
50-
51161
public void error(final String tag, final String msg, final Throwable t) { }
52162
}
53163

54-
/**
55-
* Logger that prints to stdout
56-
*/
57164
public static final class ConsoleLogger implements Logger {
58165
public void debug(final String tag, final String msg) {
59-
System.out.println("[DEBUG][" + tag + "] " + msg);
166+
System.out.println("[DEBUG][" + tag + "] " + msg);
60167
}
61168

62-
@Override
63169
public void debug(final String tag, final String msg, final Throwable throwable) {
64170
debug(tag, msg);
65171
throwable.printStackTrace(System.err);
66172
}
67-
68173
public void warn(final String tag, final String msg) {
69174
System.out.println("[WARN ][" + tag + "] " + msg);
70175
}
71176

72-
@Override
73177
public void warn(final String tag, final String msg, final Throwable t) {
74178
warn(tag, msg);
75179
t.printStackTrace(System.err);

0 commit comments

Comments
 (0)