Examples of using Consolix framework.
Usage Examples
This page demonstrates how to use various features of the Consolix framework. It covers key components and utilities with practical examples.
Basic Setup
Here's how to initialize and use Consolix in a simple console application:
#define CONSOLIX_USE_LOGIT 1
int main(
int argc,
char* argv[]) {
options.add_options()("debug", "Enable debug mode");
}, argc, argv);
});
}
#define CONSOLIX_STREAM()
Fallback for general logging.
Single include header for Consolix framework.
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...
void run()
Runs the application. Processes all components in the application's main loop.
Using the ServiceLocator
The ServiceLocator
provides global access to shared resources:
return std::make_shared<std::string>("Hello, Consolix!");
});
std::cout << message << std::endl;
}
}
Entry point for including all core headers of the Consolix framework.
void register_service()
Registers a resource with default construction globally. Registers a resource or service using defaul...
T & get_service()
Retrieves a resource globally. Retrieves a reference to a globally registered resource from the Servi...
bool has_service()
Checks if a resource is registered globally.
JSON Configuration Management
Load and manage application configuration from a JSON file:
std::string app_name;
int max_connections;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(
AppConfig, app_name, max_connections)
std::cout << "App Name: " << config.app_name << "\n";
std::cout << "Max Connections: " << config.max_connections << "\n";
}
Entry point for including all Consolix components.
Application configuration structure.
Custom Loop Example
Define and use a custom execution loop:
[]() -> bool {
std::cout << "Initialization complete.\n";
return true;
},
[]() {
std::cout << "Executing loop iteration.\n";
std::this_thread::sleep_for(std::chrono::seconds(1));
},
[](int signal) {
std::cout << "Shutting down on signal: " << signal << "\n";
}
);
}
Logging with LogIt
Use the integrated LoggerComponent
to manage logging:
#define CONSOLIX_USE_LOGIT 1
LOGIT_PRINT_ERROR("This is an error message.");
return 0;
}
Working with Path Utilities
Resolve paths relative to the executable directory:
std::string relative_path = "config/settings.json";
std::string absolute_path = consolix::resolve_to_executable_dir(relative_path);
std::cout << "Absolute Path: " << absolute_path << std::endl;
}
Entry point for including utility headers for Consolix.