Commit b34bf169 authored by Pavel Vainerman's avatar Pavel Vainerman

[debug log]; supported "verbosity level"

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