mirror of
https://github.com/valitydev/osquery-1.git
synced 2024-11-07 01:55:20 +00:00
simplify logger initilization (#5078)
This commit is contained in:
parent
8702537fe9
commit
a06af88b18
@ -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();
|
||||
}
|
||||
|
@ -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());
|
||||
}
|
||||
BufferedLogSink::get().setUp();
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
google::AddLogSink(&get());
|
||||
}
|
||||
|
||||
void BufferedLogSink::enable() {
|
||||
WriteLock lock(enable_mutex_);
|
||||
|
||||
if (!enabled_) {
|
||||
enabled_ = true;
|
||||
if (!active_) {
|
||||
active_ = true;
|
||||
google::AddLogSink(&get());
|
||||
}
|
||||
}
|
||||
enabled_ = true;
|
||||
}
|
||||
|
||||
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,
|
||||
|
Loading…
Reference in New Issue
Block a user