aboutsummaryrefslogtreecommitdiff
path: root/clangd/Trace.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'clangd/Trace.cpp')
-rw-r--r--clangd/Trace.cpp47
1 files changed, 27 insertions, 20 deletions
diff --git a/clangd/Trace.cpp b/clangd/Trace.cpp
index 60340149..e0f09218 100644
--- a/clangd/Trace.cpp
+++ b/clangd/Trace.cpp
@@ -24,9 +24,9 @@ using namespace llvm;
namespace {
// The current implementation is naive: each thread writes to Out guarded by Mu.
// Perhaps we should replace this by something that disturbs performance less.
-class Tracer {
+class JSONTracer : public EventTracer {
public:
- Tracer(raw_ostream &Out, bool Pretty)
+ JSONTracer(raw_ostream &Out, bool Pretty)
: Out(Out), Sep(""), Start(std::chrono::system_clock::now()),
JSONFormat(Pretty ? "{0:2}" : "{0}") {
// The displayTimeUnit must be ns to avoid low-precision overlap
@@ -39,14 +39,15 @@ public:
});
}
- ~Tracer() {
+ ~JSONTracer() {
Out << "\n]}";
Out.flush();
}
// Record an event on the current thread. ph, pid, tid, ts are set.
// Contents must be a list of the other JSON key/values.
- void event(StringRef Phase, json::obj &&Contents) {
+ void event(const Context &Ctx, StringRef Phase,
+ json::obj &&Contents) override {
uint64_t TID = get_threadid();
std::lock_guard<std::mutex> Lock(Mu);
// If we haven't already, emit metadata describing this thread.
@@ -90,33 +91,38 @@ private:
const char *JSONFormat;
};
-static Tracer *T = nullptr;
+EventTracer *T = nullptr;
} // namespace
-std::unique_ptr<Session> Session::create(raw_ostream &OS, bool Pretty) {
- assert(!T && "A session is already active");
- T = new Tracer(OS, Pretty);
- return std::unique_ptr<Session>(new Session());
+Session::Session(EventTracer &Tracer) {
+ assert(!T && "Resetting global tracer is not allowed.");
+ T = &Tracer;
}
-Session::~Session() {
- delete T;
- T = nullptr;
+Session::~Session() { T = nullptr; }
+
+std::unique_ptr<EventTracer> createJSONTracer(llvm::raw_ostream &OS,
+ bool Pretty) {
+ return llvm::make_unique<JSONTracer>(OS, Pretty);
}
-void log(const Twine &Message) {
+void log(const Context &Ctx, const Twine &Message) {
if (!T)
return;
- T->event("i", json::obj{
- {"name", "Log"},
- {"args", json::obj{{"Message", Message.str()}}},
- });
+ T->event(Ctx, "i",
+ json::obj{
+ {"name", "Log"},
+ {"args", json::obj{{"Message", Message.str()}}},
+ });
}
-Span::Span(std::string Name) {
+Span::Span(const Context &Ctx, std::string Name) {
if (!T)
return;
- T->event("B", json::obj{{"name", std::move(Name)}});
+ // Clone the context, so that the original Context can be moved.
+ this->Ctx.emplace(Ctx.clone());
+
+ T->event(*this->Ctx, "B", json::obj{{"name", std::move(Name)}});
Args = llvm::make_unique<json::obj>();
}
@@ -125,7 +131,8 @@ Span::~Span() {
return;
if (!Args)
Args = llvm::make_unique<json::obj>();
- T->event("E", Args ? json::obj{{"args", std::move(*Args)}} : json::obj{});
+ T->event(*Ctx, "E",
+ Args ? json::obj{{"args", std::move(*Args)}} : json::obj{});
}
} // namespace trace