1/*
2 * Copyright 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.android.ex.camera2.utils;
17
18import android.util.Log;
19
20/**
21 * Writes trace events to the system trace buffer.  These trace events can be
22 * collected and visualized using the Systrace tool.
23 *
24 * <p>
25 * This tracing mechanism is independent of the method tracing mechanism
26 * offered by {@link Debug#startMethodTracing}.  In particular, it enables
27 * tracing of events that occur across multiple processes.
28 * </p>
29 *
30 * <p>
31 * All traces are written using the <pre>APP</pre> tag.
32 * </p>
33 */
34public final class SysTrace {
35
36    private static final String TAG = "SysTrace";
37    private static final boolean VERBOSE = Log.isLoggable(TAG, Log.VERBOSE);
38
39    private static int sNestingLevel = 0;
40
41    /**
42     * Writes trace message to indicate the value of a given counter.
43     *
44     * @param counterName The counter name to appear in the trace.
45     * @param counterValue The counter value.
46     *
47     */
48    public static void traceCounter(String counterName, int counterValue) {
49        if (VERBOSE) {
50            Log.v(TAG, "traceCounter " + counterName + " " + counterValue);
51        }
52    }
53
54    /**
55     * Writes a trace message to indicate that a given section of code has begun. This call must
56     * be followed by a corresponding call to {@link #endSection()} on the same thread.
57     *
58     * <p class="note"> At this time the vertical bar character '|', newline character '\n', and
59     * null character '\0' are used internally by the tracing mechanism.  If sectionName contains
60     * these characters they will be replaced with a space character in the trace.
61     *
62     * @param sectionName The name of the code section to appear in the trace.  This may be at
63     * most 127 Unicode code units long.
64     */
65    public static void beginSection(String sectionName) {
66        if (VERBOSE) {
67            Log.v(TAG, String.format("beginSection[%d] %s", sNestingLevel, sectionName));
68            sNestingLevel++;
69        }
70    }
71
72    /**
73     * Writes a trace message to indicate that a given section of code has
74     * ended.
75     * <p>
76     * This call must be preceded by a corresponding call to
77     * {@link #beginSection(String)}. Calling this method will mark the end of
78     * the most recently begun section of code, so care must be taken to ensure
79     * that beginSection / endSection pairs are properly nested and called from
80     * the same thread.
81     * </p>
82     */
83    public static void endSection() {
84        if (VERBOSE) {
85            sNestingLevel--;
86            Log.v(TAG, String.format("endSection[%d]", sNestingLevel));
87        }
88    }
89
90    /**
91     * Writes a trace message to indicate that a given section of code has
92     * begun.
93     *
94     * <p>Must be followed by a call to {@link #endSectionAsync} using the same
95     * tag. Unlike {@link #beginSection} and {@link #endSection},
96     * asynchronous events do not need to be nested. The name and cookie used to
97     * begin an event must be used to end it.</p>
98     *
99     * @param methodName The method name to appear in the trace.
100     * @param cookie Unique identifier for distinguishing simultaneous events
101     */
102    public static void beginSectionAsync(String methodName, int cookie) {
103        if (VERBOSE) {
104            Log.v(TAG, "beginSectionAsync " + methodName + " " + cookie);
105        }
106    }
107
108    /**
109     * Writes a trace message to indicate that the current method has ended.
110     * Must be called exactly once for each call to {@link #beginSectionAsync}
111     * using the same tag, name and cookie.
112     *
113     * @param methodName The method name to appear in the trace.
114     * @param cookie Unique identifier for distinguishing simultaneous events
115     */
116    public static void endSectionAsync(String methodName, int cookie) {
117        if (VERBOSE) {
118            Log.v(TAG, "endSectionAsync " + methodName + " " + cookie);
119        }
120    }
121}
122