Loading...
Searching...
No Matches
logStream.h
Go to the documentation of this file.
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//
#ifndef BALL_COMMON_LOGSTREAM_H
#define BALL_COMMON_LOGSTREAM_H
#include <BALL/COMMON/global.h>
#include <BALL/COMMON/debug.h>
#include <BALL/CONFIG/config.h>
#ifdef BALL_HAS_SYS_TIME_H
# include <sys/time.h>
#endif
#ifdef BALL_HAS_TIME_H
# include <time.h>
#endif
#include <iostream>
#include <list>
#include <sstream>
#include <vector>
using std::list;
using std::vector;
using std::string;
namespace BALL
{
// forward declarations
class LogStream;
class LogStreamNotifier;
class BALL_EXPORT LogStreamBuf
: public std::streambuf
{
friend class LogStream;
public:
static const int MAX_LEVEL;
static const int MIN_LEVEL;
static const Time MAX_TIME;
LogStreamBuf();
virtual ~LogStreamBuf();
virtual void dump(std::ostream& s);
virtual int sync();
int sync(bool force_flush);
virtual int overflow(int c = -1);
struct BALL_EXPORT StreamStruct
{
std::ostream* stream;
string prefix;
int min_level;
int max_level;
LogStreamNotifier* target;
bool disabled;
StreamStruct()
: stream(0),
min_level(MIN_LEVEL),
max_level(MAX_LEVEL),
target(0)
{
}
// Delete the notification target.
~StreamStruct()
{
}
};
protected:
struct LoglineStruct
{
int level;
string text;
Time time;
LoglineStruct()
: level(0),
text(""),
time(0)
{}
};
typedef struct LoglineStruct Logline;
// interpret the prefix format string and return the expanded prefix
string expandPrefix_(const string& prefix, int level, Time time) const;
char* pbuf_;
vector<Logline> loglines_;
int level_;
int tmp_level_;
list<StreamStruct> stream_list_;
string incomplete_line_;
};
class BALL_EXPORT LogStreamNotifier
{
public:
LogStreamNotifier();
virtual ~LogStreamNotifier();
virtual void logNotify();
void registerAt(LogStream& log_stream,
int min_level = LogStreamBuf::MIN_LEVEL,
int max_level = LogStreamBuf::MAX_LEVEL);
void unregister();
protected:
std::stringstream stream_;
LogStream* registered_at_;
};
class BALL_EXPORT LogStream
: public std::ostream
{
public:
enum LogStreamLevel
{
ERROR_LEVEL = 2000 ,
WARNING_LEVEL = 1000,
INFORMATION_LEVEL = 0
};
LogStream(LogStreamBuf* buf = 0, bool delete_buf = true, bool associate_stdio = false);
virtual ~LogStream();
LogStreamBuf* rdbuf();
LogStreamBuf* operator -> ();
void setLevel(int level);
int getLevel();
LogStream& level(int level);
LogStream& info(int n = 0);
LogStream& error(int n = 0);
LogStream& warn(int n = 0);
void insert
(std::ostream& s, int min_level = LogStreamBuf::MIN_LEVEL,
int max_level = LogStreamBuf::MAX_LEVEL);
void remove(std::ostream& s);
void insertNotification(std::ostream& s,
LogStreamNotifier& target,
int min_level = LogStreamBuf::MIN_LEVEL,
int max_level = LogStreamBuf::MAX_LEVEL);
void setMinLevel(const std::ostream& s, int min_level);
void setMaxLevel(const std::ostream& s, int max_level);
void setPrefix(const std::ostream& s, const string& prefix);
void disableOutput() ;
void enableOutput() ;
bool outputEnabled() const
;
std::ostream& flush();
void clear();
Size getNumberOfLines
(int min_level = LogStreamBuf::MIN_LEVEL,
int max_level = LogStreamBuf::MAX_LEVEL) const;
string getLineText(const Index& index) const;
Time getLineTime(const Index& index) const;
int getLineLevel(const Index& index) const;
list<int> filterLines
(int min_level = LogStreamBuf::MIN_LEVEL, int max_level = LogStreamBuf::MAX_LEVEL,
Time earliest = 0, Time latest = LogStreamBuf::MAX_TIME,
const string& s = "") const;
private:
typedef std::list<LogStreamBuf::StreamStruct>::iterator StreamIterator;
StreamIterator findStream_(const std::ostream& stream);
bool hasStream_(std::ostream& stream);
bool bound_() const;
// flag needed by the destructor to decide whether the streambuf
// has to be deleted. If the default ctor is used to create
// the LogStreamBuf, delete_buffer_ is set to true and the ctor
// also deletes the buffer.
bool delete_buffer_;
bool disable_output_;
};
BALL_EXPORT extern LogStream Log;
# ifndef BALL_NO_INLINE_FUNCTIONS
# include <BALL/COMMON/logStream.iC>
# endif
} // namespace BALL
#endif // BALL_COMMON_LOGSTREAM_H
global.h
BALL_EXPORT
#define BALL_EXPORT
Definition COMMON/global.h:50
BALL::LogStreamBuf
Definition logStream.h:82
BALL::LogStreamBuf::stream_list_
list< StreamStruct > stream_list_
Definition logStream.h:203
BALL::LogStreamBuf::tmp_level_
int tmp_level_
Definition logStream.h:201
BALL::LogStreamBuf::Logline
struct LoglineStruct Logline
Definition logStream.h:189
BALL::LogStreamBuf::level_
int level_
Definition logStream.h:199
BALL::LogStreamBuf::MAX_LEVEL
static const int MAX_LEVEL
Definition logStream.h:91
BALL::LogStreamBuf::expandPrefix_
string expandPrefix_(const string &prefix, int level, Time time) const
BALL::LogStreamBuf::sync
int sync(bool force_flush)
BALL::LogStreamBuf::LogStreamBuf
LogStreamBuf()
BALL::LogStreamBuf::incomplete_line_
string incomplete_line_
Definition logStream.h:205
BALL::LogStreamBuf::pbuf_
char * pbuf_
Definition logStream.h:195
BALL::LogStreamBuf::MAX_TIME
static const Time MAX_TIME
Definition logStream.h:93
BALL::LogStreamBuf::loglines_
vector< Logline > loglines_
Definition logStream.h:197
BALL::LogStreamBuf::~LogStreamBuf
virtual ~LogStreamBuf()
BALL::LogStreamBuf::dump
virtual void dump(std::ostream &s)
BALL::LogStreamBuf::LogStream
friend class LogStream
Definition logStream.h:84
BALL::LogStreamBuf::overflow
virtual int overflow(int c=-1)
BALL::LogStreamBuf::sync
virtual int sync()
BALL::LogStreamBuf::MIN_LEVEL
static const int MIN_LEVEL
Definition logStream.h:92
BALL::LogStreamNotifier
Definition logStream.h:211
BALL::LogStreamNotifier::registered_at_
LogStream * registered_at_
Definition logStream.h:234
BALL::LogStreamNotifier::LogStreamNotifier
LogStreamNotifier()
BALL::LogStreamNotifier::unregister
void unregister()
BALL::LogStreamNotifier::~LogStreamNotifier
virtual ~LogStreamNotifier()
BALL::LogStreamNotifier::stream_
std::stringstream stream_
Definition logStream.h:232
BALL::LogStreamNotifier::registerAt
void registerAt(LogStream &log_stream, int min_level=LogStreamBuf::MIN_LEVEL, int max_level=LogStreamBuf::MAX_LEVEL)
BALL::LogStreamNotifier::logNotify
virtual void logNotify()
BALL::LogStream
Definition logStream.h:246
BALL::LogStream::insert
void insert(std::ostream &s, int min_level=LogStreamBuf::MIN_LEVEL, int max_level=LogStreamBuf::MAX_LEVEL)
BALL::LogStream::setMinLevel
void setMinLevel(const std::ostream &s, int min_level)
BALL::LogStream::remove
void remove(std::ostream &s)
BALL::LogStream::error
LogStream & error(int n=0)
BALL::LogStream::filterLines
list< int > filterLines(int min_level=LogStreamBuf::MIN_LEVEL, int max_level=LogStreamBuf::MAX_LEVEL, Time earliest=0, Time latest=LogStreamBuf::MAX_TIME, const string &s="") const
BALL::LogStream::setMaxLevel
void setMaxLevel(const std::ostream &s, int max_level)
BALL::LogStream::disableOutput
void disableOutput()
Disable all output.
BALL::LogStream::insertNotification
void insertNotification(std::ostream &s, LogStreamNotifier &target, int min_level=LogStreamBuf::MIN_LEVEL, int max_level=LogStreamBuf::MAX_LEVEL)
BALL::LogStream::getLineText
string getLineText(const Index &index) const
BALL::LogStream::getNumberOfLines
Size getNumberOfLines(int min_level=LogStreamBuf::MIN_LEVEL, int max_level=LogStreamBuf::MAX_LEVEL) const
BALL::LogStream::outputEnabled
bool outputEnabled() const
Is Output enabled?
BALL::LogStream::setLevel
void setLevel(int level)
BALL::LogStream::clear
void clear()
BALL::LogStream::rdbuf
LogStreamBuf * rdbuf()
BALL::LogStream::LogStream
LogStream(LogStreamBuf *buf=0, bool delete_buf=true, bool associate_stdio=false)
BALL::LogStream::getLineTime
Time getLineTime(const Index &index) const
BALL::LogStream::enableOutput
void enableOutput()
Enable all output.
BALL::LogStream::info
LogStream & info(int n=0)
BALL::LogStream::~LogStream
virtual ~LogStream()
BALL::LogStream::LogStreamLevel
LogStreamLevel
Definition logStream.h:262
BALL::LogStream::WARNING_LEVEL
@ WARNING_LEVEL
Definition logStream.h:270
BALL::LogStream::ERROR_LEVEL
@ ERROR_LEVEL
Definition logStream.h:266
BALL::LogStream::INFORMATION_LEVEL
@ INFORMATION_LEVEL
Definition logStream.h:273
BALL::LogStream::flush
std::ostream & flush()
BALL::LogStream::level
LogStream & level(int level)
BALL::LogStream::getLevel
int getLevel()
BALL::LogStream::warn
LogStream & warn(int n=0)
BALL::LogStream::setPrefix
void setPrefix(const std::ostream &s, const string &prefix)
BALL::LogStream::getLineLevel
int getLineLevel(const Index &index) const
debug.h
BALL
Definition constants.h:13
BALL::Size
BALL_SIZE_TYPE Size
Definition COMMON/global.h:114
BALL::Log
BALL_EXPORT LogStream Log
BALL::Time
time_t Time
Definition COMMON/global.h:123
BALL::Index
BALL_INDEX_TYPE Index
Definition COMMON/global.h:105
BALL::LogStreamBuf::LoglineStruct
Definition logStream.h:177
BALL::LogStreamBuf::LoglineStruct::text
string text
Definition logStream.h:179
BALL::LogStreamBuf::LoglineStruct::LoglineStruct
LoglineStruct()
Definition logStream.h:182
BALL::LogStreamBuf::LoglineStruct::level
int level
Definition logStream.h:178
BALL::LogStreamBuf::LoglineStruct::time
Time time
Definition logStream.h:180
BALL::LogStreamBuf::StreamStruct::max_level
int max_level
Definition logStream.h:155
BALL::LogStreamBuf::StreamStruct::disabled
bool disabled
Definition logStream.h:157
BALL::LogStreamBuf::StreamStruct::min_level
int min_level
Definition logStream.h:154
BALL::LogStreamBuf::StreamStruct::stream
std::ostream * stream
Definition logStream.h:152
BALL::LogStreamBuf::StreamStruct::target
LogStreamNotifier * target
Definition logStream.h:156
BALL::LogStreamBuf::StreamStruct::prefix
string prefix
Definition logStream.h:153
BALL::LogStreamBuf::StreamStruct::~StreamStruct
~StreamStruct()
Definition logStream.h:168
BALL::LogStreamBuf::StreamStruct::StreamStruct
StreamStruct()
Definition logStream.h:159