diff --git a/gsyVideoPlayer-java/src/main/java/com/shuyu/gsyvideoplayer/utils/StorageUtils.java b/gsyVideoPlayer-java/src/main/java/com/shuyu/gsyvideoplayer/utils/StorageUtils.java index 8a50c6f675a82396fa8296eabc62160d81c25e92..b2fe16deadb85bfb5fb7dc943ef4d7b59ea074fa 100644 --- a/gsyVideoPlayer-java/src/main/java/com/shuyu/gsyvideoplayer/utils/StorageUtils.java +++ b/gsyVideoPlayer-java/src/main/java/com/shuyu/gsyvideoplayer/utils/StorageUtils.java @@ -3,9 +3,6 @@ package com.shuyu.gsyvideoplayer.utils; import android.content.Context; import android.os.Environment; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import java.io.File; import static android.os.Environment.MEDIA_MOUNTED; @@ -15,7 +12,6 @@ import static android.os.Environment.MEDIA_MOUNTED; */ public class StorageUtils { - private static final Logger LOG = LoggerFactory.getLogger("StorageUtils"); private static final String INDIVIDUAL_DIR_NAME = "video-cache"; /** @@ -58,7 +54,6 @@ public class StorageUtils { } if (appCacheDir == null) { String cacheDirPath = "/data/data/" + context.getPackageName() + "/cache/"; - LOG.warn("Can't define system cache directory! '" + cacheDirPath + "%s' will be used."); appCacheDir = new File(cacheDirPath); } return appCacheDir; @@ -69,7 +64,6 @@ public class StorageUtils { File appCacheDir = new File(new File(dataDir, context.getPackageName()), "cache"); if (!appCacheDir.exists()) { if (!appCacheDir.mkdirs()) { - LOG.warn("Unable to create external cache directory"); return null; } } diff --git a/gsyvideoplayer-androidvideocache/build.gradle b/gsyvideoplayer-androidvideocache/build.gradle index e6c5bf1890adea01437e18290ea742e97b5b7aac..af572899847d906a59560495f0630e9da85e8701 100644 --- a/gsyvideoplayer-androidvideocache/build.gradle +++ b/gsyvideoplayer-androidvideocache/build.gradle @@ -30,11 +30,9 @@ android { dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) - api 'org.slf4j:slf4j-android:1.7.21' //compile fileTree(dir: 'libs', include: ['*.jar']) - //compile 'org.slf4j:slf4j-android:1.7.21' } apply from: './bintray.gradle' diff --git a/gsyvideoplayer-androidvideocache/src/main/java/com/danikula/videocache/HttpProxyCacheDebuger.java b/gsyvideoplayer-androidvideocache/src/main/java/com/danikula/videocache/HttpProxyCacheDebuger.java new file mode 100644 index 0000000000000000000000000000000000000000..53c7fe98fb9c2bd8e75816ffad22acfc8189eaab --- /dev/null +++ b/gsyvideoplayer-androidvideocache/src/main/java/com/danikula/videocache/HttpProxyCacheDebuger.java @@ -0,0 +1,80 @@ +package com.danikula.videocache; + +import android.app.Activity; +import android.text.TextUtils; +import android.util.Log; +import android.widget.Toast; + +/** + * Created by guoshuyu on 2018/2/7. + */ + +public class HttpProxyCacheDebuger { + + static final String LOG_TAG = "HttpProxyCacheDebuger"; + + static boolean DEBUG_TAG = true; + + public static void enable() { + DEBUG_TAG = true; + } + + public static void disable() { + DEBUG_TAG = false; + } + + public static boolean getDebugMode() { + return DEBUG_TAG; + } + + public static void printfLog(String tag, String log) { + if (DEBUG_TAG && log != null) { + if (!TextUtils.isEmpty(log)) + Log.i(tag, log); + } + } + + public static void printfLog(String log) { + printfLog(LOG_TAG, log); + } + + public static void printfWarning(String tag, String log) { + if (DEBUG_TAG && log != null) { + if (!TextUtils.isEmpty(log)) + Log.w(tag, log); + } + } + + public static void printfWarning(String log) { + printfWarning(LOG_TAG, log); + } + + public static void printfError(String log) { + if (DEBUG_TAG) { + if (!TextUtils.isEmpty(log)) + Log.e(LOG_TAG, log); + } + } + + public static void printfError(String Tag, String log) { + if (DEBUG_TAG) { + if (!TextUtils.isEmpty(log)) + Log.e(Tag, log); + } + } + + public static void printfError(String log, Exception e) { + if (DEBUG_TAG) { + if (!TextUtils.isEmpty(log)) + Log.e(LOG_TAG, log); + e.printStackTrace(); + } + } + + public static void Toast(Activity activity, String log) { + if (DEBUG_TAG) { + if (!TextUtils.isEmpty(log)) + Toast.makeText(activity, log, Toast.LENGTH_SHORT).show(); + } + } +} diff --git a/gsyvideoplayer-androidvideocache/src/main/java/com/danikula/videocache/HttpProxyCacheServer.java b/gsyvideoplayer-androidvideocache/src/main/java/com/danikula/videocache/HttpProxyCacheServer.java index e04a1977ded9bae1e4e8a1c77e8c4b65ceeb8e12..2c5adfaf65333600c1fdaf8fc8c21153df0eeb74 100644 --- a/gsyvideoplayer-androidvideocache/src/main/java/com/danikula/videocache/HttpProxyCacheServer.java +++ b/gsyvideoplayer-androidvideocache/src/main/java/com/danikula/videocache/HttpProxyCacheServer.java @@ -13,9 +13,6 @@ import com.danikula.videocache.headers.HeaderInjector; import com.danikula.videocache.sourcestorage.SourceInfoStorage; import com.danikula.videocache.sourcestorage.SourceInfoStorageFactory; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import java.io.File; import java.io.IOException; import java.net.InetAddress; @@ -53,7 +50,6 @@ import static com.danikula.videocache.Preconditions.checkNotNull; */ public class HttpProxyCacheServer { - private static final Logger LOG = LoggerFactory.getLogger("HttpProxyCacheServer"); private static final String PROXY_HOST = "127.0.0.1"; private final Object clientsLock = new Object(); @@ -81,7 +77,7 @@ public class HttpProxyCacheServer { this.waitConnectionThread.start(); startSignal.await(); // freeze thread, wait for server starts this.pinger = new Pinger(PROXY_HOST, port); - LOG.info("Proxy cache server started. Is it alive? " + isAlive()); + HttpProxyCacheDebuger.printfLog("Proxy cache server started. Is it alive? " + isAlive()); } catch (IOException | InterruptedException e) { socketProcessor.shutdown(); throw new IllegalStateException("Error starting local proxy server", e); @@ -128,7 +124,7 @@ public class HttpProxyCacheServer { try { getClients(url).registerCacheListener(cacheListener); } catch (ProxyCacheException e) { - LOG.warn("Error registering cache listener", e); + HttpProxyCacheDebuger.printfWarning("Error registering cache listener", e.getMessage()); } } } @@ -139,7 +135,7 @@ public class HttpProxyCacheServer { try { getClients(url).unregisterCacheListener(cacheListener); } catch (ProxyCacheException e) { - LOG.warn("Error registering cache listener", e); + HttpProxyCacheDebuger.printfWarning("Error registering cache listener", e.getMessage()); } } } @@ -165,7 +161,7 @@ public class HttpProxyCacheServer { } public void shutdown() { - LOG.info("Shutdown proxy server"); + HttpProxyCacheDebuger.printfLog("Shutdown proxy server"); shutdownClients(); @@ -199,7 +195,7 @@ public class HttpProxyCacheServer { try { config.diskUsage.touch(cacheFile); } catch (IOException e) { - LOG.error("Error touching file " + cacheFile, e); + HttpProxyCacheDebuger.printfError("Error touching file " + cacheFile, e); } } @@ -216,7 +212,6 @@ public class HttpProxyCacheServer { try { while (!Thread.currentThread().isInterrupted()) { Socket socket = serverSocket.accept(); - LOG.debug("Accept new socket " + socket); socketProcessor.submit(new SocketProcessorRunnable(socket)); } } catch (IOException e) { @@ -227,7 +222,6 @@ public class HttpProxyCacheServer { private void processSocket(Socket socket) { try { GetRequest request = GetRequest.read(socket.getInputStream()); - LOG.debug("Request to cache proxy:" + request); String url = ProxyCacheUtils.decode(request.uri); if (pinger.isPingRequest(url)) { pinger.responseToPing(socket); @@ -238,12 +232,11 @@ public class HttpProxyCacheServer { } catch (SocketException e) { // There is no way to determine that client closed connection http://stackoverflow.com/a/10241044/999458 // So just to prevent log flooding don't log stacktrace - LOG.debug("Closing socket… Socket is closed by client."); } catch (ProxyCacheException | IOException e) { onError(new ProxyCacheException("Error processing request", e)); } finally { releaseSocket(socket); - LOG.debug("Opened connections: " + getClientsCount()); + HttpProxyCacheDebuger.printfLog("Opened connections: " + getClientsCount()); } } @@ -282,7 +275,6 @@ public class HttpProxyCacheServer { } catch (SocketException e) { // There is no way to determine that client closed connection http://stackoverflow.com/a/10241044/999458 // So just to prevent log flooding don't log stacktrace - LOG.debug("Releasing input stream… Socket is closed by client."); } catch (IOException e) { // onError(new ProxyCacheException("Error closing socket input stream", e)); } @@ -294,7 +286,7 @@ public class HttpProxyCacheServer { socket.shutdownOutput(); } } catch (IOException e) { - LOG.warn("Failed to close socket on proxy side: {}. It seems client have already closed connection.", e.getMessage()); + HttpProxyCacheDebuger.printfWarning("Failed to close socket on proxy side: {}. It seems client have already closed connection.", e.getMessage()); } } @@ -309,7 +301,7 @@ public class HttpProxyCacheServer { } private void onError(Throwable e) { - LOG.error("HttpProxyCacheServer error", e); + HttpProxyCacheDebuger.printfError("HttpProxyCacheServer error", e.getMessage()); } private final class WaitRequestsRunnable implements Runnable { diff --git a/gsyvideoplayer-androidvideocache/src/main/java/com/danikula/videocache/HttpUrlSource.java b/gsyvideoplayer-androidvideocache/src/main/java/com/danikula/videocache/HttpUrlSource.java index b6d0784d14d7296b8ea0dc273853219faccb81da..32ed082afead2ba672dbf294196e7ebce79697ae 100644 --- a/gsyvideoplayer-androidvideocache/src/main/java/com/danikula/videocache/HttpUrlSource.java +++ b/gsyvideoplayer-androidvideocache/src/main/java/com/danikula/videocache/HttpUrlSource.java @@ -7,8 +7,6 @@ import com.danikula.videocache.headers.HeaderInjector; import com.danikula.videocache.sourcestorage.SourceInfoStorage; import com.danikula.videocache.sourcestorage.SourceInfoStorageFactory; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import java.io.BufferedInputStream; import java.io.IOException; @@ -33,8 +31,6 @@ import static java.net.HttpURLConnection.HTTP_SEE_OTHER; */ public class HttpUrlSource implements Source { - private static final Logger LOG = LoggerFactory.getLogger("HttpUrlSource"); - private static final int MAX_REDIRECTS = 5; private final SourceInfoStorage sourceInfoStorage; private final HeaderInjector headerInjector; @@ -109,7 +105,7 @@ public class HttpUrlSource implements Source { "https://github.com/danikula/AndroidVideoCache/issues."; throw new RuntimeException(message, e); } catch (ArrayIndexOutOfBoundsException e) { - LOG.error("Error closing connection correctly. Should happen only on Android L. " + + HttpProxyCacheDebuger.printfError("Error closing connection correctly. Should happen only on Android L. " + "If anybody know how to fix it, please visit https://github.com/danikula/AndroidVideoCache/issues/88. " + "Until good solution is not know, just ignore this issue :(", e); } @@ -131,7 +127,6 @@ public class HttpUrlSource implements Source { } private void fetchContentInfo() throws ProxyCacheException { - LOG.debug("Read content info from " + sourceInfo.url); HttpURLConnection urlConnection = null; InputStream inputStream = null; try { @@ -141,9 +136,8 @@ public class HttpUrlSource implements Source { inputStream = urlConnection.getInputStream(); this.sourceInfo = new SourceInfo(sourceInfo.url, length, mime); this.sourceInfoStorage.put(sourceInfo.url, sourceInfo); - LOG.debug("Source info fetched: " + sourceInfo); } catch (IOException e) { - LOG.error("Error fetching info from " + sourceInfo.url, e); + HttpProxyCacheDebuger.printfError("Error fetching info from " + sourceInfo.url, e); } finally { ProxyCacheUtils.close(inputStream); if (urlConnection != null) { @@ -158,7 +152,6 @@ public class HttpUrlSource implements Source { int redirectCount = 0; String url = this.sourceInfo.url; do { - LOG.debug("Open connection " + (offset > 0 ? " with offset " + offset : "") + " to " + url); connection = (HttpURLConnection) new URL(url).openConnection(); injectCustomHeaders(connection, url); if (offset > 0) { diff --git a/gsyvideoplayer-androidvideocache/src/main/java/com/danikula/videocache/Pinger.java b/gsyvideoplayer-androidvideocache/src/main/java/com/danikula/videocache/Pinger.java index bea87f235400a861a90716546cada77147e6802d..d84c23bf7a0b4f91e9a1b17e4ef107d9abf4b472 100644 --- a/gsyvideoplayer-androidvideocache/src/main/java/com/danikula/videocache/Pinger.java +++ b/gsyvideoplayer-androidvideocache/src/main/java/com/danikula/videocache/Pinger.java @@ -1,7 +1,6 @@ package com.danikula.videocache; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; + import java.io.IOException; import java.io.OutputStream; @@ -32,7 +31,6 @@ import static java.util.concurrent.TimeUnit.MILLISECONDS; class Pinger { - private static final Logger LOG = LoggerFactory.getLogger("Pinger"); private static final String PING_REQUEST = "ping"; private static final String PING_RESPONSE = "ping ok"; @@ -59,9 +57,9 @@ class Pinger { return true; } } catch (TimeoutException e) { - LOG.warn("Error pinging server (attempt: " + attempts + ", timeout: " + timeout + "). "); + HttpProxyCacheDebuger.printfWarning("Error pinging server (attempt: " + attempts + ", timeout: " + timeout + "). "); } catch (InterruptedException | ExecutionException e) { - LOG.error("Error pinging server due to unexpected error", e); + HttpProxyCacheDebuger.printfError("Error pinging server due to unexpected error", e); } attempts++; timeout *= 2; @@ -70,7 +68,7 @@ class Pinger { "If you see this message, please, report at https://github.com/danikula/AndroidVideoCache/issues/134. " + "Default proxies are: %s" , attempts, timeout / 2, getDefaultProxies()); - LOG.error(error, new ProxyCacheException(error)); + HttpProxyCacheDebuger.printfError(error, new ProxyCacheException(error)); return false; } @@ -102,10 +100,10 @@ class Pinger { byte[] response = new byte[expectedResponse.length]; source.read(response); boolean pingOk = Arrays.equals(expectedResponse, response); - LOG.info("Ping response: `" + new String(response) + "`, pinged? " + pingOk); + HttpProxyCacheDebuger.printfLog("Ping response: `" + new String(response) + "`, pinged? " + pingOk); return pingOk; } catch (ProxyCacheException e) { - LOG.error("Error reading ping response", e); + HttpProxyCacheDebuger.printfError("Error reading ping response", e); return false; } finally { source.close(); diff --git a/gsyvideoplayer-androidvideocache/src/main/java/com/danikula/videocache/ProxyCache.java b/gsyvideoplayer-androidvideocache/src/main/java/com/danikula/videocache/ProxyCache.java index eeea9713db6dfde848e06260fb734137a63b9462..2451c81f97cc02ebc7dcfc1bec9cc779b4ad47b1 100644 --- a/gsyvideoplayer-androidvideocache/src/main/java/com/danikula/videocache/ProxyCache.java +++ b/gsyvideoplayer-androidvideocache/src/main/java/com/danikula/videocache/ProxyCache.java @@ -1,7 +1,5 @@ package com.danikula.videocache; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import java.util.concurrent.atomic.AtomicInteger; @@ -18,7 +16,6 @@ import static com.danikula.videocache.Preconditions.checkNotNull; */ class ProxyCache { - private static final Logger LOG = LoggerFactory.getLogger("ProxyCache"); private static final int MAX_READ_SOURCE_ATTEMPTS = 1; private final Source source; @@ -62,7 +59,6 @@ class ProxyCache { public void shutdown() { synchronized (stopLock) { - LOG.debug("Shutdown proxy for " + source); try { stopped = true; if (sourceReaderThread != null) { @@ -174,9 +170,9 @@ class ProxyCache { protected final void onError(final Throwable e) { boolean interruption = e instanceof InterruptedProxyCacheException; if (interruption) { - LOG.debug("ProxyCache is interrupted"); + HttpProxyCacheDebuger.printfLog("ProxyCache is interrupted"); } else { - LOG.error("ProxyCache error", e); + HttpProxyCacheDebuger.printfError("ProxyCache error", e.getMessage()); } } diff --git a/gsyvideoplayer-androidvideocache/src/main/java/com/danikula/videocache/ProxyCacheUtils.java b/gsyvideoplayer-androidvideocache/src/main/java/com/danikula/videocache/ProxyCacheUtils.java index 7e26a2b2fb4f10253b15e34b359f7f52f2b225e9..09cbdaec1af9a968f8f03447f6b075a3f08438e0 100644 --- a/gsyvideoplayer-androidvideocache/src/main/java/com/danikula/videocache/ProxyCacheUtils.java +++ b/gsyvideoplayer-androidvideocache/src/main/java/com/danikula/videocache/ProxyCacheUtils.java @@ -3,8 +3,6 @@ package com.danikula.videocache; import android.text.TextUtils; import android.webkit.MimeTypeMap; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import java.io.Closeable; import java.io.IOException; @@ -25,7 +23,6 @@ import static com.danikula.videocache.Preconditions.checkNotNull; */ public class ProxyCacheUtils { - private static final Logger LOG = LoggerFactory.getLogger("ProxyCacheUtils"); static final int DEFAULT_BUFFER_SIZE = 8 * 1024; static final int MAX_ARRAY_PREVIEW = 16; @@ -72,7 +69,7 @@ public class ProxyCacheUtils { try { closeable.close(); } catch (IOException e) { - LOG.error("Error closing resource", e); + HttpProxyCacheDebuger.printfError("Error closing resource", e); } } } diff --git a/gsyvideoplayer-androidvideocache/src/main/java/com/danikula/videocache/StorageUtils.java b/gsyvideoplayer-androidvideocache/src/main/java/com/danikula/videocache/StorageUtils.java index b8afd3264cf424394fa1db33cc9721ee79dc54c8..9e14c2046cee89e7053e90eac1acad14f72d51dd 100644 --- a/gsyvideoplayer-androidvideocache/src/main/java/com/danikula/videocache/StorageUtils.java +++ b/gsyvideoplayer-androidvideocache/src/main/java/com/danikula/videocache/StorageUtils.java @@ -3,8 +3,7 @@ package com.danikula.videocache; import android.content.Context; import android.os.Environment; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; + import java.io.File; @@ -20,7 +19,6 @@ import static android.os.Environment.MEDIA_MOUNTED; */ final class StorageUtils { - private static final Logger LOG = LoggerFactory.getLogger("StorageUtils"); private static final String INDIVIDUAL_DIR_NAME = "video-cache"; /** @@ -63,7 +61,7 @@ final class StorageUtils { } if (appCacheDir == null) { String cacheDirPath = "/data/data/" + context.getPackageName() + "/cache/"; - LOG.warn("Can't define system cache directory! '" + cacheDirPath + "%s' will be used."); + HttpProxyCacheDebuger.printfWarning("Can't define system cache directory! '" + cacheDirPath + "%s' will be used."); appCacheDir = new File(cacheDirPath); } return appCacheDir; @@ -74,7 +72,7 @@ final class StorageUtils { File appCacheDir = new File(new File(dataDir, context.getPackageName()), "cache"); if (!appCacheDir.exists()) { if (!appCacheDir.mkdirs()) { - LOG.warn("Unable to create external cache directory"); + HttpProxyCacheDebuger.printfWarning("Unable to create external cache directory"); return null; } } diff --git a/gsyvideoplayer-androidvideocache/src/main/java/com/danikula/videocache/file/Files.java b/gsyvideoplayer-androidvideocache/src/main/java/com/danikula/videocache/file/Files.java index 5f5234a5238967fa327681ccf242196a726f31c6..0f95c8b2c5eb5e041c9175a9d6f41d590dcc020c 100644 --- a/gsyvideoplayer-androidvideocache/src/main/java/com/danikula/videocache/file/Files.java +++ b/gsyvideoplayer-androidvideocache/src/main/java/com/danikula/videocache/file/Files.java @@ -1,7 +1,6 @@ package com.danikula.videocache.file; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import com.danikula.videocache.HttpProxyCacheDebuger; import java.io.File; import java.io.IOException; @@ -20,7 +19,6 @@ import java.util.List; */ class Files { - private static final Logger LOG = LoggerFactory.getLogger("Files"); static void makeDir(File directory) throws IOException { if (directory.exists()) { @@ -53,7 +51,8 @@ class Files { modify(file); if (file.lastModified() < now) { // NOTE: apparently this is a known issue (see: http://stackoverflow.com/questions/6633748/file-lastmodified-is-never-what-was-set-with-file-setlastmodified) - LOG.warn("Last modified date {} is not set for file {}", new Date(file.lastModified()), file.getAbsolutePath()); + HttpProxyCacheDebuger.printfWarning("Last modified date {} is not set for file {}", new Date(file.lastModified()).toString() + "\n" + file.getAbsolutePath()); + } } } diff --git a/gsyvideoplayer-androidvideocache/src/main/java/com/danikula/videocache/file/LruDiskUsage.java b/gsyvideoplayer-androidvideocache/src/main/java/com/danikula/videocache/file/LruDiskUsage.java index 0b0b03ad38390c37120ada9e2b2488fe0fc73204..a0ed3e9e114c99509424eaf67aee75c01d2346fd 100644 --- a/gsyvideoplayer-androidvideocache/src/main/java/com/danikula/videocache/file/LruDiskUsage.java +++ b/gsyvideoplayer-androidvideocache/src/main/java/com/danikula/videocache/file/LruDiskUsage.java @@ -1,7 +1,7 @@ package com.danikula.videocache.file; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import com.danikula.videocache.HttpProxyCacheDebuger; + import java.io.File; import java.io.IOException; @@ -17,7 +17,7 @@ import java.util.concurrent.Executors; */ public abstract class LruDiskUsage implements DiskUsage { - private static final Logger LOG = LoggerFactory.getLogger("LruDiskUsage"); + private final ExecutorService workerThread = Executors.newSingleThreadExecutor(); @Override @@ -44,9 +44,9 @@ public abstract class LruDiskUsage implements DiskUsage { if (deleted) { totalCount--; totalSize -= fileSize; - LOG.info("Cache file " + file + " is deleted because it exceeds cache limit"); + HttpProxyCacheDebuger.printfLog("Cache file " + file + " is deleted because it exceeds cache limit"); } else { - LOG.error("Error deleting file " + file + " for trimming cache"); + HttpProxyCacheDebuger.printfError("Error deleting file " + file + " for trimming cache"); } } }