77 lines · cpp
1//===--- Monitor.cpp - Request server monitoring information through CLI --===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "MonitoringService.grpc.pb.h"10#include "MonitoringService.pb.h"11 12#include "support/Logger.h"13#include "clang/Basic/Version.h"14#include "llvm/Support/CommandLine.h"15#include "llvm/Support/Signals.h"16 17#include <chrono>18#include <google/protobuf/util/json_util.h>19#include <grpc++/grpc++.h>20 21namespace clang {22namespace clangd {23namespace remote {24namespace {25 26static constexpr char Overview[] = R"(27This tool requests monitoring information (uptime, index freshness) from the28server and prints it to stdout.29)";30 31llvm::cl::opt<std::string>32 ServerAddress("server-address", llvm::cl::Positional,33 llvm::cl::desc("Address of the invoked server."),34 llvm::cl::Required);35 36} // namespace37} // namespace remote38} // namespace clangd39} // namespace clang40 41int main(int argc, char *argv[]) {42 using namespace clang::clangd::remote;43 llvm::cl::ParseCommandLineOptions(argc, argv, Overview);44 llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);45 46 const auto Channel =47 grpc::CreateChannel(ServerAddress, grpc::InsecureChannelCredentials());48 const auto Stub = clang::clangd::remote::v1::Monitor::NewStub(Channel);49 grpc::ClientContext Context;50 Context.set_deadline(std::chrono::system_clock::now() +51 std::chrono::seconds(10));52 Context.AddMetadata("version", clang::getClangToolFullVersion("clangd"));53 const clang::clangd::remote::v1::MonitoringInfoRequest Request;54 clang::clangd::remote::v1::MonitoringInfoReply Response;55 const auto Status = Stub->MonitoringInfo(&Context, Request, &Response);56 if (!Status.ok()) {57 clang::clangd::elog("Can not request monitoring information ({0}): {1}\n",58 Status.error_code(), Status.error_message());59 return -1;60 }61 std::string Output;62 google::protobuf::util::JsonPrintOptions Options;63 Options.add_whitespace = true;64 Options.always_print_primitive_fields = true;65 Options.preserve_proto_field_names = true;66 const auto JsonStatus =67 google::protobuf::util::MessageToJsonString(Response, &Output, Options);68 if (!JsonStatus.ok()) {69 clang::clangd::elog("Can not convert response ({0}) to JSON ({1}): {2}\n",70 Response.DebugString(),71 static_cast<int>(JsonStatus.code()),72 JsonStatus.message().as_string());73 return -1;74 }75 llvm::outs() << Output;76}77