2017-09-20 15:24:15 +08:00
|
|
|
//===--- Logger.cpp - Logger interface for clangd -------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Logger.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] Pass Context implicitly using TLS.
Summary:
Instead of passing Context explicitly around, we now have a thread-local
Context object `Context::current()` which is an implicit argument to
every function.
Most manipulation of this should use the WithContextValue helper, which
augments the current Context to add a single KV pair, and restores the
old context on destruction.
Advantages are:
- less boilerplate in functions that just propagate contexts
- reading most code doesn't require understanding context at all, and
using context as values in fewer places still
- fewer options to pass the "wrong" context when it changes within a
scope (e.g. when using Span)
- contexts pass through interfaces we can't modify, such as VFS
- propagating contexts across threads was slightly tricky (e.g.
copy vs move, no move-init in lambdas), and is now encapsulated in
the threadpool
Disadvantages are all the usual TLS stuff - hidden magic, and
potential for higher memory usage on threads that don't use the
context. (In practice, it's just one pointer)
Reviewers: ilya-biryukov
Subscribers: klimek, jkorous-apple, ioeric, cfe-commits
Differential Revision: https://reviews.llvm.org/D42517
llvm-svn: 323872
2018-01-31 21:40:48 +08:00
|
|
|
void log(const llvm::Twine &Message) {
|
2018-01-18 18:24:01 +08:00
|
|
|
if (L)
|
[clangd] Pass Context implicitly using TLS.
Summary:
Instead of passing Context explicitly around, we now have a thread-local
Context object `Context::current()` which is an implicit argument to
every function.
Most manipulation of this should use the WithContextValue helper, which
augments the current Context to add a single KV pair, and restores the
old context on destruction.
Advantages are:
- less boilerplate in functions that just propagate contexts
- reading most code doesn't require understanding context at all, and
using context as values in fewer places still
- fewer options to pass the "wrong" context when it changes within a
scope (e.g. when using Span)
- contexts pass through interfaces we can't modify, such as VFS
- propagating contexts across threads was slightly tricky (e.g.
copy vs move, no move-init in lambdas), and is now encapsulated in
the threadpool
Disadvantages are all the usual TLS stuff - hidden magic, and
potential for higher memory usage on threads that don't use the
context. (In practice, it's just one pointer)
Reviewers: ilya-biryukov
Subscribers: klimek, jkorous-apple, ioeric, cfe-commits
Differential Revision: https://reviews.llvm.org/D42517
llvm-svn: 323872
2018-01-31 21:40:48 +08:00
|
|
|
L->log(Message);
|
2018-01-18 18:24:01 +08:00
|
|
|
else {
|
|
|
|
static std::mutex Mu;
|
|
|
|
std::lock_guard<std::mutex> Guard(Mu);
|
|
|
|
llvm::errs() << Message << "\n";
|
|
|
|
}
|
2017-09-20 15:24:15 +08:00
|
|
|
}
|
|
|
|
|
2017-12-13 20:51:22 +08:00
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|