aboutsummaryrefslogtreecommitdiff
path: root/util/trace_logging/scoped_trace_operations.cc
blob: 0795e9c9edeebe512afbdc16421cf58e9f134543 (plain)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright 2018 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 "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 internal {

// static
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;
  }
  current_platform->LogAsyncEnd(line, file, end_time, id, e);
  return true;
}

ScopedTraceOperation::ScopedTraceOperation(TraceId trace_id,
                                           TraceId parent_id,
                                           TraceId root_id) {
  if (traces_ == nullptr) {
    // Create the stack if it doesnt' exist.
    traces_ = new TraceStack();

    // Create a new root node. This will re-call this constructor and add the
    // root node to the stack before proceeding with the original node.
    root_node_ = new TraceIdSetter(TraceIdHierarchy::Empty());
    OSP_DCHECK(!traces_->empty());
  }

  // Setting trace id fields.
  root_id_ = root_id != kUnsetTraceId ? root_id : traces_->top()->root_id_;
  parent_id_ =
      parent_id != kUnsetTraceId ? parent_id : traces_->top()->trace_id_;
  trace_id_ =
      trace_id != kUnsetTraceId ? trace_id : trace_id_counter_.fetch_add(1);

  // Add this item to the stack.
  traces_->push(this);
  OSP_DCHECK(traces_->size() < 1024);
}

ScopedTraceOperation::~ScopedTraceOperation() {
  OSP_CHECK(traces_ != nullptr && !traces_->empty());
  OSP_CHECK_EQ(traces_->top(), this);
  traces_->pop();

  // If there's only one item left, it must be the root node. Deleting the root
  // node will re-call this destructor and delete the traces_ stack.
  if (traces_->size() == 1) {
    OSP_CHECK_EQ(traces_->top(), root_node_);
    delete root_node_;
    root_node_ = nullptr;
  } else if (traces_->empty()) {
    delete traces_;
    traces_ = nullptr;
  }
}

// static
thread_local ScopedTraceOperation::TraceStack* ScopedTraceOperation::traces_ =
    nullptr;

// static
thread_local ScopedTraceOperation* ScopedTraceOperation::root_node_ = nullptr;

// static
std::atomic<std::uint64_t> ScopedTraceOperation::trace_id_counter_{
    uint64_t{0x01} << (sizeof(TraceId) * 8 - 1)};

TraceLoggerBase::TraceLoggerBase(TraceCategory::Value category,
                                 const char* name,
                                 const char* file,
                                 uint32_t line,
                                 TraceId current,
                                 TraceId parent,
                                 TraceId root)
    : ScopedTraceOperation(current, parent, root),
      start_time_(platform::Clock::now()),
      result_(Error::Code::kNone),
      name_(name),
      file_name_(file),
      line_number_(line),
      category_(category) {}

TraceLoggerBase::TraceLoggerBase(TraceCategory::Value category,
                                 const char* name,
                                 const char* file,
                                 uint32_t line,
                                 TraceIdHierarchy ids)
    : TraceLoggerBase(category,
                      name,
                      file,
                      line,
                      ids.current,
                      ids.parent,
                      ids.root) {}

SynchronousTraceLogger::~SynchronousTraceLogger() {
  auto* const current_platform = platform::GetTracingDestination();
  if (current_platform == nullptr) {
    return;
  }
  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* const current_platform = platform::GetTracingDestination();
  if (current_platform == nullptr) {
    return;
  }
  current_platform->LogAsyncStart(this->name_, this->line_number_,
                                  this->file_name_, this->start_time_,
                                  this->to_hierarchy());
}

TraceIdSetter::~TraceIdSetter() = default;

}  // namespace internal
}  // namespace openscreen

#endif  // defined(ENABLE_TRACE_LOGGING)