[clangd] Configure clangd tracing with CLANGD_TRACE env instead of -trace flag

llvm-svn: 325097
This commit is contained in:
Sam McCall 2018-02-14 03:20:07 +00:00
parent f1f20e6802
commit ed2717ad33
2 changed files with 9 additions and 11 deletions

View File

@ -17,6 +17,7 @@
#include "llvm/Support/Path.h"
#include "llvm/Support/Program.h"
#include "llvm/Support/raw_ostream.h"
#include <cstdlib>
#include <iostream>
#include <memory>
#include <string>
@ -123,12 +124,6 @@ static llvm::cl::opt<Path> InputMirrorFile(
"Mirror all LSP input to the specified file. Useful for debugging."),
llvm::cl::init(""), llvm::cl::Hidden);
static llvm::cl::opt<Path> TraceFile(
"trace",
llvm::cl::desc(
"Trace internal events and timestamps in chrome://tracing JSON format"),
llvm::cl::init(""), llvm::cl::Hidden);
static llvm::cl::opt<bool> EnableIndexBasedCompletion(
"enable-index-based-completion",
llvm::cl::desc(
@ -176,15 +171,18 @@ int main(int argc, char *argv[]) {
}
}
// Setup tracing facilities.
// Setup tracing facilities if CLANGD_TRACE is set. In practice enabling a
// trace flag in your editor's config is annoying, launching with
// `CLANGD_TRACE=trace.json vim` is easier.
llvm::Optional<llvm::raw_fd_ostream> TraceStream;
std::unique_ptr<trace::EventTracer> Tracer;
if (!TraceFile.empty()) {
if (auto *TraceFile = getenv("CLANGD_TRACE")) {
std::error_code EC;
TraceStream.emplace(TraceFile, /*ref*/ EC, llvm::sys::fs::F_RW);
if (EC) {
TraceFile.reset();
llvm::errs() << "Error while opening trace file: " << EC.message();
TraceStream.reset();
llvm::errs() << "Error while opening trace file " << TraceFile << ": "
<< EC.message();
} else {
Tracer = trace::createJSONTracer(*TraceStream, PrettyPrint);
}

View File

@ -1,4 +1,4 @@
# RUN: clangd -lit-test -trace %t < %s && FileCheck %s < %t
# RUN: CLANGD_TRACE=%t clangd -lit-test < %s && FileCheck %s < %t
{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":123,"rootPath":"clangd","capabilities":{},"trace":"off"}}
---
{"jsonrpc":"2.0","method":"textDocument/didOpen","params":{"textDocument":{"uri":"test:///foo.c","languageId":"c","version":1,"text":"void main() {}"}}}