xref: /aosp_15_r20/external/openscreen/util/trace_logging.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_H_
6 #define UTIL_TRACE_LOGGING_H_
7 
8 #include "platform/base/trace_logging_types.h"
9 
10 // All compile-time macros for tracing.
11 // NOTE: The ternary operator is used here to ensure that the TraceLogger object
12 // is only constructed if tracing is enabled, but at the same time is created in
13 // the caller's scope. The C++ standards guide guarantees that the constructor
14 // should only be called when IsTraceLoggingEnabled(...) evaluates to true.
15 // static_cast calls are used because if the type of the result of the ternary
16 // operator does not match the expected type, temporary storage is used for the
17 // created object, which results in an extra call to the constructor and
18 // destructor of the tracing objects.
19 //
20 // Further details about how these macros are used can be found in
21 // docs/trace_logging.md.
22 // TODO(rwkeane): Add support for user-provided properties.
23 
24 #if defined(ENABLE_TRACE_LOGGING)
25 
26 #define INCLUDING_FROM_UTIL_TRACE_LOGGING_H_
27 #include "util/trace_logging/macro_support.h"
28 #undef INCLUDING_FROM_UTIL_TRACE_LOGGING_H_
29 
30 #define TRACE_SET_RESULT(result)                                      \
31   do {                                                                \
32     if (TRACE_IS_ENABLED(openscreen::TraceCategory::Value::kAny)) {   \
33       openscreen::internal::ScopedTraceOperation::set_result(result); \
34     }                                                                 \
35   } while (false)
36 #define TRACE_SET_HIERARCHY(ids) TRACE_SET_HIERARCHY_INTERNAL(__LINE__, ids)
37 #define TRACE_HIERARCHY                                          \
38   (TRACE_IS_ENABLED(openscreen::TraceCategory::Value::kAny)      \
39        ? openscreen::internal::ScopedTraceOperation::hierarchy() \
40        : openscreen::TraceIdHierarchy::Empty())
41 #define TRACE_CURRENT_ID                                          \
42   (TRACE_IS_ENABLED(openscreen::TraceCategory::Value::kAny)       \
43        ? openscreen::internal::ScopedTraceOperation::current_id() \
44        : kEmptyTraceId)
45 #define TRACE_ROOT_ID                                          \
46   (TRACE_IS_ENABLED(openscreen::TraceCategory::Value::kAny)    \
47        ? openscreen::internal::ScopedTraceOperation::root_id() \
48        : kEmptyTraceId)
49 
50 // Synchronous Trace Macros.
51 #define TRACE_SCOPED(category, name, ...) \
52   TRACE_SCOPED_INTERNAL(__LINE__, category, name, ##__VA_ARGS__)
53 #define TRACE_DEFAULT_SCOPED(category, ...) \
54   TRACE_SCOPED(category, __PRETTY_FUNCTION__, ##__VA_ARGS__)
55 
56 // Asynchronous Trace Macros.
57 #define TRACE_ASYNC_START(category, name, ...) \
58   TRACE_ASYNC_START_INTERNAL(__LINE__, category, name, ##__VA_ARGS__)
59 
60 #define TRACE_ASYNC_END(category, id, result)                  \
61   TRACE_IS_ENABLED(category)                                   \
62   ? openscreen::internal::ScopedTraceOperation::TraceAsyncEnd( \
63         __LINE__, __FILE__, id, result)                        \
64   : false
65 
66 #else  // ENABLE_TRACE_LOGGING not defined
67 
68 namespace openscreen {
69 namespace internal {
70 // Consumes |args| (to avoid "warn unused variable" errors at compile time), and
71 // provides a "void" result type in the macros below.
72 template <typename... Args>
DoNothingForTracing(Args...args)73 inline void DoNothingForTracing(Args... args) {}
74 }  // namespace internal
75 }  // namespace openscreen
76 
77 #define TRACE_SET_RESULT(result) \
78   openscreen::internal::DoNothingForTracing(result)
79 #define TRACE_SET_HIERARCHY(ids) openscreen::internal::DoNothingForTracing(ids)
80 #define TRACE_HIERARCHY openscreen::TraceIdHierarchy::Empty()
81 #define TRACE_CURRENT_ID openscreen::kEmptyTraceId
82 #define TRACE_ROOT_ID openscreen::kEmptyTraceId
83 #define TRACE_SCOPED(category, name, ...) \
84   openscreen::internal::DoNothingForTracing(category, name, ##__VA_ARGS__)
85 #define TRACE_DEFAULT_SCOPED(category, ...) \
86   TRACE_SCOPED(category, __PRETTY_FUNCTION__, ##__VA_ARGS__)
87 #define TRACE_ASYNC_START(category, name, ...) \
88   openscreen::internal::DoNothingForTracing(category, name, ##__VA_ARGS__)
89 #define TRACE_ASYNC_END(category, id, result) \
90   openscreen::internal::DoNothingForTracing(category, id, result)
91 
92 #endif  // defined(ENABLE_TRACE_LOGGING)
93 
94 #endif  // UTIL_TRACE_LOGGING_H_
95