Skip to content

Commit

Permalink
Make Platform.log exception parameter optional (square#5482)
Browse files Browse the repository at this point in the history
Make Platform.log exception parameters optional, and reorders to allow level defaulting to INFO.
  • Loading branch information
yschimke authored Sep 29, 2019
1 parent ccbc87a commit 16173e2
Show file tree
Hide file tree
Showing 18 changed files with 33 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ class OkHttpTest {
moshi.adapter(HowsMySslResults::class.java).fromJson(response.body!!.string())!!
}

Platform.get().log(Platform.WARN, "results $results", null)
Platform.get().log("results $results", Platform.WARN)

assertTrue(results.session_ticket_supported)
assertEquals("Probably Okay", results.rating)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ private SSLSocket doSsl(Socket socket) throws IOException {
send404(stream, path);
}
} catch (IOException e) {
Platform.get().log(INFO, "Failure serving Http2Stream: " + e.getMessage(), null);
Platform.get().log("Failure serving Http2Stream: " + e.getMessage(), INFO, null);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ class DnsOverHttps internal constructor(
@Throws(Exception::class)
private fun readResponse(hostname: String, response: Response): List<InetAddress> {
if (response.cacheResponse == null && response.protocol !== Protocol.HTTP_2) {
Platform.get().log(Platform.WARN, "Incorrect protocol: ${response.protocol}", null)
Platform.get().log("Incorrect protocol: ${response.protocol}", Platform.WARN)
}

response.use {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import okhttp3.OkHttpClient
import okhttp3.Response
import okhttp3.internal.http.promisesBody
import okhttp3.internal.platform.Platform
import okhttp3.internal.platform.Platform.Companion.INFO
import okio.Buffer
import okio.GzipSource
import java.io.IOException
Expand Down Expand Up @@ -113,7 +112,7 @@ class HttpLoggingInterceptor @JvmOverloads constructor(
@JvmField
val DEFAULT: Logger = object : Logger {
override fun log(message: String) {
Platform.get().log(INFO, message, null)
Platform.get().log(message)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,24 @@ public final class EventSourceRecorder extends EventSourceListener {
private final BlockingQueue<Object> events = new LinkedBlockingDeque<>();

@Override public void onOpen(EventSource eventSource, Response response) {
Platform.get().log(Platform.INFO, "[ES] onOpen", null);
Platform.get().log("[ES] onOpen", Platform.INFO, null);
events.add(new Open(eventSource, response));
}

@Override public void onEvent(EventSource eventSource, @Nullable String id, @Nullable String type,
String data) {
Platform.get().log(Platform.INFO, "[ES] onEvent", null);
Platform.get().log("[ES] onEvent", Platform.INFO, null);
events.add(new Event(id, type, data));
}

@Override public void onClosed(EventSource eventSource) {
Platform.get().log(Platform.INFO, "[ES] onClosed", null);
Platform.get().log("[ES] onClosed", Platform.INFO, null);
events.add(new Closed());
}

@Override
public void onFailure(EventSource eventSource, @Nullable Throwable t, @Nullable Response response) {
Platform.get().log(Platform.INFO, "[ES] onFailure", t);
Platform.get().log("[ES] onFailure", Platform.INFO, t);
events.add(new Failure(t, response));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class JavaNetCookieJar(private val cookieHandler: CookieHandler) : CookieJar {
try {
cookieHandler.put(url.toUri(), multimap)
} catch (e: IOException) {
Platform.get().log(WARN, "Saving cookies failed for " + url.resolve("/...")!!, e)
Platform.get().log("Saving cookies failed for " + url.resolve("/...")!!, WARN, e)
}
}

Expand All @@ -46,7 +46,7 @@ class JavaNetCookieJar(private val cookieHandler: CookieHandler) : CookieJar {
// The RI passes all headers. We don't have 'em, so we don't pass 'em!
cookieHandler.get(url.toUri(), emptyMap<String, List<String>>())
} catch (e: IOException) {
Platform.get().log(WARN, "Loading cookies failed for " + url.resolve("/...")!!, e)
Platform.get().log("Loading cookies failed for " + url.resolve("/...")!!, WARN, e)
return emptyList()
}

Expand Down
2 changes: 1 addition & 1 deletion okhttp/src/main/java/okhttp3/RealCall.kt
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ internal class RealCall private constructor(
} catch (e: IOException) {
if (signalledCallback) {
// Do not signal the callback twice!
Platform.get().log(INFO, "Callback failure for ${toLoggableString()}", e)
Platform.get().log("Callback failure for ${toLoggableString()}", INFO, e)
} else {
responseCallback.onFailure(this@RealCall, e)
}
Expand Down
3 changes: 2 additions & 1 deletion okhttp/src/main/java/okhttp3/internal/cache/DiskLruCache.kt
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,9 @@ class DiskLruCache internal constructor(
initialized = true
return
} catch (journalIsCorrupt: IOException) {
Platform.get().log(WARN,
Platform.get().log(
"DiskLruCache $directory is corrupt: ${journalIsCorrupt.message}, removing",
WARN,
journalIsCorrupt)
}

Expand Down
2 changes: 1 addition & 1 deletion okhttp/src/main/java/okhttp3/internal/http/HttpHeaders.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ fun Headers.parseChallenges(headerName: String): List<Challenge> {
try {
header.readChallengeHeader(result)
} catch (e: EOFException) {
Platform.get().log(Platform.WARN, "Unable to parse challenge", e)
Platform.get().log("Unable to parse challenge", Platform.WARN, e)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ class Http2Connection internal constructor(builder: Builder) : Closeable {
try {
listener.onStream(newStream)
} catch (e: IOException) {
Platform.get().log(INFO, "Http2Connection.Listener failure for $connectionName", e)
Platform.get().log("Http2Connection.Listener failure for $connectionName", INFO, e)
ignoreIoExceptions {
newStream.close(ErrorCode.PROTOCOL_ERROR, e)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Android10Platform : Platform() {
// No TLS extensions if the socket class is custom.
socketAdapters.find { it.matchesSocket(sslSocket) }?.getSelectedProtocol(sslSocket)

override fun log(level: Int, message: String, t: Throwable?) {
override fun log(message: String, level: Int, t: Throwable?) {
androidLog(level, message, t)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class AndroidPlatform : Platform() {
// No TLS extensions if the socket class is custom.
socketAdapters.find { it.matchesSocket(sslSocket) }?.getSelectedProtocol(sslSocket)

override fun log(level: Int, message: String, t: Throwable?) {
override fun log(message: String, level: Int, t: Throwable?) {
androidLog(level, message, t)
}

Expand All @@ -93,7 +93,7 @@ class AndroidPlatform : Platform() {
val reported = closeGuard.warnIfOpen(stackTrace)
if (!reported) {
// Unable to report via CloseGuard. As a last-ditch effort, send it to the logger.
log(WARN, message, null)
log(message, WARN)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ class Jdk8WithJettyBootPlatform(
try {
val provider = Proxy.getInvocationHandler(getMethod.invoke(null, sslSocket)) as AlpnProvider
if (!provider.unsupported && provider.selected == null) {
Platform.get().log(INFO,
"ALPN callback dropped: HTTP/2 is disabled. " + "Is alpn-boot on the boot class path?",
null)
log("ALPN callback dropped: HTTP/2 is disabled. " + "Is alpn-boot on the boot class path?")
return null
}
return if (provider.unsupported) null else provider.selected
Expand Down
4 changes: 2 additions & 2 deletions okhttp/src/main/java/okhttp3/internal/platform/Platform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ open class Platform {
socket.connect(address, connectTimeout)
}

open fun log(level: Int, message: String, t: Throwable?) {
open fun log(message: String, level: Int = INFO, t: Throwable? = null) {
val logLevel = if (level == WARN) Level.WARNING else Level.INFO
logger.log(logLevel, message, t)
}
Expand All @@ -141,7 +141,7 @@ open class Platform {
logMessage += " To see where this was allocated, set the OkHttpClient logger level to " +
"FINE: Logger.getLogger(OkHttpClient.class.getName()).setLevel(Level.FINE);"
}
log(WARN, logMessage, stackTrace as Throwable?)
log(logMessage, WARN, stackTrace as Throwable?)
}

open fun buildCertificateChainCleaner(trustManager: X509TrustManager): CertificateChainCleaner =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class DeferredSocketAdapter(private val socketPackage: String) : SocketAdapter {
delegate = AndroidSocketAdapter(possibleClass)
} catch (e: Exception) {
Platform.get()
.log(Platform.WARN, "Failed to initialize DeferredSocketAdapter $socketPackage", e)
.log("Failed to initialize DeferredSocketAdapter $socketPackage", Platform.WARN, e)
}

initialized = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ private const val MAX_LOG_LENGTH = 4000

internal fun androidLog(level: Int, message: String, t: Throwable?) {
var logMessage = message
val logLevel = if (level == Platform.WARN) Log.WARN else Log.DEBUG
val logLevel = when (level) {
Platform.WARN -> Log.WARN
else -> Log.DEBUG
}
if (t != null) logMessage = logMessage + '\n'.toString() + Log.getStackTraceString(t)

// Split by line, then ensure each line can fit into Log's maximum length.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ class PublicSuffixDatabase {
Thread.interrupted() // Temporarily clear the interrupted state.
interrupted = true
} catch (e: IOException) {
Platform.get().log(Platform.WARN, "Failed to read public suffix list", e)
Platform.get().log("Failed to read public suffix list", Platform.WARN, e)
return
}
}
Expand Down
12 changes: 6 additions & 6 deletions okhttp/src/test/java/okhttp3/internal/ws/WebSocketRecorder.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void setNextEventDelegate(WebSocketListener delegate) {
}

@Override public void onOpen(WebSocket webSocket, Response response) {
Platform.get().log(Platform.INFO, "[WS " + name + "] onOpen", null);
Platform.get().log("[WS " + name + "] onOpen", Platform.INFO, null);

WebSocketListener delegate = this.delegate;
if (delegate != null) {
Expand All @@ -56,7 +56,7 @@ public void setNextEventDelegate(WebSocketListener delegate) {
}

@Override public void onMessage(WebSocket webSocket, ByteString bytes) {
Platform.get().log(Platform.INFO, "[WS " + name + "] onMessage", null);
Platform.get().log("[WS " + name + "] onMessage", Platform.INFO, null);

WebSocketListener delegate = this.delegate;
if (delegate != null) {
Expand All @@ -69,7 +69,7 @@ public void setNextEventDelegate(WebSocketListener delegate) {
}

@Override public void onMessage(WebSocket webSocket, String text) {
Platform.get().log(Platform.INFO, "[WS " + name + "] onMessage", null);
Platform.get().log("[WS " + name + "] onMessage", Platform.INFO, null);

WebSocketListener delegate = this.delegate;
if (delegate != null) {
Expand All @@ -82,7 +82,7 @@ public void setNextEventDelegate(WebSocketListener delegate) {
}

@Override public void onClosing(WebSocket webSocket, int code, String reason) {
Platform.get().log(Platform.INFO, "[WS " + name + "] onClosing " + code, null);
Platform.get().log("[WS " + name + "] onClosing " + code, Platform.INFO, null);

WebSocketListener delegate = this.delegate;
if (delegate != null) {
Expand All @@ -94,7 +94,7 @@ public void setNextEventDelegate(WebSocketListener delegate) {
}

@Override public void onClosed(WebSocket webSocket, int code, String reason) {
Platform.get().log(Platform.INFO, "[WS " + name + "] onClosed " + code, null);
Platform.get().log("[WS " + name + "] onClosed " + code, Platform.INFO, null);

WebSocketListener delegate = this.delegate;
if (delegate != null) {
Expand All @@ -106,7 +106,7 @@ public void setNextEventDelegate(WebSocketListener delegate) {
}

@Override public void onFailure(WebSocket webSocket, Throwable t, @Nullable Response response) {
Platform.get().log(Platform.INFO, "[WS " + name + "] onFailure", t);
Platform.get().log("[WS " + name + "] onFailure", Platform.INFO, t);

WebSocketListener delegate = this.delegate;
if (delegate != null) {
Expand Down

0 comments on commit 16173e2

Please sign in to comment.