Skip to content

Commit f920d6a

Browse files
BotellaApanquez
authored andcommitted
feat(ProgressLogger): external client support
1 parent 5f47b5b commit f920d6a

File tree

6 files changed

+220
-9
lines changed

6 files changed

+220
-9
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 <string>
27+
28+
#include <geode/basic/common.h>
29+
#include <geode/basic/pimpl.h>
30+
#include <geode/basic/progress_logger_client.h>
31+
32+
namespace geode
33+
{
34+
class opengeode_basic_api ConsoleProgressLoggerClient
35+
: public ProgressLoggerClient
36+
{
37+
public:
38+
ConsoleProgressLoggerClient( const std::string& message );
39+
~ConsoleProgressLoggerClient();
40+
41+
private:
42+
void start( index_t nb_steps ) override;
43+
44+
void update( index_t current_step, index_t nb_steps ) override;
45+
46+
void end( index_t current_step, index_t nb_steps ) override;
47+
48+
private:
49+
IMPLEMENTATION_MEMBER( impl_ );
50+
};
51+
} // 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 <string>
27+
28+
#include <geode/basic/common.h>
29+
#include <geode/basic/factory.h>
30+
31+
namespace geode
32+
{
33+
class opengeode_basic_api ProgressLoggerClient
34+
{
35+
public:
36+
virtual ~ProgressLoggerClient() = default;
37+
38+
virtual void start( index_t nb_steps ) = 0;
39+
40+
virtual void update( index_t current_step, index_t nb_steps ) = 0;
41+
42+
virtual void end( index_t current_step, index_t nb_steps ) = 0;
43+
44+
protected:
45+
ProgressLoggerClient( const std::string& message ) : message_{ message }
46+
{
47+
}
48+
49+
absl::string_view message() const
50+
{
51+
return message_;
52+
}
53+
54+
private:
55+
const std::string& message_;
56+
};
57+
58+
using ProgressLoggerClientFactory =
59+
Factory< std::string, ProgressLoggerClient, const std::string& >;
60+
} // namespace geode

src/geode/basic/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ add_geode_library(
2727
"bitsery_input.cpp"
2828
"bitsery_output.cpp"
2929
"common.cpp"
30+
"console_progress_logger_client.cpp"
3031
"filename.cpp"
3132
"identifier.cpp"
3233
"identifier_builder.cpp"
@@ -46,6 +47,7 @@ add_geode_library(
4647
"attribute.h"
4748
"bitsery_archive.h"
4849
"common.h"
50+
"console_progress_logger_client.h"
4951
"factory.h"
5052
"filename.h"
5153
"identifier.h"
@@ -58,6 +60,7 @@ add_geode_library(
5860
"pimpl.h"
5961
"pimpl_impl.h"
6062
"progress_logger.h"
63+
"progress_logger_client.h"
6164
"range.h"
6265
"singleton.h"
6366
"string.h"

src/geode/basic/common.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@
2323

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

26+
#include <geode/basic/console_progress_logger_client.h>
27+
2628
namespace
2729
{
28-
OPENGEODE_LIBRARY_INITIALIZE( OpenGeode_basic ) {}
30+
OPENGEODE_LIBRARY_INITIALIZE( OpenGeode_basic )
31+
{
32+
geode::ProgressLoggerClientFactory::register_creator<
33+
geode::ConsoleProgressLoggerClient >( "console" );
34+
}
2935
} // namespace
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
#include <geode/basic/console_progress_logger_client.h>
25+
26+
#include <geode/basic/logger.h>
27+
#include <geode/basic/pimpl_impl.h>
28+
#include <geode/basic/timer.h>
29+
30+
namespace geode
31+
{
32+
class ConsoleProgressLoggerClient::Impl
33+
{
34+
public:
35+
void start( absl::string_view message, index_t /*nb_steps */ )
36+
{
37+
Logger::info( message, " started" );
38+
}
39+
40+
void update(
41+
absl::string_view message, index_t current, index_t nb_steps )
42+
{
43+
const auto percent = std::floor( current / nb_steps * 100 );
44+
Logger::info(
45+
message, " ", current, "/", nb_steps, "(", percent, "%)" );
46+
}
47+
48+
void end( absl::string_view message, index_t current, index_t nb_steps )
49+
{
50+
const auto status = current == nb_steps ? "completed" : "failed";
51+
Logger::info( message, " ", status, " in ", timer_.duration() );
52+
}
53+
54+
private:
55+
DEBUG_CONST Timer timer_;
56+
};
57+
58+
ConsoleProgressLoggerClient::ConsoleProgressLoggerClient(
59+
const std::string& message )
60+
: ProgressLoggerClient{ message }
61+
{
62+
}
63+
64+
ConsoleProgressLoggerClient::~ConsoleProgressLoggerClient() {}
65+
66+
void ConsoleProgressLoggerClient::start( index_t nb_steps )
67+
{
68+
impl_->start( message(), nb_steps );
69+
}
70+
71+
void ConsoleProgressLoggerClient::update(
72+
index_t current, index_t nb_steps )
73+
{
74+
impl_->update( message(), current, nb_steps );
75+
}
76+
77+
void ConsoleProgressLoggerClient::end( index_t current, index_t nb_steps )
78+
{
79+
impl_->end( message(), current, nb_steps );
80+
}
81+
} // namespace geode

src/geode/basic/progress_logger.cpp

Lines changed: 18 additions & 8 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/timer.h>
30+
#include <geode/basic/progress_logger_client.h>
3131

3232
namespace
3333
{
@@ -44,13 +44,21 @@ namespace geode
4444
nb_steps_( nb_steps ),
4545
current_time_{ absl::Now() }
4646
{
47-
Logger::info( message_, " started" );
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+
}
4854
}
4955

5056
~Impl()
5157
{
52-
const auto status = current_ == nb_steps_ ? "completed" : "failed";
53-
Logger::info( message_, " ", status, " in ", timer_.duration() );
58+
for( auto& logger : loggers_ )
59+
{
60+
logger->end( current_, nb_steps_ );
61+
}
5462
}
5563

5664
void increment()
@@ -61,9 +69,10 @@ namespace geode
6169
if( now - current_time_ > SLEEP )
6270
{
6371
current_time_ = now;
64-
const auto percent = std::floor( current_ / nb_steps_ * 100 );
65-
Logger::info( message_, " ", current_, "/", nb_steps_, " (",
66-
percent, "%)" );
72+
for( auto& logger : loggers_ )
73+
{
74+
logger->update( current_, nb_steps_ );
75+
}
6776
}
6877
}
6978

@@ -77,9 +86,10 @@ namespace geode
7786
DEBUG_CONST std::string message_;
7887
double nb_steps_;
7988
index_t current_{ 0 };
80-
DEBUG_CONST Timer timer_;
8189
absl::Time current_time_;
8290
absl::Mutex lock_;
91+
absl::InlinedVector< std::unique_ptr< ProgressLoggerClient >, 2 >
92+
loggers_;
8393
};
8494

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

0 commit comments

Comments
 (0)