Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Logger): you can choose the LEVEL of ProgressLogger messages #1075

Open
wants to merge 3 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions include/geode/basic/console_progress_logger_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,33 @@ namespace geode

private:
void start( const uuid& progress_logger_id,
Logger::LEVEL level,
const std::string& message,
index_t nb_steps ) override;

void update( const uuid& progress_logger_id,
Logger::LEVEL level,
index_t current_step,
index_t nb_steps ) override;

void completed( const uuid& progress_logger_id ) override;
void completed(
const uuid& progress_logger_id, Logger::LEVEL level ) override;

void failed( const uuid& progress_logger_id ) override;
void failed(
const uuid& progress_logger_id, Logger::LEVEL level ) override;

[[deprecated]] void start( const uuid& progress_logger_id,
const std::string& message,
index_t nb_steps ) override;

[[deprecated]] void update( const uuid& progress_logger_id,
index_t current_step,
index_t nb_steps ) override;

[[deprecated]] void completed(
const uuid& progress_logger_id ) override;

[[deprecated]] void failed( const uuid& progress_logger_id ) override;

private:
IMPLEMENTATION_MEMBER( impl_ );
Expand Down
7 changes: 7 additions & 0 deletions include/geode/basic/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ namespace geode

static void set_level( LEVEL level );

template < typename... Args >
static void log( LEVEL level, const Args &...args )
{
log( level, absl::StrCat( args... ) );
}

