1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62

media / base / scoped_async_trace.h [blame]

// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MEDIA_BASE_SCOPED_ASYNC_TRACE_H_
#define MEDIA_BASE_SCOPED_ASYNC_TRACE_H_

#include <memory>

#include "base/memory/raw_ptr.h"
#include "media/base/media_export.h"

namespace media {

// When adding a new TraceCategory there are two additional steps in the .cc:
//
//   1. Add a new Category::Name() implementation at the top.
//   2. Add a new  TypedScopedAsyncTrace<$NEW_CATEGORY> entry at the bottom.
//
// For frequently used scoped traces you may also add a "using" entry along with
// the "using ScopedAsyncTrace..." entry below.
enum class TraceCategory : uint32_t {
  kMedia,
  kMediaStream,
  kVideoAndImageCapture,
};

// Utility class that starts and stops an async trace event.  The intention is
// that it it will be created somewhere to start the trace event, passed around
// such as via unique_ptr argument in a callback, and eventually freed to end
// the trace event.  This guarantees that it'll be closed, even if the callback
// is destroyed without being run.
template <TraceCategory category>
class MEDIA_EXPORT TypedScopedAsyncTrace {
 public:
  // Create a TypedScopedAsyncTrace if tracing for `cateogory` is enabled,
  // returns nullptr otherwise. `name` will be the trace name.
  //
  // IMPORTANT: All string parameters must outlive |this|, since tracing needs
  // them. Use literal strings only; see trace_event_common.h.
  static std::unique_ptr<TypedScopedAsyncTrace<category>> CreateIfEnabled(
      const char* name);

  ~TypedScopedAsyncTrace();

  // Adds a nested step under the current trace.
  void AddStep(const char* step_name);

 private:
  explicit TypedScopedAsyncTrace(const char* name);
  TypedScopedAsyncTrace(const char* name, const void* id);

  const char* name_;
  raw_ptr<const void> id_;
  std::unique_ptr<TypedScopedAsyncTrace<category>> step_;
};

using ScopedAsyncTrace = TypedScopedAsyncTrace<TraceCategory::kMedia>;

}  // namespace media

#endif  // MEDIA_BASE_SCOPED_ASYNC_TRACE_H_