Consolix
Loading...
Searching...
No Matches
example_application_main_loop.cpp
struct AppConfig {
std::string text;
std::vector<std::string> items;
int period;
bool debug_mode;
NLOHMANN_DEFINE_TYPE_INTRUSIVE(AppConfig, text, items, period, debug_mode)
};
public:
virtual ~CustomLoop() = default;
bool on_once() override {
if (args.count("help")) {
std::exit(0);
}
CONSOLIX_SET_DEBUG_MODE(config.debug_mode);
LOGIT_TRACE0();
// Делаем тестовую запись в логгер с уникальными файлами
CONSOLIX_UNIQUE_FILE_STREAM() << "Test 123";
LOGIT_PRINT_INFO("Unique log file: ", CONSOLIX_UNIQUE_FILE_NAME());
return true;
}
void on_loop() override {
// Retrieve configuration
const auto& config = consolix::get_service<AppConfig>();
// Print text and items
CONSOLIX_STREAM() << config.text;
for (auto &item : config.items) {
}
// Sleep for the configured period
std::this_thread::sleep_for(std::chrono::milliseconds(config.period));
LOGIT_TRACE0();
}
void on_shutdown(int signal) override {
CONSOLIX_STREAM() << "Application is shutting down. Received signal: " << signal;
}
}; // CustomLoop
int main(int argc, char* argv[]) {
// Set the console title to the application name
consolix::add<consolix::TitleComponent>(u8"Consolix - консольное приложение");
// Initialize the logger. This must be the first component.
// Initialize command-line argument handler. Depends on LoggerComponent.
"Consolix",
"A demonstration program showcasing the features of the Consolix library, "
"including logging, configuration management, and command-line argument parsing.",
[](consolix::CliOptions& options){
options.add_options()
("c,config", "Path to the configuration file", cxxopts::value<std::string>())
("d,debug", "Enable debugging mode", cxxopts::value<bool>()->default_value("false"))
("p,period", "Period in milliseconds", cxxopts::value<int>()->default_value("10"))
("h,help", "Show help message");
options.allow_unrecognised_options();
},
argc,
argv);
// Add logo component to display the application logo at startup.
// Load configuration from a JSON file.
"config.json", "config");
// Add the custom loop component.
// Start the application and run all components.
// UTF-8 string support for older versions of Windows
consolix::color(consolix::TextColor::Green) << u8"Привет, мир!";
});
return 0;
}
#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 after a stop request or termination signal.
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.