diff --git a/clang-tools-extra/clangd/tool/ClangdMain.cpp b/clang-tools-extra/clangd/tool/ClangdMain.cpp index 80737706704d..310ddbcd7f21 100644 --- a/clang-tools-extra/clangd/tool/ClangdMain.cpp +++ b/clang-tools-extra/clangd/tool/ClangdMain.cpp @@ -17,6 +17,7 @@ #include "llvm/Support/Path.h" #include "llvm/Support/Program.h" #include "llvm/Support/raw_ostream.h" +#include #include #include #include @@ -123,12 +124,6 @@ static llvm::cl::opt InputMirrorFile( "Mirror all LSP input to the specified file. Useful for debugging."), llvm::cl::init(""), llvm::cl::Hidden); -static llvm::cl::opt 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 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 TraceStream; std::unique_ptr 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); } diff --git a/clang-tools-extra/test/clangd/trace.test b/clang-tools-extra/test/clangd/trace.test index 7c391f7dbdfe..711235b84af1 100644 --- a/clang-tools-extra/test/clangd/trace.test +++ b/clang-tools-extra/test/clangd/trace.test @@ -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() {}"}}}