osquery-1/osquery/logger/logger.cpp

71 lines
2.0 KiB
C++
Raw Normal View History

/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
2014-07-31 00:35:19 +00:00
#include <algorithm>
#include <thread>
#include <glog/logging.h>
#include <osquery/flags.h>
#include <osquery/logger.h>
#include <osquery/logger/plugin.h>
2014-10-24 22:02:27 +00:00
using osquery::Status;
2014-07-31 00:35:19 +00:00
2014-08-15 07:25:30 +00:00
namespace osquery {
2014-07-31 00:35:19 +00:00
/// `log_receiver` defines the default log receiver plugin name.
DEFINE_osquery_flag(string,
log_receiver,
"filesystem",
"The upstream log receiver to log messages to.");
2014-07-31 00:35:19 +00:00
DEFINE_osquery_flag(bool,
log_result_events,
true,
"Log scheduled results as events.");
2014-10-24 22:02:27 +00:00
2014-07-31 00:35:19 +00:00
Status logString(const std::string& s) {
return logString(s, FLAGS_log_receiver);
}
Status logString(const std::string& s, const std::string& receiver) {
if (REGISTERED_LOGGER_PLUGINS.find(receiver) ==
REGISTERED_LOGGER_PLUGINS.end()) {
LOG(ERROR) << "Logger receiver " << receiver << " not found";
return Status(1, "Logger receiver not found");
}
2014-08-15 07:25:30 +00:00
auto log_status = REGISTERED_LOGGER_PLUGINS.at(receiver)->logString(s);
2014-07-31 00:35:19 +00:00
if (!log_status.ok()) {
return log_status;
}
return Status(0, "OK");
}
2014-09-21 21:29:28 +00:00
Status logScheduledQueryLogItem(const osquery::ScheduledQueryLogItem& results) {
2014-07-31 00:35:19 +00:00
return logScheduledQueryLogItem(results, FLAGS_log_receiver);
}
2014-09-21 21:29:28 +00:00
Status logScheduledQueryLogItem(const osquery::ScheduledQueryLogItem& results,
const std::string& receiver) {
2014-07-31 00:35:19 +00:00
std::string json;
2014-10-24 22:02:27 +00:00
Status status;
if (FLAGS_log_result_events) {
status = serializeScheduledQueryLogItemAsEventsJSON(results, json);
} else {
status = serializeScheduledQueryLogItemJSON(results, json);
}
if (!status.ok()) {
return status;
2014-07-31 00:35:19 +00:00
}
return logString(json, receiver);
}
2014-08-15 07:25:30 +00:00
}