2017-09-20 15:24:15 +08:00
|
|
|
//===--- Logger.cpp - Logger interface for clangd -------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-09-20 15:24:15 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
[clangd] Move non-clang base pieces into separate support/ lib. NFCI
Summary:
This enforces layering, reduces a sprawling clangd/ directory, and makes life
easier for embedders.
Reviewers: kbobyrev
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D79014
2020-04-28 23:49:17 +08:00
|
|
|
#include "support/Logger.h"
|
|
|
|
#include "support/Trace.h"
|
2018-10-17 15:39:32 +08:00
|
|
|
#include "llvm/Support/Chrono.h"
|
[clangd] Add error() function for creating formatv-style llvm::Errors. NFC
Summary:
This is considerably terser than the makeStringError and friends, and
avoids verbosity cliffs that discourage adding log information.
It follows the syntax used in log/elog/vlog/dlog that have been successful.
The main caveats are:
- it's strictly out-of-place in logger.h, though kind of fits thematically and
in implementation
- it claims the "error" identifier, which seems a bit too opinionated
to put higher up in llvm
I've updated some users of StringError mostly at random - there are lots
more mechanical changes but I'd like to get this reviewed before making
them all.
Reviewers: kbobyrev, hokein
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, kadircet, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D83419
2020-07-09 03:49:38 +08:00
|
|
|
#include "llvm/Support/Error.h"
|
2018-10-17 15:39:32 +08:00
|
|
|
#include "llvm/Support/FormatVariadic.h"
|
2018-01-18 18:24:01 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <mutex>
|
2017-09-20 15:24:15 +08:00
|
|
|
|
2017-12-13 20:51:22 +08:00
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
2017-09-20 15:24:15 +08:00
|
|
|
|
2017-12-13 20:51:22 +08:00
|
|
|
namespace {
|
|
|
|
Logger *L = nullptr;
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
LoggingSession::LoggingSession(clangd::Logger &Instance) {
|
|
|
|
assert(!L);
|
|
|
|
L = &Instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
LoggingSession::~LoggingSession() { L = nullptr; }
|
|
|
|
|
[clangd] Add -log=public to redact all request info from index server logs
The index server has access to potentially-sensitive information, e.g. a
sequence of fuzzyFind requests reveals details about code completions in the
editor which in turn reveals details about the code being edited.
This information is necessary to provide the service, and our intention[1] is it
should never be retained beyond the scope of the request (e.g. not logged).
At the same time, some log messages should be exposed:
- server startup/index reloads etc that don't pertain to a user request
- basic request logs (method, latency, #results, error code) for monitoring
- errors while handling requests, without request-specific data
The -log=public design accommodates these by allowing three types of logs:
- those not associated with any user RPC request (via context-propagation)
- those explicitly tagged as [public] in the log line
- logging of format strings only, with no interpolated data (error level only)
[1] Specifically: Google is likely to run public instances of this server
for LLVM and potentially other projects, they will run in this configuration.
The details agreed in a Google-internal privacy review.
As clangd developers, we'd encourage others to use this configuration for public
instances too.
Differential Revision: https://reviews.llvm.org/D90526
2020-10-31 18:30:26 +08:00
|
|
|
void detail::logImpl(Logger::Level Level, const char *Fmt,
|
|
|
|
const llvm::formatv_object_base &Message) {
|
2018-01-18 18:24:01 +08:00
|
|
|
if (L)
|
[clangd] Add -log=public to redact all request info from index server logs
The index server has access to potentially-sensitive information, e.g. a
sequence of fuzzyFind requests reveals details about code completions in the
editor which in turn reveals details about the code being edited.
This information is necessary to provide the service, and our intention[1] is it
should never be retained beyond the scope of the request (e.g. not logged).
At the same time, some log messages should be exposed:
- server startup/index reloads etc that don't pertain to a user request
- basic request logs (method, latency, #results, error code) for monitoring
- errors while handling requests, without request-specific data
The -log=public design accommodates these by allowing three types of logs:
- those not associated with any user RPC request (via context-propagation)
- those explicitly tagged as [public] in the log line
- logging of format strings only, with no interpolated data (error level only)
[1] Specifically: Google is likely to run public instances of this server
for LLVM and potentially other projects, they will run in this configuration.
The details agreed in a Google-internal privacy review.
As clangd developers, we'd encourage others to use this configuration for public
instances too.
Differential Revision: https://reviews.llvm.org/D90526
2020-10-31 18:30:26 +08:00
|
|
|
L->log(Level, Fmt, Message);
|
2018-01-18 18:24:01 +08:00
|
|
|
else {
|
|
|
|
static std::mutex Mu;
|
|
|
|
std::lock_guard<std::mutex> Guard(Mu);
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::errs() << Message << "\n";
|
2018-01-18 18:24:01 +08:00
|
|
|
}
|
2017-09-20 15:24:15 +08:00
|
|
|
}
|
|
|
|
|
[clangd] Upgrade logging facilities with levels and formatv.
Summary:
log() is split into four functions:
- elog()/log()/vlog() have different severity levels, allowing filtering
- dlog() is a lazy macro which uses LLVM_DEBUG - it logs to the logger, but
conditionally based on -debug-only flag and is omitted in release builds
All logging functions use formatv-style format strings now, e.g:
log("Could not resolve URI {0}: {1}", URI, Result.takeError());
Existing log sites have been split between elog/log/vlog by best guess.
This includes a workaround for passing Error to formatv that can be
simplified when D49170 or similar lands.
Subscribers: ilya-biryukov, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D49008
llvm-svn: 336785
2018-07-11 18:35:11 +08:00
|
|
|
const char *detail::debugType(const char *Filename) {
|
|
|
|
if (const char *Slash = strrchr(Filename, '/'))
|
|
|
|
return Slash + 1;
|
|
|
|
if (const char *Backslash = strrchr(Filename, '\\'))
|
|
|
|
return Backslash + 1;
|
|
|
|
return Filename;
|
|
|
|
}
|
|
|
|
|
[clangd] Add -log=public to redact all request info from index server logs
The index server has access to potentially-sensitive information, e.g. a
sequence of fuzzyFind requests reveals details about code completions in the
editor which in turn reveals details about the code being edited.
This information is necessary to provide the service, and our intention[1] is it
should never be retained beyond the scope of the request (e.g. not logged).
At the same time, some log messages should be exposed:
- server startup/index reloads etc that don't pertain to a user request
- basic request logs (method, latency, #results, error code) for monitoring
- errors while handling requests, without request-specific data
The -log=public design accommodates these by allowing three types of logs:
- those not associated with any user RPC request (via context-propagation)
- those explicitly tagged as [public] in the log line
- logging of format strings only, with no interpolated data (error level only)
[1] Specifically: Google is likely to run public instances of this server
for LLVM and potentially other projects, they will run in this configuration.
The details agreed in a Google-internal privacy review.
As clangd developers, we'd encourage others to use this configuration for public
instances too.
Differential Revision: https://reviews.llvm.org/D90526
2020-10-31 18:30:26 +08:00
|
|
|
void StreamLogger::log(Logger::Level Level, const char *Fmt,
|
2019-01-07 23:45:19 +08:00
|
|
|
const llvm::formatv_object_base &Message) {
|
2018-10-17 15:39:32 +08:00
|
|
|
if (Level < MinLevel)
|
|
|
|
return;
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::sys::TimePoint<> Timestamp = std::chrono::system_clock::now();
|
2018-10-17 15:39:32 +08:00
|
|
|
trace::log(Message);
|
|
|
|
std::lock_guard<std::mutex> Guard(StreamMutex);
|
2019-01-07 23:45:19 +08:00
|
|
|
Logs << llvm::formatv("{0}[{1:%H:%M:%S.%L}] {2}\n", indicator(Level),
|
|
|
|
Timestamp, Message);
|
2018-10-17 15:39:32 +08:00
|
|
|
Logs.flush();
|
|
|
|
}
|
|
|
|
|
[clangd] Add error() function for creating formatv-style llvm::Errors. NFC
Summary:
This is considerably terser than the makeStringError and friends, and
avoids verbosity cliffs that discourage adding log information.
It follows the syntax used in log/elog/vlog/dlog that have been successful.
The main caveats are:
- it's strictly out-of-place in logger.h, though kind of fits thematically and
in implementation
- it claims the "error" identifier, which seems a bit too opinionated
to put higher up in llvm
I've updated some users of StringError mostly at random - there are lots
more mechanical changes but I'd like to get this reviewed before making
them all.
Reviewers: kbobyrev, hokein
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, kadircet, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D83419
2020-07-09 03:49:38 +08:00
|
|
|
namespace {
|
|
|
|
// Like llvm::StringError but with fewer options and no gratuitous copies.
|
|
|
|
class SimpleStringError : public llvm::ErrorInfo<SimpleStringError> {
|
|
|
|
std::error_code EC;
|
|
|
|
std::string Message;
|
|
|
|
|
|
|
|
public:
|
|
|
|
SimpleStringError(std::error_code EC, std::string &&Message)
|
|
|
|
: EC(EC), Message(std::move(Message)) {}
|
|
|
|
void log(llvm::raw_ostream &OS) const override { OS << Message; }
|
|
|
|
std::string message() const override { return Message; }
|
|
|
|
std::error_code convertToErrorCode() const override { return EC; }
|
|
|
|
static char ID;
|
|
|
|
};
|
|
|
|
char SimpleStringError::ID;
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
llvm::Error detail::error(std::error_code EC, std::string &&Msg) {
|
|
|
|
return llvm::make_error<SimpleStringError>(EC, std::move(Msg));
|
|
|
|
}
|
|
|
|
|
2017-12-13 20:51:22 +08:00
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|