template < typename... Args >
static void trace( const Args &...args )
{
Expand Down Expand Up @@ -96,6 +102,7 @@ namespace geode

[[nodiscard]] static Logger &instance();

static void log( LEVEL level, const std::string &message );
static void log_trace( const std::string &message );
static void log_debug( const std::string &message );
static void log_info( const std::string &message );
Expand Down
6 changes: 5 additions & 1 deletion include/geode/basic/progress_logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <string>

#include <geode/basic/common.hpp>
#include <geode/basic/logger.hpp>
#include <geode/basic/pimpl.hpp>

namespace absl
Expand All @@ -38,7 +39,10 @@ namespace geode
class opengeode_basic_api ProgressLogger
{
public:
ProgressLogger( const std::string& message, index_t nb_steps );
[[deprecated]] ProgressLogger(
const std::string& message, index_t nb_steps );
ProgressLogger(
Logger::LEVEL level, const std::string& message, index_t nb_steps );
~ProgressLogger();

index_t increment();
Expand Down
23 changes: 21 additions & 2 deletions include/geode/basic/progress_logger_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <string>

#include <geode/basic/common.hpp>
#include <geode/basic/logger.hpp>

namespace geode
{
Expand All @@ -40,16 +41,34 @@ namespace geode
virtual ~ProgressLoggerClient() = default;

virtual void start( const uuid& progress_logger_id,
Logger::LEVEL level,
const std::string& message,
index_t nb_steps ) = 0;

virtual void update( const uuid& progress_logger_id,
Logger::LEVEL level,
index_t current_step,
index_t nb_steps ) = 0;

virtual void completed( const uuid& progress_logger_id ) = 0;
virtual void completed(
const uuid& progress_logger_id, Logger::LEVEL level ) = 0;

virtual void failed( const uuid& progress_logger_id ) = 0;
virtual void failed(
const uuid& progress_logger_id, Logger::LEVEL level ) = 0;

[[deprecated]] virtual void start( const uuid& progress_logger_id,
const std::string& message,
index_t nb_steps ) = 0;

[[deprecated]] virtual void update( const uuid& progress_logger_id,
index_t current_step,
index_t nb_steps ) = 0;

[[deprecated]] virtual void completed(
const uuid& progress_logger_id ) = 0;

[[deprecated]] virtual void failed(
const uuid& progress_logger_id ) = 0;

protected:
ProgressLoggerClient() = default;
Expand Down
20 changes: 18 additions & 2 deletions include/geode/basic/progress_logger_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,32 @@ namespace geode
std::unique_ptr< ProgressLoggerClient >&& client );

static void start( const uuid& progress_logger_id,
Logger::LEVEL level,
const std::string& message,
index_t nb_steps );

static void update( const uuid& progress_logger_id,
Logger::LEVEL level,
index_t current_step,
index_t nb_steps );

static void completed( const uuid& progress_logger_id );
static void completed(
const uuid& progress_logger_id, Logger::LEVEL level );

static void failed( const uuid& progress_logger_id );
static void failed(
const uuid& progress_logger_id, Logger::LEVEL level );

[[deprecated]] static void start( const uuid& progress_logger_id,
const std::string& message,
index_t nb_steps );

[[deprecated]] static void update( const uuid& progress_logger_id,
index_t current_step,
index_t nb_steps );

[[deprecated]] static void completed( const uuid& progress_logger_id );

[[deprecated]] static void failed( const uuid& progress_logger_id );

private:
ProgressLoggerManager();
Expand Down
62 changes: 48 additions & 14 deletions src/geode/basic/console_progress_logger_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,34 +48,38 @@ namespace geode

public:
void start( const uuid& progress_logger_id,
Logger::LEVEL level,
const std::string& message,
index_t /*nb_steps */ )
{
info_.emplace( progress_logger_id, message );
Logger::info( message, " started" );
Logger::log( level, message, " started" );
}

void update(
const uuid& progress_logger_id, index_t current, index_t nb_steps )
void update( const uuid& progress_logger_id,
Logger::LEVEL level,
index_t current,
index_t nb_steps )
{
const auto percent =
std::floor( static_cast< double >( current ) / nb_steps * 100 );
Logger::info( info_.at( progress_logger_id ).message, " ", current,
"/", nb_steps, " (", percent, "%)" );
Logger::log( level, info_.at( progress_logger_id ).message, " ",
current, "/", nb_steps, " (", percent, "%)" );
}

void completed( const uuid& progress_logger_id )
void completed( const uuid& progress_logger_id, Logger::LEVEL level )
{
const auto& info = info_.at( progress_logger_id );
Logger::info(
info.message, " completed in ", info.timer.duration() );
Logger::log(
level, info.message, " completed in ", info.timer.duration() );
info_.erase( progress_logger_id );
}

void failed( const uuid& progress_logger_id )
void failed( const uuid& progress_logger_id, Logger::LEVEL level )
{
const auto& info = info_.at( progress_logger_id );
Logger::info( info.message, " failed in ", info.timer.duration() );
Logger::log(
level, info.message, " failed in ", info.timer.duration() );
info_.erase( progress_logger_id );
}

Expand All @@ -88,26 +92,56 @@ namespace geode
ConsoleProgressLoggerClient::~ConsoleProgressLoggerClient() = default;

void ConsoleProgressLoggerClient::start( const uuid& progress_logger_id,
Logger::LEVEL level,
const std::string& message,
index_t nb_steps )
{
impl_->start( progress_logger_id, message, nb_steps );
impl_->start( progress_logger_id, level, message, nb_steps );
}

void ConsoleProgressLoggerClient::update( const uuid& progress_logger_id,
Logger::LEVEL level,
index_t current,
index_t nb_steps )
{
impl_->update( progress_logger_id, level, current, nb_steps );
}

void ConsoleProgressLoggerClient::completed(
const uuid& progress_logger_id, Logger::LEVEL level )
{
impl_->completed( progress_logger_id, level );
}

void ConsoleProgressLoggerClient::failed(
const uuid& progress_logger_id, Logger::LEVEL level )
{
impl_->failed( progress_logger_id, level );
}

void ConsoleProgressLoggerClient::start( const uuid& progress_logger_id,
const std::string& message,
index_t nb_steps )
{
impl_->start(
progress_logger_id, Logger::LEVEL::info, message, nb_steps );
}

void ConsoleProgressLoggerClient::update(
const uuid& progress_logger_id, index_t current, index_t nb_steps )
{
impl_->update( progress_logger_id, current, nb_steps );
impl_->update(
progress_logger_id, Logger::LEVEL::info, current, nb_steps );
}

void ConsoleProgressLoggerClient::completed(
const uuid& progress_logger_id )
{
impl_->completed( progress_logger_id );
impl_->completed( progress_logger_id, Logger::LEVEL::info );
}

void ConsoleProgressLoggerClient::failed( const uuid& progress_logger_id )
{
impl_->failed( progress_logger_id );
impl_->failed( progress_logger_id, Logger::LEVEL::info );
}
} // namespace geode
23 changes: 23 additions & 0 deletions src/geode/basic/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include <geode/basic/logger.hpp>
#include <iostream>

#include <absl/container/flat_hash_map.h>

#include <geode/basic/logger_manager.hpp>
#include <geode/basic/pimpl_impl.hpp>

Expand All @@ -41,6 +43,11 @@ namespace geode
level_ = level;
}

