simplify logger initilization (#5078)

This commit is contained in:
Giorgi Guliashvili 2018-08-29 00:21:59 +01:00 committed by GitHub
parent 8702537fe9
commit a06af88b18
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 47 deletions

View File

@ -392,6 +392,11 @@ Initializer::Initializer(int& argc, char**& argv, ToolType tool)
FLAGS_disable_watchdog = true;
}
if (isWatcher()) {
FLAGS_disable_database = true;
FLAGS_disable_logging = true;
}
// Initialize the status and results logger.
initStatusLogger(binary_, init_glog);
if (kToolType != ToolType::EXTENSION) {
@ -486,8 +491,6 @@ void Initializer::initShell() const {
void Initializer::initWatcher() const {
// The watcher should not log into or use a persistent database.
if (isWatcher()) {
FLAGS_disable_database = true;
FLAGS_disable_logging = true;
DatabasePlugin::setAllowOpen(true);
DatabasePlugin::initPlugin();
}

View File

@ -126,9 +126,6 @@ class BufferedLogSink : public google::LogSink, private boost::noncopyable {
/// Accessor/mutator to dump all of the buffered logs.
std::vector<StatusLogLine>& dump();
/// Remove the buffered log sink from Glog.
void disable();
/// Add the buffered log sink to Glog.
void enable();
@ -165,7 +162,7 @@ class BufferedLogSink : public google::LogSink, private boost::noncopyable {
/// Create the log sink as buffering or forwarding.
BufferedLogSink() = default;
/// Remove the log sink.
/// Stop the log sink.
~BufferedLogSink();
private:
@ -180,14 +177,8 @@ class BufferedLogSink : public google::LogSink, private boost::noncopyable {
*/
std::atomic<bool> enabled_{false};
/// Boolean to help the logger disabler, no need to take action if not active.
bool active_{false};
/// Track multiple loggers that should receive sinks from the send forwarder.
std::vector<std::string> sinks_;
/// Mutex protecting activation and enabling of the buffered status logger.
Mutex enable_mutex_;
};
/// Mutex protecting accesses to buffered status logs.
@ -278,12 +269,13 @@ void initStatusLogger(const std::string& name, bool init_glog) {
if (init_glog) {
google::InitGoogleLogging(name.c_str());
}
if (!FLAGS_disable_logging) {
BufferedLogSink::get().setUp();
}
}
void initLogger(const std::string& name) {
// Stop the buffering sink and store the intermediate logs.
BufferedLogSink::get().disable();
BufferedLogSink::get().resetPlugins();
bool forward = false;
@ -324,36 +316,11 @@ BufferedLogSink& BufferedLogSink::get() {
}
void BufferedLogSink::setUp() {
WriteLock lock(enable_mutex_);
if (!active_) {
active_ = true;
google::AddLogSink(&get());
}
}
void BufferedLogSink::disable() {
WriteLock lock(enable_mutex_);
if (enabled_) {
enabled_ = false;
if (active_) {
active_ = false;
google::RemoveLogSink(&get());
}
}
}
void BufferedLogSink::enable() {
WriteLock lock(enable_mutex_);
if (!enabled_) {
enabled_ = true;
if (!active_) {
active_ = true;
google::AddLogSink(&get());
}
}
}
void BufferedLogSink::send(google::LogSeverity severity,
@ -363,10 +330,6 @@ void BufferedLogSink::send(google::LogSeverity severity,
const struct ::tm* tm_time,
const char* message,
size_t message_len) {
if (FLAGS_disable_logging) {
return;
}
// WARNING, be extremely careful when accessing data here.
// This should not cause any persistent storage or logging actions.
{
@ -423,7 +386,7 @@ const std::vector<std::string>& BufferedLogSink::enabledPlugins() const {
}
BufferedLogSink::~BufferedLogSink() {
disable();
enabled_ = false;
}
Status LoggerPlugin::call(const PluginRequest& request,