Skip to content

Commit dde82b6

Browse files
committed
fix(ProgressLogger): add a Manager to better handle extensibility
1 parent 2676b07 commit dde82b6

File tree

9 files changed

+266
-61
lines changed

9 files changed

+266
-61
lines changed

include/geode/basic/console_progress_logger_client.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,17 @@ namespace geode
3535
: public ProgressLoggerClient
3636
{
3737
public:
38-
ConsoleProgressLoggerClient( const std::string& message );
38+
ConsoleProgressLoggerClient();
3939
~ConsoleProgressLoggerClient();
4040

4141
private:
42-
void start( index_t nb_steps ) override;
42+
void start( const std::string& message, index_t nb_steps ) override;
4343

4444
void update( index_t current_step, index_t nb_steps ) override;
4545

46-
void end( index_t current_step, index_t nb_steps ) override;
46+
void completed() override;
47+
48+
void failed() override;
4749

4850
private:
4951
IMPLEMENTATION_MEMBER( impl_ );

include/geode/basic/progress_logger_client.h

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#include <string>
2727

2828
#include <geode/basic/common.h>
29-
#include <geode/basic/factory.h>
3029

3130
namespace geode
3231
{
@@ -35,26 +34,15 @@ namespace geode
3534
public:
3635
virtual ~ProgressLoggerClient() = default;
3736

38-
virtual void start( index_t nb_steps ) = 0;
37+
virtual void start( const std::string& message, index_t nb_steps ) = 0;
3938

4039
virtual void update( index_t current_step, index_t nb_steps ) = 0;
4140

42-
virtual void end( index_t current_step, index_t nb_steps ) = 0;
41+
virtual void completed() = 0;
4342

44-
protected:
45-
ProgressLoggerClient( const std::string& message ) : message_{ message }
46-
{
47-
}
48-
49-
absl::string_view message() const
50-
{
51-
return message_;
52-
}
43+
virtual void failed() = 0;
5344

54-
private:
55-
const std::string& message_;
45+
protected:
46+
ProgressLoggerClient() = default;
5647
};
57-
58-
using ProgressLoggerClientFactory =
59-
Factory< std::string, ProgressLoggerClient, const std::string& >;
6048
} // namespace geode
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright (c) 2019 - 2022 Geode-solutions
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*
22+
*/
23+
24+
#pragma once
25+
26+
#include <geode/basic/common.h>
27+
#include <geode/basic/pimpl.h>
28+
29+
namespace geode
30+
{
31+
class ProgressLoggerClient;
32+
} // namespace geode
33+
34+
namespace geode
35+
{
36+
class opengeode_basic_api ProgressLoggerManager
37+
{
38+
public:
39+
~ProgressLoggerManager();
40+
41+
static void register_client(
42+
std::unique_ptr< ProgressLoggerClient >&& client );
43+
44+
static void start( const std::string& message, index_t nb_steps );
45+
46+
static void update( index_t current_step, index_t nb_steps );
47+
48+
static void completed();
49+
50+
static void failed();
51+
52+
private:
53+
ProgressLoggerManager();
54+
55+
static ProgressLoggerManager& instance();
56+
57+
private:
58+
IMPLEMENTATION_MEMBER( impl_ );
59+
};
60+
} // namespace geode

