22
33import android .content .Context ;
44import android .os .SystemClock ;
5- import android .util .Log ;
65
76import com .danikula .videocache .file .DiskUsage ;
87import com .danikula .videocache .file .FileNameGenerator ;
1211import com .danikula .videocache .sourcestorage .SourceInfoStorage ;
1312import com .danikula .videocache .sourcestorage .SourceInfoStorageFactory ;
1413
14+ import org .slf4j .Logger ;
15+ import org .slf4j .LoggerFactory ;
16+
1517import java .io .File ;
1618import java .io .IOException ;
1719import java .io .OutputStream ;
3335
3436import static com .danikula .videocache .Preconditions .checkAllNotNull ;
3537import static com .danikula .videocache .Preconditions .checkNotNull ;
36- import static com .danikula .videocache .ProxyCacheUtils .LOG_TAG ;
3738import static java .util .concurrent .TimeUnit .MILLISECONDS ;
3839
3940/**
5758 */
5859public 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 {
0 commit comments