Consolix
Toggle main menu visibility
Loading...
Searching...
No Matches
example_application_main_loop.cpp
Go to the documentation of this file.
1
2
3
#include <
consolix/core.hpp
>
4
9
struct
AppConfig
{
10
std::string
text
;
11
std::vector<std::string>
items
;
12
int
period
;
13
bool
debug_mode
;
14
16
NLOHMANN_DEFINE_TYPE_INTRUSIVE(
AppConfig
,
text
,
items
,
period
,
debug_mode
)
17
};
18
26
class
CustomLoop
final :
public
consolix::BaseLoopComponent
{
27
public
:
28
29
virtual
~CustomLoop
() =
default
;
30
32
bool
on_once
()
override
{
33
CONSOLIX_STREAM
() <<
34
consolix::color
(
consolix::TextColor::Green
) <<
"Hello, "
<<
35
consolix::color
(
consolix::TextColor::Yellow
) <<
"world!"
;
36
37
auto
args =
consolix::get_service<consolix::CliArguments>
();
38
if
(args.count(
"help"
)) {
39
CONSOLIX_STREAM
() <<
consolix::get_service<consolix::CliOptions>
().help();
40
std::exit(0);
41
}
42
43
auto
config =
consolix::get_service<AppConfig>
();
44
CONSOLIX_SET_DEBUG_MODE(config.debug_mode);
45
46
LOGIT_TRACE0();
47
48
// Делаем тестовую запись в логгер с уникальными файлами
49
CONSOLIX_UNIQUE_FILE_STREAM() <<
"Test 123"
;
50
LOGIT_PRINT_INFO(
"Unique log file: "
, CONSOLIX_UNIQUE_FILE_NAME());
51
return
true
;
52
}
53
55
void
on_loop
()
override
{
56
// Retrieve configuration
57
const
auto
& config =
consolix::get_service<AppConfig>
();
58
59
// Print text and items
60
CONSOLIX_STREAM
() << config.text;
61
CONSOLIX_STREAM
() <<
consolix::color
(
consolix::TextColor::Green
) <<
"items:"
;
62
for
(
auto
&item : config.items) {
63
CONSOLIX_STREAM
() <<
consolix::color
(
consolix::TextColor::Cyan
) << item;
64
}
65
66
// Sleep for the configured period
67
std::this_thread::sleep_for(std::chrono::milliseconds(config.period));
68
69
LOGIT_TRACE0();
70
}
71
74
void
on_shutdown
(
int
signal)
override
{
75
CONSOLIX_STREAM
() <<
"Application is shutting down. Received signal: "
<< signal;
76
}
77
};
// CustomLoop
78
79
int
main
(
int
argc,
char
* argv[]) {
80
// Set the console title to the application name
81
consolix::add<consolix::TitleComponent>
(u8
"Consolix - консольное приложение"
);
82
83
// Initialize the logger. This must be the first component.
84
consolix::add<consolix::LoggerComponent>
();
85
86
// Initialize command-line argument handler. Depends on LoggerComponent.
87
consolix::add<consolix::CliComponent>
(
88
"Consolix"
,
89
"A demonstration program showcasing the features of the Consolix library, "
90
"including logging, configuration management, and command-line argument parsing."
,
91
[](consolix::CliOptions& options){
92
options.add_options()
93
(
"c,config"
,
"Path to the configuration file"
, cxxopts::value<std::string>())
94
(
"d,debug"
,
"Enable debugging mode"
, cxxopts::value<bool>()->default_value(
"false"
))
95
(
"p,period"
,
"Period in milliseconds"
, cxxopts::value<int>()->default_value(
"10"
))
96
(
"h,help"
,
"Show help message"
);
97
options.allow_unrecognised_options();
98
},
99
argc,
100
argv);
101
102
// Add logo component to display the application logo at startup.
103
consolix::add<consolix::LogoComponent>
(
consolix::TextColor::Yellow
);
104
105
// Load configuration from a JSON file.
106
consolix::add<consolix::ConfigComponent<AppConfig>
>(
107
"config.json"
,
"config"
);
108
109
// Add the custom loop component.
110
consolix::add<CustomLoop>
();
111
112
// Start the application and run all components.
113
consolix::run
([](){
114
// UTF-8 string support for older versions of Windows
115
CONSOLIX_STREAM
() <<
116
consolix::color
(
consolix::TextColor::Green
) << u8
"Привет, мир!"
;
117
});
118
return
0;
119
}
CONSOLIX_STREAM
#define CONSOLIX_STREAM()
Fallback for general logging.
Definition
LoggerComponent.hpp:257
CustomLoop
Custom loop component for the main application logic.
Definition
example_application_main_loop.cpp:26
CustomLoop::~CustomLoop
virtual ~CustomLoop()=default
CustomLoop::on_once
bool on_once() override
Called once at the start of the loop.
Definition
example_application_main_loop.cpp:32
CustomLoop::on_loop
void on_loop() override
Called repeatedly during the execution loop.
Definition
example_application_main_loop.cpp:55
CustomLoop::on_shutdown
void on_shutdown(int signal) override
Called during application shutdown after a stop request or termination signal.
Definition
example_application_main_loop.cpp:74
consolix::BaseLoopComponent
Abstract base class for application components with looping functionality.
Definition
BaseLoopComponent.hpp:23
core.hpp
Entry point for including all core headers of the Consolix framework.
consolix::color
ColorManipulator color(TextColor color)
Creates a color manipulator for use in output streams.
Definition
ColorManipulator.hpp:52
consolix::TextColor::Cyan
@ Cyan
Definition
enums.hpp:34
consolix::TextColor::Yellow
@ Yellow
Definition
enums.hpp:31
consolix::TextColor::Green
@ Green
Definition
enums.hpp:30
consolix::add
std::shared_ptr< Component > add(Args &&... args)
Adds a new component to the application. Creates a new instance of the specified component type and a...
Definition
application_utils.hpp:50
consolix::get_service
T & get_service()
Retrieves a resource globally. Retrieves a reference to a globally registered resource from the Servi...
Definition
service_utils.hpp:51
consolix::run
void run()
Runs the application. Processes all components in the application's main loop.
Definition
application_utils.hpp:63
AppConfig
Application configuration structure.
Definition
example_application_main_loop.cpp:9
AppConfig::debug_mode
bool debug_mode
Enable or disable debugging mode.
Definition
example_application_main_loop.cpp:13
AppConfig::items
std::vector< std::string > items
List of items to display.
Definition
example_application_main_loop.cpp:11
AppConfig::period
int period
Delay between loop iterations in milliseconds.
Definition
example_application_main_loop.cpp:12
AppConfig::text
std::string text
Text to display in each loop iteration.
Definition
example_application_main_loop.cpp:10
main
int main()
Definition
test_strip_json_comments.cpp:28
examples
example_application_main_loop.cpp
Generated by
1.17.0