xref: /aosp_15_r20/external/openscreen/util/trace_logging/macro_support.h (revision 3f982cf4871df8771c9d4abe6e9a6f8d829b2736)
1 // Copyright 2019 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 #ifndef UTIL_TRACE_LOGGING_MACRO_SUPPORT_H_
6 #define UTIL_TRACE_LOGGING_MACRO_SUPPORT_H_
7 
8 #ifndef INCLUDING_FROM_UTIL_TRACE_LOGGING_H_
9 #error "Do not include this header directly. Use util/trace_logging.h."
10 #endif
11 
12 #ifndef ENABLE_TRACE_LOGGING
13 #error "BUG: This file should not have been reached."
14 #endif
15 
16 #include "platform/api/trace_logging_platform.h"
17 #include "platform/base/trace_logging_activation.h"
18 #include "platform/base/trace_logging_types.h"
19 #include "util/trace_logging/scoped_trace_operations.h"
20 
21 // Helper macros. These are used to simplify the macros below.
22 // NOTE: These cannot be #undef'd or they will stop working outside this file.
23 // NOTE: Two of these below macros are intentionally the same. This is to work
24 // around optimizations in the C++ Precompiler.
25 #define TRACE_INTERNAL_CONCAT(a, b) a##b
26 #define TRACE_INTERNAL_CONCAT_CONST(a, b) TRACE_INTERNAL_CONCAT(a, b)
27 #define TRACE_INTERNAL_UNIQUE_VAR_NAME(a) \
28   TRACE_INTERNAL_CONCAT_CONST(a, __LINE__)
29 
30 // Because we need to suppress unused variables, and this code is used
31 // repeatedly in below macros, define helper macros to do this on a per-compiler
32 // basis until we begin using C++ 17 which supports [[maybe_unused]] officially.
33 #if defined(__clang__)
34 #define TRACE_INTERNAL_IGNORE_UNUSED_VAR [[maybe_unused]]
35 #elif defined(__GNUC__)
36 #define TRACE_INTERNAL_IGNORE_UNUSED_VAR __attribute__((unused))
37 #else
38 #define TRACE_INTERNAL_IGNORE_UNUSED_VAR [[maybe_unused]]
39 #endif  // defined(__clang__)
40 
41 namespace openscreen {
42 namespace internal {
43 
IsTraceLoggingEnabled(TraceCategory::Value category)44 inline bool IsTraceLoggingEnabled(TraceCategory::Value category) {
45   const CurrentTracingDestination destination;
46   return destination && destination->IsTraceLoggingEnabled(category);
47 }
48 
49 }  // namespace internal
50 }  // namespace openscreen
51 
52 #define TRACE_IS_ENABLED(category) \
53   openscreen::internal::IsTraceLoggingEnabled(category)
54 
55 // Internal logging macros.
56 #define TRACE_SET_HIERARCHY_INTERNAL(line, ids)                            \
57   alignas(32) uint8_t TRACE_INTERNAL_CONCAT_CONST(                         \
58       tracing_storage, line)[sizeof(openscreen::internal::TraceIdSetter)]; \
59   TRACE_INTERNAL_IGNORE_UNUSED_VAR                                         \
60   const auto TRACE_INTERNAL_UNIQUE_VAR_NAME(trace_ref_) =                  \
61       TRACE_IS_ENABLED(openscreen::TraceCategory::Value::kAny)             \
62           ? openscreen::internal::TraceInstanceHelper<                     \
63                 openscreen::internal::TraceIdSetter>::                     \
64                 Create(TRACE_INTERNAL_CONCAT_CONST(tracing_storage, line), \
65                        ids)                                                \
66           : openscreen::internal::TraceInstanceHelper<                     \
67                 openscreen::internal::TraceIdSetter>::Empty()
68 
69 #define TRACE_SCOPED_INTERNAL(line, category, name, ...)                   \
70   alignas(32) uint8_t TRACE_INTERNAL_CONCAT_CONST(                         \
71       tracing_storage,                                                     \
72       line)[sizeof(openscreen::internal::SynchronousTraceLogger)];         \
73   TRACE_INTERNAL_IGNORE_UNUSED_VAR                                         \
74   const auto TRACE_INTERNAL_UNIQUE_VAR_NAME(trace_ref_) =                  \
75       TRACE_IS_ENABLED(category)                                           \
76           ? openscreen::internal::TraceInstanceHelper<                     \
77                 openscreen::internal::SynchronousTraceLogger>::            \
78                 Create(TRACE_INTERNAL_CONCAT_CONST(tracing_storage, line), \
79                        category, name, __FILE__, __LINE__, ##__VA_ARGS__)  \
80           : openscreen::internal::TraceInstanceHelper<                     \
81                 openscreen::internal::SynchronousTraceLogger>::Empty()
82 
83 #define TRACE_ASYNC_START_INTERNAL(line, category, name, ...)             \
84   alignas(32) uint8_t TRACE_INTERNAL_CONCAT_CONST(                        \
85       temp_storage,                                                       \
86       line)[sizeof(openscreen::internal::AsynchronousTraceLogger)];       \
87   TRACE_INTERNAL_IGNORE_UNUSED_VAR                                        \
88   const auto TRACE_INTERNAL_UNIQUE_VAR_NAME(trace_ref_) =                 \
89       TRACE_IS_ENABLED(category)                                          \
90           ? openscreen::internal::TraceInstanceHelper<                    \
91                 openscreen::internal::AsynchronousTraceLogger>::          \
92                 Create(TRACE_INTERNAL_CONCAT_CONST(temp_storage, line),   \
93                        category, name, __FILE__, __LINE__, ##__VA_ARGS__) \
94           : openscreen::internal::TraceInstanceHelper<                    \
95                 openscreen::internal::AsynchronousTraceLogger>::Empty()
96 
97 #endif  // UTIL_TRACE_LOGGING_MACRO_SUPPORT_H_
98