@@ -40,13 +40,10 @@ your applications. Here are some tools that you'll use most often:</p>
4040 your application, which can help you profile the performance of your application.</dd>
4141 <dt><strong><a href="{@docRoot}guide/developing/tools/ddms.html#logcat">logcat</a></strong></dt>
4242 <dd>Dumps a log of system
43- messages. The messages include a stack trace when the device throws an error,
43+ messages. The messages include a stack trace when the emulator throws an error,
4444 as well as {@link android.util.Log} messages you've written from your application. To run
45- logcat, execute <code>adb logcat</code> from your Android SDK {@code tools/} directory or,
46- from DDMS, select <strong>Device > Run
47- logcat</strong>. When using the <a href="{@docRoot}sdk/eclipse-adt.html">ADT plugin for
48- Eclipse</a>, you can also view logcat messages by opening the Logcat view, available from
49- <strong>Window > Show View > Other > Android > Logcat</strong>.
45+ logcat, execute <code>adb logcat</code> or, from DDMS, select <strong>Device > Run
46+ logcat</strong>.
5047 <p>{@link android.util.Log} is a logging
5148 class you can use to print out messages to the logcat. You can read messages
5249 in real time if you run logcat on DDMS (covered next). Common logging methods include:
@@ -151,7 +148,72 @@ following options (among others):</p>
151148
152149<h2 id="DebuggingWebPages">Debugging Web Pages</h2>
153150
154- <p>See the <a href="{@docRoot}guide/webapps/debugging.html">Debugging Web Apps</a> document.</p>
151+ <p>If you're developing a web application for Android devices, you can debug your JavaScript in the
152+ Android Browser using the Console APIs, which will output messages to logcat. If you're familiar
153+ debugging web pages with Firefox's FireBug or WebKit's Web Inspector, then you're probably familiar
154+ with the Console APIs. The Android Browser (and the {@link android.webkit.WebChromeClient}) supports
155+ most of the same APIs.</p>
156+
157+ <p>When you call a function from the Console APIs (in the DOM's {@code window.console} object),
158+ you will see the output in logcat as a warning. For example, if your web page
159+ executes the following JavaScript:</p>
160+ <pre class="no-pretty-print">
161+ console.log("Hello World");
162+ </pre>
163+ <p>Then the logcat output from the Android Browser will look like this:</p>
164+ <pre class="no-pretty-print">
165+ W/browser ( 202): Console: Hello World http://www.example.com/hello.html :82
166+ </pre>
167+
168+ <p>All Console messages from the Android Browser are tagged with the name "browser" on Android
169+ platforms running API Level 7 or higher. On platforms running API Level 6 or lower, Browser
170+ messages are tagged with the name "WebCore". The Android Browser also formats console messages
171+ with the log message
172+ preceded by "Console:" and then followed by the address and line number where the
173+ message occurred. (The format for the address and line number will appear different from the example
174+ above on platforms running API Level 6 or lower.)</p>
175+
176+ <p>The Android Browser (and {@link android.webkit.WebChromeClient}) does not implement all of the
177+ Console APIs provided by Firefox or other WebKit-based browsers. Primarily, you need to depend
178+ on the basic text logging functions:</p>
179+ <ul>
180+ <li>{@code console.log(String)}</li>
181+ <li>{@code console.info(String)}</li>
182+ <li>{@code console.warn(String)}</li>
183+ <li>{@code console.error(String)}</li>
184+ </ul>
185+ <p>Although the Android Browser may not fully implement other Console functions, they will not raise
186+ run-time errors, but may not behave the same as they do on other desktop browsers.</p>
187+
188+ <p>If you've implemented a custom {@link android.webkit.WebView} in your application, then in order
189+ to receive messages that are sent through the Console APIs, you must provide a {@link
190+ android.webkit.WebChromeClient} that implements the {@link
191+ android.webkit.WebChromeClient#onConsoleMessage(String,int,String) onConsoleMessage()} callback
192+ method. For example, assuming that the {@code myWebView} field references the {@link
193+ android.webkit.WebView} in your application, you can log debug messages like this:</p>
194+ <pre>
195+ myWebView.setWebChromeClient(new WebChromeClient() {
196+ public void onConsoleMessage(String message, int lineNumber, String sourceID) {
197+ Log.d("MyApplication", message + " -- From line " + lineNumber + " of " + sourceID);
198+ }
199+ });
200+ </pre>
201+ <p>The {@link android.webkit.WebChromeClient#onConsoleMessage(String,int,String)
202+ onConsoleMessage()} method will be called each time one of the Console methods is called from
203+ within your {@link android.webkit.WebView}.</p>
204+ <p>When the "Hello World" log is executed through your {@link android.webkit.WebView}, it will
205+ now look like this:</p>
206+ <pre class="no-pretty-print">
207+ D/MyApplication ( 430): Hello World -- From line 82 of http://www.example.com/hello.html
208+ </pre>
209+
210+ <p class="note"><strong>Note:</strong> The {@link
211+ android.webkit.WebChromeClient#onConsoleMessage(String,int,String) onConsoleMessage()} callback
212+ method was added with API Level 7. If you are using a custom {@link
213+ android.webkit.WebView} on a platform running API Level 6 or lower, then your Console messages will
214+ automatically be sent to logcat with the "WebCore" logging tag.</p>
215+
216+
155217
156218
157219<h2 id="toptips">Top Debugging Tips</h2>
0 commit comments