Skip to content
Snippets Groups Projects
Commit 33ad3875 authored by Dr. Felix Tobias Schindler's avatar Dr. Felix Tobias Schindler
Browse files

[common.logstreams] update memory handling

This commit should get rid of some warnings and small inconsistencies.
The LogStream class now derives from StorageProvider which holds the
buffer before the buffer is given to the ostream base class. FileLogStream
and EmptyLogStream now merely create the corresponding stream buffer
and forward it to LogStream. Thus overriding rdbuf does not seem necessary
any more.
parent 48fbbd73
No related branches found
No related tags found
No related merge requests found
...@@ -11,6 +11,7 @@ namespace Dune { ...@@ -11,6 +11,7 @@ namespace Dune {
namespace Stuff { namespace Stuff {
namespace Common { namespace Common {
SuspendableStrBuffer::SuspendableStrBuffer(int loglevel, int& logflags) SuspendableStrBuffer::SuspendableStrBuffer(int loglevel, int& logflags)
: logflags_(logflags) : logflags_(logflags)
, loglevel_(loglevel) , loglevel_(loglevel)
...@@ -68,15 +69,16 @@ int SuspendableStrBuffer::pubsync() ...@@ -68,15 +69,16 @@ int SuspendableStrBuffer::pubsync()
return 0; return 0;
} }
LogStream& LogStream::flush() LogStream& LogStream::flush()
{ {
assert(this->buffer_); assert(&this->storage_access());
this->buffer_->pubsync(); this->storage_access().pubsync();
return *this; return *this;
} }
int FileLogStream::FileBuffer::sync() int FileBuffer::sync()
{ {
// flush buffer into stream // flush buffer into stream
std::lock_guard<std::mutex> guard(sync_mutex_); std::lock_guard<std::mutex> guard(sync_mutex_);
...@@ -89,7 +91,7 @@ int FileLogStream::FileBuffer::sync() ...@@ -89,7 +91,7 @@ int FileLogStream::FileBuffer::sync()
} }
int EmptyLogStream::EmptyBuffer::sync() int EmptyBuffer::sync()
{ {
str(""); str("");
return 0; return 0;
......
...@@ -6,7 +6,6 @@ ...@@ -6,7 +6,6 @@
#ifndef DUNE_STUFF_LOGSTREAMS_HH #ifndef DUNE_STUFF_LOGSTREAMS_HH
#define DUNE_STUFF_LOGSTREAMS_HH #define DUNE_STUFF_LOGSTREAMS_HH
#include "string.hh"
#include <ostream> #include <ostream>
#include <fstream> #include <fstream>
#include <sstream> #include <sstream>
...@@ -14,12 +13,14 @@ ...@@ -14,12 +13,14 @@
#include <type_traits> #include <type_traits>
#include <mutex> #include <mutex>
#include <dune/stuff/common/disable_warnings.hh> #include "memory.hh"
#include "string.hh"
namespace Dune { namespace Dune {
namespace Stuff { namespace Stuff {
namespace Common { namespace Common {
enum LogFlags enum LogFlags
{ {
LOG_NONE = 1, LOG_NONE = 1,
...@@ -31,6 +32,7 @@ enum LogFlags ...@@ -31,6 +32,7 @@ enum LogFlags
LOG_NEXT = 64 LOG_NEXT = 64
}; };
class SuspendableStrBuffer : public std::basic_stringbuf<char, std::char_traits<char>> class SuspendableStrBuffer : public std::basic_stringbuf<char, std::char_traits<char>>
{ {
typedef std::basic_stringbuf<char, std::char_traits<char>> BaseType; typedef std::basic_stringbuf<char, std::char_traits<char>> BaseType;
...@@ -72,24 +74,43 @@ private: ...@@ -72,24 +74,43 @@ private:
bool is_suspended_; bool is_suspended_;
PriorityType suspend_priority_; PriorityType suspend_priority_;
std::mutex mutex_; std::mutex mutex_;
}; }; // class SuspendableStrBuffer
class BufferContainer class FileBuffer : public SuspendableStrBuffer
{ {
public: public:
BufferContainer(SuspendableStrBuffer* buffer) FileBuffer(int loglevel, int& logflags, std::ofstream& file)
: buffer_(buffer) : SuspendableStrBuffer(loglevel, logflags)
, logfile_(file)
{ {
} }
private:
std::ofstream& logfile_;
std::mutex sync_mutex_;
protected: protected:
SuspendableStrBuffer* buffer_; virtual int sync();
}; // class BufferContainer }; // class FileBuffer
class LogStream : public BufferContainer, public std::basic_ostream<char, std::char_traits<char>> class EmptyBuffer : public SuspendableStrBuffer
{ {
public:
EmptyBuffer(int loglevel, int& logflags)
: SuspendableStrBuffer(loglevel, logflags)
{
}
protected:
virtual int sync();
}; // class EmptyBuffer
class LogStream : StorageProvider<SuspendableStrBuffer>, public std::basic_ostream<char, std::char_traits<char>>
{
typedef StorageProvider<SuspendableStrBuffer> StorageBaseType;
typedef std::basic_ostream<char, std::char_traits<char>> BaseType; typedef std::basic_ostream<char, std::char_traits<char>> BaseType;
public: public:
...@@ -97,21 +118,11 @@ public: ...@@ -97,21 +118,11 @@ public:
static const PriorityType default_suspend_priority = SuspendableStrBuffer::default_suspend_priority; static const PriorityType default_suspend_priority = SuspendableStrBuffer::default_suspend_priority;
LogStream(SuspendableStrBuffer* buffer) LogStream(SuspendableStrBuffer* buffer)
: BufferContainer(buffer) : StorageBaseType(buffer)
, BaseType(this->buffer_) , BaseType(&this->storage_access())
{ {
} }
/** \brief Tunnel buffer setting to ostream base
* for some weird reason all derived classes MUST call
* it again after base class init
**/
void rdbuf(SuspendableStrBuffer* buf)
{
this->buffer_ = buf;
BaseType::rdbuf(buf);
}
virtual ~LogStream() virtual ~LogStream()
{ {
flush(); flush();
...@@ -126,8 +137,8 @@ public: ...@@ -126,8 +137,8 @@ public:
***/ ***/
void suspend(PriorityType priority = default_suspend_priority) void suspend(PriorityType priority = default_suspend_priority)
{ {
assert(this->buffer_); assert(&this->storage_access());
this->buffer_->suspend(priority); this->storage_access().suspend(priority);
} }
/** \brief start accepting input into the buffer again /** \brief start accepting input into the buffer again
...@@ -135,8 +146,8 @@ public: ...@@ -135,8 +146,8 @@ public:
***/ ***/
void resume(PriorityType priority = default_suspend_priority) void resume(PriorityType priority = default_suspend_priority)
{ {
assert(this->buffer_); assert(&this->storage_access());
this->buffer_->resume(priority); this->storage_access().resume(priority);
} // Resume } // Resume
}; // LogStream }; // LogStream
...@@ -144,63 +155,22 @@ public: ...@@ -144,63 +155,22 @@ public:
//! ostream compatible class wrapping file and console output //! ostream compatible class wrapping file and console output
class FileLogStream : public LogStream class FileLogStream : public LogStream
{ {
private:
class FileBuffer : public SuspendableStrBuffer
{
public:
FileBuffer(int loglevel, int& logflags, std::ofstream& file)
: SuspendableStrBuffer(loglevel, logflags)
, logfile_(file)
{
}
private:
std::ofstream& logfile_;
std::mutex sync_mutex_;
protected:
virtual int sync();
};
public: public:
FileLogStream(int loglevel, int& logflags, /*std::ofstream& file,*/ std::ofstream& fileWoTime) FileLogStream(int loglevel, int& logflags, /*std::ofstream& file,*/ std::ofstream& fileWoTime)
: logBuffer_(loglevel, logflags, /*file,*/ fileWoTime) : LogStream(new FileBuffer(loglevel, logflags, /*file,*/ fileWoTime))
, LogStream(&logBuffer_)
{ {
LogStream::rdbuf(&logBuffer_);
} }
private:
FileBuffer logBuffer_;
}; // class FileLogStream }; // class FileLogStream
//! /dev/null //! /dev/null
class EmptyLogStream : public LogStream class EmptyLogStream : public LogStream
{ {
private:
class EmptyBuffer : public SuspendableStrBuffer
{
public:
EmptyBuffer(int loglevel, int& logflags)
: SuspendableStrBuffer(loglevel, logflags)
{
}
protected:
virtual int sync();
};
public: public:
EmptyLogStream(int& logflags) EmptyLogStream(int& logflags)
: logBuffer_(int(LOG_NONE), logflags) : LogStream(new EmptyBuffer(int(LOG_NONE), logflags))
, LogStream(&logBuffer_)
{ {
LogStream::rdbuf(&logBuffer_);
} }
private:
EmptyBuffer logBuffer_;
}; // class EmptyLogStream }; // class EmptyLogStream
...@@ -209,10 +179,9 @@ int dev_null_logflag; ...@@ -209,10 +179,9 @@ int dev_null_logflag;
EmptyLogStream dev_null(dev_null_logflag); EmptyLogStream dev_null(dev_null_logflag);
} }
} // namespace Common } // namespace Common
} // namespace Stuff } // namespace Stuff
} // namespace Dune } // namespace Dune
#include <dune/stuff/common/reenable_warnings.hh>
#endif // LOGSTREAMS_HH #endif // LOGSTREAMS_HH
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment