2014-12-18 18:50:47 +00:00
|
|
|
/*
|
2016-02-11 19:48:58 +00:00
|
|
|
* Copyright (c) 2014-present, Facebook, Inc.
|
2014-12-18 18:50:47 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
2015-05-12 06:31:13 +00:00
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
2014-12-18 18:50:47 +00:00
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*
|
|
|
|
*/
|
2014-07-31 00:35:19 +00:00
|
|
|
|
2016-07-07 22:16:28 +00:00
|
|
|
#ifndef WIN32
|
|
|
|
#include <syslog.h>
|
|
|
|
#endif
|
|
|
|
|
2014-07-31 00:35:19 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <thread>
|
|
|
|
|
2015-07-01 22:26:26 +00:00
|
|
|
#include <boost/noncopyable.hpp>
|
2015-02-16 02:15:06 +00:00
|
|
|
|
2016-05-14 02:48:40 +00:00
|
|
|
#include <osquery/events.h>
|
2015-07-01 22:26:26 +00:00
|
|
|
#include <osquery/extensions.h>
|
2015-02-16 02:15:06 +00:00
|
|
|
#include <osquery/filesystem.h>
|
2014-12-03 23:14:02 +00:00
|
|
|
#include <osquery/flags.h>
|
|
|
|
#include <osquery/logger.h>
|
2014-07-31 00:35:19 +00:00
|
|
|
|
2016-02-12 17:39:20 +00:00
|
|
|
#include "osquery/core/conversions.h"
|
2016-09-02 22:04:03 +00:00
|
|
|
#include "osquery/core/json.h"
|
2016-02-12 17:39:20 +00:00
|
|
|
|
2015-02-16 02:15:06 +00:00
|
|
|
namespace pt = boost::property_tree;
|
|
|
|
|
2014-08-15 07:25:30 +00:00
|
|
|
namespace osquery {
|
2014-07-31 00:35:19 +00:00
|
|
|
|
2015-02-17 08:36:20 +00:00
|
|
|
FLAG(bool, verbose, false, "Enable verbose informational messages");
|
|
|
|
FLAG_ALIAS(bool, verbose_debug, verbose);
|
|
|
|
FLAG_ALIAS(bool, debug, verbose);
|
|
|
|
|
2015-07-18 22:08:51 +00:00
|
|
|
/// Despite being a configurable option, this is only read/used at load.
|
2015-02-17 08:36:20 +00:00
|
|
|
FLAG(bool, disable_logging, false, "Disable ERROR/INFO logging");
|
|
|
|
|
2015-03-03 23:03:14 +00:00
|
|
|
FLAG(string, logger_plugin, "filesystem", "Logger plugin name");
|
2015-02-17 08:36:20 +00:00
|
|
|
|
2016-08-29 13:59:55 +00:00
|
|
|
FLAG(bool, logger_event_type, true, "Log scheduled results as events");
|
|
|
|
FLAG_ALIAS(bool, log_result_events, logger_event_type);
|
2015-02-16 02:15:06 +00:00
|
|
|
|
2016-03-27 06:20:41 +00:00
|
|
|
class LoggerDisabler;
|
|
|
|
|
2015-02-16 02:15:06 +00:00
|
|
|
/**
|
|
|
|
* @brief A custom Glog log sink for forwarding or buffering status logs.
|
|
|
|
*
|
|
|
|
* This log sink has two modes, it can buffer Glog status logs until an osquery
|
|
|
|
* logger is initialized or forward Glog status logs to an initialized and
|
|
|
|
* appropriate logger. The appropriateness is determined by the logger when its
|
|
|
|
* LoggerPlugin::init method is called. If the `init` method returns success
|
|
|
|
* then a BufferedLogSink will start forwarding status logs to
|
|
|
|
* LoggerPlugin::logStatus.
|
|
|
|
*
|
|
|
|
* This facility will start buffering when first used and stop buffering
|
|
|
|
* (aka remove itself as a Glog sink) using the exposed APIs. It will live
|
|
|
|
* throughout the life of the process for two reasons: (1) It makes sense when
|
|
|
|
* the active logger plugin is handling Glog status logs and (2) it must remove
|
|
|
|
* itself as a Glog target.
|
|
|
|
*/
|
2015-07-01 22:26:26 +00:00
|
|
|
class BufferedLogSink : public google::LogSink, private boost::noncopyable {
|
2015-02-16 02:15:06 +00:00
|
|
|
public:
|
|
|
|
/// We create this as a Singleton for proper disable/shutdown.
|
|
|
|
static BufferedLogSink& instance() {
|
|
|
|
static BufferedLogSink sink;
|
|
|
|
return sink;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The Glog-API LogSink call-in method.
|
|
|
|
void send(google::LogSeverity severity,
|
|
|
|
const char* full_filename,
|
|
|
|
const char* base_filename,
|
|
|
|
int line,
|
|
|
|
const struct ::tm* tm_time,
|
|
|
|
const char* message,
|
|
|
|
size_t message_len);
|
|
|
|
|
2015-11-24 05:52:00 +00:00
|
|
|
public:
|
2015-02-16 02:15:06 +00:00
|
|
|
/// Accessor/mutator to dump all of the buffered logs.
|
2016-08-29 13:59:55 +00:00
|
|
|
static std::vector<StatusLogLine>& dump() {
|
|
|
|
return instance().logs_;
|
|
|
|
}
|
2015-02-16 02:15:06 +00:00
|
|
|
|
|
|
|
/// Set the forwarding mode of the buffering sink.
|
2016-08-29 13:59:55 +00:00
|
|
|
static void forward(bool forward = false) {
|
|
|
|
instance().forward_ = forward;
|
|
|
|
}
|
2015-02-16 02:15:06 +00:00
|
|
|
|
|
|
|
/// Remove the buffered log sink from Glog.
|
|
|
|
static void disable() {
|
|
|
|
if (instance().enabled_) {
|
|
|
|
instance().enabled_ = false;
|
|
|
|
google::RemoveLogSink(&instance());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add the buffered log sink to Glog.
|
|
|
|
static void enable() {
|
|
|
|
if (!instance().enabled_) {
|
|
|
|
instance().enabled_ = true;
|
|
|
|
google::AddLogSink(&instance());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-24 05:52:00 +00:00
|
|
|
/**
|
|
|
|
* @brief Add a logger plugin that should receive status updates.
|
|
|
|
*
|
|
|
|
* Since the logger may support multiple active logger plugins the sink
|
|
|
|
* will keep track of those plugins that returned success after ::init.
|
|
|
|
* This list of plugins will received forwarded messages from the sink.
|
|
|
|
*
|
|
|
|
* This list is important because sending logs to plugins that also use
|
|
|
|
* and active Glog Sink (supports multiple) will create a logging loop.
|
|
|
|
*/
|
|
|
|
static void addPlugin(const std::string& name) {
|
|
|
|
instance().sinks_.push_back(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Retrieve the list of enabled plugins that should have logs forwarded.
|
|
|
|
static const std::vector<std::string>& enabledPlugins() {
|
|
|
|
return instance().sinks_;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
BufferedLogSink(BufferedLogSink const&) = delete;
|
|
|
|
void operator=(BufferedLogSink const&) = delete;
|
|
|
|
|
2015-02-16 02:15:06 +00:00
|
|
|
private:
|
|
|
|
/// Create the log sink as buffering or forwarding.
|
2015-02-17 00:37:39 +00:00
|
|
|
BufferedLogSink() : forward_(false), enabled_(false) {}
|
2015-02-16 02:15:06 +00:00
|
|
|
|
|
|
|
/// Remove the log sink.
|
2016-08-29 13:59:55 +00:00
|
|
|
~BufferedLogSink() {
|
|
|
|
disable();
|
|
|
|
}
|
2015-02-16 02:15:06 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
/// Intermediate log storage until an osquery logger is initialized.
|
|
|
|
std::vector<StatusLogLine> logs_;
|
2015-11-24 05:52:00 +00:00
|
|
|
|
|
|
|
/// Should the sending act in a forwarding mode.
|
|
|
|
bool forward_{false};
|
2016-08-29 13:59:55 +00:00
|
|
|
|
|
|
|
/// Is the logger temporarily disabled.
|
2015-11-24 05:52:00 +00:00
|
|
|
bool enabled_{false};
|
|
|
|
|
|
|
|
/// Track multiple loggers that should receive sinks from the send forwarder.
|
|
|
|
std::vector<std::string> sinks_;
|
2016-03-27 06:20:41 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
friend class LoggerDisabler;
|
2015-02-16 02:15:06 +00:00
|
|
|
};
|
|
|
|
|
2015-07-01 22:26:26 +00:00
|
|
|
/// Scoped helper to perform logging actions without races.
|
|
|
|
class LoggerDisabler {
|
|
|
|
public:
|
2016-03-27 06:20:41 +00:00
|
|
|
LoggerDisabler()
|
|
|
|
: stderr_status_(FLAGS_logtostderr),
|
|
|
|
enabled_(BufferedLogSink::instance().enabled_) {
|
2015-07-01 22:26:26 +00:00
|
|
|
BufferedLogSink::disable();
|
|
|
|
FLAGS_logtostderr = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
~LoggerDisabler() {
|
2016-03-27 06:20:41 +00:00
|
|
|
// Only enable if the sink was enabled when the disabler was requested.
|
|
|
|
if (enabled_) {
|
|
|
|
BufferedLogSink::enable();
|
|
|
|
}
|
2015-07-01 22:26:26 +00:00
|
|
|
FLAGS_logtostderr = stderr_status_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-08-29 13:59:55 +00:00
|
|
|
/// Value of the 'logtostderr' Glog status when constructed.
|
2015-07-01 22:26:26 +00:00
|
|
|
bool stderr_status_;
|
2016-08-29 13:59:55 +00:00
|
|
|
|
|
|
|
/// Value of the BufferedLogSink's enabled status when constructed.
|
2016-03-27 06:20:41 +00:00
|
|
|
bool enabled_;
|
2015-07-01 22:26:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void serializeIntermediateLog(const std::vector<StatusLogLine>& log,
|
|
|
|
PluginRequest& request) {
|
2015-02-16 02:15:06 +00:00
|
|
|
pt::ptree tree;
|
|
|
|
for (const auto& log_item : log) {
|
|
|
|
pt::ptree child;
|
|
|
|
child.put("s", log_item.severity);
|
|
|
|
child.put("f", log_item.filename);
|
|
|
|
child.put("i", log_item.line);
|
|
|
|
child.put("m", log_item.message);
|
|
|
|
tree.push_back(std::make_pair("", child));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the log as a request JSON string.
|
|
|
|
std::ostringstream output;
|
|
|
|
pt::write_json(output, tree, false);
|
|
|
|
request["log"] = output.str();
|
|
|
|
}
|
|
|
|
|
2015-07-01 22:26:26 +00:00
|
|
|
static void deserializeIntermediateLog(const PluginRequest& request,
|
|
|
|
std::vector<StatusLogLine>& log) {
|
2015-02-16 02:15:06 +00:00
|
|
|
if (request.count("log") == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the plugin request string into a JSON tree and enumerate.
|
|
|
|
pt::ptree tree;
|
2015-05-03 20:37:11 +00:00
|
|
|
try {
|
|
|
|
std::stringstream input;
|
|
|
|
input << request.at("log");
|
|
|
|
pt::read_json(input, tree);
|
2016-09-02 22:04:03 +00:00
|
|
|
} catch (const pt::json_parser::json_parser_error& /* e */) {
|
2015-05-03 20:37:11 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-02-16 02:15:06 +00:00
|
|
|
|
|
|
|
for (const auto& item : tree.get_child("")) {
|
|
|
|
log.push_back({
|
2015-03-30 19:49:57 +00:00
|
|
|
(StatusLogSeverity)item.second.get<int>("s", O_INFO),
|
|
|
|
item.second.get<std::string>("f", "<unknown>"),
|
2016-08-29 13:59:55 +00:00
|
|
|
item.second.get<int>("i", 0),
|
|
|
|
item.second.get<std::string>("m", ""),
|
2015-02-16 02:15:06 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-30 01:53:25 +00:00
|
|
|
void setVerboseLevel() {
|
|
|
|
if (Flag::getValue("verbose") == "true") {
|
2015-02-16 02:15:06 +00:00
|
|
|
// Turn verbosity up to 1.
|
|
|
|
// Do log DEBUG, INFO, WARNING, ERROR to their log files.
|
|
|
|
// Do log the above and verbose=1 to stderr.
|
2016-08-31 22:09:01 +00:00
|
|
|
FLAGS_minloglevel = google::GLOG_INFO;
|
|
|
|
FLAGS_stderrthreshold = google::GLOG_INFO;
|
2015-02-16 02:15:06 +00:00
|
|
|
FLAGS_v = 1;
|
|
|
|
} else {
|
|
|
|
// Do NOT log INFO, WARNING, ERROR to stderr.
|
|
|
|
// Do log only WARNING, ERROR to log sinks.
|
2016-08-31 22:09:01 +00:00
|
|
|
if (kToolType == ToolType::DAEMON) {
|
|
|
|
FLAGS_minloglevel = google::GLOG_INFO;
|
|
|
|
FLAGS_stderrthreshold = google::GLOG_INFO;
|
|
|
|
} else {
|
|
|
|
FLAGS_minloglevel = google::GLOG_WARNING;
|
|
|
|
FLAGS_stderrthreshold = google::GLOG_WARNING;
|
|
|
|
}
|
2015-02-16 02:15:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (FLAGS_disable_logging) {
|
|
|
|
// Do log ERROR to stderr.
|
|
|
|
// Do NOT log INFO, WARNING, ERROR to their log files.
|
|
|
|
FLAGS_logtostderr = true;
|
|
|
|
}
|
2015-04-30 01:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void initStatusLogger(const std::string& name) {
|
|
|
|
FLAGS_alsologtostderr = false;
|
2015-10-11 21:37:49 +00:00
|
|
|
FLAGS_colorlogtostderr = true;
|
2015-04-30 01:53:25 +00:00
|
|
|
FLAGS_logbufsecs = 0; // flush the log buffer immediately
|
|
|
|
FLAGS_stop_logging_if_full_disk = true;
|
|
|
|
FLAGS_max_log_size = 10; // max size for individual log file is 10MB
|
|
|
|
FLAGS_logtostderr = true;
|
2015-02-16 02:15:06 +00:00
|
|
|
|
2015-04-30 01:53:25 +00:00
|
|
|
setVerboseLevel();
|
2015-02-16 02:15:06 +00:00
|
|
|
// Start the logging, and announce the daemon is starting.
|
|
|
|
google::InitGoogleLogging(name.c_str());
|
|
|
|
|
|
|
|
// If logging is disabled then do not buffer intermediate logs.
|
|
|
|
if (!FLAGS_disable_logging) {
|
|
|
|
// Create an instance of the buffered log sink and do not forward logs yet.
|
2015-02-17 00:37:39 +00:00
|
|
|
BufferedLogSink::enable();
|
2015-02-16 02:15:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-29 13:59:55 +00:00
|
|
|
void initLogger(const std::string& name) {
|
2015-07-01 22:26:26 +00:00
|
|
|
// Check if logging is disabled, if so then no need to shuttle intermediates.
|
2015-02-16 02:15:06 +00:00
|
|
|
if (FLAGS_disable_logging) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-01 22:26:26 +00:00
|
|
|
// Stop the buffering sink and store the intermediate logs.
|
2015-02-16 02:15:06 +00:00
|
|
|
BufferedLogSink::disable();
|
|
|
|
auto intermediate_logs = std::move(BufferedLogSink::dump());
|
2016-08-29 13:59:55 +00:00
|
|
|
|
2015-12-03 08:53:54 +00:00
|
|
|
// Start the custom status logging facilities, which may instruct Glog as is
|
|
|
|
// the case with filesystem logging.
|
2016-08-29 13:59:55 +00:00
|
|
|
PluginRequest init_request = {{"init", name}};
|
|
|
|
serializeIntermediateLog(intermediate_logs, init_request);
|
|
|
|
if (!init_request["log"].empty()) {
|
|
|
|
init_request["log"].pop_back();
|
2015-12-03 08:53:54 +00:00
|
|
|
}
|
|
|
|
|
2016-08-29 13:59:55 +00:00
|
|
|
bool forward = false;
|
|
|
|
PluginRequest features_request = {{"action", "features"}};
|
2015-11-24 05:52:00 +00:00
|
|
|
const auto& logger_plugin = Registry::getActive("logger");
|
|
|
|
// Allow multiple loggers, make sure each is accessible.
|
|
|
|
for (const auto& logger : osquery::split(logger_plugin, ",")) {
|
|
|
|
if (!Registry::exists("logger", logger)) {
|
|
|
|
continue;
|
|
|
|
}
|
2015-02-16 02:15:06 +00:00
|
|
|
|
2016-08-29 13:59:55 +00:00
|
|
|
Registry::call("logger", logger, init_request);
|
|
|
|
auto status = Registry::call("logger", logger, features_request);
|
|
|
|
if ((status.getCode() & LOGGER_FEATURE_LOGSTATUS) > 0) {
|
|
|
|
// Glog status logs are forwarded to logStatus.
|
|
|
|
forward = true;
|
2015-11-24 05:52:00 +00:00
|
|
|
// To support multiple plugins we only add the names of plugins that
|
|
|
|
// return a success status after initialization.
|
|
|
|
BufferedLogSink::addPlugin(logger);
|
|
|
|
}
|
2016-05-14 02:48:40 +00:00
|
|
|
|
|
|
|
if ((status.getCode() & LOGGER_FEATURE_LOGEVENT) > 0) {
|
|
|
|
EventFactory::addForwarder(logger);
|
|
|
|
}
|
2015-02-16 02:15:06 +00:00
|
|
|
}
|
2016-08-29 13:59:55 +00:00
|
|
|
|
|
|
|
if (forward) {
|
|
|
|
// Turn on buffered log forwarding only after all plugins have going through
|
|
|
|
// their initialization.
|
|
|
|
BufferedLogSink::forward(true);
|
|
|
|
BufferedLogSink::enable();
|
|
|
|
}
|
2015-02-16 02:15:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BufferedLogSink::send(google::LogSeverity severity,
|
|
|
|
const char* full_filename,
|
|
|
|
const char* base_filename,
|
|
|
|
int line,
|
|
|
|
const struct ::tm* tm_time,
|
|
|
|
const char* message,
|
|
|
|
size_t message_len) {
|
|
|
|
// Either forward the log to an enabled logger or buffer until one exists.
|
|
|
|
if (forward_) {
|
2015-11-24 05:52:00 +00:00
|
|
|
const auto& logger_plugin = Registry::getActive("logger");
|
|
|
|
for (const auto& logger : osquery::split(logger_plugin, ",")) {
|
|
|
|
auto& enabled = BufferedLogSink::enabledPlugins();
|
|
|
|
if (std::find(enabled.begin(), enabled.end(), logger) != enabled.end()) {
|
|
|
|
// May use the logs_ storage to buffer/delay sending logs.
|
|
|
|
std::vector<StatusLogLine> log;
|
2016-08-29 13:59:55 +00:00
|
|
|
log.push_back({(StatusLogSeverity)severity,
|
|
|
|
std::string(base_filename),
|
|
|
|
line,
|
|
|
|
std::string(message, message_len)});
|
2015-11-24 05:52:00 +00:00
|
|
|
PluginRequest request = {{"status", "true"}};
|
|
|
|
serializeIntermediateLog(log, request);
|
2015-12-03 08:53:54 +00:00
|
|
|
if (!request["log"].empty()) {
|
|
|
|
request["log"].pop_back();
|
|
|
|
}
|
2015-11-24 05:52:00 +00:00
|
|
|
Registry::call("logger", logger, request);
|
|
|
|
}
|
|
|
|
}
|
2015-02-16 02:15:06 +00:00
|
|
|
} else {
|
2016-08-29 13:59:55 +00:00
|
|
|
logs_.push_back({(StatusLogSeverity)severity,
|
|
|
|
std::string(base_filename),
|
|
|
|
line,
|
|
|
|
std::string(message, message_len)});
|
2015-02-16 02:15:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-30 18:44:25 +00:00
|
|
|
Status LoggerPlugin::call(const PluginRequest& request,
|
|
|
|
PluginResponse& response) {
|
2015-04-27 21:57:04 +00:00
|
|
|
QueryLogItem item;
|
2015-02-16 02:15:06 +00:00
|
|
|
std::vector<StatusLogLine> intermediate_logs;
|
|
|
|
if (request.count("string") > 0) {
|
2015-04-30 01:53:25 +00:00
|
|
|
return this->logString(request.at("string"));
|
2015-04-27 21:57:04 +00:00
|
|
|
} else if (request.count("snapshot") > 0) {
|
2015-04-30 01:53:25 +00:00
|
|
|
return this->logSnapshot(request.at("snapshot"));
|
2015-02-16 02:15:06 +00:00
|
|
|
} else if (request.count("init") > 0) {
|
2015-03-30 19:49:57 +00:00
|
|
|
deserializeIntermediateLog(request, intermediate_logs);
|
2016-05-11 19:05:09 +00:00
|
|
|
this->init(request.at("init"), intermediate_logs);
|
2016-08-29 13:59:55 +00:00
|
|
|
return Status(0);
|
2015-02-16 02:15:06 +00:00
|
|
|
} else if (request.count("status") > 0) {
|
2015-03-30 19:49:57 +00:00
|
|
|
deserializeIntermediateLog(request, intermediate_logs);
|
2015-02-16 02:15:06 +00:00
|
|
|
return this->logStatus(intermediate_logs);
|
2016-05-14 02:48:40 +00:00
|
|
|
} else if (request.count("event") > 0) {
|
|
|
|
return this->logEvent(request.at("event"));
|
|
|
|
} else if (request.count("action") && request.at("action") == "features") {
|
|
|
|
size_t features = 0;
|
|
|
|
features |= (usesLogStatus()) ? LOGGER_FEATURE_LOGSTATUS : 0;
|
|
|
|
features |= (usesLogEvent()) ? LOGGER_FEATURE_LOGEVENT : 0;
|
2016-09-02 22:04:03 +00:00
|
|
|
return Status(static_cast<int>(features));
|
2015-02-16 02:15:06 +00:00
|
|
|
} else {
|
|
|
|
return Status(1, "Unsupported call to logger plugin");
|
2015-01-30 18:44:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-27 21:57:04 +00:00
|
|
|
Status logString(const std::string& message, const std::string& category) {
|
|
|
|
return logString(message, category, Registry::getActive("logger"));
|
2014-07-31 00:35:19 +00:00
|
|
|
}
|
|
|
|
|
2015-04-27 21:57:04 +00:00
|
|
|
Status logString(const std::string& message,
|
|
|
|
const std::string& category,
|
|
|
|
const std::string& receiver) {
|
2016-03-27 06:20:41 +00:00
|
|
|
if (FLAGS_disable_logging) {
|
|
|
|
return Status(0, "Logging disabled");
|
|
|
|
}
|
|
|
|
|
2016-02-22 01:56:47 +00:00
|
|
|
return Registry::call(
|
2015-04-27 21:57:04 +00:00
|
|
|
"logger", receiver, {{"string", message}, {"category", category}});
|
2014-07-31 00:35:19 +00:00
|
|
|
}
|
|
|
|
|
2015-04-27 21:57:04 +00:00
|
|
|
Status logQueryLogItem(const QueryLogItem& results) {
|
|
|
|
return logQueryLogItem(results, Registry::getActive("logger"));
|
2014-07-31 00:35:19 +00:00
|
|
|
}
|
|
|
|
|
2015-04-27 21:57:04 +00:00
|
|
|
Status logQueryLogItem(const QueryLogItem& results,
|
|
|
|
const std::string& receiver) {
|
2016-03-27 06:20:41 +00:00
|
|
|
if (FLAGS_disable_logging) {
|
|
|
|
return Status(0, "Logging disabled");
|
|
|
|
}
|
|
|
|
|
2015-12-03 08:53:54 +00:00
|
|
|
std::vector<std::string> json_items;
|
2014-10-24 22:02:27 +00:00
|
|
|
Status status;
|
|
|
|
if (FLAGS_log_result_events) {
|
2015-12-03 08:53:54 +00:00
|
|
|
status = serializeQueryLogItemAsEventsJSON(results, json_items);
|
2014-10-24 22:02:27 +00:00
|
|
|
} else {
|
2015-12-03 08:53:54 +00:00
|
|
|
std::string json;
|
2015-04-27 21:57:04 +00:00
|
|
|
status = serializeQueryLogItemJSON(results, json);
|
2015-12-03 08:53:54 +00:00
|
|
|
json_items.push_back(json);
|
2014-10-24 22:02:27 +00:00
|
|
|
}
|
|
|
|
if (!status.ok()) {
|
|
|
|
return status;
|
2014-07-31 00:35:19 +00:00
|
|
|
}
|
2015-12-03 08:53:54 +00:00
|
|
|
|
|
|
|
for (auto& json : json_items) {
|
2016-02-21 22:12:58 +00:00
|
|
|
if (!json.empty() && json.back() == '\n') {
|
2015-12-03 08:53:54 +00:00
|
|
|
json.pop_back();
|
|
|
|
status = logString(json, "event", receiver);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return status;
|
2015-04-27 21:57:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Status logSnapshotQuery(const QueryLogItem& item) {
|
2016-03-27 06:20:41 +00:00
|
|
|
if (FLAGS_disable_logging) {
|
|
|
|
return Status(0, "Logging disabled");
|
|
|
|
}
|
|
|
|
|
2015-04-27 21:57:04 +00:00
|
|
|
std::string json;
|
|
|
|
if (!serializeQueryLogItemJSON(item, json)) {
|
|
|
|
return Status(1, "Could not serialize snapshot");
|
|
|
|
}
|
2016-02-21 22:12:58 +00:00
|
|
|
if (!json.empty() && json.back() == '\n') {
|
2015-12-03 08:53:54 +00:00
|
|
|
json.pop_back();
|
|
|
|
}
|
2015-04-27 21:57:04 +00:00
|
|
|
return Registry::call("logger", {{"snapshot", json}});
|
|
|
|
}
|
|
|
|
|
2015-07-01 22:26:26 +00:00
|
|
|
void relayStatusLogs() {
|
2016-03-27 06:20:41 +00:00
|
|
|
if (FLAGS_disable_logging) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-24 05:52:00 +00:00
|
|
|
// Prevent our dumping and registry calling from producing additional logs.
|
2015-07-01 22:26:26 +00:00
|
|
|
LoggerDisabler disabler;
|
|
|
|
|
|
|
|
// Construct a status log plugin request.
|
2015-12-03 08:53:54 +00:00
|
|
|
PluginRequest request = {{"status", "true"}};
|
2015-07-01 22:26:26 +00:00
|
|
|
auto& status_logs = BufferedLogSink::dump();
|
|
|
|
if (status_logs.size() == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2015-12-03 08:53:54 +00:00
|
|
|
serializeIntermediateLog(status_logs, request);
|
|
|
|
if (!request["log"].empty()) {
|
|
|
|
request["log"].pop_back();
|
|
|
|
}
|
2015-07-01 22:26:26 +00:00
|
|
|
|
|
|
|
// Skip the registry's logic, and send directly to the core's logger.
|
2015-12-03 08:53:54 +00:00
|
|
|
PluginResponse response;
|
2016-03-27 06:20:41 +00:00
|
|
|
Registry::call("logger", request, response);
|
|
|
|
|
|
|
|
// Flush the buffered status logs.
|
|
|
|
// If the logger called failed then the logger is experiencing a catastrophic
|
|
|
|
// failure, since it is missing from the registry. The logger plugin may
|
|
|
|
// return failure, but it should have buffered independently of the failure.
|
|
|
|
status_logs.clear();
|
2015-07-01 22:26:26 +00:00
|
|
|
}
|
2016-07-07 22:16:28 +00:00
|
|
|
|
|
|
|
void systemLog(const std::string& line) {
|
|
|
|
#ifndef WIN32
|
|
|
|
syslog(LOG_NOTICE, "%s", line.c_str());
|
|
|
|
#endif
|
|
|
|
}
|
2014-08-15 07:25:30 +00:00
|
|
|
}
|