src/geode/basic/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ add_geode_library(
3434
"logger.cpp"
3535
"permutation.cpp"
3636
"progress_logger.cpp"
37+
"progress_logger_manager.cpp"
3738
"singleton.cpp"
3839
"string.cpp"
3940
"timer.cpp"
@@ -61,6 +62,7 @@ add_geode_library(
6162
"pimpl_impl.h"
6263
"progress_logger.h"
6364
"progress_logger_client.h"
65+
"progress_logger_manager.h"
6466
"range.h"
6567
"singleton.h"
6668
"string.h"

src/geode/basic/common.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,16 @@
2323

2424
#include <geode/basic/common.h>
2525

26+
#include <absl/memory/memory.h>
27+
2628
#include <geode/basic/console_progress_logger_client.h>
29+
#include <geode/basic/progress_logger_manager.h>
2730

2831
namespace
2932
{
3033
OPENGEODE_LIBRARY_INITIALIZE( OpenGeode_basic )
3134
{
32-
geode::ProgressLoggerClientFactory::register_creator<
33-
geode::ConsoleProgressLoggerClient >( "console" );
35+
geode::ProgressLoggerManager::register_client(
36+
absl::make_unique< geode::ConsoleProgressLoggerClient >() );
3437
}
3538
} // namespace

src/geode/basic/console_progress_logger_client.cpp

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,50 +32,58 @@ namespace geode
3232
class ConsoleProgressLoggerClient::Impl
3333
{
3434
public:
35-
void start( absl::string_view message, index_t /*nb_steps */ )
35+
void start( const std::string& message, index_t /*nb_steps */ )
3636
{
37-
Logger::info( message, " started" );
37+
message_ = message;
38+
Logger::info( message_, " started" );
3839
}
3940

40-
void update(
41-
absl::string_view message, index_t current, index_t nb_steps )
41+
void update( index_t current, index_t nb_steps )
4242
{
43-
const auto percent = std::floor( current / nb_steps * 100 );
43+
const auto percent =
44+
std::floor( static_cast< double >( current ) / nb_steps * 100 );
4445
Logger::info(
45-
message, " ", current, "/", nb_steps, "(", percent, "%)" );
46+
message_, " ", current, "/", nb_steps, " (", percent, "%)" );
4647
}
4748

48-
void end( absl::string_view message, index_t current, index_t nb_steps )
49+
void completed()
4950
{
50-
const auto status = current == nb_steps ? "completed" : "failed";
51-
Logger::info( message, " ", status, " in ", timer_.duration() );
51+
Logger::info( message_, " completed in ", timer_.duration() );
52+
}
53+
54+
void failed()
55+
{
56+
Logger::info( message_, " failed in ", timer_.duration() );
5257
}
5358

5459
private:
5560
DEBUG_CONST Timer timer_;
61+
std::string message_;
5662
};
5763

58-
ConsoleProgressLoggerClient::ConsoleProgressLoggerClient(
59-
const std::string& message )
60-
: ProgressLoggerClient{ message }
61-
{
62-
}
64+
ConsoleProgressLoggerClient::ConsoleProgressLoggerClient() {}
6365

6466
ConsoleProgressLoggerClient::~ConsoleProgressLoggerClient() {}
6567

66-
void ConsoleProgressLoggerClient::start( index_t nb_steps )
68+
void ConsoleProgressLoggerClient::start(
69+
const std::string& message, index_t nb_steps )
6770
{
68-
impl_->start( message(), nb_steps );
71+
impl_->start( message, nb_steps );
6972
}
7073

7174
void ConsoleProgressLoggerClient::update(
7275
index_t current, index_t nb_steps )
7376
{
74-
impl_->update( message(), current, nb_steps );
77+
impl_->update( current, nb_steps );
78+
}
79+
80+
void ConsoleProgressLoggerClient::completed()
81+
{
82+
impl_->completed();
7583
}
7684

77-
void ConsoleProgressLoggerClient::end( index_t current, index_t nb_steps )
85+
void ConsoleProgressLoggerClient::failed()
7886
{
79-
impl_->end( message(), current, nb_steps );
87+
impl_->failed();
8088
}
8189
} // namespace geode

src/geode/basic/progress_logger.cpp

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
#include <geode/basic/logger.h>
2929
#include <geode/basic/pimpl_impl.h>
30-
#include <geode/basic/progress_logger_client.h>
30+
#include <geode/basic/progress_logger_manager.h>
3131

3232
namespace
3333
{
@@ -40,24 +40,20 @@ namespace geode
4040
{
4141
public:
4242
Impl( std::string message, index_t nb_steps )
43-
: message_{ std::move( message ) },
44-
nb_steps_( nb_steps ),
45-
current_time_{ absl::Now() }
43+
: nb_steps_( nb_steps ), current_time_{ absl::Now() }
4644
{
47-
for( const auto& key :
48-
ProgressLoggerClientFactory::list_creators() )
49-
{
50-
auto& logger = loggers_.emplace_back(
51-
ProgressLoggerClientFactory::create( key, message_ ) );
52-
logger->start( nb_steps_ );
53-
}
45+
ProgressLoggerManager::start( std::move( message ), nb_steps_ );
5446
}
5547

5648
~Impl()
5749
{
58-
for( auto& logger : loggers_ )
50+
if( current_ == nb_steps_ )
51+
{
52+
ProgressLoggerManager::completed();
53+
}
54+
else
5955
{
60-
logger->end( current_, nb_steps_ );
56+
ProgressLoggerManager::failed();
6157
}
6258
}
6359

@@ -69,10 +65,7 @@ namespace geode
6965
if( now - current_time_ > SLEEP )
7066
{
7167
current_time_ = now;
72-
for( auto& logger : loggers_ )
73-
{
74-
logger->update( current_, nb_steps_ );
75-
}
68+
ProgressLoggerManager::update( current_, nb_steps_ );
7669
}
7770
}
7871

@@ -83,13 +76,10 @@ namespace geode
8376
}
8477

8578
private:
86-
DEBUG_CONST std::string message_;
8779
double nb_steps_;
8880
index_t current_{ 0 };
8981
absl::Time current_time_;
9082
absl::Mutex lock_;
91-
absl::InlinedVector< std::unique_ptr< ProgressLoggerClient >, 2 >
92-
loggers_;
9383
};
9484

9585
ProgressLogger::ProgressLogger( std::string message, index_t nb_steps )

0 commit comments

Comments
 (0)