Skip to content

Commit

Permalink
📦 2.6.2: use slf4j for logging
Browse files Browse the repository at this point in the history
  • Loading branch information
danikula committed Sep 27, 2016
1 parent 76c13ba commit 44318d8
Show file tree
Hide file tree
Showing 21 changed files with 87 additions and 115 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ repositories {
jcenter()
}
dependencies {
compile 'com.danikula:videocache:2.6.1'
compile 'com.danikula:videocache:2.6.2'
}
```

Expand Down
3 changes: 2 additions & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ sourceCompatibility = '1.7'

dependencies {
compile 'com.google.android:android:1.6_r2'
compile 'org.slf4j:slf4j-android:1.7.21'
}

publish {
userOrg = 'alexeydanilov'
groupId = 'com.danikula'
artifactId = 'videocache'
publishVersion = '2.6.1'
publishVersion = '2.6.2'
description = 'Cache support for android VideoView'
website = 'https://github.com/danikula/AndroidVideoCache'
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.content.Context;
import android.os.SystemClock;
import android.util.Log;

import com.danikula.videocache.file.DiskUsage;
import com.danikula.videocache.file.FileNameGenerator;
Expand All @@ -12,6 +11,9 @@
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.io.OutputStream;
Expand All @@ -33,7 +35,6 @@

import static com.danikula.videocache.Preconditions.checkAllNotNull;
import static com.danikula.videocache.Preconditions.checkNotNull;
import static com.danikula.videocache.ProxyCacheUtils.LOG_TAG;
import static java.util.concurrent.TimeUnit.MILLISECONDS;

/**
Expand All @@ -57,6 +58,8 @@
*/
public class HttpProxyCacheServer {

private static final Logger LOG = LoggerFactory.getLogger("HttpProxyCacheServer");

private static final String PROXY_HOST = "127.0.0.1";
private static final String PING_REQUEST = "ping";
private static final String PING_RESPONSE = "ping ok";
Expand Down Expand Up @@ -84,7 +87,7 @@ private HttpProxyCacheServer(Config config) {
this.waitConnectionThread = new Thread(new WaitRequestsRunnable(startSignal));
this.waitConnectionThread.start();
startSignal.await(); // freeze thread, wait for server starts
Log.i(LOG_TAG, "Proxy cache server started. Ping it...");
LOG.info("Proxy cache server started. Ping it...");
makeSureServerWorks();
} catch (IOException | InterruptedException e) {
socketProcessor.shutdown();
Expand All @@ -105,12 +108,12 @@ private void makeSureServerWorks() {
}
SystemClock.sleep(delay);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
Log.e(LOG_TAG, "Error pinging server [attempt: " + pingAttempts + ", timeout: " + delay + "]. ", e);
LOG.error("Error pinging server [attempt: " + pingAttempts + ", timeout: " + delay + "]. ", e);
}
pingAttempts++;
delay *= 2;
}
Log.e(LOG_TAG, "Shutdown server… Error pinging server [attempts: " + pingAttempts + ", max timeout: " + delay / 2 + "]. " +
LOG.error("Shutdown server… Error pinging server [attempts: " + pingAttempts + ", max timeout: " + delay / 2 + "]. " +
"If you see this message, please, email me danikula@gmail.com");
shutdown();
}
Expand All @@ -124,10 +127,10 @@ private boolean pingServer() throws ProxyCacheException {
byte[] response = new byte[expectedResponse.length];
source.read(response);
boolean pingOk = Arrays.equals(expectedResponse, response);
Log.d(LOG_TAG, "Ping response: `" + new String(response) + "`, pinged? " + pingOk);
LOG.info("Ping response: `" + new String(response) + "`, pinged? " + pingOk);
return pingOk;
} catch (ProxyCacheException e) {
Log.e(LOG_TAG, "Error reading ping response", e);
LOG.error("Error reading ping response", e);
return false;
} finally {
source.close();
Expand All @@ -136,7 +139,7 @@ private boolean pingServer() throws ProxyCacheException {

public String getProxyUrl(String url) {
if (!pinged) {
Log.e(LOG_TAG, "Proxy server isn't pinged. Caching doesn't work. If you see this message, please, email me danikula@gmail.com");
LOG.error("Proxy server isn't pinged. Caching doesn't work. If you see this message, please, email me danikula@gmail.com");
}
return pinged ? appendToProxyUrl(url) : url;
}
Expand All @@ -151,7 +154,7 @@ public void registerCacheListener(CacheListener cacheListener, String url) {
try {
getClients(url).registerCacheListener(cacheListener);
} catch (ProxyCacheException e) {
Log.d(LOG_TAG, "Error registering cache listener", e);
LOG.warn("Error registering cache listener", e);
}
}
}
Expand All @@ -162,7 +165,7 @@ public void unregisterCacheListener(CacheListener cacheListener, String url) {
try {
getClients(url).unregisterCacheListener(cacheListener);
} catch (ProxyCacheException e) {
Log.d(LOG_TAG, "Error registering cache listener", e);
LOG.warn("Error registering cache listener", e);
}
}
}
Expand Down Expand Up @@ -191,7 +194,7 @@ public boolean isCached(String url) {
}

public void shutdown() {
Log.i(LOG_TAG, "Shutdown proxy server");
LOG.info("Shutdown proxy server");

shutdownClients();

Expand Down Expand Up @@ -220,7 +223,7 @@ private void waitForRequest() {
try {
while (!Thread.currentThread().isInterrupted()) {
Socket socket = serverSocket.accept();
Log.d(LOG_TAG, "Accept new socket " + socket);
LOG.debug("Accept new socket " + socket);
socketProcessor.submit(new SocketProcessorRunnable(socket));
}
} catch (IOException e) {
Expand All @@ -231,7 +234,7 @@ private void waitForRequest() {
private void processSocket(Socket socket) {
try {
GetRequest request = GetRequest.read(socket.getInputStream());
Log.i(LOG_TAG, "Request to cache proxy:" + request);
LOG.debug("Request to cache proxy:" + request);
String url = ProxyCacheUtils.decode(request.uri);
if (PING_REQUEST.equals(url)) {
responseToPing(socket);
Expand All @@ -242,12 +245,12 @@ private void processSocket(Socket socket) {
} 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.d(LOG_TAG, "Closing socket… Socket is closed by client.");
LOG.debug("Closing socket… Socket is closed by client.");
} catch (ProxyCacheException | IOException e) {
onError(new ProxyCacheException("Error processing request", e));
} finally {
releaseSocket(socket);
Log.d(LOG_TAG, "Opened connections: " + getClientsCount());
LOG.debug("Opened connections: " + getClientsCount());
}
}

Expand Down Expand Up @@ -292,7 +295,7 @@ private void closeSocketInput(Socket socket) {
} 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.d(LOG_TAG, "Releasing input stream… Socket is closed by client.");
LOG.debug("Releasing input stream… Socket is closed by client.");
} catch (IOException e) {
onError(new ProxyCacheException("Error closing socket input stream", e));
}
Expand All @@ -319,7 +322,7 @@ private void closeSocket(Socket socket) {
}

private void onError(Throwable e) {
Log.e(LOG_TAG, "HttpProxyCacheServer error", e);
LOG.error("HttpProxyCacheServer error", e);
}

private final class WaitRequestsRunnable implements Runnable {
Expand Down
15 changes: 9 additions & 6 deletions library/src/main/java/com/danikula/videocache/HttpUrlSource.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.danikula.videocache;

import android.text.TextUtils;
import android.util.Log;

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;
import java.io.InputStream;
Expand All @@ -15,7 +17,6 @@

import static com.danikula.videocache.Preconditions.checkNotNull;
import static com.danikula.videocache.ProxyCacheUtils.DEFAULT_BUFFER_SIZE;
import static com.danikula.videocache.ProxyCacheUtils.LOG_TAG;
import static java.net.HttpURLConnection.HTTP_MOVED_PERM;
import static java.net.HttpURLConnection.HTTP_MOVED_TEMP;
import static java.net.HttpURLConnection.HTTP_OK;
Expand All @@ -29,6 +30,8 @@
*/
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 SourceInfo sourceInfo;
Expand Down Expand Up @@ -108,7 +111,7 @@ public int read(byte[] buffer) throws ProxyCacheException {
}

private void fetchContentInfo() throws ProxyCacheException {
Log.d(LOG_TAG, "Read content info from " + sourceInfo.url);
LOG.debug("Read content info from " + sourceInfo.url);
HttpURLConnection urlConnection = null;
InputStream inputStream = null;
try {
Expand All @@ -118,9 +121,9 @@ private void fetchContentInfo() throws ProxyCacheException {
inputStream = urlConnection.getInputStream();
this.sourceInfo = new SourceInfo(sourceInfo.url, length, mime);
this.sourceInfoStorage.put(sourceInfo.url, sourceInfo);
Log.i(LOG_TAG, "Source info fetched: " + sourceInfo);
LOG.debug("Source info fetched: " + sourceInfo);
} catch (IOException e) {
Log.e(LOG_TAG, "Error fetching info from " + sourceInfo.url, e);
LOG.error("Error fetching info from " + sourceInfo.url, e);
} finally {
ProxyCacheUtils.close(inputStream);
if (urlConnection != null) {
Expand All @@ -135,7 +138,7 @@ private HttpURLConnection openConnection(int offset, int timeout) throws IOExcep
int redirectCount = 0;
String url = this.sourceInfo.url;
do {
Log.d(LOG_TAG, "Open connection " + (offset > 0 ? " with offset " + offset : "") + " to " + url);
LOG.debug("Open connection " + (offset > 0 ? " with offset " + offset : "") + " to " + url);
connection = (HttpURLConnection) new URL(url).openConnection();
if (offset > 0) {
connection.setRequestProperty("Range", "bytes=" + offset + "-");
Expand Down
11 changes: 6 additions & 5 deletions library/src/main/java/com/danikula/videocache/ProxyCache.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.danikula.videocache;

import android.util.Log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.atomic.AtomicInteger;

import static com.danikula.videocache.Preconditions.checkNotNull;
import static com.danikula.videocache.ProxyCacheUtils.LOG_TAG;

/**
* Proxy for {@link Source} with caching support ({@link Cache}).
Expand All @@ -18,6 +18,7 @@
*/
class ProxyCache {

private static final Logger LOG = LoggerFactory.getLogger("ProxyCache");
private static final int MAX_READ_SOURCE_ATTEMPTS = 1;

private final Source source;
Expand Down Expand Up @@ -61,7 +62,7 @@ private void checkReadSourceErrorsCount() throws ProxyCacheException {

public void shutdown() {
synchronized (stopLock) {
Log.d(LOG_TAG, "Shutdown proxy for " + source);
LOG.debug("Shutdown proxy for " + source);
try {
stopped = true;
if (sourceReaderThread != null) {
Expand Down Expand Up @@ -173,9 +174,9 @@ private void closeSource() {
protected final void onError(final Throwable e) {
boolean interruption = e instanceof InterruptedProxyCacheException;
if (interruption) {
Log.d(LOG_TAG, "ProxyCache is interrupted");
LOG.debug("ProxyCache is interrupted");
} else {
Log.e(LOG_TAG, "ProxyCache error", e);
LOG.error("ProxyCache error", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.danikula.videocache;

import android.text.TextUtils;
import android.util.Log;
import android.webkit.MimeTypeMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Closeable;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
Expand All @@ -23,7 +25,7 @@
*/
public class ProxyCacheUtils {

static final String LOG_TAG = "ProxyCache";
private static final Logger LOG = LoggerFactory.getLogger("ProxyCacheUtils");
static final int DEFAULT_BUFFER_SIZE = 8 * 1024;
static final int MAX_ARRAY_PREVIEW = 16;

Expand Down Expand Up @@ -70,7 +72,7 @@ static void close(Closeable closeable) {
try {
closeable.close();
} catch (IOException e) {
Log.e(LOG_TAG, "Error closing resource", e);
LOG.error("Error closing resource", e);
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions library/src/main/java/com/danikula/videocache/StorageUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;

import static android.os.Environment.MEDIA_MOUNTED;
import static com.danikula.videocache.ProxyCacheUtils.LOG_TAG;

/**
* Provides application storage paths
Expand All @@ -19,6 +20,7 @@
*/
final class StorageUtils {

private static final Logger LOG = LoggerFactory.getLogger("StorageUtils");
private static final String INDIVIDUAL_DIR_NAME = "video-cache";

/**
Expand Down Expand Up @@ -61,7 +63,7 @@ private static File getCacheDirectory(Context context, boolean preferExternal) {
}
if (appCacheDir == null) {
String cacheDirPath = "/data/data/" + context.getPackageName() + "/cache/";
Log.w(LOG_TAG, "Can't define system cache directory! '" + cacheDirPath + "%s' will be used.");
LOG.warn("Can't define system cache directory! '" + cacheDirPath + "%s' will be used.");
appCacheDir = new File(cacheDirPath);
}
return appCacheDir;
Expand All @@ -72,7 +74,7 @@ private static File getExternalCacheDir(Context context) {
File appCacheDir = new File(new File(dataDir, context.getPackageName()), "cache");
if (!appCacheDir.exists()) {
if (!appCacheDir.mkdirs()) {
Log.w(LOG_TAG, "Unable to create external cache directory");
LOG.warn("Unable to create external cache directory");
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.danikula.videocache.file;

import android.util.Log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
Expand All @@ -16,7 +17,7 @@
*/
abstract class LruDiskUsage implements DiskUsage {

private static final String LOG_TAG = "ProxyCache";
private static final Logger LOG = LoggerFactory.getLogger("LruDiskUsage");
private final ExecutorService workerThread = Executors.newSingleThreadExecutor();

@Override
Expand All @@ -43,9 +44,9 @@ private void trim(List<File> files) {
if (deleted) {
totalCount--;
totalSize -= fileSize;
Log.i(LOG_TAG, "Cache file " + file + " is deleted because it exceeds cache limit");
LOG.info("Cache file " + file + " is deleted because it exceeds cache limit");
} else {
Log.e(LOG_TAG, "Error deleting file " + file + " for trimming cache");
LOG.error("Error deleting file " + file + " for trimming cache");
}
}
}
Expand Down

0 comments on commit 44318d8

Please sign in to comment.