Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.schabi.newpipe.extractor.localization.ContentCountry;
import org.schabi.newpipe.extractor.localization.Localization;
import org.schabi.newpipe.extractor.localization.TimeAgoParser;
import org.schabi.newpipe.extractor.utils.ExtractorLogger;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
Expand All @@ -15,6 +16,8 @@
import java.util.Objects;

public abstract class Extractor {
private final String TAG = getClass().getSimpleName() + "@" + hashCode();

/**
* {@link StreamingService} currently related to this extractor.<br>
* Useful for getting other things from a service (like the url handlers for
Expand Down Expand Up @@ -54,7 +57,9 @@ public LinkHandler getLinkHandler() {
* @throws ExtractionException if the pages content is not understood
*/
public void fetchPage() throws IOException, ExtractionException {
ExtractorLogger.d(TAG, "base fetchPage called");
if (pageFetched) {
ExtractorLogger.d(TAG, "Page already fetched; returning");
return;
}
onFetchPage(downloader);
Expand Down Expand Up @@ -151,4 +156,9 @@ public ContentCountry getExtractorContentCountry() {
public TimeAgoParser getTimeAgoParser() {
return getService().getTimeAgoParser(getExtractorLocalization());
}

@Override
public String toString() {
return getClass().getSimpleName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.linkhandler.LinkHandler;
import org.schabi.newpipe.extractor.utils.ExtractorLogger;

import java.io.Serializable;
import java.util.ArrayList;
Expand All @@ -10,6 +11,7 @@

public abstract class Info implements Serializable {

private static final String TAG = "Info";
private final int serviceId;
/**
* Id of this Info object <br>
Expand Down Expand Up @@ -52,6 +54,7 @@ public Info(final int serviceId,
this.url = url;
this.originalUrl = originalUrl;
this.name = name;
ExtractorLogger.d(TAG, "Base Created {}", this);
}

public Info(final int serviceId, final LinkHandler linkHandler, final String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.localization.ContentCountry;
import org.schabi.newpipe.extractor.localization.Localization;
import org.schabi.newpipe.extractor.utils.ExtractorLogger;

import java.util.List;

Expand All @@ -34,6 +35,7 @@
* Provides access to streaming services supported by NewPipe.
*/
public final class NewPipe {
private static final String TAG = NewPipe.class.getSimpleName();
private static Downloader downloader;
private static Localization preferredLocalization;
private static ContentCountry preferredContentCountry;
Expand All @@ -42,15 +44,19 @@ private NewPipe() {
}

public static void init(final Downloader d) {
ExtractorLogger.d(TAG, "Default init called");
init(d, Localization.DEFAULT);
}

public static void init(final Downloader d, final Localization l) {
ExtractorLogger.d(TAG, "Default init called with localization={}");
init(d, l, l.getCountryCode().isEmpty()
? ContentCountry.DEFAULT : new ContentCountry(l.getCountryCode()));
}

public static void init(final Downloader d, final Localization l, final ContentCountry c) {
ExtractorLogger.d(TAG, "Initializing with downloader={}, localization={}, country={}",
d, l, c);
downloader = d;
preferredLocalization = l;
preferredContentCountry = c;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,9 @@ public Response postWithContentTypeJson(final String url,
*/
public abstract Response execute(@Nonnull Request request)
throws IOException, ReCaptchaException;

@Override
public String toString() {
return getClass().getSimpleName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.localization.DateWrapper;
import org.schabi.newpipe.extractor.utils.ExtractorHelper;
import org.schabi.newpipe.extractor.utils.ExtractorLogger;

import java.io.IOException;
import java.util.List;
Expand All @@ -44,7 +45,7 @@
* Info object for opened contents, i.e. the content ready to play.
*/
public class StreamInfo extends Info {

private static final String TAG = StreamInfo.class.getSimpleName();
public static class StreamExtractException extends ExtractionException {
StreamExtractException(final String message) {
super(message);
Expand All @@ -61,19 +62,36 @@ public StreamInfo(final int serviceId,
super(serviceId, id, url, originalUrl, name);
this.streamType = streamType;
this.ageLimit = ageLimit;
ExtractorLogger.d(TAG, "Created {}", this);
}

@Override
public String toString() {
return TAG + "["
+ "serviceId=" + getServiceId()
+ ", url='" + getUrl() + '\''
+ ", originalUrl='" + getOriginalUrl() + '\''
+ ", id='" + getId() + '\''
+ ", name='" + getName() + '\''
+ ", streamType=" + streamType
+ ", ageLimit=" + ageLimit
+ ']';
}

public static StreamInfo getInfo(final String url) throws IOException, ExtractionException {
ExtractorLogger.d(TAG, "getInfo({url})", url);
return getInfo(NewPipe.getServiceByUrl(url), url);
}

public static StreamInfo getInfo(@Nonnull final StreamingService service,
final String url) throws IOException, ExtractionException {
ExtractorLogger.d(TAG, "getInfo({service},{url})", service, url);
return getInfo(service.getStreamExtractor(url));
}

public static StreamInfo getInfo(@Nonnull final StreamExtractor extractor)
throws ExtractionException, IOException {
ExtractorLogger.d(TAG, "getInfo({extractor)", extractor);
extractor.fetchPage();
final StreamInfo streamInfo;
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
package org.schabi.newpipe.extractor.utils;

public final class ExtractorLogger {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few lines of class doc would be good to have here.


private ExtractorLogger() { }

private static final Logger EMPTY_LOGGER = new EmptyLogger();
private static volatile Logger logger = EMPTY_LOGGER;

public static void setLogger(final Logger customLogger) {
logger = customLogger != null ? customLogger : EMPTY_LOGGER;
}

public enum Level { DEBUG, WARN, ERROR }

@SuppressWarnings("checkstyle:NeedBraces")
private static void log(final Level level,
final String tag,
final String message,
final Throwable t) {
if (logger == EMPTY_LOGGER) return;
switch (level) {
case DEBUG:
if (t == null) {
logger.debug(tag, message);
} else {
logger.debug(tag, message, t);
}
break;
case WARN:
if (t == null) {
logger.warn(tag, message);
} else {
logger.warn(tag, message, t);
}
break;
case ERROR:
if (t == null) {
logger.error(tag, message);
} else {
logger.error(tag, message, t);
}
break;
}
}

@SuppressWarnings("checkstyle:NeedBraces")
private static void logFormat(final Level level,
final String tag,
final Throwable t,
final String template,
final Object... args) {
if (logger == EMPTY_LOGGER) return;
log(level, tag, format(template, args), t);
}

// DEBUG
public static void d(final String tag, final String msg) {
log(Level.DEBUG, tag, msg, null);
}

public static void d(final String tag, final String msg, final Throwable t) {
log(Level.DEBUG, tag, msg, t);
}

public static void d(final String tag, final String template, final Object... args) {
logFormat(Level.DEBUG, tag, null, template, args);
}

public static void d(final String tag,
final Throwable t,
final String template,
final Object... args) {
logFormat(Level.DEBUG, tag, t, template, args);
}

// WARN
public static void w(final String tag, final String msg) {
log(Level.WARN, tag, msg, null);
}

public static void w(final String tag, final String msg, final Throwable t) {
log(Level.WARN, tag, msg, t);
}

public static void w(final String tag, final String template, final Object... args) {
logFormat(Level.WARN, tag, null, template, args);
}

public static void w(final String tag,
final Throwable t,
final String template,
final Object... args) {
logFormat(Level.WARN, tag, t, template, args);
}

// ERROR
public static void e(final String tag, final String msg) {
log(Level.ERROR, tag, msg, null);
}

public static void e(final String tag, final String msg, final Throwable t) {
log(Level.ERROR, tag, msg, t);
}

public static void e(final String tag, final String template, final Object... args) {
logFormat(Level.ERROR, tag, null, template, args);
}

public static void e(final String tag,
final Throwable t,
final String template,
final Object... args) {
logFormat(Level.ERROR, tag, t, template, args);
}

/**
* Simple string format method for easier logging in the form of
* {@code ExtractorLogger.d("Hello my name {Name} {}", name, surname)}
* @param template The template string to format
* @param args Arguments to replace identifiers with in {@code template}
* @return Formatted string with arguments replaced
*/
private static String format(final String template, final Object... args) {
if (template == null || args == null || args.length == 0) {
return template;
}
final var out = new StringBuilder(template.length() + Math.min(32, 16 * args.length));
int cursorIndex = 0;
int argIndex = 0;
final int n = template.length();
while (cursorIndex < n) {
// Find first/next open brace
final int openBraceIndex = template.indexOf('{', cursorIndex);
if (openBraceIndex < 0) {
// If none found then there's no more arguments to replace
out.append(template, cursorIndex, n); break;
}

// Find matching closing brace
final int close = template.indexOf('}', openBraceIndex + 1);
if (close < 0) {
// If none found then there's no more arguments to replace
out.append(template, cursorIndex, n); break;
}
// Append everything from cursor up to before the open brace
out.append(template, cursorIndex, openBraceIndex);
// Append arguments in the brace
out.append(argIndex < args.length
? String.valueOf(args[argIndex++])
: template.substring(openBraceIndex, close + 1));
cursorIndex = close + 1;
}
return out.toString();
}

private static final class EmptyLogger implements Logger {
public void debug(final String tag, final String msg) { }
public void debug(final String tag, final String msg, final Throwable throwable) { }
public void warn(final String tag, final String msg) { }
public void warn(final String tag, final String msg, final Throwable t) { }
public void error(final String tag, final String msg) { }
public void error(final String tag, final String msg, final Throwable t) { }
}

public static final class ConsoleLogger implements Logger {
public void debug(final String tag, final String msg) {
System.out.println("[DEBUG][" + tag + "] " + msg);
}

public void debug(final String tag, final String msg, final Throwable throwable) {
debug(tag, msg);
throwable.printStackTrace(System.err);
}
public void warn(final String tag, final String msg) {
System.out.println("[WARN ][" + tag + "] " + msg);
}

public void warn(final String tag, final String msg, final Throwable t) {
warn(tag, msg);
t.printStackTrace(System.err);
}

public void error(final String tag, final String msg) {
System.err.println("[ERROR][" + tag + "] " + msg);
}

public void error(final String tag, final String msg, final Throwable t) {
System.err.println("[ERROR][" + tag + "] " + msg);
t.printStackTrace(System.err);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.schabi.newpipe.extractor.utils;

public interface Logger {
void debug(String tag, String message);
void debug(String tag, String message, Throwable throwable);
void warn(String tag, String message);
void warn(String tag, String message, Throwable throwable);
void error(String tag, String message);
void error(String tag, String message, Throwable t);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.schabi.newpipe.extractor;

import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.schabi.newpipe.extractor.utils.ExtractorLogger;

public class LoggerExtension implements BeforeAllCallback {
private static boolean set = false;

@Override
public void beforeAll(ExtensionContext context) {
if (set) return;
set = true;
ExtractorLogger.setLogger(new ExtractorLogger.ConsoleLogger());
}
}
Loading