Skip to content

Commit

Permalink
feat(ProgressLogger): allow to change the refresh interval
Browse files Browse the repository at this point in the history
  • Loading branch information
yo35 committed Aug 13, 2024
1 parent 963b9c6 commit 1b2cfaa
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
6 changes: 5 additions & 1 deletion include/geode/basic/progress_logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

#include <string>

#include <absl/time/clock.h>

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

Expand All @@ -33,7 +35,9 @@ namespace geode
class opengeode_basic_api ProgressLogger
{
public:
ProgressLogger( const std::string& message, index_t nb_steps );
ProgressLogger( const std::string& message,
index_t nb_steps,
absl::Duration refresh_interval = absl::Seconds( 1 ) );
~ProgressLogger();

index_t increment();
Expand Down
25 changes: 12 additions & 13 deletions src/geode/basic/progress_logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,22 @@

#include <mutex>

#include <absl/time/clock.h>

#include <geode/basic/logger.hpp>
#include <geode/basic/pimpl_impl.hpp>
#include <geode/basic/progress_logger_manager.hpp>
#include <geode/basic/uuid.hpp>

namespace
{
constexpr absl::Duration SLEEP = absl::Seconds( 1 );
} // namespace

namespace geode
{
class ProgressLogger::Impl
{
public:
Impl( const std::string& message, index_t nb_steps )
: nb_steps_( nb_steps ), current_time_{ absl::Now() }
Impl( const std::string& message,
index_t nb_steps,
absl::Duration refresh_interval )
: nb_steps_( nb_steps ),
current_time_{ absl::Now() },
refresh_interval_{ refresh_interval }
{
ProgressLoggerManager::start( id_, message, nb_steps_ );
}
Expand All @@ -65,7 +62,7 @@ namespace geode
const std::lock_guard< std::mutex > locking{ lock_ };
current_ += nb_increments;
auto now = absl::Now();
if( now - current_time_ > SLEEP )
if( now - current_time_ > refresh_interval_ )
{
current_time_ = now;
ProgressLoggerManager::update( id_, current_, nb_steps_ );
Expand All @@ -85,12 +82,14 @@ namespace geode
index_t nb_steps_;
index_t current_{ 0 };
absl::Time current_time_;
absl::Duration refresh_interval_;
std::mutex lock_;
};

ProgressLogger::ProgressLogger(
const std::string& message, index_t nb_steps )
: impl_( message, nb_steps )
ProgressLogger::ProgressLogger( const std::string& message,
index_t nb_steps,
absl::Duration refresh_interval )
: impl_( message, nb_steps, refresh_interval )
{
}

Expand Down

0 comments on commit 1b2cfaa

Please sign in to comment.