Permalink
Browse files

do not even format log messages if the verbosity level won't allow th…

…em to be emitted
  • Loading branch information...
1 parent 4e12442 commit a2e8c5a538656f8604411e17e2296aa930a7cbd4 @kobolog kobolog committed Apr 18, 2012
@@ -16,6 +16,7 @@
namespace cocaine {
struct config_t;
+
class context_t;
class object_t;
@@ -19,14 +19,13 @@ class syslog_t:
public sink_t
{
public:
- syslog_t(const std::string& identity, priorities verbosity);
+ syslog_t(priorities verbosity, const std::string& identity);
virtual void emit(priorities priority,
const std::string& message) const;
private:
const std::string m_identity;
- const priorities m_verbosity;
};
}}
@@ -29,7 +29,8 @@ enum priorities {
debug,
info,
warning,
- error
+ error,
+ ignore
};
class sink_t;
@@ -63,16 +64,23 @@ class sink_t:
public boost::noncopyable
{
public:
+ sink_t(priorities verbosity);
virtual ~sink_t() = 0;
// XXX: Might be a better idea to return the logger by reference.
boost::shared_ptr<logger_t> get(const std::string& name);
+
+ bool ignores(priorities priority) const {
+ return priority < m_verbosity;
+ }
public:
virtual void emit(priorities priority,
const std::string& message) const = 0;
private:
+ const priorities m_verbosity;
+
typedef std::map<
const std::string,
boost::shared_ptr<logger_t>
@@ -86,6 +94,8 @@ class void_sink_t:
public sink_t
{
public:
+ void_sink_t();
+
virtual void emit(priorities, const std::string&) const;
};
@@ -144,7 +144,6 @@ struct busy:
const std::auto_ptr<job_t>& job() const {
return context<alive>().job();
}
-
};
struct dead:
View
@@ -18,18 +18,14 @@
using namespace cocaine::logging;
-syslog_t::syslog_t(const std::string& identity, priorities verbosity):
- m_identity(identity),
- m_verbosity(verbosity)
+syslog_t::syslog_t(priorities verbosity, const std::string& identity):
+ sink_t(verbosity),
+ m_identity(identity)
{
- openlog(m_identity.c_str(), LOG_PID | LOG_NDELAY, LOG_USER);
+ openlog(m_identity.c_str(), LOG_PID, LOG_USER);
}
void syslog_t::emit(priorities priority, const std::string& message) const {
- if(priority < m_verbosity) {
- return;
- }
-
// NOTE: Replacing all newlines with spaces here because certain sysloggers
// fail miserably interpreting them correctly.
std::string m = boost::algorithm::replace_all_copy(message, "\n", " ");
@@ -38,14 +34,20 @@ void syslog_t::emit(priorities priority, const std::string& message) const {
case debug:
syslog(LOG_DEBUG, "%s", m.c_str());
break;
+
case info:
syslog(LOG_INFO, "%s", m.c_str());
break;
+
case warning:
syslog(LOG_WARNING, "%s", m.c_str());
break;
+
case error:
syslog(LOG_ERR, "%s", m.c_str());
break;
+
+ default:
+ break;
}
}
View
@@ -55,14 +55,24 @@ void logger_t::error(const char * format, ...) const {
}
void logger_t::emit(priorities priority, const char * format, va_list args) const {
+ if(m_sink.ignores(priority)) {
+ return;
+ }
+
boost::lock_guard<boost::mutex> lock(m_mutex);
+
vsnprintf(m_buffer, LOG_BUFFER_SIZE, format, args);
+
m_sink.emit(priority, m_name + ": " + m_buffer);
}
// Logging sinks
// -------------
+sink_t::sink_t(priorities verbosity):
+ m_verbosity(verbosity)
+{ }
+
sink_t::~sink_t() { }
boost::shared_ptr<logger_t> sink_t::get(const std::string& name) {
@@ -88,4 +98,8 @@ boost::shared_ptr<logger_t> sink_t::get(const std::string& name) {
// Void logger
// -----------
+void_sink_t::void_sink_t():
+ sink_t(ignore)
+{ }
+
void void_sink_t::emit(priorities, const std::string&) const { }
View
@@ -125,8 +125,8 @@ int main(int argc, char * argv[]) {
cfg.sink.reset(
new logging::syslog_t(
- "cocaine",
- vm.count("verbose") ? logging::debug : logging::info
+ vm.count("verbose") ? logging::debug : logging::info,
+ "cocaine"
)
);

0 comments on commit a2e8c5a

Please sign in to comment.