Skip to content

Commit

Permalink
Logger
Browse files Browse the repository at this point in the history
* added internal diagnostic to tract produced errors and warnings for
  now, maybe we add later a generic tracking of all available Log::Level
  kinds
* added new API to fetch 'errors()' and 'warnings()' counter values from
  the internal diagnostic of a logger
  • Loading branch information
ppaulweber committed May 7, 2017
1 parent 9124498 commit 84fe1df
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
24 changes: 24 additions & 0 deletions cpp/Log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ Logger::Logger( libstdhl::Log::Stream& stream )
: m_stream( stream )
, m_source( Source::defaultSource() )
, m_category( Category::defaultCategory() )
, m_errors( 0 )
, m_warnings( 0 )
{
}

Expand Down Expand Up @@ -122,6 +124,11 @@ void Logger::error( const char* format, ... )
va_end( args );
}

u64 Logger::errors( void ) const
{
return m_errors;
}

void Logger::warning( const std::string& text )
{
log( Log::Level::ID::WARNING, m_source, m_category, text );
Expand All @@ -135,6 +142,11 @@ void Logger::warning( const char* format, ... )
va_end( args );
}

u64 Logger::warnings( void ) const
{
return m_warnings;
}

void Logger::info( const std::string& text )
{
log( Log::Level::ID::INFORMATIONAL, m_source, m_category, text );
Expand Down Expand Up @@ -209,6 +221,18 @@ Category::Ptr Logger::category( void ) const
return m_category;
}

void Logger::diagnostic( const Log::Data& data )
{
if( data.level().id() == Log::Level::ID::ERROR )
{
m_errors++;
}
else if( data.level().id() == Log::Level::ID::WARNING )
{
m_warnings++;
}
}

//
// Local variables:
// mode: c++
Expand Down
8 changes: 8 additions & 0 deletions cpp/Log.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,11 @@ namespace libstdhl

void error( const std::string& text );
void error( const char* format, ... );
u64 errors( void ) const;

void warning( const std::string& text );
void warning( const char* format, ... );
u64 warnings( void ) const;

void info( const std::string& text );
void info( const char* format, ... );
Expand All @@ -109,13 +111,15 @@ namespace libstdhl
void log( Args&&... args )
{
m_stream.add( std::forward< Args >( args )... );
diagnostic( m_stream.data().back() );
}

template < const Log::Level::ID LEVEL, typename... Args >
void log( Args&&... args )
{
m_stream.add( LEVEL, source(), category(),
Log::Items( { std::forward< Args >( args )... } ) );
diagnostic( m_stream.data().back() );
}

Log::Stream& stream( void );
Expand All @@ -129,9 +133,13 @@ namespace libstdhl
Log::Category::Ptr category( void ) const;

private:
void diagnostic( const Log::Data& data );

Log::Stream& m_stream;
Log::Source::Ptr m_source;
Log::Category::Ptr m_category;
u64 m_errors;
u64 m_warnings;
};
}

Expand Down

0 comments on commit 84fe1df

Please sign in to comment.