aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuri Wiitala <miu@chromium.org>2019-11-20 11:20:54 -0800
committerCommit Bot <commit-bot@chromium.org>2019-11-20 19:29:19 +0000
commit8e6db3b055baf1fd37ffb4805a363a9e78cb886e (patch)
treecb2c5141fac64566b1267c240599506d972b562e
parentcbf8daad50ce2889c58f488b9b4d1702118caebc (diff)
downloadopenscreen-8e6db3b055baf1fd37ffb4805a363a9e78cb886e.tar.gz
Move platform/api/trace_logging.h → util/trace_logging.h, etc...
This change moves the parts of trace logging that are library code (and not platform specific) into util or platform/base, as appropriate. Library code using the tracing macros is pointed to util/trace_logging.h. Overall, this "minifies" the platform/api an embedder must implement to just the TraceLoggerPlatform interface. Removed "enable flag" from build config, and started a new features.h config. Will revisit this mechanism in a future change (and consolidate other feature flags). Fixed issues related to enabling the feature (bots were not verifying anything because it had been left off). Fixed a few C++ style issues found in code as it was moved around. This change also fixes unit test crashing due to dangling pointers to detsroyed MockLoggingPlatforms being left around (the global pointer was not being nulled-out after the tracing unit tests were executed). To simplify, the ctor/dtor now automatically set/clear the global pointer. The crashing seems not to have been discovered because the BUILD.gn arg "enable_trace_logging" defaults to false and so our bots were not testing things. Will revisit this in a later change... Bug: openscreen:77 Change-Id: I638a3998af0a3aacf4d8b8e0f059b331582313cb Reviewed-on: https://chromium-review.googlesource.com/c/openscreen/+/1920299 Commit-Queue: Yuri Wiitala <miu@chromium.org> Reviewed-by: mark a. foltz <mfoltz@chromium.org>
-rw-r--r--DEPS1
-rw-r--r--build/config/BUILD.gn6
-rw-r--r--build/config/features.h13
-rw-r--r--discovery/mdns/mdns_receiver.cc4
-rw-r--r--docs/trace_logging.md49
-rw-r--r--osp/demo/osp_demo.cc3
-rw-r--r--osp/impl/discovery/mdns/mdns_responder_adapter_impl.cc4
-rw-r--r--osp/impl/mdns_responder_service.cc4
-rw-r--r--osp/impl/presentation/presentation_receiver.cc4
-rw-r--r--osp/impl/quic/quic_connection_factory_impl.cc4
-rw-r--r--osp/impl/quic/quic_connection_impl.cc4
-rw-r--r--platform/BUILD.gn10
-rw-r--r--platform/api/internal/trace_logging_macros_internal.h90
-rw-r--r--platform/api/trace_logging.h77
-rw-r--r--platform/api/trace_logging_platform.cc19
-rw-r--r--platform/api/trace_logging_platform.h29
-rw-r--r--platform/base/trace_logging_activation.cc32
-rw-r--r--platform/base/trace_logging_activation.h25
-rw-r--r--platform/base/trace_logging_types.h (renamed from platform/api/trace_logging_types.h)10
-rw-r--r--platform/impl/logging_posix.cc2
-rw-r--r--platform/impl/task_runner.h9
-rw-r--r--platform/impl/text_trace_logging_platform.cc18
-rw-r--r--platform/impl/text_trace_logging_platform.h7
-rw-r--r--platform/impl/tls_connection_factory_posix.cc6
-rw-r--r--platform/test/trace_logging_helpers.h20
-rw-r--r--util/BUILD.gn7
-rw-r--r--util/trace_logging.h91
-rw-r--r--util/trace_logging/macro_support.h98
-rw-r--r--util/trace_logging/scoped_trace_operations.cc (renamed from platform/api/internal/trace_logging_internal.cc)36
-rw-r--r--util/trace_logging/scoped_trace_operations.h (renamed from platform/api/internal/trace_logging_internal.h)101
-rw-r--r--util/trace_logging/scoped_trace_operations_unittest.cc (renamed from platform/api/internal/trace_logging_internal_unittest.cc)32
-rw-r--r--util/trace_logging_unittest.cc (renamed from platform/api/trace_logging_unittest.cc)141
32 files changed, 557 insertions, 399 deletions
diff --git a/DEPS b/DEPS
index 73599236..9955ada4 100644
--- a/DEPS
+++ b/DEPS
@@ -105,6 +105,7 @@ recursedeps = [
]
include_rules = [
+ '+build/config/features.h',
'+util',
'+platform/api',
'+platform/base',
diff --git a/build/config/BUILD.gn b/build/config/BUILD.gn
index b6d48531..67e37634 100644
--- a/build/config/BUILD.gn
+++ b/build/config/BUILD.gn
@@ -11,9 +11,6 @@ declare_args() {
# Enable thread sanitizer.
is_tsan = false
-
- # Enable trace logging.
- enable_trace_logging = false
}
config("compiler_defaults") {
@@ -122,9 +119,6 @@ config("openscreen_code") {
if (dcheck_always_on) {
defines += [ "DCHECK_ALWAYS_ON" ]
}
- if (enable_trace_logging) {
- defines += [ "ENABLE_TRACE_LOGGING" ]
- }
}
config("default_optimization") {
diff --git a/build/config/features.h b/build/config/features.h
new file mode 100644
index 00000000..d0617bdf
--- /dev/null
+++ b/build/config/features.h
@@ -0,0 +1,13 @@
+// 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 BUILD_CONFIG_FEATURES_H_
+#define BUILD_CONFIG_FEATURES_H_
+
+// TODO(miu): This header should be auto-generated from GN args. This will be
+// revisited in a soon-upcoming change.
+
+#define ENABLE_TRACE_LOGGING
+
+#endif // BUILD_CONFIG_FEATURES_H_
diff --git a/discovery/mdns/mdns_receiver.cc b/discovery/mdns/mdns_receiver.cc
index 424575a4..2d311724 100644
--- a/discovery/mdns/mdns_receiver.cc
+++ b/discovery/mdns/mdns_receiver.cc
@@ -5,7 +5,9 @@
#include "discovery/mdns/mdns_receiver.h"
#include "discovery/mdns/mdns_reader.h"
-#include "platform/api/trace_logging.h"
+#include "util/trace_logging.h"
+
+using openscreen::platform::TraceCategory;
namespace openscreen {
namespace discovery {
diff --git a/docs/trace_logging.md b/docs/trace_logging.md
index 523b5cb7..0139b3f2 100644
--- a/docs/trace_logging.md
+++ b/docs/trace_logging.md
@@ -15,7 +15,7 @@ when calling `gn gen` as part of building this library.
## Imports
To use TraceLogging, import the following header file:
- * *platform/api/trace_logging.h*
+ * *util/trace_logging.h*
This file will import all other Trace Logging dependencies.
## Trace IDs
@@ -207,42 +207,31 @@ associated with the trace.
## File Division
The code for Trace Logging is divided up as follows:
- * *platform/api/trace_logging.h*: the macros external callers are expected to
- use.
- * *platform/api/trace_logging_types.h*: the types/enums used by all trace
- logging classes that external callers are expected to use. This file must
- be separate from trace_logging.h to prevent circular dependency issues.
- * *platform/api/trace_logging_internal.h/.cc*: the internal infrastructure
- backing the externally-facing macros.
- * *platform/api/trace_logging_platform.h*: the platform layer we expect
- implemented to pass the actual call along.
- * *platform/base/trace_logging_platform.cc*: implementation of
- trace_logging_platform.h
+ * *util/trace_logging.h*: the macros Open Screen library code is expected to
+ use to log trace events.
+ * *platform/base/trace_logging_types.h*: the types/enums used in the platform
+ API as well as internal library code.
+ * *util/trace_logging/*: the internal infrastructure backing the macros in
+ *trace_logging.h*, and connecting it to the platform API.
+ * *platform/api/trace_logging_platform.h*: the platform implementation that is
+ used as the trace logging destination while tracing is active.
This information is intended to be only eplanatory for embedders - only the one
file mentioned above in Imports must be imported.
## Embedder-Specific Tracing Implementations
-For an embedder to create a custom TraceLogging implementation, there are 3
-steps:
+For an embedder to create a custom TraceLogging implementation:
1. *Create a TraceLoggingPlatform*
In platform/api/trace_logging_platform.h, the interface TraceLoggingPlatform
- is defined. An embedder must define a class implementing this interface.
-
-2. *Define method `IsTraceLoggingEnabled(...)`*
- In platform/api/trace_logging_platform.h, the following function is defined:
- `bool IsLoggingEnabled(TraceCategory category);`
- A TraceLogging implementation must define this method. Note that the
- implementation of this method should be as performance-optimal as possible, as
- this will be called frequently even when logging is disabled, so any
- performance issues present in this method's implementation have the potential
- to have a noticible effect on this entire library's performance.
-
-3. *Define method `TraceLoggingPlatform::GetDefaultTracingPlatform()`*
- `GetDefaultTracingPlatform()` is used to create a static TraceLoggingPlatform
- singleton instance, then return this instance wherever the TraceLogging
- internals require it.
+ is defined. An embedder must define a class implementing this interface. The
+ methods should be as performance-optimal as possible, since they might be
+ called frequently (especially `IsLoggingEnabled(TraceCategory)`) and are often
+ in the critical execution path of the library's code.
+
+2. *Call `openscreen::platform::StartTracing()` and `StopTracing()`*
+ These activate/deactivate tracing by providing the TraceLoggingPlatform
+ instance and later clearing references to it.
**The default implementation of this layer can be seen in
-platform/base/trace_logging_platform.cc.**
+platform/impl/trace_logging_platform.cc.**
diff --git a/osp/demo/osp_demo.cc b/osp/demo/osp_demo.cc
index 229a03c4..3f97a08e 100644
--- a/osp/demo/osp_demo.cc
+++ b/osp/demo/osp_demo.cc
@@ -28,13 +28,13 @@
#include "osp/public/service_publisher.h"
#include "platform/api/network_interface.h"
#include "platform/api/time.h"
-#include "platform/api/trace_logging.h"
#include "platform/impl/logging.h"
#include "platform/impl/platform_client_posix.h"
#include "platform/impl/task_runner.h"
#include "platform/impl/text_trace_logging_platform.h"
#include "platform/impl/udp_socket_reader_posix.h"
#include "third_party/tinycbor/src/src/cbor.h"
+#include "util/trace_logging.h"
using Clock = openscreen::platform::Clock;
using PlatformClientPosix = openscreen::platform::PlatformClientPosix;
@@ -607,7 +607,6 @@ int main(int argc, char** argv) {
LogLevel level = args.is_verbose ? LogLevel::kVerbose : LogLevel::kInfo;
openscreen::platform::SetLogLevel(level);
openscreen::platform::TextTraceLoggingPlatform text_logging_platform;
- TRACE_SET_DEFAULT_PLATFORM(&text_logging_platform);
PlatformClientPosix::Create(Clock::duration{50}, Clock::duration{50});
diff --git a/osp/impl/discovery/mdns/mdns_responder_adapter_impl.cc b/osp/impl/discovery/mdns/mdns_responder_adapter_impl.cc
index 04485e68..329a1329 100644
--- a/osp/impl/discovery/mdns/mdns_responder_adapter_impl.cc
+++ b/osp/impl/discovery/mdns/mdns_responder_adapter_impl.cc
@@ -11,7 +11,9 @@
#include <memory>
#include "platform/api/logging.h"
-#include "platform/api/trace_logging.h"
+#include "util/trace_logging.h"
+
+using openscreen::platform::TraceCategory;
namespace openscreen {
namespace mdns {
diff --git a/osp/impl/mdns_responder_service.cc b/osp/impl/mdns_responder_service.cc
index aac68c4b..ff75f3ec 100644
--- a/osp/impl/mdns_responder_service.cc
+++ b/osp/impl/mdns_responder_service.cc
@@ -10,8 +10,10 @@
#include "osp/impl/internal_services.h"
#include "platform/api/logging.h"
-#include "platform/api/trace_logging.h"
#include "platform/base/error.h"
+#include "util/trace_logging.h"
+
+using openscreen::platform::TraceCategory;
namespace openscreen {
namespace {
diff --git a/osp/impl/presentation/presentation_receiver.cc b/osp/impl/presentation/presentation_receiver.cc
index 5590561e..c98dcce3 100644
--- a/osp/impl/presentation/presentation_receiver.cc
+++ b/osp/impl/presentation/presentation_receiver.cc
@@ -14,7 +14,9 @@
#include "osp/public/protocol_connection_server.h"
#include "platform/api/logging.h"
#include "platform/api/time.h"
-#include "platform/api/trace_logging.h"
+#include "util/trace_logging.h"
+
+using openscreen::platform::TraceCategory;
namespace openscreen {
namespace presentation {
diff --git a/osp/impl/quic/quic_connection_factory_impl.cc b/osp/impl/quic/quic_connection_factory_impl.cc
index 0b5fbd95..2f25f5d2 100644
--- a/osp/impl/quic/quic_connection_factory_impl.cc
+++ b/osp/impl/quic/quic_connection_factory_impl.cc
@@ -11,12 +11,14 @@
#include "platform/api/logging.h"
#include "platform/api/task_runner.h"
#include "platform/api/time.h"
-#include "platform/api/trace_logging.h"
#include "platform/base/error.h"
#include "third_party/chromium_quic/src/base/location.h"
#include "third_party/chromium_quic/src/base/task_runner.h"
#include "third_party/chromium_quic/src/net/third_party/quic/core/quic_constants.h"
#include "third_party/chromium_quic/src/net/third_party/quic/platform/impl/quic_chromium_clock.h"
+#include "util/trace_logging.h"
+
+using openscreen::platform::TraceCategory;
namespace openscreen {
class QuicTaskRunner final : public ::base::TaskRunner {
diff --git a/osp/impl/quic/quic_connection_impl.cc b/osp/impl/quic/quic_connection_impl.cc
index 3821ecae..1c15daf2 100644
--- a/osp/impl/quic/quic_connection_impl.cc
+++ b/osp/impl/quic/quic_connection_impl.cc
@@ -10,9 +10,11 @@
#include "absl/types/optional.h"
#include "osp/impl/quic/quic_connection_factory_impl.h"
#include "platform/api/logging.h"
-#include "platform/api/trace_logging.h"
#include "platform/base/error.h"
#include "third_party/chromium_quic/src/net/third_party/quic/platform/impl/quic_chromium_clock.h"
+#include "util/trace_logging.h"
+
+using openscreen::platform::TraceCategory;
namespace openscreen {
diff --git a/platform/BUILD.gn b/platform/BUILD.gn
index 1e7f529d..49f6f26f 100644
--- a/platform/BUILD.gn
+++ b/platform/BUILD.gn
@@ -9,9 +9,6 @@ source_set("platform") {
sources = [
"api/internal/logging_macros.h",
- "api/internal/trace_logging_internal.cc",
- "api/internal/trace_logging_internal.h",
- "api/internal/trace_logging_macros_internal.h",
"api/logging.h",
"api/network_interface.cc",
"api/network_interface.h",
@@ -24,10 +21,8 @@ source_set("platform") {
"api/tls_connection.h",
"api/tls_connection_factory.cc",
"api/tls_connection_factory.h",
- "api/trace_logging.h",
"api/trace_logging_platform.cc",
"api/trace_logging_platform.h",
- "api/trace_logging_types.h",
"api/udp_read_callback.h",
"api/udp_socket.cc",
"api/udp_socket.h",
@@ -43,6 +38,9 @@ source_set("platform") {
"base/tls_credentials.cc",
"base/tls_credentials.h",
"base/tls_listen_options.h",
+ "base/trace_logging_activation.cc",
+ "base/trace_logging_activation.h",
+ "base/trace_logging_types.h",
"base/udp_packet.cc",
"base/udp_packet.h",
]
@@ -145,9 +143,7 @@ source_set("unittests") {
testonly = true
sources = [
- "api/internal/trace_logging_internal_unittest.cc",
"api/time_unittest.cc",
- "api/trace_logging_unittest.cc",
"base/error_unittest.cc",
"base/ip_address_unittest.cc",
"base/location_unittest.cc",
diff --git a/platform/api/internal/trace_logging_macros_internal.h b/platform/api/internal/trace_logging_macros_internal.h
deleted file mode 100644
index 8ab4ba90..00000000
--- a/platform/api/internal/trace_logging_macros_internal.h
+++ /dev/null
@@ -1,90 +0,0 @@
-// 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 PLATFORM_API_INTERNAL_TRACE_LOGGING_MACROS_INTERNAL_H_
-#define PLATFORM_API_INTERNAL_TRACE_LOGGING_MACROS_INTERNAL_H_
-
-#include "platform/api/internal/trace_logging_internal.h"
-#include "platform/api/trace_logging_types.h"
-
-// Using statements to simplify the below macros.
-using openscreen::TraceCategory;
-using openscreen::platform::internal::AsynchronousTraceLogger;
-using openscreen::platform::internal::SynchronousTraceLogger;
-using openscreen::platform::internal::TraceIdSetter;
-using openscreen::platform::internal::TraceInstanceHelper;
-
-namespace openscreen {
-
-// 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__)
-
-// Define a macro to check if tracing is enabled or not for testing and
-// compilation reasons.
-#ifndef TRACE_FORCE_ENABLE
-#define TRACE_IS_ENABLED(category) \
- openscreen::platform::IsTraceLoggingEnabled(TraceCategory::Value::Any)
-#ifndef ENABLE_TRACE_LOGGING
-#define TRACE_FORCE_DISABLE true
-#endif // ENABLE_TRACE_LOGGING
-#else // TRACE_FORCE_ENABLE defined
-#define TRACE_IS_ENABLED(category) true
-#endif
-
-// Internal logging macros.
-#define TRACE_SET_HIERARCHY_INTERNAL(line, ids) \
- alignas(32) uint8_t TRACE_INTERNAL_CONCAT_CONST( \
- tracing_storage, \
- line)[sizeof(openscreen::platform::internal::TraceIdSetter)]; \
- TRACE_INTERNAL_IGNORE_UNUSED_VAR \
- const auto TRACE_INTERNAL_UNIQUE_VAR_NAME(trace_ref_) = \
- TRACE_IS_ENABLED(TraceCategory::Value::Any) \
- ? TraceInstanceHelper<TraceIdSetter>::Create( \
- TRACE_INTERNAL_CONCAT_CONST(tracing_storage, line), ids) \
- : TraceInstanceHelper<TraceIdSetter>::Empty()
-
-#define TRACE_SCOPED_INTERNAL(line, category, name, ...) \
- alignas(32) uint8_t TRACE_INTERNAL_CONCAT_CONST( \
- tracing_storage, \
- line)[sizeof(openscreen::platform::internal::SynchronousTraceLogger)]; \
- TRACE_INTERNAL_IGNORE_UNUSED_VAR \
- const auto TRACE_INTERNAL_UNIQUE_VAR_NAME(trace_ref_) = \
- TRACE_IS_ENABLED(category) \
- ? TraceInstanceHelper<SynchronousTraceLogger>::Create( \
- TRACE_INTERNAL_CONCAT_CONST(tracing_storage, line), category, \
- name, __FILE__, __LINE__, ##__VA_ARGS__) \
- : TraceInstanceHelper<SynchronousTraceLogger>::Empty()
-
-#define TRACE_ASYNC_START_INTERNAL(line, category, name, ...) \
- alignas(32) uint8_t TRACE_INTERNAL_CONCAT_CONST( \
- temp_storage, \
- line)[sizeof(openscreen::platform::internal::AsynchronousTraceLogger)]; \
- TRACE_INTERNAL_IGNORE_UNUSED_VAR \
- const auto TRACE_INTERNAL_UNIQUE_VAR_NAME(trace_ref_) = \
- TRACE_IS_ENABLED(category) \
- ? TraceInstanceHelper<AsynchronousTraceLogger>::Create( \
- TRACE_INTERNAL_CONCAT_CONST(temp_storage, line), category, \
- name, __FILE__, __LINE__, ##__VA_ARGS__) \
- : TraceInstanceHelper<AsynchronousTraceLogger>::Empty()
-
-} // namespace openscreen
-
-#endif // PLATFORM_API_INTERNAL_TRACE_LOGGING_MACROS_INTERNAL_H_
diff --git a/platform/api/trace_logging.h b/platform/api/trace_logging.h
deleted file mode 100644
index 8ab4e139..00000000
--- a/platform/api/trace_logging.h
+++ /dev/null
@@ -1,77 +0,0 @@
-// 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 PLATFORM_API_TRACE_LOGGING_H_
-#define PLATFORM_API_TRACE_LOGGING_H_
-
-#include "platform/api/internal/trace_logging_internal.h"
-#include "platform/api/internal/trace_logging_macros_internal.h"
-#include "platform/api/trace_logging_types.h"
-
-namespace openscreen {
-
-// 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.
-// TODO(rwkeane): Add support for user-provided properties.
-
-#ifndef TRACE_FORCE_DISABLE
-#define TRACE_SET_RESULT(result) \
- do { \
- if (TRACE_IS_ENABLED(TraceCategory::Value::Any)) { \
- openscreen::platform::internal::ScopedTraceOperation::set_result( \
- result); \
- } \
- } while (false)
-#define TRACE_SET_HIERARCHY(ids) TRACE_SET_HIERARCHY_INTERNAL(__LINE__, ids)
-#define TRACE_HIERARCHY \
- (TRACE_IS_ENABLED(TraceCategory::Value::Any) \
- ? openscreen::platform::internal::ScopedTraceOperation::hierarchy() \
- : TraceIdHierarchy::Empty())
-#define TRACE_CURRENT_ID \
- (TRACE_IS_ENABLED(TraceCategory::Value::Any) \
- ? openscreen::platform::internal::ScopedTraceOperation::current_id() \
- : kEmptyTraceId)
-#define TRACE_ROOT_ID \
- (TRACE_IS_ENABLED(TraceCategory::Value::Any) \
- ? openscreen::platform::internal::ScopedTraceOperation::root_id() \
- : kEmptyTraceId)
-
-// Synchronous Trace Macro.
-#define TRACE_SCOPED(category, name, ...) \
- TRACE_SCOPED_INTERNAL(__LINE__, category, name, ##__VA_ARGS__)
-
-// Asynchronous Trace Macros.
-#define TRACE_ASYNC_START(category, name, ...) \
- TRACE_ASYNC_START_INTERNAL(__LINE__, category, name, ##__VA_ARGS__)
-
-#define TRACE_ASYNC_END(category, id, result) \
- TRACE_IS_ENABLED(category) \
- ? openscreen::platform::internal::TraceBase::TraceAsyncEnd( \
- __LINE__, __FILE__, id, result) \
- : false
-
-#else // TRACE_FORCE_DISABLE defined
-#define TRACE_SET_RESULT(result)
-#define TRACE_SET_HIERARCHY(ids)
-#define TRACE_HIERARCHY TraceIdHierarchy::Empty()
-#define TRACE_CURRENT_ID kEmptyTraceId
-#define TRACE_ROOT_ID kEmptyTraceId
-#define TRACE_SCOPED(category, name, ...)
-#define TRACE_ASYNC_START(category, name, ...)
-#define TRACE_ASYNC_END(category, id, result) false
-#endif // TRACE_FORCE_DISABLE
-
-} // namespace openscreen
-
-#endif // PLATFORM_API_TRACE_LOGGING_H_
diff --git a/platform/api/trace_logging_platform.cc b/platform/api/trace_logging_platform.cc
index 17168034..2725b404 100644
--- a/platform/api/trace_logging_platform.cc
+++ b/platform/api/trace_logging_platform.cc
@@ -4,29 +4,10 @@
#include "platform/api/trace_logging_platform.h"
-#include "platform/api/logging.h"
-
namespace openscreen {
namespace platform {
TraceLoggingPlatform::~TraceLoggingPlatform() = default;
-// static
-TraceLoggingPlatform* TraceLoggingPlatform::GetDefaultTracingPlatform() {
- return default_platform_;
-}
-
-// static
-void TraceLoggingPlatform::SetDefaultTraceLoggingPlatform(
- TraceLoggingPlatform* platform) {
- // NOTE: A DCHECK cannot be added here because BuildBots run UTs without
- // destroying static variables in between runs, so the DCHECK causes UTs to
- // fail unexpectedly.
- default_platform_ = platform;
-}
-
-// static
-TraceLoggingPlatform* TraceLoggingPlatform::default_platform_ = nullptr;
-
} // namespace platform
} // namespace openscreen
diff --git a/platform/api/trace_logging_platform.h b/platform/api/trace_logging_platform.h
index a65b787f..76c7e7a9 100644
--- a/platform/api/trace_logging_platform.h
+++ b/platform/api/trace_logging_platform.h
@@ -6,28 +6,23 @@
#define PLATFORM_API_TRACE_LOGGING_PLATFORM_H_
#include "platform/api/time.h"
-#include "platform/api/trace_logging_platform.h"
-#include "platform/api/trace_logging_types.h"
#include "platform/base/error.h"
+#include "platform/base/trace_logging_activation.h"
+#include "platform/base/trace_logging_types.h"
namespace openscreen {
namespace platform {
-#define TRACE_SET_DEFAULT_PLATFORM(new_platform) \
- openscreen::platform::TraceLoggingPlatform::SetDefaultTraceLoggingPlatform( \
- new_platform)
-
-// Platform API base class. The platform will be an extension of this
-// method, with an instantiation of that class being provided to the below
-// TracePlatformWrapper class.
+// Optional platform API to support logging trace events from Open Screen. To
+// use this, implement the TraceLoggingPlatform interface and call
+// StartTracing() and StopTracing() to turn tracing on/off (see
+// platform/base/trace_logging_activation.h).
class TraceLoggingPlatform {
public:
- virtual ~TraceLoggingPlatform() = 0;
+ virtual ~TraceLoggingPlatform();
- // Returns a static TraceLoggingPlatform to be used when generating trace
- // logs.
- static TraceLoggingPlatform* GetDefaultTracingPlatform();
- static void SetDefaultTraceLoggingPlatform(TraceLoggingPlatform* platform);
+ // Determines whether trace logging is enabled for the given category.
+ virtual bool IsTraceLoggingEnabled(TraceCategory::Value category) = 0;
// Log a synchronous trace.
virtual void LogTrace(const char* name,
@@ -51,14 +46,8 @@ class TraceLoggingPlatform {
Clock::time_point timestamp,
TraceId trace_id,
Error::Code error) = 0;
-
- private:
- static TraceLoggingPlatform* default_platform_;
};
-// Determines whether trace logging is enabled for the given category.
-bool IsTraceLoggingEnabled(TraceCategory::Value category);
-
} // namespace platform
} // namespace openscreen
diff --git a/platform/base/trace_logging_activation.cc b/platform/base/trace_logging_activation.cc
new file mode 100644
index 00000000..6a23f2d7
--- /dev/null
+++ b/platform/base/trace_logging_activation.cc
@@ -0,0 +1,32 @@
+// 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.
+
+#include "platform/base/trace_logging_activation.h"
+
+#include "platform/api/logging.h"
+
+namespace openscreen {
+namespace platform {
+
+namespace {
+TraceLoggingPlatform* g_current_destination = nullptr;
+} // namespace
+
+TraceLoggingPlatform* GetTracingDestination() {
+ return g_current_destination;
+}
+
+void StartTracing(TraceLoggingPlatform* destination) {
+ // TODO(crbug.com/openscreen/85): Need to revisit this to ensure thread-safety
+ // around the sequencing of starting and stopping tracing.
+ OSP_DCHECK(!g_current_destination);
+ g_current_destination = destination;
+}
+
+void StopTracing() {
+ g_current_destination = nullptr;
+}
+
+} // namespace platform
+} // namespace openscreen
diff --git a/platform/base/trace_logging_activation.h b/platform/base/trace_logging_activation.h
new file mode 100644
index 00000000..05be7343
--- /dev/null
+++ b/platform/base/trace_logging_activation.h
@@ -0,0 +1,25 @@
+// 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 PLATFORM_BASE_TRACE_LOGGING_ACTIVATION_H_
+#define PLATFORM_BASE_TRACE_LOGGING_ACTIVATION_H_
+
+namespace openscreen {
+namespace platform {
+
+class TraceLoggingPlatform;
+
+// Start or Stop trace logging. It is illegal to call StartTracing() a second
+// time without having called StopTracing() to stop the prior tracing session.
+void StartTracing(TraceLoggingPlatform* destination);
+void StopTracing();
+
+// If tracing is active, returns the current destination. Otherwise, returns
+// nullptr.
+TraceLoggingPlatform* GetTracingDestination();
+
+} // namespace platform
+} // namespace openscreen
+
+#endif // PLATFORM_BASE_TRACE_LOGGING_ACTIVATION_H_
diff --git a/platform/api/trace_logging_types.h b/platform/base/trace_logging_types.h
index 8b90f2a0..ce33edb4 100644
--- a/platform/api/trace_logging_types.h
+++ b/platform/base/trace_logging_types.h
@@ -2,12 +2,13 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#ifndef PLATFORM_API_TRACE_LOGGING_TYPES_H_
-#define PLATFORM_API_TRACE_LOGGING_TYPES_H_
+#ifndef PLATFORM_BASE_TRACE_LOGGING_TYPES_H_
+#define PLATFORM_BASE_TRACE_LOGGING_TYPES_H_
#include "absl/types/optional.h"
namespace openscreen {
+namespace platform {
// Define TraceId type here since other TraceLogging files import it.
using TraceId = uint64_t;
@@ -35,11 +36,13 @@ struct TraceIdHierarchy {
bool HasParent() { return parent != kUnsetTraceId; }
bool HasRoot() { return root != kUnsetTraceId; }
};
+
inline bool operator==(const TraceIdHierarchy& lhs,
const TraceIdHierarchy& rhs) {
return lhs.current == rhs.current && lhs.parent == rhs.parent &&
lhs.root == rhs.root;
}
+
inline bool operator!=(const TraceIdHierarchy& lhs,
const TraceIdHierarchy& rhs) {
return !(lhs == rhs);
@@ -59,6 +62,7 @@ struct TraceCategory {
};
};
+} // namespace platform
} // namespace openscreen
-#endif // PLATFORM_API_TRACE_LOGGING_TYPES_H_
+#endif // PLATFORM_BASE_TRACE_LOGGING_TYPES_H_
diff --git a/platform/impl/logging_posix.cc b/platform/impl/logging_posix.cc
index f0c862a0..ab921168 100644
--- a/platform/impl/logging_posix.cc
+++ b/platform/impl/logging_posix.cc
@@ -12,8 +12,8 @@
#include <iostream>
#include <sstream>
-#include "platform/api/trace_logging.h"
#include "platform/impl/logging.h"
+#include "util/trace_logging.h"
namespace openscreen {
namespace platform {
diff --git a/platform/impl/task_runner.h b/platform/impl/task_runner.h
index 81799f7c..35da181b 100644
--- a/platform/impl/task_runner.h
+++ b/platform/impl/task_runner.h
@@ -15,10 +15,11 @@
#include "absl/base/thread_annotations.h"
#include "absl/types/optional.h"
+#include "build/config/features.h"
#include "platform/api/task_runner.h"
#include "platform/api/time.h"
-#include "platform/api/trace_logging.h"
#include "platform/base/error.h"
+#include "util/trace_logging.h"
namespace openscreen {
namespace platform {
@@ -72,7 +73,7 @@ class TaskRunnerImpl final : public TaskRunner {
void RequestStopSoon();
private:
-#ifndef TRACE_FORCE_DISABLE
+#if defined(ENABLE_TRACE_LOGGING)
// Wrapper around a Task used to store the TraceId Metadata along with the
// task itself, and to set the current TraceIdHierarchy before executing the
// task.
@@ -93,9 +94,9 @@ class TaskRunnerImpl final : public TaskRunner {
Task task_;
TraceIdHierarchy trace_ids_;
};
-#else // TRACE_FORCE_DISABLE defined
+#else // !defined(ENABLE_TRACE_LOGGING)
using TaskWithMetadata = Task;
-#endif // TRACE_FORCE_DISABLE
+#endif // defined(ENABLE_TRACE_LOGGING)
// Helper that runs all tasks in |running_tasks_| and then clears it.
void RunRunnableTasks();
diff --git a/platform/impl/text_trace_logging_platform.cc b/platform/impl/text_trace_logging_platform.cc
index 09a71ff7..c0e56efd 100644
--- a/platform/impl/text_trace_logging_platform.cc
+++ b/platform/impl/text_trace_logging_platform.cc
@@ -11,18 +11,22 @@
namespace openscreen {
namespace platform {
-bool IsTraceLoggingEnabled(TraceCategory::Value category) {
-#ifndef NDEBUG
+bool TextTraceLoggingPlatform::IsTraceLoggingEnabled(
+ TraceCategory::Value category) {
constexpr uint64_t kAllLogCategoriesMask =
std::numeric_limits<uint64_t>::max();
return (kAllLogCategoriesMask & category) != 0;
-#else
- return false;
-#endif
}
-TextTraceLoggingPlatform::TextTraceLoggingPlatform() = default;
-TextTraceLoggingPlatform::~TextTraceLoggingPlatform() = default;
+TextTraceLoggingPlatform::TextTraceLoggingPlatform() {
+ OSP_DCHECK(!GetTracingDestination());
+ StartTracing(this);
+}
+
+TextTraceLoggingPlatform::~TextTraceLoggingPlatform() {
+ OSP_DCHECK_EQ(GetTracingDestination(), this);
+ StopTracing();
+}
void TextTraceLoggingPlatform::LogTrace(const char* name,
const uint32_t line,
diff --git a/platform/impl/text_trace_logging_platform.h b/platform/impl/text_trace_logging_platform.h
index b564a47a..3d2a93c6 100644
--- a/platform/impl/text_trace_logging_platform.h
+++ b/platform/impl/text_trace_logging_platform.h
@@ -5,8 +5,7 @@
#ifndef PLATFORM_IMPL_TEXT_TRACE_LOGGING_PLATFORM_H_
#define PLATFORM_IMPL_TEXT_TRACE_LOGGING_PLATFORM_H_
-#include "platform/api/trace_logging.h"
-#include "platform/api/trace_logging_types.h"
+#include "platform/api/trace_logging_platform.h"
namespace openscreen {
namespace platform {
@@ -14,7 +13,9 @@ namespace platform {
class TextTraceLoggingPlatform : public TraceLoggingPlatform {
public:
TextTraceLoggingPlatform();
- ~TextTraceLoggingPlatform();
+ ~TextTraceLoggingPlatform() override;
+
+ bool IsTraceLoggingEnabled(TraceCategory::Value category) override;
void LogTrace(const char* name,
const uint32_t line,
diff --git a/platform/impl/tls_connection_factory_posix.cc b/platform/impl/tls_connection_factory_posix.cc
index c6b43f94..fd3a0115 100644
--- a/platform/impl/tls_connection_factory_posix.cc
+++ b/platform/impl/tls_connection_factory_posix.cc
@@ -19,13 +19,13 @@
#include "platform/api/logging.h"
#include "platform/api/task_runner.h"
#include "platform/api/tls_connection_factory.h"
-#include "platform/api/trace_logging.h"
#include "platform/base/tls_connect_options.h"
#include "platform/base/tls_credentials.h"
#include "platform/base/tls_listen_options.h"
#include "platform/impl/stream_socket.h"
#include "platform/impl/tls_connection_posix.h"
#include "util/crypto/openssl_util.h"
+#include "util/trace_logging.h"
namespace openscreen {
namespace platform {
@@ -60,7 +60,7 @@ void TlsConnectionFactoryPosix::Connect(const IPEndpoint& remote_address,
new TlsConnectionPosix(version, task_runner_, platform_client_));
Error connect_error = connection->socket_->Connect(remote_address);
if (!connect_error.ok()) {
- TRACE_SET_RESULT(connect_error.error());
+ TRACE_SET_RESULT(connect_error);
DispatchConnectionFailed(remote_address);
return;
}
@@ -166,7 +166,7 @@ void TlsConnectionFactoryPosix::OnSocketAccepted(
const int connection_status = SSL_accept(connection->ssl_.get());
if (connection_status != 1) {
DispatchConnectionFailed(connection->GetRemoteEndpoint());
- TRACE_SET_RESULT(GetSSLError(ssl.get(), connection_status));
+ TRACE_SET_RESULT(GetSSLError(connection->ssl_.get(), connection_status));
return;
}
diff --git a/platform/test/trace_logging_helpers.h b/platform/test/trace_logging_helpers.h
index 794f0b7b..1058d6cf 100644
--- a/platform/test/trace_logging_helpers.h
+++ b/platform/test/trace_logging_helpers.h
@@ -2,11 +2,15 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "platform/api/trace_logging.h"
+#include "platform/api/trace_logging_platform.h"
#ifndef PLATFORM_TEST_TRACE_LOGGING_HELPERS_H_
#define PLATFORM_TEST_TRACE_LOGGING_HELPERS_H_
+#include "gmock/gmock.h"
+#include "platform/api/logging.h"
+#include "platform/base/trace_logging_activation.h"
+
namespace openscreen {
namespace platform {
@@ -16,6 +20,20 @@ enum ArgumentId { kFirst, kSecond };
class MockLoggingPlatform : public TraceLoggingPlatform {
public:
+ MockLoggingPlatform() {
+ OSP_DCHECK(!GetTracingDestination());
+ StartTracing(this);
+
+ ON_CALL(*this, IsTraceLoggingEnabled(::testing::_))
+ .WillByDefault(::testing::Return(true));
+ }
+
+ ~MockLoggingPlatform() override {
+ OSP_DCHECK_EQ(GetTracingDestination(), this);
+ StopTracing();
+ }
+
+ MOCK_METHOD1(IsTraceLoggingEnabled, bool(TraceCategory::Value category));
MOCK_METHOD7(LogTrace,
void(const char*,
const uint32_t,
diff --git a/util/BUILD.gn b/util/BUILD.gn
index de31d291..4c6e2cba 100644
--- a/util/BUILD.gn
+++ b/util/BUILD.gn
@@ -28,6 +28,10 @@ source_set("util") {
"saturate_cast.h",
"std_util.h",
"stringprintf.h",
+ "trace_logging.h",
+ "trace_logging/macro_support.h",
+ "trace_logging/scoped_trace_operations.cc",
+ "trace_logging/scoped_trace_operations.h",
"yet_another_bit_vector.cc",
"yet_another_bit_vector.h",
]
@@ -55,11 +59,14 @@ source_set("unittests") {
"json/json_writer_unittest.cc",
"operation_loop_unittest.cc",
"saturate_cast_unittest.cc",
+ "trace_logging/scoped_trace_operations_unittest.cc",
+ "trace_logging_unittest.cc",
"yet_another_bit_vector_unittest.cc",
]
deps = [
":util",
+ "../platform:test",
"../third_party/abseil",
"../third_party/boringssl",
"../third_party/googletest:gmock",
diff --git a/util/trace_logging.h b/util/trace_logging.h
new file mode 100644
index 00000000..cb454e1b
--- /dev/null
+++ b/util/trace_logging.h
@@ -0,0 +1,91 @@
+// 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_H_
+#define UTIL_TRACE_LOGGING_H_
+
+#include "build/config/features.h"
+#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.
+// TODO(rwkeane): Add support for user-provided properties.
+
+#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) \
+ do { \
+ if (TRACE_IS_ENABLED(openscreen::platform::TraceCategory::Value::Any)) { \
+ openscreen::internal::ScopedTraceOperation::set_result(result); \
+ } \
+ } while (false)
+#define TRACE_SET_HIERARCHY(ids) TRACE_SET_HIERARCHY_INTERNAL(__LINE__, ids)
+#define TRACE_HIERARCHY \
+ (TRACE_IS_ENABLED(openscreen::platform::TraceCategory::Value::Any) \
+ ? openscreen::internal::ScopedTraceOperation::hierarchy() \
+ : openscreen::platform::TraceIdHierarchy::Empty())
+#define TRACE_CURRENT_ID \
+ (TRACE_IS_ENABLED(openscreen::platform::TraceCategory::Value::Any) \
+ ? openscreen::internal::ScopedTraceOperation::current_id() \
+ : kEmptyTraceId)
+#define TRACE_ROOT_ID \
+ (TRACE_IS_ENABLED(openscreen::platform::TraceCategory::Value::Any) \
+ ? openscreen::internal::ScopedTraceOperation::root_id() \
+ : kEmptyTraceId)
+
+// Synchronous Trace Macro.
+#define TRACE_SCOPED(category, name, ...) \
+ TRACE_SCOPED_INTERNAL(__LINE__, category, name, ##__VA_ARGS__)
+
+// Asynchronous Trace Macros.
+#define TRACE_ASYNC_START(category, name, ...) \
+ TRACE_ASYNC_START_INTERNAL(__LINE__, category, name, ##__VA_ARGS__)
+
+#define TRACE_ASYNC_END(category, id, result) \
+ TRACE_IS_ENABLED(category) \
+ ? openscreen::internal::ScopedTraceOperation::TraceAsyncEnd( \
+ __LINE__, __FILE__, id, result) \
+ : false
+
+#else // ENABLE_TRACE_LOGGING not defined
+
+namespace openscreen {
+namespace 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 internal
+} // namespace openscreen
+
+#define TRACE_SET_RESULT(result) \
+ openscreen::internal::DoNothingForTracing(result)
+#define TRACE_SET_HIERARCHY(ids) openscreen::internal::DoNothingForTracing(ids)
+#define TRACE_HIERARCHY openscreen::platform::TraceIdHierarchy::Empty()
+#define TRACE_CURRENT_ID openscreen::platform::kEmptyTraceId
+#define TRACE_ROOT_ID openscreen::platform::kEmptyTraceId
+#define TRACE_SCOPED(category, name, ...) \
+ openscreen::internal::DoNothingForTracing(category, name, ##__VA_ARGS__)
+#define TRACE_ASYNC_START(category, name, ...) \
+ openscreen::internal::DoNothingForTracing(category, name, ##__VA_ARGS__)
+#define TRACE_ASYNC_END(category, id, result) \
+ openscreen::internal::DoNothingForTracing(category, id, result)
+
+#endif // defined(ENABLE_TRACE_LOGGING)
+
+#endif // UTIL_TRACE_LOGGING_H_
diff --git a/util/trace_logging/macro_support.h b/util/trace_logging/macro_support.h
new file mode 100644
index 00000000..30865f4c
--- /dev/null
+++ b/util/trace_logging/macro_support.h
@@ -0,0 +1,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(platform::TraceCategory::Value category) {
+ auto* const destination = platform::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::platform::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_
diff --git a/platform/api/internal/trace_logging_internal.cc b/util/trace_logging/scoped_trace_operations.cc
index 2618b415..0795e9c9 100644
--- a/platform/api/internal/trace_logging_internal.cc
+++ b/util/trace_logging/scoped_trace_operations.cc
@@ -2,22 +2,31 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "platform/api/internal/trace_logging_internal.h"
+#include "util/trace_logging/scoped_trace_operations.h"
#include "absl/types/optional.h"
+#include "build/config/features.h"
#include "platform/api/logging.h"
+#include "platform/api/trace_logging_platform.h"
+#include "platform/base/trace_logging_activation.h"
+
+#if defined(ENABLE_TRACE_LOGGING)
+
+using openscreen::platform::kUnsetTraceId;
+using openscreen::platform::TraceCategory;
+using openscreen::platform::TraceId;
+using openscreen::platform::TraceIdHierarchy;
namespace openscreen {
-namespace platform {
namespace internal {
// static
-bool TraceBase::TraceAsyncEnd(const uint32_t line,
- const char* file,
- TraceId id,
- Error::Code e) {
- auto end_time = Clock::now();
- auto current_platform = TraceLoggingPlatform::GetDefaultTracingPlatform();
+bool ScopedTraceOperation::TraceAsyncEnd(const uint32_t line,
+ const char* file,
+ TraceId id,
+ Error::Code e) {
+ auto end_time = platform::Clock::now();
+ auto* const current_platform = platform::GetTracingDestination();
if (current_platform == nullptr) {
return false;
}
@@ -86,7 +95,7 @@ TraceLoggerBase::TraceLoggerBase(TraceCategory::Value category,
TraceId parent,
TraceId root)
: ScopedTraceOperation(current, parent, root),
- start_time_(Clock::now()),
+ start_time_(platform::Clock::now()),
result_(Error::Code::kNone),
name_(name),
file_name_(file),
@@ -107,18 +116,18 @@ TraceLoggerBase::TraceLoggerBase(TraceCategory::Value category,
ids.root) {}
SynchronousTraceLogger::~SynchronousTraceLogger() {
- auto* current_platform = TraceLoggingPlatform::GetDefaultTracingPlatform();
+ auto* const current_platform = platform::GetTracingDestination();
if (current_platform == nullptr) {
return;
}
- auto end_time = Clock::now();
+ auto end_time = platform::Clock::now();
current_platform->LogTrace(this->name_, this->line_number_, this->file_name_,
this->start_time_, end_time, this->to_hierarchy(),
this->result_);
}
AsynchronousTraceLogger::~AsynchronousTraceLogger() {
- auto* current_platform = TraceLoggingPlatform::GetDefaultTracingPlatform();
+ auto* const current_platform = platform::GetTracingDestination();
if (current_platform == nullptr) {
return;
}
@@ -130,5 +139,6 @@ AsynchronousTraceLogger::~AsynchronousTraceLogger() {
TraceIdSetter::~TraceIdSetter() = default;
} // namespace internal
-} // namespace platform
} // namespace openscreen
+
+#endif // defined(ENABLE_TRACE_LOGGING)
diff --git a/platform/api/internal/trace_logging_internal.h b/util/trace_logging/scoped_trace_operations.h
index d1682648..592d4528 100644
--- a/platform/api/internal/trace_logging_internal.h
+++ b/util/trace_logging/scoped_trace_operations.h
@@ -2,64 +2,50 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#ifndef PLATFORM_API_INTERNAL_TRACE_LOGGING_INTERNAL_H_
-#define PLATFORM_API_INTERNAL_TRACE_LOGGING_INTERNAL_H_
+#ifndef UTIL_TRACE_LOGGING_SCOPED_TRACE_OPERATIONS_H_
+#define UTIL_TRACE_LOGGING_SCOPED_TRACE_OPERATIONS_H_
#include <atomic>
#include <cstring>
#include <stack>
#include <vector>
-#include "platform/api/logging.h"
+#include "build/config/features.h"
#include "platform/api/time.h"
-#include "platform/api/trace_logging_platform.h"
-#include "platform/api/trace_logging_types.h"
#include "platform/base/error.h"
+#include "platform/base/trace_logging_types.h"
+
+#if defined(ENABLE_TRACE_LOGGING)
namespace openscreen {
-namespace platform {
namespace internal {
-// Base class needed for macro calls. It has been intentionally left with
-// no instance variables so that construction is simple and fast.
-class TraceBase {
- public:
- TraceBase() = default;
- virtual ~TraceBase() = default;
-
- // Traces the end of an asynchronous call.
- // NOTE: This returns a bool rather than a void because it keeps the syntax of
- // the ternary operator in the macros simpler.
- static bool TraceAsyncEnd(const uint32_t line,
- const char* file,
- TraceId id,
- Error::Code e);
-};
-
// A base class for all trace logging objects which will create new entries in
// the Trace Hierarchy.
// 1) The sharing of all static and thread_local variables across template
// specializations.
// 2) Including all children in the same traces vector.
-class ScopedTraceOperation : public TraceBase {
+class ScopedTraceOperation {
public:
// Define the destructor to remove this item from the stack when it's
// destroyed.
- ~ScopedTraceOperation() override;
+ virtual ~ScopedTraceOperation();
// Getters the current Trace Hierarchy. If the traces_ stack hasn't been
// created yet, return as if the empty root node is there.
- static TraceId current_id() {
- return traces_ == nullptr ? kEmptyTraceId : traces_->top()->trace_id_;
+ static platform::TraceId current_id() {
+ return traces_ == nullptr ? platform::kEmptyTraceId
+ : traces_->top()->trace_id_;
}
- static TraceId root_id() {
- return traces_ == nullptr ? kEmptyTraceId : traces_->top()->root_id_;
+ static platform::TraceId root_id() {
+ return traces_ == nullptr ? platform::kEmptyTraceId
+ : traces_->top()->root_id_;
}
- static TraceIdHierarchy hierarchy() {
+ static platform::TraceIdHierarchy hierarchy() {
if (traces_ == nullptr) {
- return TraceIdHierarchy::Empty();
+ return platform::TraceIdHierarchy::Empty();
}
return traces_->top()->to_hierarchy();
@@ -74,6 +60,14 @@ class ScopedTraceOperation : public TraceBase {
traces_->top()->SetTraceResult(error);
}
+ // Traces the end of an asynchronous call.
+ // NOTE: This returns a bool rather than a void because it keeps the syntax of
+ // the ternary operator in the macros simpler.
+ static bool TraceAsyncEnd(const uint32_t line,
+ const char* file,
+ platform::TraceId id,
+ Error::Code e);
+
protected:
// Sets the result of this trace log.
// NOTE: this must be define in this class rather than TraceLogger so that it
@@ -82,16 +76,18 @@ class ScopedTraceOperation : public TraceBase {
virtual void SetTraceResult(Error::Code error) = 0;
// Constructor to set all trace id information.
- ScopedTraceOperation(TraceId current_id = kUnsetTraceId,
- TraceId parent_id = kUnsetTraceId,
- TraceId root_id = kUnsetTraceId);
+ ScopedTraceOperation(platform::TraceId current_id = platform::kUnsetTraceId,
+ platform::TraceId parent_id = platform::kUnsetTraceId,
+ platform::TraceId root_id = platform::kUnsetTraceId);
// Current TraceId information.
- TraceId trace_id_;
- TraceId parent_id_;
- TraceId root_id_;
+ platform::TraceId trace_id_;
+ platform::TraceId parent_id_;
+ platform::TraceId root_id_;
- TraceIdHierarchy to_hierarchy() { return {trace_id_, parent_id_, root_id_}; }
+ platform::TraceIdHierarchy to_hierarchy() {
+ return {trace_id_, parent_id_, root_id_};
+ }
private:
// NOTE: A std::vector is used for backing the stack because it provides the
@@ -116,26 +112,26 @@ class ScopedTraceOperation : public TraceBase {
// The class which does actual trace logging.
class TraceLoggerBase : public ScopedTraceOperation {
public:
- TraceLoggerBase(TraceCategory::Value category,
+ TraceLoggerBase(platform::TraceCategory::Value category,
const char* name,
const char* file,
uint32_t line,
- TraceId current = kUnsetTraceId,
- TraceId parent = kUnsetTraceId,
- TraceId root = kUnsetTraceId);
+ platform::TraceId current = platform::kUnsetTraceId,
+ platform::TraceId parent = platform::kUnsetTraceId,
+ platform::TraceId root = platform::kUnsetTraceId);
- TraceLoggerBase(TraceCategory::Value category,
+ TraceLoggerBase(platform::TraceCategory::Value category,
const char* name,
const char* file,
uint32_t line,
- TraceIdHierarchy ids);
+ platform::TraceIdHierarchy ids);
protected:
// Set the result.
void SetTraceResult(Error::Code error) override { result_ = error; }
// Timestamp for when the object was created.
- Clock::time_point start_time_;
+ platform::Clock::time_point start_time_;
// Result of this operation.
Error::Code result_;
@@ -150,7 +146,7 @@ class TraceLoggerBase : public ScopedTraceOperation {
uint32_t line_number_;
// Category of this trace log.
- TraceCategory::Value category_;
+ platform::TraceCategory::Value category_;
private:
OSP_DISALLOW_COPY_AND_ASSIGN(TraceLoggerBase);
@@ -160,7 +156,7 @@ class SynchronousTraceLogger : public TraceLoggerBase {
public:
using TraceLoggerBase::TraceLoggerBase;
- virtual ~SynchronousTraceLogger() override;
+ ~SynchronousTraceLogger() override;
private:
OSP_DISALLOW_COPY_AND_ASSIGN(SynchronousTraceLogger);
@@ -170,7 +166,7 @@ class AsynchronousTraceLogger : public TraceLoggerBase {
public:
using TraceLoggerBase::TraceLoggerBase;
- virtual ~AsynchronousTraceLogger() override;
+ ~AsynchronousTraceLogger() override;
private:
OSP_DISALLOW_COPY_AND_ASSIGN(AsynchronousTraceLogger);
@@ -180,7 +176,7 @@ class AsynchronousTraceLogger : public TraceLoggerBase {
// the current TraceId Hierarchy manually.
class TraceIdSetter : public ScopedTraceOperation {
public:
- explicit TraceIdSetter(TraceIdHierarchy ids)
+ explicit TraceIdSetter(platform::TraceIdHierarchy ids)
: ScopedTraceOperation(ids.current, ids.parent, ids.root) {}
~TraceIdSetter() final;
@@ -200,12 +196,12 @@ class TraceIdSetter : public ScopedTraceOperation {
template <class T>
class TraceInstanceHelper {
private:
- class TraceBaseStackDeleter {
+ class TraceOperationOnStackDeleter {
public:
void operator()(T* ptr) { ptr->~T(); }
};
- using TraceInstanceWrapper = std::unique_ptr<T, TraceBaseStackDeleter>;
+ using TraceInstanceWrapper = std::unique_ptr<T, TraceOperationOnStackDeleter>;
public:
template <typename... Args>
@@ -217,7 +213,8 @@ class TraceInstanceHelper {
};
} // namespace internal
-} // namespace platform
} // namespace openscreen
-#endif // PLATFORM_API_INTERNAL_TRACE_LOGGING_INTERNAL_H_
+#endif // defined(ENABLE_TRACE_LOGGING)
+
+#endif // UTIL_TRACE_LOGGING_SCOPED_TRACE_OPERATIONS_H_
diff --git a/platform/api/internal/trace_logging_internal_unittest.cc b/util/trace_logging/scoped_trace_operations_unittest.cc
index 9cbce709..63fa1cba 100644
--- a/platform/api/internal/trace_logging_internal_unittest.cc
+++ b/util/trace_logging/scoped_trace_operations_unittest.cc
@@ -3,28 +3,30 @@
// found in the LICENSE file.
#include <chrono>
-#include <iostream>
#include <thread>
-#include "absl/types/optional.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
-#include "platform/api/trace_logging.h"
#include "platform/test/trace_logging_helpers.h"
+#include "util/trace_logging.h"
+
+#if defined(ENABLE_TRACE_LOGGING)
// TODO(crbug.com/openscreen/52): Remove duplicate code from trace
// logging+internal unit tests
namespace openscreen {
-namespace platform {
namespace internal {
using ::testing::_;
using ::testing::DoAll;
using ::testing::Invoke;
+using platform::kEmptyTraceId;
+using platform::MockLoggingPlatform;
+
// These tests validate that parameters are passed correctly by using the Trace
// Internals.
-constexpr TraceCategory::Value category = TraceCategory::mDNS;
+constexpr auto category = platform::TraceCategory::mDNS;
constexpr uint32_t line = 10;
TEST(TraceLoggingInternalTest, CreatingNoTraceObjectValid) {
@@ -34,11 +36,11 @@ TEST(TraceLoggingInternalTest, CreatingNoTraceObjectValid) {
TEST(TraceLoggingInternalTest, TestMacroStyleInitializationTrue) {
constexpr uint32_t delay_in_ms = 50;
MockLoggingPlatform platform;
- TRACE_SET_DEFAULT_PLATFORM(&platform);
EXPECT_CALL(platform, LogTrace(_, _, _, _, _, _, _))
.Times(1)
- .WillOnce(DoAll(Invoke(ValidateTraceTimestampDiff<delay_in_ms>),
- Invoke(ValidateTraceErrorCode<Error::Code::kNone>)));
+ .WillOnce(
+ DoAll(Invoke(platform::ValidateTraceTimestampDiff<delay_in_ms>),
+ Invoke(platform::ValidateTraceErrorCode<Error::Code::kNone>)));
{
uint8_t temp[sizeof(SynchronousTraceLogger)];
@@ -57,7 +59,6 @@ TEST(TraceLoggingInternalTest, TestMacroStyleInitializationTrue) {
TEST(TraceLoggingInternalTest, TestMacroStyleInitializationFalse) {
MockLoggingPlatform platform;
- TRACE_SET_DEFAULT_PLATFORM(&platform);
EXPECT_CALL(platform, LogTrace(_, _, _, _, _, _, _)).Times(0);
{
@@ -78,21 +79,15 @@ TEST(TraceLoggingInternalTest, TestMacroStyleInitializationFalse) {
TEST(TraceLoggingInternalTest, ExpectParametersPassedToResult) {
MockLoggingPlatform platform;
- TRACE_SET_DEFAULT_PLATFORM(&platform);
EXPECT_CALL(platform, LogTrace(testing::StrEq("Name"), line,
testing::StrEq(__FILE__), _, _, _, _))
- .WillOnce(Invoke(ValidateTraceErrorCode<Error::Code::kNone>));
+ .WillOnce(Invoke(platform::ValidateTraceErrorCode<Error::Code::kNone>));
- {
- TRACE_INTERNAL_IGNORE_UNUSED_VAR const TraceBase&
- TRACE_INTERNAL_UNIQUE_VAR_NAME(trace_ref) =
- SynchronousTraceLogger{category, "Name", __FILE__, line};
- }
+ { SynchronousTraceLogger{category, "Name", __FILE__, line}; }
}
TEST(TraceLoggingInternalTest, CheckTraceAsyncStartLogsCorrectly) {
MockLoggingPlatform platform;
- TRACE_SET_DEFAULT_PLATFORM(&platform);
EXPECT_CALL(platform, LogAsyncStart(testing::StrEq("Name"), line,
testing::StrEq(__FILE__), _, _))
.Times(1);
@@ -118,5 +113,6 @@ TEST(TraceLoggingInternalTest, ValidateSetResultDoesntSegfaultOnEmptyStack) {
}
} // namespace internal
-} // namespace platform
} // namespace openscreen
+
+#endif // defined(ENABLE_TRACE_LOGGING)
diff --git a/platform/api/trace_logging_unittest.cc b/util/trace_logging_unittest.cc
index 6a6e3658..d34ed303 100644
--- a/platform/api/trace_logging_unittest.cc
+++ b/util/trace_logging_unittest.cc
@@ -2,54 +2,67 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include "util/trace_logging.h"
+
#include <chrono>
#include <thread>
#include "absl/types/optional.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
-
-#define TRACE_FORCE_ENABLE true
-
-#include "platform/api/trace_logging.h"
#include "platform/test/trace_logging_helpers.h"
-// TODO(crbug.com/openscreen/52): Remove duplicate code from trace
-// logging+internal unit tests
namespace openscreen {
namespace platform {
namespace {
+
+#if defined(ENABLE_TRACE_LOGGING)
constexpr TraceHierarchyParts kAllParts = static_cast<TraceHierarchyParts>(
TraceHierarchyParts::kRoot | TraceHierarchyParts::kParent |
TraceHierarchyParts::kCurrent);
constexpr TraceHierarchyParts kParentAndRoot = static_cast<TraceHierarchyParts>(
TraceHierarchyParts::kRoot | TraceHierarchyParts::kParent);
constexpr TraceId kEmptyId = TraceId{0};
-} // namespace
+#endif
using ::testing::_;
+using ::testing::AtLeast;
using ::testing::DoAll;
using ::testing::Invoke;
+// All the unit tests below should use TestTraceLoggingPlatform for the
+// library's tracing output mock. This is a StrictMock to ensure that, when not
+// compiling with ENABLE_TRACE_LOGGING, the mock receives no method calls.
+using StrictMockLoggingPlatform = ::testing::StrictMock<MockLoggingPlatform>;
+
TEST(TraceLoggingTest, MacroCallScopedDoesntSegFault) {
- MockLoggingPlatform platform;
- TRACE_SET_DEFAULT_PLATFORM(&platform);
+ StrictMockLoggingPlatform platform;
+#if defined(ENABLE_TRACE_LOGGING)
+ EXPECT_CALL(platform, IsTraceLoggingEnabled(TraceCategory::Value::Any))
+ .Times(AtLeast(1));
EXPECT_CALL(platform, LogTrace(_, _, _, _, _, _, _)).Times(1);
+#endif
{ TRACE_SCOPED(TraceCategory::Value::Any, "test"); }
}
TEST(TraceLoggingTest, MacroCallUnscopedDoesntSegFault) {
- MockLoggingPlatform platform;
- TRACE_SET_DEFAULT_PLATFORM(&platform);
+ StrictMockLoggingPlatform platform;
+#if defined(ENABLE_TRACE_LOGGING)
+ EXPECT_CALL(platform, IsTraceLoggingEnabled(TraceCategory::Value::Any))
+ .Times(AtLeast(1));
EXPECT_CALL(platform, LogAsyncStart(_, _, _, _, _)).Times(1);
+#endif
{ TRACE_ASYNC_START(TraceCategory::Value::Any, "test"); }
}
TEST(TraceLoggingTest, MacroVariablesUniquelyNames) {
- MockLoggingPlatform platform;
- TRACE_SET_DEFAULT_PLATFORM(&platform);
+ StrictMockLoggingPlatform platform;
+#if defined(ENABLE_TRACE_LOGGING)
+ EXPECT_CALL(platform, IsTraceLoggingEnabled(TraceCategory::Value::Any))
+ .Times(AtLeast(1));
EXPECT_CALL(platform, LogTrace(_, _, _, _, _, _, _)).Times(2);
EXPECT_CALL(platform, LogAsyncStart(_, _, _, _, _)).Times(2);
+#endif
{
TRACE_SCOPED(TraceCategory::Value::Any, "test1");
@@ -61,11 +74,14 @@ TEST(TraceLoggingTest, MacroVariablesUniquelyNames) {
TEST(TraceLoggingTest, ExpectTimestampsReflectDelay) {
constexpr uint32_t delay_in_ms = 50;
- MockLoggingPlatform platform;
- TRACE_SET_DEFAULT_PLATFORM(&platform);
+ StrictMockLoggingPlatform platform;
+#if defined(ENABLE_TRACE_LOGGING)
+ EXPECT_CALL(platform, IsTraceLoggingEnabled(TraceCategory::Value::Any))
+ .Times(AtLeast(1));
EXPECT_CALL(platform, LogTrace(_, _, _, _, _, _, _))
.WillOnce(DoAll(Invoke(ValidateTraceTimestampDiff<delay_in_ms>),
Invoke(ValidateTraceErrorCode<Error::Code::kNone>)));
+#endif
{
TRACE_SCOPED(TraceCategory::Value::Any, "Name");
@@ -75,10 +91,13 @@ TEST(TraceLoggingTest, ExpectTimestampsReflectDelay) {
TEST(TraceLoggingTest, ExpectErrorsPassedToResult) {
constexpr Error::Code result_code = Error::Code::kParseError;
- MockLoggingPlatform platform;
- TRACE_SET_DEFAULT_PLATFORM(&platform);
+ StrictMockLoggingPlatform platform;
+#if defined(ENABLE_TRACE_LOGGING)
+ EXPECT_CALL(platform, IsTraceLoggingEnabled(TraceCategory::Value::Any))
+ .Times(AtLeast(1));
EXPECT_CALL(platform, LogTrace(_, _, _, _, _, _, _))
.WillOnce(Invoke(ValidateTraceErrorCode<result_code>));
+#endif
{
TRACE_SCOPED(TraceCategory::Value::Any, "Name");
@@ -87,9 +106,12 @@ TEST(TraceLoggingTest, ExpectErrorsPassedToResult) {
}
TEST(TraceLoggingTest, ExpectUnsetTraceIdNotSet) {
- MockLoggingPlatform platform;
- TRACE_SET_DEFAULT_PLATFORM(&platform);
+ StrictMockLoggingPlatform platform;
+#if defined(ENABLE_TRACE_LOGGING)
+ EXPECT_CALL(platform, IsTraceLoggingEnabled(TraceCategory::Value::Any))
+ .Times(AtLeast(1));
EXPECT_CALL(platform, LogTrace(_, _, _, _, _, _, _)).Times(1);
+#endif
TraceIdHierarchy h = {kUnsetTraceId, kUnsetTraceId, kUnsetTraceId};
{
@@ -106,18 +128,22 @@ TEST(TraceLoggingTest, ExpectCreationWithIdsToWork) {
constexpr TraceId current = 0x32;
constexpr TraceId parent = 0x47;
constexpr TraceId root = 0x84;
- MockLoggingPlatform platform;
- TRACE_SET_DEFAULT_PLATFORM(&platform);
+ StrictMockLoggingPlatform platform;
+#if defined(ENABLE_TRACE_LOGGING)
+ EXPECT_CALL(platform, IsTraceLoggingEnabled(TraceCategory::Value::Any))
+ .Times(AtLeast(1));
EXPECT_CALL(platform, LogTrace(_, _, _, _, _, _, _))
.WillOnce(
DoAll(Invoke(ValidateTraceErrorCode<Error::Code::kNone>),
Invoke(ValidateTraceIdHierarchyOnSyncTrace<current, parent,
root, kAllParts>)));
+#endif
{
TraceIdHierarchy h = {current, parent, root};
TRACE_SCOPED(TraceCategory::Value::Any, "Name", h);
+#if defined(ENABLE_TRACE_LOGGING)
auto ids = TRACE_HIERARCHY;
EXPECT_EQ(ids.current, current);
EXPECT_EQ(ids.parent, parent);
@@ -125,6 +151,7 @@ TEST(TraceLoggingTest, ExpectCreationWithIdsToWork) {
EXPECT_EQ(TRACE_CURRENT_ID, current);
EXPECT_EQ(TRACE_ROOT_ID, root);
+#endif
}
}
@@ -132,8 +159,10 @@ TEST(TraceLoggingTest, ExpectHirearchyToBeApplied) {
constexpr TraceId current = 0x32;
constexpr TraceId parent = 0x47;
constexpr TraceId root = 0x84;
- MockLoggingPlatform platform;
- TRACE_SET_DEFAULT_PLATFORM(&platform);
+ StrictMockLoggingPlatform platform;
+#if defined(ENABLE_TRACE_LOGGING)
+ EXPECT_CALL(platform, IsTraceLoggingEnabled(TraceCategory::Value::Any))
+ .Times(AtLeast(1));
EXPECT_CALL(platform, LogTrace(_, _, _, _, _, _, _))
.WillOnce(DoAll(
Invoke(ValidateTraceErrorCode<Error::Code::kNone>),
@@ -143,20 +172,25 @@ TEST(TraceLoggingTest, ExpectHirearchyToBeApplied) {
DoAll(Invoke(ValidateTraceErrorCode<Error::Code::kNone>),
Invoke(ValidateTraceIdHierarchyOnSyncTrace<current, parent,
root, kAllParts>)));
+#endif
{
TraceIdHierarchy h = {current, parent, root};
TRACE_SCOPED(TraceCategory::Value::Any, "Name", h);
+#if defined(ENABLE_TRACE_LOGGING)
auto ids = TRACE_HIERARCHY;
EXPECT_EQ(ids.current, current);
EXPECT_EQ(ids.parent, parent);
EXPECT_EQ(ids.root, root);
+#endif
TRACE_SCOPED(TraceCategory::Value::Any, "Name");
+#if defined(ENABLE_TRACE_LOGGING)
ids = TRACE_HIERARCHY;
EXPECT_NE(ids.current, current);
EXPECT_EQ(ids.parent, current);
EXPECT_EQ(ids.root, root);
+#endif
}
}
@@ -164,13 +198,16 @@ TEST(TraceLoggingTest, ExpectHirearchyToEndAfterScopeWhenSetWithSetter) {
constexpr TraceId current = 0x32;
constexpr TraceId parent = 0x47;
constexpr TraceId root = 0x84;
- MockLoggingPlatform platform;
- TRACE_SET_DEFAULT_PLATFORM(&platform);
+ StrictMockLoggingPlatform platform;
+#if defined(ENABLE_TRACE_LOGGING)
+ EXPECT_CALL(platform, IsTraceLoggingEnabled(TraceCategory::Value::Any))
+ .Times(AtLeast(1));
EXPECT_CALL(platform, LogTrace(_, _, _, _, _, _, _))
.WillOnce(DoAll(
Invoke(ValidateTraceErrorCode<Error::Code::kNone>),
Invoke(ValidateTraceIdHierarchyOnSyncTrace<kEmptyId, current, root,
kParentAndRoot>)));
+#endif
{
TraceIdHierarchy ids = {current, parent, root};
@@ -178,15 +215,19 @@ TEST(TraceLoggingTest, ExpectHirearchyToEndAfterScopeWhenSetWithSetter) {
{
TRACE_SCOPED(TraceCategory::Value::Any, "Name");
ids = TRACE_HIERARCHY;
+#if defined(ENABLE_TRACE_LOGGING)
EXPECT_NE(ids.current, current);
EXPECT_EQ(ids.parent, current);
EXPECT_EQ(ids.root, root);
+#endif
}
ids = TRACE_HIERARCHY;
+#if defined(ENABLE_TRACE_LOGGING)
EXPECT_EQ(ids.current, current);
EXPECT_EQ(ids.parent, parent);
EXPECT_EQ(ids.root, root);
+#endif
}
}
@@ -194,8 +235,10 @@ TEST(TraceLoggingTest, ExpectHirearchyToEndAfterScope) {
constexpr TraceId current = 0x32;
constexpr TraceId parent = 0x47;
constexpr TraceId root = 0x84;
- MockLoggingPlatform platform;
- TRACE_SET_DEFAULT_PLATFORM(&platform);
+ StrictMockLoggingPlatform platform;
+#if defined(ENABLE_TRACE_LOGGING)
+ EXPECT_CALL(platform, IsTraceLoggingEnabled(TraceCategory::Value::Any))
+ .Times(AtLeast(1));
EXPECT_CALL(platform, LogTrace(_, _, _, _, _, _, _))
.WillOnce(DoAll(
Invoke(ValidateTraceErrorCode<Error::Code::kNone>),
@@ -205,6 +248,7 @@ TEST(TraceLoggingTest, ExpectHirearchyToEndAfterScope) {
DoAll(Invoke(ValidateTraceErrorCode<Error::Code::kNone>),
Invoke(ValidateTraceIdHierarchyOnSyncTrace<current, parent,
root, kAllParts>)));
+#endif
{
TraceIdHierarchy ids = {current, parent, root};
@@ -212,15 +256,19 @@ TEST(TraceLoggingTest, ExpectHirearchyToEndAfterScope) {
{
TRACE_SCOPED(TraceCategory::Value::Any, "Name");
ids = TRACE_HIERARCHY;
+#if defined(ENABLE_TRACE_LOGGING)
EXPECT_NE(ids.current, current);
EXPECT_EQ(ids.parent, current);
EXPECT_EQ(ids.root, root);
+#endif
}
ids = TRACE_HIERARCHY;
+#if defined(ENABLE_TRACE_LOGGING)
EXPECT_EQ(ids.current, current);
EXPECT_EQ(ids.parent, parent);
EXPECT_EQ(ids.root, root);
+#endif
}
}
@@ -228,34 +276,44 @@ TEST(TraceLoggingTest, ExpectSetHierarchyToApply) {
constexpr TraceId current = 0x32;
constexpr TraceId parent = 0x47;
constexpr TraceId root = 0x84;
- MockLoggingPlatform platform;
- TRACE_SET_DEFAULT_PLATFORM(&platform);
+ StrictMockLoggingPlatform platform;
+#if defined(ENABLE_TRACE_LOGGING)
+ EXPECT_CALL(platform, IsTraceLoggingEnabled(TraceCategory::Value::Any))
+ .Times(AtLeast(1));
EXPECT_CALL(platform, LogTrace(_, _, _, _, _, _, _))
.WillOnce(DoAll(
Invoke(ValidateTraceErrorCode<Error::Code::kNone>),
Invoke(ValidateTraceIdHierarchyOnSyncTrace<kEmptyId, current, root,
kParentAndRoot>)));
+#endif
{
TraceIdHierarchy ids = {current, parent, root};
TRACE_SET_HIERARCHY(ids);
ids = TRACE_HIERARCHY;
+#if defined(ENABLE_TRACE_LOGGING)
EXPECT_EQ(ids.current, current);
EXPECT_EQ(ids.parent, parent);
EXPECT_EQ(ids.root, root);
+#endif
TRACE_SCOPED(TraceCategory::Value::Any, "Name");
ids = TRACE_HIERARCHY;
+#if defined(ENABLE_TRACE_LOGGING)
EXPECT_NE(ids.current, current);
EXPECT_EQ(ids.parent, current);
EXPECT_EQ(ids.root, root);
+#endif
}
}
TEST(TraceLoggingTest, CheckTraceAsyncStartLogsCorrectly) {
- MockLoggingPlatform platform;
- TRACE_SET_DEFAULT_PLATFORM(&platform);
+ StrictMockLoggingPlatform platform;
+#if defined(ENABLE_TRACE_LOGGING)
+ EXPECT_CALL(platform, IsTraceLoggingEnabled(TraceCategory::Value::Any))
+ .Times(AtLeast(1));
EXPECT_CALL(platform, LogAsyncStart(_, _, _, _, _)).Times(1);
+#endif
{ TRACE_ASYNC_START(TraceCategory::Value::Any, "Name"); }
}
@@ -264,12 +322,15 @@ TEST(TraceLoggingTest, CheckTraceAsyncStartSetsHierarchy) {
constexpr TraceId current = 32;
constexpr TraceId parent = 47;
constexpr TraceId root = 84;
- MockLoggingPlatform platform;
- TRACE_SET_DEFAULT_PLATFORM(&platform);
+ StrictMockLoggingPlatform platform;
+#if defined(ENABLE_TRACE_LOGGING)
+ EXPECT_CALL(platform, IsTraceLoggingEnabled(TraceCategory::Value::Any))
+ .Times(AtLeast(1));
EXPECT_CALL(platform, LogAsyncStart(_, _, _, _, _))
.WillOnce(
Invoke(ValidateTraceIdHierarchyOnAsyncTrace<kEmptyId, current, root,
kParentAndRoot>));
+#endif
{
TraceIdHierarchy ids = {current, parent, root};
@@ -277,29 +338,35 @@ TEST(TraceLoggingTest, CheckTraceAsyncStartSetsHierarchy) {
{
TRACE_ASYNC_START(TraceCategory::Value::Any, "Name");
ids = TRACE_HIERARCHY;
+#if defined(ENABLE_TRACE_LOGGING)
EXPECT_NE(ids.current, current);
EXPECT_EQ(ids.parent, current);
EXPECT_EQ(ids.root, root);
+#endif
}
ids = TRACE_HIERARCHY;
+#if defined(ENABLE_TRACE_LOGGING)
EXPECT_EQ(ids.current, current);
EXPECT_EQ(ids.parent, parent);
EXPECT_EQ(ids.root, root);
+#endif
}
}
TEST(TraceLoggingTest, CheckTraceAsyncEndLogsCorrectly) {
constexpr TraceId id = 12345;
constexpr Error::Code result = Error::Code::kAgain;
- MockLoggingPlatform platform;
- TRACE_SET_DEFAULT_PLATFORM(&platform);
+ StrictMockLoggingPlatform platform;
+#if defined(ENABLE_TRACE_LOGGING)
+ EXPECT_CALL(platform, IsTraceLoggingEnabled(TraceCategory::Value::Any))
+ .Times(AtLeast(1));
EXPECT_CALL(platform, LogAsyncEnd(_, _, _, id, result)).Times(1);
+#endif
TRACE_ASYNC_END(TraceCategory::Value::Any, id, result);
}
+} // namespace
} // namespace platform
} // namespace openscreen
-
-#undef TRACE_FORCE_ENABLE