[clangd] Introduce a log-prefix flag to remote-index-server

Differential Revision: https://reviews.llvm.org/D104843
This commit is contained in:
Kadir Cetinkaya 2021-06-24 11:38:23 +02:00
parent 3aa6ca8def
commit 8f2bf93b5b
No known key found for this signature in database
GPG key ID: E39E36B8D2057ED6
2 changed files with 64 additions and 17 deletions

View file

@ -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;

View file

@ -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"}