Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(497)

Side by Side Diff: content/public/android/java/org/chromium/content/browser/TraceEvent.java

Issue 10377059: Android content shell bringup. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package org.chromium.content.browser;
6
7 import android.os.Looper;
8 import android.util.Printer;
9
10 // Java mirror of Chrome trace event API. See
11 // base/debug/trace_event.h. Unlike the native version, Java does not
12 // have stack objects, so a TRACE_EVENT() which does both
13 // TRACE_EVENT_BEGIN() and TRACE_EVENT_END() in ctor/dtor is not
14 // possible.
15 // It is OK to use tracing before the native library has loaded, but such traces will
16 // be ignored. (Perhaps we could devise to buffer them up in future?).
17 public class TraceEvent {
18
19 private static boolean sEnabled = false;
20
21 private static class LooperTracePrinter implements Printer {
22 private static final String NAME = "Looper.dispatchMessage";
23 @Override
24 public void println(String line) {
25 if (line.startsWith(">>>>>")) {
26 TraceEvent.begin(NAME, line);
27 } else {
28 assert line.startsWith("<<<<<");
29 TraceEvent.end(NAME);
30 }
31 }
32 }
33
34 /**
35 * Calling this will cause enabled() to be updated to match that set on the native side.
36 * The native library must be loaded before calling this method.
37 */
38 public static void setEnabledToMatchNative() {
39 setEnabled(nativeTraceEnabled());
40 }
41
42 /**
43 * Enables or disables tracing.
44 * The native library must be loaded before the first call with enabled == t rue.
45 */
46 public static synchronized void setEnabled(boolean enabled) {
47 if (enabled) {
48 LibraryLoader.checkIsReady();
49 }
50 if (sEnabled == enabled) {
51 return;
52 }
53 sEnabled = enabled;
54 Looper.getMainLooper().setMessageLogging(enabled ? new LooperTracePrinte r() : null);
55 }
56
57 /**
58 * @return True if tracing is enabled, false otherwise.
59 * It is safe to call trace methods without checking if TraceEvent
60 * is enabled.
61 */
62 public static boolean enabled() {
63 return sEnabled;
64 }
65
66 public static void instant(String name) {
67 if (sEnabled) {
68 nativeInstant(name, null);
69 }
70 }
71
72 public static void instant(String name, String arg) {
73 if (sEnabled) {
74 nativeInstant(name, arg);
75 }
76 }
77
78 /**
79 * Convenience wrapper around the versions of begin() that take string param eters.
80 * The name of the event will be derived from the class and function name th at call this.
81 * IMPORTANT: if using this version, ensure end() (no parameters) is always called from the
82 * same calling context.
83 */
84 public static void begin() {
85 if (sEnabled) {
86 nativeBegin(getCallerName(), null);
87 }
88 }
89
90 public static void begin(String name) {
91 if (sEnabled) {
92 nativeBegin(name, null);
93 }
94 }
95
96 public static void begin(String name, String arg) {
97 if (sEnabled) {
98 nativeBegin(name, arg);
99 }
100 }
101
102 /**
103 * Convenience wrapper around the versions of end() that take string paramet ers. See begin()
104 * for more information.
105 */
106 public static void end() {
107 if (sEnabled) {
108 nativeEnd(getCallerName(), null);
109 }
110 }
111
112 public static void end(String name) {
113 if (sEnabled) {
114 nativeEnd(name, null);
115 }
116 }
117
118 public static void end(String name, String arg) {
119 if (sEnabled) {
120 nativeEnd(name, arg);
121 }
122 }
123
124 private static String getCallerName() {
125 // This was measured to take about 1ms on Trygon device.
126 StackTraceElement[] stack = java.lang.Thread.currentThread().getStackTra ce();
127
128 // Commented out to avoid excess call overhead, but these lines can be u seful to debug
129 // exactly where the TraceEvent's client is on the callstack.
130 // int index = 0;
131 // while (!stack[index].getClassName().equals(TraceEvent.class.getName( ))) ++index;
132 // while (stack[index].getClassName().equals(TraceEvent.class.getName() )) ++index;
133 // System.logW("TraceEvent caller is at stack index " + index);
134
135 // '4' Was derived using the above commented out code snippet.
136 return stack[4].getClassName() + "." + stack[4].getMethodName();
137 }
138
139 private static native boolean nativeTraceEnabled();
140 private static native void nativeInstant(String name, String arg);
141 private static native void nativeBegin(String name, String arg);
142 private static native void nativeEnd(String name, String arg);
143 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698