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
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/openscreen_platform/event_trace_logging_platform.h"
#include <chrono>
#include <limits>
#include <sstream>
#include "base/hash/hash.h"
#include "base/logging.h"
#include "base/no_destructor.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/trace_event/trace_event.h"
#include "third_party/openscreen/src/platform/base/error.h"
namespace openscreen_platform {
// static
void EventTraceLoggingPlatform::EnsureInstance() {
static base::NoDestructor<EventTraceLoggingPlatform> g_platform;
}
bool EventTraceLoggingPlatform::IsTraceLoggingEnabled(
openscreen::TraceCategory category) {
// Chrome controls what categories are enabled by use of the subsystem field
// in the trace event macro.
return true;
}
EventTraceLoggingPlatform::EventTraceLoggingPlatform() {
openscreen::StartTracing(this);
}
EventTraceLoggingPlatform::~EventTraceLoggingPlatform() {
openscreen::StopTracing();
}
void EventTraceLoggingPlatform::LogTrace(
openscreen::TraceEvent event,
openscreen::Clock::time_point end_time) {
const size_t total_runtime =
std::chrono::microseconds(end_time - event.start_time).count();
// We only get two fields in Chrome's trace logging system, so we elect to
// track the duration for profiling reasons as well as the error code, and
// ignore the trace ID hierarchy (which is difficult to parse as a user
// anyway).
TRACE_EVENT_INSTANT2(
kOpenscreenTraceLoggingCategory, event.name, TRACE_EVENT_SCOPE_THREAD,
"event", event.ToString(), "duration",
base::StrCat({base::NumberToString(total_runtime), "µs"}));
}
void EventTraceLoggingPlatform::LogAsyncStart(openscreen::TraceEvent event) {
TRACE_EVENT_NESTABLE_ASYNC_BEGIN1(kOpenscreenTraceLoggingCategory, event.name,
TRACE_ID_LOCAL(event.ids.current), "event",
event.ToString());
}
void EventTraceLoggingPlatform::LogAsyncEnd(openscreen::TraceEvent event) {
TRACE_EVENT_NESTABLE_ASYNC_END1(kOpenscreenTraceLoggingCategory, event.name,
TRACE_ID_LOCAL(event.ids.current), "event",
event.ToString());
}
} // namespace openscreen_platform
|