chromium/third_party/openscreen/src/util/trace_logging.h

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

#ifndef UTIL_TRACE_LOGGING_H_
#define UTIL_TRACE_LOGGING_H_

#include <string>
#include <utility>
#include <vector>

#include "platform/base/trace_logging_types.h"

// All compile-time macros for tracing.
// NOTE: The ternary operator is used here to ensure that the TraceLogger object
// is only constructed if tracing is enabled, but at the same time is created in
// the caller's scope. The C++ standards guide guarantees that the constructor
// should only be called when IsTraceLoggingEnabled(...) evaluates to true.
// static_cast calls are used because if the type of the result of the ternary
// operator does not match the expected type, temporary storage is used for the
// created object, which results in an extra call to the constructor and
// destructor of the tracing objects.
//
// Further details about how these macros are used can be found in
// docs/trace_logging.md.

#if defined(ENABLE_TRACE_LOGGING)

#define INCLUDING_FROM_UTIL_TRACE_LOGGING_H_
#include "util/trace_logging/macro_support.h"
#undef INCLUDING_FROM_UTIL_TRACE_LOGGING_H_

#define TRACE_SET_RESULT(result)
#define TRACE_SET_HIERARCHY(ids)
#define TRACE_HIERARCHY
#define TRACE_CURRENT_ID
#define TRACE_ROOT_ID

// NOTE: this method requires arguments to be passed as already serialized
// strings.
inline std::vector<openscreen::TraceEvent::Argument> ToArgumentArray(
    const char* argname = {}

// Synchronous Trace Macros.
//
// Scoped traces with no arguments.
#define TRACE_SCOPED(category, name, ...)
#define TRACE_DEFAULT_SCOPED(category, ...)

// Scoped traces with one argument.
#define TRACE_SCOPED1(category, name, argname, argval, ...)
#define TRACE_DEFAULT_SCOPED1(category, argname, argval, ...)

// Scoped traces with two arguments.
#define TRACE_SCOPED2(category, name, argname, argval, argname_two, \
                      argval_two, ...)
#define TRACE_DEFAULT_SCOPED2(category, argname, argval, argname_two,        \
                              argval_two, ...)

// Asynchronous Trace Macros.
#define TRACE_ASYNC_START(category, name, ...)

#define TRACE_ASYNC_START1(category, name, argname, argval, ...)

#define TRACE_ASYNC_START2(category, name, argname, argval, argname_two, \
                           argval_two, ...)

#define TRACE_ASYNC_END(category, id, result)

#else  // ENABLE_TRACE_LOGGING not defined

namespace openscreen::internal {
// Consumes |args| (to avoid "warn unused variable" errors at compile time), and
// provides a "void" result type in the macros below.
template <typename... Args>
inline void DoNothingForTracing(Args... args) {}
}  // namespace openscreen::internal

#define TRACE_SET_RESULT
#define TRACE_SET_HIERARCHY
#define TRACE_HIERARCHY
#define TRACE_CURRENT_ID
#define TRACE_ROOT_ID
#define TRACE_SCOPED
#define TRACE_DEFAULT_SCOPED

#define TRACE_SCOPED1
#define TRACE_DEFAULT_SCOPED1

#define TRACE_SCOPED2
#define TRACE_DEFAULT_SCOPED2

#define TRACE_ASYNC_START
#define TRACE_ASYNC_END

#endif  // defined(ENABLE_TRACE_LOGGING)

#endif  // UTIL_TRACE_LOGGING_H_