-
-
Notifications
You must be signed in to change notification settings - Fork 501
Add logging to extractor #1403
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
absurdlylongusername
wants to merge
3
commits into
TeamNewPipe:dev
Choose a base branch
from
absurdlylongusername:add-extractor-logging
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add logging to extractor #1403
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
193 changes: 193 additions & 0 deletions
193
extractor/src/main/java/org/schabi/newpipe/extractor/utils/ExtractorLogger.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,193 @@ | ||
| package org.schabi.newpipe.extractor.utils; | ||
|
|
||
| public final class ExtractorLogger { | ||
|
|
||
| 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); | ||
| } | ||
| } | ||
| } | ||
10 changes: 10 additions & 0 deletions
10
extractor/src/main/java/org/schabi/newpipe/extractor/utils/Logger.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } |
16 changes: 16 additions & 0 deletions
16
extractor/src/test/java/org/schabi/newpipe/extractor/LoggerExtension.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.