void log( LEVEL level, const std::string &message )
{
leveled_log.at( level )( message );
}

void log_trace( const std::string &message )
{
if( level_ <= LEVEL::trace )
Expand Down Expand Up @@ -90,6 +97,17 @@ namespace geode
}

private:
const absl::flat_hash_map< geode::Logger::LEVEL,
std::function< void( const std::string & ) > >
leveled_log{
{ geode::Logger::LEVEL::trace, geode::Logger::log_trace },
{ geode::Logger::LEVEL::debug, geode::Logger::log_debug },
{ geode::Logger::LEVEL::info, geode::Logger::log_info },
{ geode::Logger::LEVEL::warn, geode::Logger::log_warn },
{ geode::Logger::LEVEL::err, geode::Logger::log_error },
{ geode::Logger::LEVEL::critical, geode::Logger::log_critical }
};

LEVEL level_{ LEVEL::info };
};

Expand All @@ -113,6 +131,11 @@ namespace geode
instance().impl_->set_level( level );
}

void Logger::log( LEVEL level, const std::string &message )
{
instance().impl_->log( level, message );
}

void Logger::log_trace( const std::string &message )
{
instance().impl_->log_trace( message );
Expand Down
25 changes: 18 additions & 7 deletions src/geode/basic/progress_logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,24 @@ namespace geode
class ProgressLogger::Impl
{
public:
Impl( const std::string& message, index_t nb_steps )
: nb_steps_( nb_steps ), current_time_{ absl::Now() }
Impl(
Logger::LEVEL level, const std::string& message, index_t nb_steps )
: nb_steps_( nb_steps ),
current_time_{ absl::Now() },
level_( level )
{
ProgressLoggerManager::start( id_, message, nb_steps_ );
ProgressLoggerManager::start( id_, level, message, nb_steps_ );
}

~Impl()
{
if( current_ == nb_steps_ )
{
ProgressLoggerManager::completed( id_ );
ProgressLoggerManager::completed( id_, level_ );
}
else
{
ProgressLoggerManager::failed( id_ );
ProgressLoggerManager::failed( id_, level_ );
}
}

Expand All @@ -63,7 +66,8 @@ namespace geode
if( now - current_time_ > refresh_interval_ )
{
current_time_ = now;
ProgressLoggerManager::update( id_, current_, nb_steps_ );
ProgressLoggerManager::update(
id_, level_, current_, nb_steps_ );
}
return current_;
}
Expand All @@ -87,11 +91,18 @@ namespace geode
absl::Time current_time_;
std::mutex lock_;
absl::Duration refresh_interval_{ absl::Seconds( 1 ) };
Logger::LEVEL level_;
};

ProgressLogger::ProgressLogger(
const std::string& message, index_t nb_steps )
: impl_( message, nb_steps )
: ProgressLogger( Logger::LEVEL::info, message, nb_steps )
{
}

ProgressLogger::ProgressLogger(
Logger::LEVEL level, const std::string& message, index_t nb_steps )
: impl_( level, message, nb_steps )
{
}

Expand Down
Loading
Loading