Skip to content

Commit

Permalink
Merge pull request facebook#9 from jasta/fun-with-clog
Browse files Browse the repository at this point in the history
Fun with CLog
  • Loading branch information
jasta committed Feb 6, 2015
2 parents 6849dde + db592db commit 52596d1
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 21 deletions.
2 changes: 1 addition & 1 deletion stetho/src/main/java/com/facebook/stetho/Stetho.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public Iterable<ChromeDevtoolsDomain> get() {
modules.add(new HeapProfiler());
modules.add(new Inspector());
modules.add(new Network(context));
modules.add(new Page());
modules.add(new Page(context));
modules.add(new Profiler());
modules.add(new Runtime());
modules.add(new Worker());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
public class CLog {
private static final String TAG = "CLog";

// @VisibleForTest
public static void writeToConsole(
ChromePeerManager chromePeerManager,
Console.MessageLevel logLevel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import com.facebook.stetho.common.LogRedirector;
import com.facebook.stetho.common.Utf8Charset;
import com.facebook.stetho.inspector.console.CLog;
import com.facebook.stetho.inspector.protocol.module.Console;
import com.facebook.stetho.inspector.protocol.module.Network;
import com.facebook.stetho.inspector.protocol.module.Page;
Expand Down Expand Up @@ -110,9 +111,10 @@ private static String readBodyAsString(
return new String(body, Utf8Charset.INSTANCE);
}
} catch (IOException e) {
writeToConsole(
CLog.writeToConsole(
peerManager,
Console.MessageLevel.WARNING,
Console.MessageSource.NETWORK,
"Could not reproduce POST body: " + e);
}
return null;
Expand Down Expand Up @@ -183,9 +185,10 @@ public InputStream interpretResponseStream(
peerManager,
responseHandler);
} catch (IOException e) {
writeToConsole(
CLog.writeToConsole(
peerManager,
Console.MessageLevel.ERROR,
Console.MessageSource.NETWORK,
"Error writing response body data for request #" + requestId);
}
}
Expand Down Expand Up @@ -274,22 +277,6 @@ private static JSONObject formatHeadersAsJSON(InspectorHeaders headers) {
return json;
}

private static void writeToConsole(
NetworkPeerManager networkPeerManager,
Console.MessageLevel logLevel,
String messageText) {
// Send to logcat to increase the chances that a developer will notice :)
LogRedirector.d(TAG, messageText);

Console.ConsoleMessage message = new Console.ConsoleMessage();
message.source = Console.MessageSource.NETWORK;
message.level = logLevel;
message.text = messageText;
Console.MessageAddedRequest messageAddedRequest = new Console.MessageAddedRequest();
messageAddedRequest.message = message;
networkPeerManager.sendNotificationToPeers("Console.messageAdded", messageAddedRequest);
}

@Nonnull
private ResourceTypeHelper getResourceTypeHelper() {
if (mResourceTypeHelper == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import java.util.Collections;
import java.util.List;

import android.content.Context;
import com.facebook.stetho.inspector.console.CLog;
import com.facebook.stetho.inspector.helper.ChromePeerManager;
import com.facebook.stetho.inspector.jsonrpc.JsonRpcPeer;
import com.facebook.stetho.inspector.jsonrpc.JsonRpcResult;
import com.facebook.stetho.inspector.protocol.ChromeDevtoolsDomain;
Expand All @@ -15,12 +18,16 @@
import org.json.JSONObject;

public class Page implements ChromeDevtoolsDomain {
public Page() {
private final Context mContext;

public Page(Context context) {
mContext = context.getApplicationContext();
}

@ChromeDevtoolsMethod
public void enable(JsonRpcPeer peer, JSONObject params) {
notifyExecutionContexts(peer);
sendWelcomeMessage(peer);
}

@ChromeDevtoolsMethod
Expand All @@ -36,6 +43,28 @@ private void notifyExecutionContexts(JsonRpcPeer peer) {
peer.invokeMethod("Runtime.executionContextCreated", params, null /* callback */);
}

private void sendWelcomeMessage(JsonRpcPeer peer) {
Console.ConsoleMessage message = new Console.ConsoleMessage();
message.source = Console.MessageSource.JAVASCRIPT;
message.level = Console.MessageLevel.LOG;
message.text =
// Note: not using Android resources so we can maintain .jar distribution for now.
"_____/\\\\\\\\\\\\\\\\\\\\\\_______________________________________________/\\\\\\_______________________\n" +
" ___/\\\\\\/////////\\\\\\____________________________________________\\/\\\\\\_______________________\n" +
" __\\//\\\\\\______\\///______/\\\\\\_________________________/\\\\\\______\\/\\\\\\_______________________\n" +
" ___\\////\\\\\\__________/\\\\\\\\\\\\\\\\\\\\\\_____/\\\\\\\\\\\\\\\\___/\\\\\\\\\\\\\\\\\\\\\\_\\/\\\\\\_____________/\\\\\\\\\\____\n" +
" ______\\////\\\\\\______\\////\\\\\\////____/\\\\\\/////\\\\\\_\\////\\\\\\////__\\/\\\\\\\\\\\\\\\\\\\\____/\\\\\\///\\\\\\__\n" +
" _________\\////\\\\\\______\\/\\\\\\_______/\\\\\\\\\\\\\\\\\\\\\\_____\\/\\\\\\______\\/\\\\\\/////\\\\\\__/\\\\\\__\\//\\\\\\_\n" +
" __/\\\\\\______\\//\\\\\\_____\\/\\\\\\_/\\\\__\\//\\\\///////______\\/\\\\\\_/\\\\__\\/\\\\\\___\\/\\\\\\_\\//\\\\\\__/\\\\\\__\n" +
" _\\///\\\\\\\\\\\\\\\\\\\\\\/______\\//\\\\\\\\\\____\\//\\\\\\\\\\\\\\\\\\\\____\\//\\\\\\\\\\___\\/\\\\\\___\\/\\\\\\__\\///\\\\\\\\\\/___\n" +
" ___\\///////////_________\\/////______\\//////////______\\/////____\\///____\\///_____\\/////_____\n" +
" Welcome to Stetho\n" +
" Attached to " + mContext.getPackageName() + "\n";
Console.MessageAddedRequest messageAddedRequest = new Console.MessageAddedRequest();
messageAddedRequest.message = message;
peer.invokeMethod("Console.messageAdded", messageAddedRequest, null /* callback */);
}

// Dog science...
@ChromeDevtoolsMethod
public JsonRpcResult getResourceTree(JsonRpcPeer peer, JSONObject params) {
Expand Down

0 comments on commit 52596d1

Please sign in to comment.