From fb22678cd67801e41af4917acceb4014ab5d007e Mon Sep 17 00:00:00 2001 From: Kirill Bobyrev Date: Wed, 29 Jul 2020 01:40:46 +0200 Subject: [PATCH] [clangd] Use elog instead of llvm::errs, log instead of llvm::outs Reviewed By: sammccall Differential Revision: https://reviews.llvm.org/D84697 --- .../clangd/index/remote/server/Server.cpp | 21 ++++++++++++++++--- .../clangd/indexer/IndexerMain.cpp | 3 ++- clang-tools-extra/clangd/tool/ClangdMain.cpp | 14 ++++++------- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/clang-tools-extra/clangd/index/remote/server/Server.cpp b/clang-tools-extra/clangd/index/remote/server/Server.cpp index 364e4db6503c2..c3f9efeb7ec05 100644 --- 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 IndexPath(llvm::cl::desc(""), 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 @@ void runServer(std::unique_ptr Index, 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 @@ int main(int argc, char *argv[]) { 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 @@ int main(int argc, char *argv[]) { 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 index dac038308d9e9..46224238c3fca 100644 --- 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 @@ int main(int argc, const char **argv) { 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 index 0d4267774c92f..daf87d11c3843 100644 --- a/clang-tools-extra/clangd/tool/ClangdMain.cpp +++ b/clang-tools-extra/clangd/tool/ClangdMain.cpp @@ -657,16 +657,16 @@ clangd accepts flags on the commandline, and in the CLANGD_FLAGS environment var // 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 @@ clangd accepts flags on the commandline, and in the CLANGD_FLAGS environment var 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();