Consolix
Loading...
Searching...
No Matches
example_application_main_loop.cpp
Go to the documentation of this file.
1
2
3#include <consolix/core.hpp>
4
9struct AppConfig {
10 std::string text;
11 std::vector<std::string> items;
12 int period;
14
16 NLOHMANN_DEFINE_TYPE_INTRUSIVE(AppConfig, text, items, period, debug_mode)
17};
18
27public:
28
29 virtual ~CustomLoop() = default;
30
32 bool on_once() override {
36
38 if (args.count("help")) {
40 std::exit(0);
41 }
42
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;
62 for (auto &item : config.items) {
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
79int 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.
85
86 // Initialize command-line argument handler. Depends on LoggerComponent.
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
100 // Add logo component to display the application logo at startup.
102
103 // Load configuration from a JSON file.
105 "config.json", "config");
106
107 // Add the custom loop component.
109
110 // Start the application and run all components.
111 consolix::run([](){
112 // UTF-8 string support for older versions of Windows
113 CONSOLIX_STREAM() <<
114 consolix::color(consolix::TextColor::Green) << u8"Привет, мир!";
115 });
116 return 0;
117}
#define CONSOLIX_STREAM()
Fallback for general logging.
Custom loop component for the main application logic.
virtual ~CustomLoop()=default
bool on_once() override
Called once at the start of the loop.
void on_loop() override
Called repeatedly during the execution loop.
void on_shutdown(int signal) override
Called during application shutdown.
Abstract base class for application components with looping functionality.
Entry point for including all core headers of the Consolix framework.
ColorManipulator color(TextColor color)
Creates a color manipulator for use in output streams.
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...
T & get_service()
Retrieves a resource globally. Retrieves a reference to a globally registered resource from the Servi...
void run()
Runs the application. Processes all components in the application's main loop.
Application configuration structure.
bool debug_mode
Enable or disable debugging mode.
std::vector< std::string > items
List of items to display.
int period
Delay between loop iterations in milliseconds.
std::string text
Text to display in each loop iteration.