diff --git a/clang-tools-extra/clangd/index/remote/server/Server.cpp b/clang-tools-extra/clangd/index/remote/server/Server.cpp --- a/clang-tools-extra/clangd/index/remote/server/Server.cpp +++ b/clang-tools-extra/clangd/index/remote/server/Server.cpp @@ -39,6 +39,15 @@ llvm::cl::opt IndexRoot(llvm::cl::desc(""), llvm::cl::Positional, llvm::cl::Required); +llvm::cl::opt LogLevel{ + "log", + llvm::cl::desc("Verbosity of log messages written to stderr"), + values(clEnumValN(Logger::Error, "error", "Error messages only"), + clEnumValN(Logger::Info, "info", "High level execution tracing"), + clEnumValN(Logger::Debug, "verbose", "Low level details")), + llvm::cl::init(Logger::Info), +}; + llvm::cl::opt TraceFile( "trace-file", llvm::cl::desc("Path to the file where tracer logs will be stored")); @@ -173,7 +182,7 @@ Builder.AddListeningPort(ServerAddress, grpc::InsecureServerCredentials()); Builder.RegisterService(&Service); std::unique_ptr Server(Builder.BuildAndStart()); - llvm::outs() << "Server listening on " << ServerAddress << '\n'; + log("Server listening on {0}", ServerAddress); Server->Wait(); } @@ -191,10 +200,16 @@ llvm::sys::PrintStackTraceOnErrorSignal(argv[0]); if (!llvm::sys::path::is_absolute(IndexRoot)) { - elog("Index root should be an absolute path."); + llvm::errs() << "Index root should be an absolute path.\n"; return -1; } + llvm::errs().SetBuffered(); + // Don't flush stdout when logging for thread safety. + llvm::errs().tie(nullptr); + clang::clangd::StreamLogger Logger(llvm::errs(), LogLevel); + clang::clangd::LoggingSession LoggingSession(Logger); + llvm::Optional TracerStream; std::unique_ptr Tracer; if (!TraceFile.empty()) { @@ -220,7 +235,7 @@ std::unique_ptr Index = openIndex(IndexPath); if (!Index) { - elog("Failed to open the index."); + llvm::errs() << "Failed to open the index.\n"; return -1; } diff --git a/clang-tools-extra/clangd/indexer/IndexerMain.cpp b/clang-tools-extra/clangd/indexer/IndexerMain.cpp --- a/clang-tools-extra/clangd/indexer/IndexerMain.cpp +++ b/clang-tools-extra/clangd/indexer/IndexerMain.cpp @@ -16,6 +16,7 @@ #include "index/Serialization.h" #include "index/Symbol.h" #include "index/SymbolCollector.h" +#include "support/Logger.h" #include "clang/Tooling/ArgumentsAdjusters.h" #include "clang/Tooling/CommonOptionsParser.h" #include "clang/Tooling/Execution.h" @@ -122,7 +123,7 @@ std::make_unique(Data), clang::tooling::getStripPluginsAdjuster()); if (Err) { - llvm::errs() << llvm::toString(std::move(Err)) << "\n"; + clang::clangd::elog("{0}", std::move(Err)); } // Emit collected data. diff --git a/clang-tools-extra/clangd/tool/ClangdMain.cpp b/clang-tools-extra/clangd/tool/ClangdMain.cpp --- a/clang-tools-extra/clangd/tool/ClangdMain.cpp +++ b/clang-tools-extra/clangd/tool/ClangdMain.cpp @@ -657,16 +657,16 @@ // continuing. llvm::SmallString<128> Path(CompileCommandsDir); if (std::error_code EC = llvm::sys::fs::make_absolute(Path)) { - llvm::errs() << "Error while converting the relative path specified by " - "--compile-commands-dir to an absolute path: " - << EC.message() << ". The argument will be ignored.\n"; + elog("Error while converting the relative path specified by " + "--compile-commands-dir to an absolute path: {0}. The argument " + "will be ignored.", + EC.message()); } else { CompileCommandsDirPath = std::string(Path.str()); } } else { - llvm::errs() - << "Path specified by --compile-commands-dir does not exist. The " - "argument will be ignored.\n"; + elog("Path specified by --compile-commands-dir does not exist. The " + "argument will be ignored."); } } @@ -750,7 +750,7 @@ elog("Couldn't determine user config file, not loading"); } std::vector ProviderPointers; - for (const auto& P : ProviderStack) + for (const auto &P : ProviderStack) ProviderPointers.push_back(P.get()); Config = config::Provider::combine(std::move(ProviderPointers)); Opts.ConfigProvider = Config.get();