Commit b34bf169 authored by Pavel Vainerman's avatar Pavel Vainerman

[debug log]; supported "verbosity level"

parent 0f03d9b7
......@@ -78,6 +78,8 @@ struct Debug
///
static type const ANY;
typedef uint8_t verbosity;
///
// friend inline void operator|=(Debug::type & d1, Debug::type d2);
......
......@@ -92,7 +92,7 @@ class DebugStream : public std::ostream
{
public:
/// Constructor, sets the debug level to t.
explicit DebugStream(Debug::type t = Debug::NONE);
explicit DebugStream(Debug::type t = Debug::NONE, Debug::verbosity v = 0);
/// Constructor, sets the log file to f, and the debug level to t.
explicit
......@@ -177,10 +177,6 @@ class DebugStream : public std::ostream
is used.
*/
std::ostream& debug(Debug::type t = Debug::ANY) noexcept;
// if (dt & t) return *this;
// return nullstream;
// }
/** This is an operator to give a more convenient use:
dbgstream[Debug::INFO] << "Info!\n";
......@@ -229,6 +225,20 @@ class DebugStream : public std::ostream
return this->operator[](l);
}
void verbose(Debug::verbosity v) noexcept
{
verb = v;
}
/// Returns the current verbose level.
Debug::verbosity verbose() const noexcept
{
return verb;
}
// example: dlog.V(1)[Debug::INFO] << "some log.." << endl;
DebugStream& V( Debug::verbosity v ) noexcept;
// короткие функции (для удобства)
// log.level1() - вывод с датой и временем "date time [LEVEL] ...",
// если вывод даты и времени не выключен при помощи showDateTime(false)
......@@ -304,6 +314,10 @@ class DebugStream : public std::ostream
bool isWriteLogFile = { false };
bool onScreen = { true };
Debug::verbosity verb = 0;
Debug::verbosity vv = 0;
};
// ------------------------------------------------------------------------------------------------
#endif
......@@ -63,8 +63,7 @@ Debug::type const Debug::ANY = Debug::type(
Debug::LEVEL9 | Debug::REPOSITORY | Debug::SYSTEM |
Debug::EXCEPTION );
Debug::type Debug::value( std::string const& val)
Debug::type Debug::value( std::string const& val )
{
type l = Debug::NONE;
std::string v(val);
......@@ -75,7 +74,6 @@ Debug::type Debug::value( std::string const& val)
//string tmp(lowercase(v.substr(0, st)));
std::string tmp(v.substr(0, st));
if(tmp.empty())
break;
......@@ -180,5 +178,4 @@ std::string Debug::str( Debug::type level ) noexcept
return "";
}
//DebugStream ulog;
......@@ -44,12 +44,13 @@ using std::cerr;
using std::ios;
//--------------------------------------------------------------------------
/// Constructor, sets the debug level to t.
DebugStream::DebugStream(Debug::type t)
DebugStream::DebugStream(Debug::type t, Debug::verbosity v)
: /* ostream(new debugbuf(cerr.rdbuf())),*/
dt(t), nullstream(new nullbuf), internal(new debugstream_internal),
show_datetime(true), show_logtype(true),
fname(""),
logname("")
logname(""),
verb(v)
{
delete rdbuf(new teebuf(cerr.rdbuf(), &internal->sbuf));
internal->sbuf.signal_overflow().connect(sigc::mem_fun(*this, &DebugStream::sbuf_overflow));
......@@ -98,6 +99,7 @@ const DebugStream& DebugStream::operator=( const DebugStream& r )
return *this;
dt = r.dt;
verb = r.verb;
show_datetime = r.show_datetime;
show_logtype = r.show_logtype;
show_msec = r.show_msec;
......@@ -138,7 +140,7 @@ void DebugStream::logFile( const std::string& f, bool truncate )
if( onScreen )
{
delete rdbuf(new threebuf(cerr.rdbuf(),
&internal->fbuf, &internal->sbuf));
&internal->fbuf, &internal->sbuf));
}
else
{
......@@ -159,19 +161,19 @@ void DebugStream::enableOnScreen()
{
onScreen = true;
// reopen streams
logFile(fname,false);
logFile(fname, false);
}
//--------------------------------------------------------------------------
void DebugStream::disableOnScreen()
{
onScreen = false;
// reopen streams
logFile(fname,false);
logFile(fname, false);
}
//--------------------------------------------------------------------------
std::ostream& DebugStream::debug(Debug::type t) noexcept
{
if(dt & t)
if( (dt & t) && (vv <= verb) )
{
uniset::ios_fmt_restorer ifs(*this);
......@@ -189,15 +191,21 @@ std::ostream& DebugStream::debug(Debug::type t) noexcept
//--------------------------------------------------------------------------
std::ostream& DebugStream::operator()(Debug::type t) noexcept
{
if(dt & t)
if( (dt & t) && (vv <= verb) )
return *this;
return nullstream;
}
//--------------------------------------------------------------------------
DebugStream& DebugStream::V( Debug::verbosity v ) noexcept
{
vv = v;
return *this;
}
//--------------------------------------------------------------------------
std::ostream& DebugStream::printDate(Debug::type t, char brk) noexcept
{
if(dt && t)
if( (dt & t) && (vv <= verb) )
{
uniset::ios_fmt_restorer ifs(*this);
......@@ -220,7 +228,7 @@ std::ostream& DebugStream::printDate(Debug::type t, char brk) noexcept
//--------------------------------------------------------------------------
std::ostream& DebugStream::printTime(Debug::type t, char brk) noexcept
{
if(dt && t)
if( (dt & t) && (vv <= verb) )
{
uniset::ios_fmt_restorer ifs(*this);
......@@ -250,7 +258,7 @@ std::ostream& DebugStream::printTime(Debug::type t, char brk) noexcept
//--------------------------------------------------------------------------
std::ostream& DebugStream::printDateTime(Debug::type t) noexcept
{
if(dt & t)
if( (dt & t) && (vv <= verb) )
{
uniset::ios_fmt_restorer ifs(*this);
......
......@@ -103,7 +103,7 @@ namespace uniset
auto lst = la->getLogList();
for( auto && l : lst )
for( auto&& l : lst )
{
auto c = conmap.find(l.log);
......@@ -217,7 +217,7 @@ namespace uniset
return a->getLogName() < b->getLogName();
});
for( auto && l : lst )
for( auto&& l : lst )
{
auto ag = dynamic_pointer_cast<LogAgregator>(l);
......@@ -350,7 +350,7 @@ namespace uniset
string p2(p + sep);
for( auto && l : lmap )
for( auto&& l : lmap )
{
auto ag = dynamic_pointer_cast<LogAgregator>(l.second);
......
......@@ -432,7 +432,7 @@ namespace uniset
else
lst = alog->getLogList(logname);
for( auto && l : lst )
for( auto&& l : lst )
defaultLogLevels[l.log.get()] = l.log->level();
}
else if( elog )
......@@ -455,7 +455,7 @@ namespace uniset
else
lst = alog->getLogList(logname);
for( auto && l : lst )
for( auto&& l : lst )
{
auto d = defaultLogLevels.find(l.log.get());
......
......@@ -502,7 +502,7 @@ namespace uniset
}
// обрабатываем команды только если нашли подходящие логи
for( auto && l : loglist )
for( auto&& l : loglist )
{
// Обработка команд..
// \warning Работа с логом ведётся без mutex-а, хотя он разделяется отдельными потоками
......
......@@ -71,3 +71,41 @@ TEST_CASE("Debugstream: del levels", "[debugstream][del]" )
REQUIRE(d.is_level3());
}
// -----------------------------------------------------------------------------
static ostringstream test_log_str;
void test_log_buffer(const std::string& txt )
{
test_log_str << txt;
}
TEST_CASE("Debugstream: verbose", "[debugstream][verbose]" )
{
DebugStream d(Debug::INFO);
d.verbose(0);
REQUIRE(d.verbose() == 0);
d.signal_stream_event().connect( &test_log_buffer );
d.V(1)[Debug::INFO] << "text" << endl;
REQUIRE(test_log_str.str() == "" );
test_log_str.str(""); // clean
d.verbose(1);
d.V(1)(Debug::INFO) << "text";
d.V(2)(Debug::INFO) << "text2";
REQUIRE(test_log_str.str() == "text" );
test_log_str.str(""); // clean
d.verbose(2);
d.V(2)(Debug::INFO) << "text";
d.V(100)(Debug::INFO) << "text100";
REQUIRE( test_log_str.str() == "text" );
test_log_str.str(""); // clean
d.verbose(0);
d.V(1).info() << "text";
d.V(2).info() << "text2";
d.V(0).warn() << "text warning";
REQUIRE(test_log_str.str() == "" );
}
// -----------------------------------------------------------------------------
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment