Skip to content

Commit 44318d8

Browse files
committed
📦 2.6.2: use slf4j for logging
1 parent 76c13ba commit 44318d8

21 files changed

+87
-115
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ repositories {
3636
jcenter()
3737
}
3838
dependencies {
39-
compile 'com.danikula:videocache:2.6.1'
39+
compile 'com.danikula:videocache:2.6.2'
4040
}
4141
```
4242

library/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@ sourceCompatibility = '1.7'
2323

2424
dependencies {
2525
compile 'com.google.android:android:1.6_r2'
26+
compile 'org.slf4j:slf4j-android:1.7.21'
2627
}
2728

2829
publish {
2930
userOrg = 'alexeydanilov'
3031
groupId = 'com.danikula'
3132
artifactId = 'videocache'
32-
publishVersion = '2.6.1'
33+
publishVersion = '2.6.2'
3334
description = 'Cache support for android VideoView'
3435
website = 'https://github.com/danikula/AndroidVideoCache'
3536
}

library/src/main/java/com/danikula/videocache/HttpProxyCacheServer.java

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import android.content.Context;
44
import android.os.SystemClock;
5-
import android.util.Log;
65

76
import com.danikula.videocache.file.DiskUsage;
87
import com.danikula.videocache.file.FileNameGenerator;
@@ -12,6 +11,9 @@
1211
import com.danikula.videocache.sourcestorage.SourceInfoStorage;
1312
import com.danikula.videocache.sourcestorage.SourceInfoStorageFactory;
1413

14+
import org.slf4j.Logger;
15+
import org.slf4j.LoggerFactory;
16+
1517
import java.io.File;
1618
import java.io.IOException;
1719
import java.io.OutputStream;
@@ -33,7 +35,6 @@
3335

3436
import static com.danikula.videocache.Preconditions.checkAllNotNull;
3537
import static com.danikula.videocache.Preconditions.checkNotNull;
36-
import static com.danikula.videocache.ProxyCacheUtils.LOG_TAG;
3738
import static java.util.concurrent.TimeUnit.MILLISECONDS;
3839

3940
/**
@@ -57,6 +58,8 @@
5758
*/
5859
public class HttpProxyCacheServer {
5960

61+
private static final Logger LOG = LoggerFactory.getLogger("HttpProxyCacheServer");
62+
6063
private static final String PROXY_HOST = "127.0.0.1";
6164
private static final String PING_REQUEST = "ping";
6265
private static final String PING_RESPONSE = "ping ok";
@@ -84,7 +87,7 @@ private HttpProxyCacheServer(Config config) {
8487
this.waitConnectionThread = new Thread(new WaitRequestsRunnable(startSignal));
8588
this.waitConnectionThread.start();
8689
startSignal.await(); // freeze thread, wait for server starts
87-
Log.i(LOG_TAG, "Proxy cache server started. Ping it...");
90+
LOG.info("Proxy cache server started. Ping it...");
8891
makeSureServerWorks();
8992
} catch (IOException | InterruptedException e) {
9093
socketProcessor.shutdown();
@@ -105,12 +108,12 @@ private void makeSureServerWorks() {
105108
}
106109
SystemClock.sleep(delay);
107110
} catch (InterruptedException | ExecutionException | TimeoutException e) {
108-
Log.e(LOG_TAG, "Error pinging server [attempt: " + pingAttempts + ", timeout: " + delay + "]. ", e);
111+
LOG.error("Error pinging server [attempt: " + pingAttempts + ", timeout: " + delay + "]. ", e);
109112
}
110113
pingAttempts++;
111114
delay *= 2;
112115
}
113-
Log.e(LOG_TAG, "Shutdown server… Error pinging server [attempts: " + pingAttempts + ", max timeout: " + delay / 2 + "]. " +
116+
LOG.error("Shutdown server… Error pinging server [attempts: " + pingAttempts + ", max timeout: " + delay / 2 + "]. " +
114117
"If you see this message, please, email me danikula@gmail.com");
115118
shutdown();
116119
}
@@ -124,10 +127,10 @@ private boolean pingServer() throws ProxyCacheException {
124127
byte[] response = new byte[expectedResponse.length];
125128
source.read(response);
126129
boolean pingOk = Arrays.equals(expectedResponse, response);
127-
Log.d(LOG_TAG, "Ping response: `" + new String(response) + "`, pinged? " + pingOk);
130+
LOG.info("Ping response: `" + new String(response) + "`, pinged? " + pingOk);
128131
return pingOk;
129132
} catch (ProxyCacheException e) {
130-
Log.e(LOG_TAG, "Error reading ping response", e);
133+
LOG.error("Error reading ping response", e);
131134
return false;
132135
} finally {
133136
source.close();
@@ -136,7 +139,7 @@ private boolean pingServer() throws ProxyCacheException {
136139

137140
public String getProxyUrl(String url) {
138141
if (!pinged) {
139-
Log.e(LOG_TAG, "Proxy server isn't pinged. Caching doesn't work. If you see this message, please, email me danikula@gmail.com");
142+
LOG.error("Proxy server isn't pinged. Caching doesn't work. If you see this message, please, email me danikula@gmail.com");
140143
}
141144
return pinged ? appendToProxyUrl(url) : url;
142145
}
@@ -151,7 +154,7 @@ public void registerCacheListener(CacheListener cacheListener, String url) {
151154
try {
152155
getClients(url).registerCacheListener(cacheListener);
153156
} catch (ProxyCacheException e) {
154-
Log.d(LOG_TAG, "Error registering cache listener", e);
157+
LOG.warn("Error registering cache listener", e);
155158
}
156159
}
157160
}
@@ -162,7 +165,7 @@ public void unregisterCacheListener(CacheListener cacheListener, String url) {
162165
try {
163166
getClients(url).unregisterCacheListener(cacheListener);
164167
} catch (ProxyCacheException e) {
165-
Log.d(LOG_TAG, "Error registering cache listener", e);
168+
LOG.warn("Error registering cache listener", e);
166169
}
167170
}
168171
}
@@ -191,7 +194,7 @@ public boolean isCached(String url) {
191194
}
192195

193196
public void shutdown() {
194-
Log.i(LOG_TAG, "Shutdown proxy server");
197+
LOG.info("Shutdown proxy server");
195198

196199
shutdownClients();
197200

@@ -220,7 +223,7 @@ private void waitForRequest() {
220223
try {
221224
while (!Thread.currentThread().isInterrupted()) {
222225
Socket socket = serverSocket.accept();
223-
Log.d(LOG_TAG, "Accept new socket " + socket);
226+
LOG.debug("Accept new socket " + socket);
224227
socketProcessor.submit(new SocketProcessorRunnable(socket));
225228
}
226229
} catch (IOException e) {
@@ -231,7 +234,7 @@ private void waitForRequest() {
231234
private void processSocket(Socket socket) {
232235
try {
233236
GetRequest request = GetRequest.read(socket.getInputStream());
234-
Log.i(LOG_TAG, "Request to cache proxy:" + request);
237+
LOG.debug("Request to cache proxy:" + request);
235238
String url = ProxyCacheUtils.decode(request.uri);
236239
if (PING_REQUEST.equals(url)) {
237240
responseToPing(socket);
@@ -242,12 +245,12 @@ private void processSocket(Socket socket) {
242245
} catch (SocketException e) {
243246
// There is no way to determine that client closed connection http://stackoverflow.com/a/10241044/999458
244247
// So just to prevent log flooding don't log stacktrace
245-
Log.d(LOG_TAG, "Closing socket… Socket is closed by client.");
248+
LOG.debug("Closing socket… Socket is closed by client.");
246249
} catch (ProxyCacheException | IOException e) {
247250
onError(new ProxyCacheException("Error processing request", e));
248251
} finally {
249252
releaseSocket(socket);
250-
Log.d(LOG_TAG, "Opened connections: " + getClientsCount());
253+
LOG.debug("Opened connections: " + getClientsCount());
251254
}
252255
}
253256

@@ -292,7 +295,7 @@ private void closeSocketInput(Socket socket) {
292295
} catch (SocketException e) {
293296
// There is no way to determine that client closed connection http://stackoverflow.com/a/10241044/999458
294297
// So just to prevent log flooding don't log stacktrace
295-
Log.d(LOG_TAG, "Releasing input stream… Socket is closed by client.");
298+
LOG.debug("Releasing input stream… Socket is closed by client.");
296299
} catch (IOException e) {
297300
onError(new ProxyCacheException("Error closing socket input stream", e));
298301
}
@@ -319,7 +322,7 @@ private void closeSocket(Socket socket) {
319322
}
320323

321324
private void onError(Throwable e) {
322-
Log.e(LOG_TAG, "HttpProxyCacheServer error", e);
325+
LOG.error("HttpProxyCacheServer error", e);
323326
}
324327

325328
private final class WaitRequestsRunnable implements Runnable {

library/src/main/java/com/danikula/videocache/HttpUrlSource.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.danikula.videocache;
22

33
import android.text.TextUtils;
4-
import android.util.Log;
54

65
import com.danikula.videocache.sourcestorage.SourceInfoStorage;
76
import com.danikula.videocache.sourcestorage.SourceInfoStorageFactory;
87

8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
10+
911
import java.io.BufferedInputStream;
1012
import java.io.IOException;
1113
import java.io.InputStream;
@@ -15,7 +17,6 @@
1517

1618
import static com.danikula.videocache.Preconditions.checkNotNull;
1719
import static com.danikula.videocache.ProxyCacheUtils.DEFAULT_BUFFER_SIZE;
18-
import static com.danikula.videocache.ProxyCacheUtils.LOG_TAG;
1920
import static java.net.HttpURLConnection.HTTP_MOVED_PERM;
2021
import static java.net.HttpURLConnection.HTTP_MOVED_TEMP;
2122
import static java.net.HttpURLConnection.HTTP_OK;
@@ -29,6 +30,8 @@
2930
*/
3031
public class HttpUrlSource implements Source {
3132

33+
private static final Logger LOG = LoggerFactory.getLogger("HttpUrlSource");
34+
3235
private static final int MAX_REDIRECTS = 5;
3336
private final SourceInfoStorage sourceInfoStorage;
3437
private SourceInfo sourceInfo;
@@ -108,7 +111,7 @@ public int read(byte[] buffer) throws ProxyCacheException {
108111
}
109112

110113
private void fetchContentInfo() throws ProxyCacheException {
111-
Log.d(LOG_TAG, "Read content info from " + sourceInfo.url);
114+
LOG.debug("Read content info from " + sourceInfo.url);
112115
HttpURLConnection urlConnection = null;
113116
InputStream inputStream = null;
114117
try {
@@ -118,9 +121,9 @@ private void fetchContentInfo() throws ProxyCacheException {
118121
inputStream = urlConnection.getInputStream();
119122
this.sourceInfo = new SourceInfo(sourceInfo.url, length, mime);
120123
this.sourceInfoStorage.put(sourceInfo.url, sourceInfo);
121-
Log.i(LOG_TAG, "Source info fetched: " + sourceInfo);
124+
LOG.debug("Source info fetched: " + sourceInfo);
122125
} catch (IOException e) {
123-
Log.e(LOG_TAG, "Error fetching info from " + sourceInfo.url, e);
126+
LOG.error("Error fetching info from " + sourceInfo.url, e);
124127
} finally {
125128
ProxyCacheUtils.close(inputStream);
126129
if (urlConnection != null) {
@@ -135,7 +138,7 @@ private HttpURLConnection openConnection(int offset, int timeout) throws IOExcep
135138
int redirectCount = 0;
136139
String url = this.sourceInfo.url;
137140
do {
138-
Log.d(LOG_TAG, "Open connection " + (offset > 0 ? " with offset " + offset : "") + " to " + url);
141+
LOG.debug("Open connection " + (offset > 0 ? " with offset " + offset : "") + " to " + url);
139142
connection = (HttpURLConnection) new URL(url).openConnection();
140143
if (offset > 0) {
141144
connection.setRequestProperty("Range", "bytes=" + offset + "-");

library/src/main/java/com/danikula/videocache/ProxyCache.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package com.danikula.videocache;
22

3-
import android.util.Log;
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
45

56
import java.util.concurrent.atomic.AtomicInteger;
67

78
import static com.danikula.videocache.Preconditions.checkNotNull;
8-
import static com.danikula.videocache.ProxyCacheUtils.LOG_TAG;
99

1010
/**
1111
* Proxy for {@link Source} with caching support ({@link Cache}).
@@ -18,6 +18,7 @@
1818
*/
1919
class ProxyCache {
2020

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

2324
private final Source source;
@@ -61,7 +62,7 @@ private void checkReadSourceErrorsCount() throws ProxyCacheException {
6162

6263
public void shutdown() {
6364
synchronized (stopLock) {
64-
Log.d(LOG_TAG, "Shutdown proxy for " + source);
65+
LOG.debug("Shutdown proxy for " + source);
6566
try {
6667
stopped = true;
6768
if (sourceReaderThread != null) {
@@ -173,9 +174,9 @@ private void closeSource() {
173174
protected final void onError(final Throwable e) {
174175
boolean interruption = e instanceof InterruptedProxyCacheException;
175176
if (interruption) {
176-
Log.d(LOG_TAG, "ProxyCache is interrupted");
177+
LOG.debug("ProxyCache is interrupted");
177178
} else {
178-
Log.e(LOG_TAG, "ProxyCache error", e);
179+
LOG.error("ProxyCache error", e);
179180
}
180181
}
181182

library/src/main/java/com/danikula/videocache/ProxyCacheUtils.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.danikula.videocache;
22

33
import android.text.TextUtils;
4-
import android.util.Log;
54
import android.webkit.MimeTypeMap;
65

6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
79
import java.io.Closeable;
810
import java.io.IOException;
911
import java.io.UnsupportedEncodingException;
@@ -23,7 +25,7 @@
2325
*/
2426
public class ProxyCacheUtils {
2527

26-
static final String LOG_TAG = "ProxyCache";
28+
private static final Logger LOG = LoggerFactory.getLogger("ProxyCacheUtils");
2729
static final int DEFAULT_BUFFER_SIZE = 8 * 1024;
2830
static final int MAX_ARRAY_PREVIEW = 16;
2931

@@ -70,7 +72,7 @@ static void close(Closeable closeable) {
7072
try {
7173
closeable.close();
7274
} catch (IOException e) {
73-
Log.e(LOG_TAG, "Error closing resource", e);
75+
LOG.error("Error closing resource", e);
7476
}
7577
}
7678
}

library/src/main/java/com/danikula/videocache/StorageUtils.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
import android.content.Context;
44
import android.os.Environment;
5-
import android.util.Log;
5+
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
68

79
import java.io.File;
810

911
import static android.os.Environment.MEDIA_MOUNTED;
10-
import static com.danikula.videocache.ProxyCacheUtils.LOG_TAG;
1112

1213
/**
1314
* Provides application storage paths
@@ -19,6 +20,7 @@
1920
*/
2021
final class StorageUtils {
2122

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

2426
/**
@@ -61,7 +63,7 @@ private static File getCacheDirectory(Context context, boolean preferExternal) {
6163
}
6264
if (appCacheDir == null) {
6365
String cacheDirPath = "/data/data/" + context.getPackageName() + "/cache/";
64-
Log.w(LOG_TAG, "Can't define system cache directory! '" + cacheDirPath + "%s' will be used.");
66+
LOG.warn("Can't define system cache directory! '" + cacheDirPath + "%s' will be used.");
6567
appCacheDir = new File(cacheDirPath);
6668
}
6769
return appCacheDir;
@@ -72,7 +74,7 @@ private static File getExternalCacheDir(Context context) {
7274
File appCacheDir = new File(new File(dataDir, context.getPackageName()), "cache");
7375
if (!appCacheDir.exists()) {
7476
if (!appCacheDir.mkdirs()) {
75-
Log.w(LOG_TAG, "Unable to create external cache directory");
77+
LOG.warn("Unable to create external cache directory");
7678
return null;
7779
}
7880
}

library/src/main/java/com/danikula/videocache/file/LruDiskUsage.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.danikula.videocache.file;
22

3-
import android.util.Log;
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
45

56
import java.io.File;
67
import java.io.IOException;
@@ -16,7 +17,7 @@
1617
*/
1718
abstract class LruDiskUsage implements DiskUsage {
1819

19-
private static final String LOG_TAG = "ProxyCache";
20+
private static final Logger LOG = LoggerFactory.getLogger("LruDiskUsage");
2021
private final ExecutorService workerThread = Executors.newSingleThreadExecutor();
2122

2223
@Override
@@ -43,9 +44,9 @@ private void trim(List<File> files) {
4344
if (deleted) {
4445
totalCount--;
4546
totalSize -= fileSize;
46-
Log.i(LOG_TAG, "Cache file " + file + " is deleted because it exceeds cache limit");
47+
LOG.info("Cache file " + file + " is deleted because it exceeds cache limit");
4748
} else {
48-
Log.e(LOG_TAG, "Error deleting file " + file + " for trimming cache");
49+
LOG.error("Error deleting file " + file + " for trimming cache");
4950
}
5051
}
5152
}

0 commit comments

Comments
 (0)