提交 d6ed609f 编写于 作者: G guoshuyu

去除cache模块的log库依赖

上级 ccd906ef
......@@ -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;
}
}
......
......@@ -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'
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();
}
}
}
......@@ -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 {
......
......@@ -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) {
......
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();
......
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());
}
}
......
......@@ -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);
}
}
}
......
......@@ -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;
}
}
......
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());
}
}
}
......
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");
}
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册