aboutsummaryrefslogtreecommitdiff
path: root/util/trace_logging/macro_support.h
blob: 7516dab8f9102d7671788d07840913cf20fa6c69 (plain)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UTIL_TRACE_LOGGING_MACRO_SUPPORT_H_
#define UTIL_TRACE_LOGGING_MACRO_SUPPORT_H_

#ifndef INCLUDING_FROM_UTIL_TRACE_LOGGING_H_
#error "Do not include this header directly. Use util/trace_logging.h."
#endif

#ifndef ENABLE_TRACE_LOGGING
#error "BUG: This file should not have been reached."
#endif

#include "build/config/features.h"
#include "platform/api/trace_logging_platform.h"
#include "platform/base/trace_logging_activation.h"
#include "platform/base/trace_logging_types.h"
#include "util/trace_logging/scoped_trace_operations.h"

// Helper macros. These are used to simplify the macros below.
// NOTE: These cannot be #undef'd or they will stop working outside this file.
// NOTE: Two of these below macros are intentionally the same. This is to work
// around optimizations in the C++ Precompiler.
#define TRACE_INTERNAL_CONCAT(a, b) a##b
#define TRACE_INTERNAL_CONCAT_CONST(a, b) TRACE_INTERNAL_CONCAT(a, b)
#define TRACE_INTERNAL_UNIQUE_VAR_NAME(a) \
  TRACE_INTERNAL_CONCAT_CONST(a, __LINE__)

// Because we need to suppress unused variables, and this code is used
// repeatedly in below macros, define helper macros to do this on a per-compiler
// basis until we begin using C++ 17 which supports [[maybe_unused]] officially.
#if defined(__clang__)
#define TRACE_INTERNAL_IGNORE_UNUSED_VAR [[maybe_unused]]
#elif defined(__GNUC__)
#define TRACE_INTERNAL_IGNORE_UNUSED_VAR __attribute__((unused))
#else
#define TRACE_INTERNAL_IGNORE_UNUSED_VAR [[maybe_unused]]
#endif  // defined(__clang__)

namespace openscreen {
namespace internal {

inline bool IsTraceLoggingEnabled(TraceCategory::Value category) {
  auto* const destination = GetTracingDestination();
  return destination && destination->IsTraceLoggingEnabled(category);
}

}  // namespace internal
}  // namespace openscreen

#define TRACE_IS_ENABLED(category) \
  openscreen::internal::IsTraceLoggingEnabled(category)

// Internal logging macros.
#define TRACE_SET_HIERARCHY_INTERNAL(line, ids)                            \
  alignas(32) uint8_t TRACE_INTERNAL_CONCAT_CONST(                         \
      tracing_storage, line)[sizeof(openscreen::internal::TraceIdSetter)]; \
  TRACE_INTERNAL_IGNORE_UNUSED_VAR                                         \
  const auto TRACE_INTERNAL_UNIQUE_VAR_NAME(trace_ref_) =                  \
      TRACE_IS_ENABLED(openscreen::TraceCategory::Value::Any)              \
          ? openscreen::internal::TraceInstanceHelper<                     \
                openscreen::internal::TraceIdSetter>::                     \
                Create(TRACE_INTERNAL_CONCAT_CONST(tracing_storage, line), \
                       ids)                                                \
          : openscreen::internal::TraceInstanceHelper<                     \
                openscreen::internal::TraceIdSetter>::Empty()

#define TRACE_SCOPED_INTERNAL(line, category, name, ...)                   \
  alignas(32) uint8_t TRACE_INTERNAL_CONCAT_CONST(                         \
      tracing_storage,                                                     \
      line)[sizeof(openscreen::internal::SynchronousTraceLogger)];         \
  TRACE_INTERNAL_IGNORE_UNUSED_VAR                                         \
  const auto TRACE_INTERNAL_UNIQUE_VAR_NAME(trace_ref_) =                  \
      TRACE_IS_ENABLED(category)                                           \
          ? openscreen::internal::TraceInstanceHelper<                     \
                openscreen::internal::SynchronousTraceLogger>::            \
                Create(TRACE_INTERNAL_CONCAT_CONST(tracing_storage, line), \
                       category, name, __FILE__, __LINE__, ##__VA_ARGS__)  \
          : openscreen::internal::TraceInstanceHelper<                     \
                openscreen::internal::SynchronousTraceLogger>::Empty()

#define TRACE_ASYNC_START_INTERNAL(line, category, name, ...)             \
  alignas(32) uint8_t TRACE_INTERNAL_CONCAT_CONST(                        \
      temp_storage,                                                       \
      line)[sizeof(openscreen::internal::AsynchronousTraceLogger)];       \
  TRACE_INTERNAL_IGNORE_UNUSED_VAR                                        \
  const auto TRACE_INTERNAL_UNIQUE_VAR_NAME(trace_ref_) =                 \
      TRACE_IS_ENABLED(category)                                          \
          ? openscreen::internal::TraceInstanceHelper<                    \
                openscreen::internal::AsynchronousTraceLogger>::          \
                Create(TRACE_INTERNAL_CONCAT_CONST(temp_storage, line),   \
                       category, name, __FILE__, __LINE__, ##__VA_ARGS__) \
          : openscreen::internal::TraceInstanceHelper<                    \
                openscreen::internal::AsynchronousTraceLogger>::Empty()

#endif  // UTIL_TRACE_LOGGING_MACRO_SUPPORT_H_