From ab18aaec4a677efa083f49b2183a22802b48808d Mon Sep 17 00:00:00 2001 From: Colin Patrick McCabe Date: Fri, 3 Dec 2010 15:33:48 -0800 Subject: [PATCH] logging: add g_conf.clog_to_syslog Add a new configuration option that allows you to send central log messages to syslog. Signed-off-by: Colin McCabe --- src/common/LogClient.cc | 50 +++++++++++++++++++++++++++++++++++++++++ src/common/LogClient.h | 5 ++++- src/config.cc | 2 ++ src/config.h | 3 +++ 4 files changed, 59 insertions(+), 1 deletion(-) diff --git a/src/common/LogClient.cc b/src/common/LogClient.cc index b3900c8a852bf..26f054411d0d4 100644 --- a/src/common/LogClient.cc +++ b/src/common/LogClient.cc @@ -27,6 +27,7 @@ #include #include #include +#include #ifdef DARWIN #include @@ -37,6 +38,28 @@ #include "config.h" +/* + * Given a clog log_type, return the equivalent syslog priority + */ +static inline int clog_type_to_syslog_prio(clog_type t) +{ + switch (t) { + case CLOG_DEBUG: + return LOG_DEBUG; + case CLOG_INFO: + return LOG_INFO; + case CLOG_WARN: + return LOG_WARNING; + case CLOG_ERROR: + return LOG_ERR; + case CLOG_SEC: + return LOG_CRIT; + default: + assert(0); + return 0; + } +} + LogClientTemp::LogClientTemp(clog_type type_, LogClient &parent_) : type(type_), parent(parent_) { @@ -87,6 +110,33 @@ void LogClient::send_log() } void LogClient::_send_log() +{ + if (g_conf.clog_to_syslog) + _send_log_to_syslog(); + if (g_conf.clog_to_monitors) + _send_log_to_monitors(); +} + +void LogClient::_send_log_to_syslog() +{ + std::deque::const_reverse_iterator rbegin = log_queue.rbegin(); + std::deque::const_reverse_iterator rend = log_queue.rend(); + for (std::deque::const_reverse_iterator a = rbegin; a != rend; ++a) { + const LogEntry entry(*a); + if (entry.seq < last_syslog) + break; + ostringstream oss; + oss << entry; + string str(oss.str()); + syslog(clog_type_to_syslog_prio(entry.type) | LOG_USER, "%s", str.c_str()); + } + if (rbegin != rend) { + const LogEntry entry(*rbegin); + last_syslog = entry.seq; + } +} + +void LogClient::_send_log_to_monitors() { if (log_queue.empty()) return; diff --git a/src/common/LogClient.h b/src/common/LogClient.h index 1738269ae03f1..c63bddd7302ad 100644 --- a/src/common/LogClient.h +++ b/src/common/LogClient.h @@ -58,7 +58,7 @@ public: LogClient(Messenger *m, MonMap *mm, MonClient *mc, enum logclient_flag_t flags) : messenger(m), monmap(mm), monc(mc), is_synchronous(flags & FLAG_SYNC), - log_lock("LogClient::log_lock"), last_log(0) { } + log_lock("LogClient::log_lock"), last_log(0), last_syslog(0) { } void send_log(); void handle_log_ack(MLogAck *m); @@ -100,6 +100,8 @@ private: void do_log(clog_type type, const std::string& s); bool ms_dispatch(Message *m); void _send_log(); + void _send_log_to_syslog(); + void _send_log_to_monitors(); void ms_handle_connect(Connection *con); bool ms_handle_reset(Connection *con) { return false; } void ms_handle_remote_reset(Connection *con) {} @@ -110,6 +112,7 @@ private: bool is_synchronous; Mutex log_lock; version_t last_log; + uint64_t last_syslog; std::deque log_queue; friend class LogClientTemp; diff --git a/src/config.cc b/src/config.cc index ad514a6a47357..abeaff8ae33d2 100644 --- a/src/config.cc +++ b/src/config.cc @@ -327,6 +327,8 @@ static struct config_option config_optionsp[] = { OPTION(log_sym_history, 0, OPT_INT, 10), OPTION(log_to_stdout, 0, OPT_BOOL, true), OPTION(log_per_instance, 0, OPT_BOOL, false), + OPTION(clog_to_monitors, 0, OPT_BOOL, true), + OPTION(clog_to_syslog, 0, OPT_BOOL, false), OPTION(pid_file, 0, OPT_STR, "/var/run/ceph/$type.$id.pid"), OPTION(conf, 'c', OPT_STR, "/etc/ceph/ceph.conf, ~/.ceph/config, ceph.conf"), OPTION(chdir, 0, OPT_STR, "/"), diff --git a/src/config.h b/src/config.h index f5853c1446311..b492e7371854a 100644 --- a/src/config.h +++ b/src/config.h @@ -84,6 +84,9 @@ struct md_config_t { bool log_to_stdout; bool log_per_instance; + bool clog_to_monitors; + bool clog_to_syslog; + const char *pid_file; const char *conf; -- 2.39.5