Author: Kadir Cetinkaya Date: 2021-06-25T16:51:29+02:00 New Revision: 8f2bf93b5bd6a1d31e28e4144827b3f6c413ac85
URL: https://github.com/llvm/llvm-project/commit/8f2bf93b5bd6a1d31e28e4144827b3f6c413ac85 DIFF: https://github.com/llvm/llvm-project/commit/8f2bf93b5bd6a1d31e28e4144827b3f6c413ac85.diff LOG: [clangd] Introduce a log-prefix flag to remote-index-server Differential Revision: https://reviews.llvm.org/D104843 Added: clang-tools-extra/clangd/test/remote-index/log-prefix.test Modified: clang-tools-extra/clangd/index/remote/server/Server.cpp Removed: ################################################################################ diff --git a/clang-tools-extra/clangd/index/remote/server/Server.cpp b/clang-tools-extra/clangd/index/remote/server/Server.cpp index 4f4e80b8a4d74..04ad0b2a1936f 100644 --- a/clang-tools-extra/clangd/index/remote/server/Server.cpp +++ b/clang-tools-extra/clangd/index/remote/server/Server.cpp @@ -37,7 +37,9 @@ #include <grpc++/grpc++.h> #include <grpc++/health_check_service_interface.h> #include <memory> +#include <string> #include <thread> +#include <utility> #if ENABLE_GRPC_REFLECTION #include <grpc++/ext/proto_server_reflection_plugin.h> @@ -78,6 +80,12 @@ llvm::cl::opt<bool> LogPublic{ llvm::cl::init(false), }; +llvm::cl::opt<std::string> LogPrefix{ + "log-prefix", + llvm::cl::desc("A string that'll be prepended to all log statements. " + "Useful when running multiple instances on same host."), +}; + llvm::cl::opt<std::string> TraceFile( "trace-file", llvm::cl::desc("Path to the file where tracer logs will be stored")); @@ -427,27 +435,48 @@ void runServerAndWait(clangd::SymbolIndex &Index, llvm::StringRef ServerAddress, ServerShutdownWatcher.join(); } -std::unique_ptr<Logger> makeLogger(llvm::raw_ostream &OS) { - if (!LogPublic) - return std::make_unique<StreamLogger>(OS, LogLevel); - // Redacted mode: - // - messages outside the scope of a request: log fully - // - messages tagged [public]: log fully - // - errors: log the format string - // - others: drop - class RedactedLogger : public StreamLogger { +std::unique_ptr<Logger> makeLogger(llvm::StringRef LogPrefix, + llvm::raw_ostream &OS) { + std::unique_ptr<Logger> Base; + if (LogPublic) { + // Redacted mode: + // - messages outside the scope of a request: log fully + // - messages tagged [public]: log fully + // - errors: log the format string + // - others: drop + class RedactedLogger : public StreamLogger { + public: + using StreamLogger::StreamLogger; + void log(Level L, const char *Fmt, + const llvm::formatv_object_base &Message) override { + if (Context::current().get(CurrentRequest) == nullptr || + llvm::StringRef(Fmt).startswith("[public]")) + return StreamLogger::log(L, Fmt, Message); + if (L >= Error) + return StreamLogger::log(L, Fmt, + llvm::formatv("[redacted] {0}", Fmt)); + } + }; + Base = std::make_unique<RedactedLogger>(OS, LogLevel); + } else { + Base = std::make_unique<StreamLogger>(OS, LogLevel); + } + + if (LogPrefix.empty()) + return Base; + class PrefixedLogger : public Logger { + std::string LogPrefix; + std::unique_ptr<Logger> Base; + public: - using StreamLogger::StreamLogger; + PrefixedLogger(llvm::StringRef LogPrefix, std::unique_ptr<Logger> Base) + : LogPrefix(LogPrefix.str()), Base(std::move(Base)) {} void log(Level L, const char *Fmt, const llvm::formatv_object_base &Message) override { - if (Context::current().get(CurrentRequest) == nullptr || - llvm::StringRef(Fmt).startswith("[public]")) - return StreamLogger::log(L, Fmt, Message); - if (L >= Error) - return StreamLogger::log(L, Fmt, llvm::formatv("[redacted] {0}", Fmt)); + Base->log(L, Fmt, llvm::formatv("[{0}] {1}", LogPrefix, Message)); } }; - return std::make_unique<RedactedLogger>(OS, LogLevel); + return std::make_unique<PrefixedLogger>(LogPrefix, std::move(Base)); } } // namespace @@ -471,7 +500,7 @@ int main(int argc, char *argv[]) { llvm::errs().SetBuffered(); // Don't flush stdout when logging for thread safety. llvm::errs().tie(nullptr); - auto Logger = makeLogger(llvm::errs()); + auto Logger = makeLogger(LogPrefix.getValue(), llvm::errs()); clang::clangd::LoggingSession LoggingSession(*Logger); llvm::Optional<llvm::raw_fd_ostream> TracerStream; diff --git a/clang-tools-extra/clangd/test/remote-index/log-prefix.test b/clang-tools-extra/clangd/test/remote-index/log-prefix.test new file mode 100644 index 0000000000000..fb00576bbd5c4 --- /dev/null +++ b/clang-tools-extra/clangd/test/remote-index/log-prefix.test @@ -0,0 +1,18 @@ +# RUN: rm -rf %t +# RUN: clangd-indexer %S/Inputs/Source.cpp > %t.idx +# RUN: %python %S/pipeline_helper.py --input-file-name=%s --server-arg=--log=verbose --server-arg=-log-prefix=test-prefix --server-log=%t.log --project-root=%S --index-file=%t.idx > /dev/null +# RUN: FileCheck %s < %t.log +# REQUIRES: clangd-remote-index + +# CHECK: [test-prefix] Server listening on +{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":123,"rootPath":"clangd","capabilities":{},"trace":"off"}} +--- +{"jsonrpc":"2.0","id":1,"method":"workspace/symbol","params":{"query":"gFoo"}} +# CHECK: [test-prefix] <<< FuzzyFindRequest +# CHECK: [test-prefix] >>> FuzzyFindReply +# CHECK: [test-prefix] [public] request v1/FuzzyFind +--- +{"jsonrpc":"2.0","id":4,"method":"shutdown"} +--- +{"jsonrpc":"2.0","method":"exit"} + _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits