Module: check_mk
Branch: master
Commit: 237d87089397171f6614532ab22c3e72ae09a5b8
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=237d8708939717…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Tue Oct 25 14:52:54 2016 +0200
Fetched Livestatus from downstream.
---
livestatus/src/Logger.cc | 15 +++++++++------
livestatus/src/Logger.h | 20 +++++++++++++++-----
2 files changed, 24 insertions(+), 11 deletions(-)
diff --git a/livestatus/src/Logger.cc b/livestatus/src/Logger.cc
index 9cd04c6..40a9c7b 100644
--- a/livestatus/src/Logger.cc
+++ b/livestatus/src/Logger.cc
@@ -37,6 +37,7 @@ using std::mutex;
using std::ostream;
using std::ostringstream;
using std::string;
+using std::unique_ptr;
ostream &operator<<(ostream &os, const LogLevel &c) {
return os << static_cast<int>(c);
@@ -47,14 +48,17 @@ void SimpleFormatter::format(ostream &os, const LogRecord
&record) {
<< "[" << record.getLevel() << "] " <<
record.getMessage();
}
-StreamHandler::StreamHandler(ostream &os) : _os(os) {}
+SharedStreamHandler::SharedStreamHandler(mutex &mutex, ostream &os)
+ : _mutex(mutex), _os(os) {}
-void StreamHandler::publish(const LogRecord &record) {
+void SharedStreamHandler::publish(const LogRecord &record) {
lock_guard<mutex> lg(_mutex);
getFormatter()->format(_os, record);
_os << endl;
}
+StreamHandler::StreamHandler(ostream &os) : SharedStreamHandler(_mutex, os) {}
+
FileHandler::FileHandler(const std::string &filename) : StreamHandler(_os) {
_os.open(filename, std::ofstream::app);
if (!_os) {
@@ -66,11 +70,10 @@ Logger::Logger(string name, Logger *parent)
: _name(move(name))
, _parent(parent)
, _level(LogLevel::debug)
- , _use_parent_handlers(true) {
- setHandler(make_unique<StreamHandler>(cerr));
-}
+ , _handler(_name.empty() ? nullptr : new StreamHandler(cerr))
+ , _use_parent_handlers(true) {}
-Logger::~Logger() { delete getHandler(); }
+Logger::~Logger() { setHandler(unique_ptr<Handler>()); }
// static
Logger *Logger::getLogger(const string &name) {
diff --git a/livestatus/src/Logger.h b/livestatus/src/Logger.h
index 3322eb0..128df41 100644
--- a/livestatus/src/Logger.h
+++ b/livestatus/src/Logger.h
@@ -100,32 +100,41 @@ class SimpleFormatter : public Formatter {
class Handler {
public:
- virtual ~Handler() { delete getFormatter(); }
+ virtual ~Handler() { setFormatter(std::unique_ptr<Formatter>()); }
virtual void publish(const LogRecord &record) = 0;
Formatter *getFormatter() const { return _formatter; }
void setFormatter(std::unique_ptr<Formatter> formatter) {
+ delete _formatter;
_formatter = formatter.release();
}
protected:
- Handler() { setFormatter(std::make_unique<SimpleFormatter>()); }
+ Handler() : _formatter(new SimpleFormatter()) {}
private:
std::atomic<Formatter *> _formatter;
};
-class StreamHandler : public Handler {
+class SharedStreamHandler : public Handler {
public:
- explicit StreamHandler(std::ostream &os);
+ SharedStreamHandler(std::mutex &mutex, std::ostream &os);
private:
- std::mutex _mutex;
+ std::mutex &_mutex;
std::ostream &_os;
void publish(const LogRecord &record) override;
};
+class StreamHandler : public SharedStreamHandler {
+public:
+ explicit StreamHandler(std::ostream &os);
+
+private:
+ std::mutex _mutex;
+};
+
class FileHandler : public StreamHandler {
public:
explicit FileHandler(const std::string &filename);
@@ -151,6 +160,7 @@ public:
Handler *getHandler() const { return _handler; }
void setHandler(std::unique_ptr<Handler> handler) {
+ delete _handler;
_handler = handler.release();
}