Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
TraceSection.java
Go to the documentation of this file.
1// Copyright 2013 The Flutter 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
5package io.flutter.util;
6
7import androidx.annotation.NonNull;
8import androidx.tracing.Trace;
9
10public final class TraceSection implements AutoCloseable {
11 /**
12 * Factory used to support the try-with-resource construct.
13 *
14 * <p>To get scoped trace events, use the try-with-resource construct, for instance:
15 *
16 * <pre>{@code
17 * try (TraceSection e = TraceSection.scoped("MyTraceEvent")) {
18 * // code.
19 * }
20 * }</pre>
21 */
22 public static TraceSection scoped(String name) {
23 return new TraceSection(name);
24 }
25
26 /** Constructor used to support the try-with-resource construct. */
27 private TraceSection(String name) {
28 begin(name);
29 }
30
31 @Override
32 public void close() {
33 end();
34 }
35
36 private static String cropSectionName(@NonNull String sectionName) {
37 return sectionName.length() < 124 ? sectionName : sectionName.substring(0, 124) + "...";
38 }
39
40 /**
41 * Wraps Trace.beginSection to ensure that the line length stays below 127 code units.
42 *
43 * @param sectionName The string to display as the section name in the trace.
44 */
45 public static void begin(@NonNull String sectionName) {
46 Trace.beginSection(cropSectionName(sectionName));
47 }
48
49 /** Wraps Trace.endSection. */
50 public static void end() throws RuntimeException {
51 Trace.endSection();
52 }
53
54 /**
55 * Wraps Trace.beginAsyncSection to ensure that the line length stays below 127 code units.
56 *
57 * @param sectionName The string to display as the section name in the trace.
58 * @param cookie Unique integer defining the section.
59 */
60 public static void beginAsyncSection(String sectionName, int cookie) {
61 Trace.beginAsyncSection(cropSectionName(sectionName), cookie);
62 }
63
64 /** Wraps Trace.endAsyncSection to ensure that the line length stays below 127 code units. */
65 public static void endAsyncSection(String sectionName, int cookie) {
66 Trace.endAsyncSection(cropSectionName(sectionName), cookie);
67 }
68}
static TraceSection scoped(String name)
static void begin(@NonNull String sectionName)
static void beginAsyncSection(String sectionName, int cookie)
static void endAsyncSection(String sectionName, int cookie)
const char * name
Definition fuchsia.cc:50