Commit be4f3692 authored by Pavel Vainerman's avatar Pavel Vainerman

[debug log]: supported labels

parent b34bf169
......@@ -22,6 +22,7 @@
#include <iostream>
#include <string>
#include <sigc++/sigc++.h>
#include <vector>
#include "Debug.h"
#ifdef TEST_DEBUGSTREAM
......@@ -220,6 +221,16 @@ class DebugStream : public std::ostream
show_logtype = s;
}
inline void showLabels(bool s) noexcept
{
show_labels = s;
}
inline void hideLabelKey(bool s) noexcept
{
hide_label_key = s;
}
inline std::ostream& log(Debug::type l) noexcept
{
return this->operator[](l);
......@@ -239,6 +250,15 @@ class DebugStream : public std::ostream
// example: dlog.V(1)[Debug::INFO] << "some log.." << endl;
DebugStream& V( Debug::verbosity v ) noexcept;
// labels
typedef std::pair<std::string, std::string> Label;
void addLabel( const std::string& key, const std::string& value ) noexcept;
void delLabel( const std::string& key ) noexcept;
void cleanupLabels() noexcept;
std::vector<Label> getLabels() noexcept;
// -----------------------------------------------------
// короткие функции (для удобства)
// log.level1() - вывод с датой и временем "date time [LEVEL] ...",
// если вывод даты и времени не выключен при помощи showDateTime(false)
......@@ -315,8 +335,12 @@ class DebugStream : public std::ostream
bool isWriteLogFile = { false };
bool onScreen = { true };
Debug::verbosity verb = 0;
Debug::verbosity vv = 0;
Debug::verbosity verb = { 0 };
Debug::verbosity vv = { 0 };
std::vector<Label> labels;
bool show_labels = { true };
bool hide_label_key = { false };
};
// ------------------------------------------------------------------------------------------------
......
......@@ -18,19 +18,13 @@
#include "Debug.h"
#include "Mutex.h"
//�Since the current C++ lib in egcs does not have a standard implementation
// of basic_streambuf and basic_filebuf we don't have to include this
// header.
//#define MODERN_STL_STREAMS
#ifdef MODERN_STL_STREAMS
#include <fstream>
#endif
#include <iostream>
#include <sstream>
#include <iomanip>
#include <time.h>
#include <iomanip>
#include <ctime>
#include <algorithm>
#include "DebugExtBuf.h"
#include "UniSetTypes.h"
......@@ -183,6 +177,19 @@ std::ostream& DebugStream::debug(Debug::type t) noexcept
if( show_logtype )
*this << "(" << std::setfill(' ') << std::setw(6) << t << "): "; // "):\t";
if( show_labels )
{
for( const auto& l : labels )
{
*this << "[";
if( !hide_label_key )
*this << l.first << "=";
*this << l.second << "]";
}
}
return *this;
}
......@@ -301,6 +308,37 @@ DebugStream::StreamEvent_Signal DebugStream::signal_stream_event()
return s_stream;
}
//--------------------------------------------------------------------------
void DebugStream::addLabel( const std::string& key, const std::string& value ) noexcept
{
auto it = std::find_if(labels.begin(), labels.end(), [key] (const Label & l)
{
return l.first == key;
} );
if( it == labels.end() )
labels.emplace_back(key, value);
}
//--------------------------------------------------------------------------
void DebugStream::delLabel( const std::string& key ) noexcept
{
auto it = std::find_if(labels.begin(), labels.end(), [key] (const Label & l)
{
return l.first == key;
} );
if( it != labels.end() )
labels.erase(it);
}
//--------------------------------------------------------------------------
void DebugStream::cleanupLabels() noexcept
{
labels.clear();
}
//--------------------------------------------------------------------------
std::vector<DebugStream::Label> DebugStream::getLabels() noexcept
{
return labels;
}
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
#ifdef TEST_DEBUGSTREAM
......
......@@ -86,10 +86,18 @@ TEST_CASE("Debugstream: verbose", "[debugstream][verbose]" )
d.signal_stream_event().connect( &test_log_buffer );
d.V(1)[Debug::INFO] << "text" << endl;
d.V(1)[Debug::INFO] << "text";
REQUIRE(test_log_str.str() == "" );
test_log_str.str(""); // clean
d.V(0)(Debug::INFO) << "text";
REQUIRE(test_log_str.str().find("text") != std::string::npos );
test_log_str.str(""); // clean
d[Debug::INFO] << "text";
REQUIRE(test_log_str.str().find("text") != std::string::npos );
test_log_str.str(""); // clean
d.verbose(1);
d.V(1)(Debug::INFO) << "text";
d.V(2)(Debug::INFO) << "text2";
......@@ -109,3 +117,37 @@ TEST_CASE("Debugstream: verbose", "[debugstream][verbose]" )
REQUIRE(test_log_str.str() == "" );
}
// -----------------------------------------------------------------------------
TEST_CASE("Debugstream: labels", "[debugstream][labels]" )
{
DebugStream d(Debug::INFO);
d.signal_stream_event().connect( &test_log_buffer );
d.addLabel("module", "uniset");
d.addLabel("version", std::to_string(100));
auto labels = d.getLabels();
REQUIRE( labels.size() == 2 );
REQUIRE( labels[0].first == "module" );
REQUIRE( labels[0].second == "uniset" );
test_log_str.str(""); // clean
d[Debug::INFO] << "text";
REQUIRE( test_log_str.str().find("[module=uniset][version=100]") != std::string::npos );
d.delLabel("module");
test_log_str.str(""); // clean
d[Debug::INFO] << "text";
REQUIRE( test_log_str.str().find("[module=uniset]") == std::string::npos );
d.cleanupLabels();
labels = d.getLabels();
REQUIRE( labels.size() == 0 );
test_log_str.str(""); // clean
d[Debug::INFO] << "text";
REQUIRE( test_log_str.str().find("[version=100]") == std::string::npos );
}
// -----------------------------------------------------------------------------
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