Common System 0.2.0
Common interfaces and patterns for system integration
Loading...
Searching...
No Matches
kcenon::common::bootstrap::SystemBootstrapper Class Reference

Fluent API for system initialization and logger registration. More...

#include <system_bootstrapper.h>

Collaboration diagram for kcenon::common::bootstrap::SystemBootstrapper:
Collaboration graph

Public Member Functions

 SystemBootstrapper ()=default
 Default constructor.
 
 ~SystemBootstrapper ()
 Destructor with automatic shutdown.
 
 SystemBootstrapper (const SystemBootstrapper &)=delete
 
SystemBootstrapperoperator= (const SystemBootstrapper &)=delete
 
 SystemBootstrapper (SystemBootstrapper &&other) noexcept
 
SystemBootstrapperoperator= (SystemBootstrapper &&other) noexcept
 
SystemBootstrapperwith_default_logger (interfaces::LoggerFactory factory)
 Register a factory for the default logger.
 
SystemBootstrapperwith_logger (const std::string &name, interfaces::LoggerFactory factory)
 Register a factory for a named logger.
 
SystemBootstrapperon_initialize (std::function< void()> callback)
 Register an initialization callback.
 
SystemBootstrapperon_shutdown (std::function< void()> callback)
 Register a shutdown callback.
 
SystemBootstrapperwith_auto_freeze (bool freeze_logger_registry=true, bool freeze_service_container=true)
 Enable automatic freezing of registries after initialization.
 
VoidResult initialize ()
 Initialize the system.
 
void shutdown ()
 Shutdown the system.
 
bool is_initialized () const noexcept
 Check if the system is initialized.
 
void reset ()
 Reset the bootstrapper to initial state.
 

Private Member Functions

VoidResult register_loggers ()
 Register all loggers with GlobalLoggerRegistry.
 
void execute_init_callbacks ()
 Execute all initialization callbacks.
 
void execute_shutdown_callbacks ()
 Execute all shutdown callbacks in reverse order.
 
void clear_loggers ()
 Clear all loggers from GlobalLoggerRegistry.
 

Private Attributes

interfaces::LoggerFactory default_logger_factory_
 
std::vector< std::pair< std::string, interfaces::LoggerFactory > > named_logger_factories_
 
std::vector< std::function< void()> > init_callbacks_
 
std::vector< std::function< void()> > shutdown_callbacks_
 
bool auto_freeze_logger_registry_ = false
 
bool auto_freeze_service_container_ = false
 
bool initialized_ = false
 
std::mutex state_mutex_
 

Detailed Description

Fluent API for system initialization and logger registration.

SystemBootstrapper provides a centralized mechanism for:

  • Registering default and named loggers using factory functions
  • Defining initialization and shutdown callbacks
  • Managing application lifecycle with RAII support

Key Features:

  • Fluent API with method chaining for expressive configuration
  • Factory-based lazy initialization of loggers
  • RAII support with automatic shutdown on destruction
  • Prevention of duplicate initialization/shutdown
  • Integration with GlobalLoggerRegistry for thread-safe logger access

Design Notes:

  • Configuration methods are not thread-safe; call from single thread
  • Once initialize() succeeds, loggers are available globally
  • Shutdown callbacks are called in reverse order of registration
  • Initialize callbacks are called in order of registration

Definition at line 90 of file system_bootstrapper.h.

Constructor & Destructor Documentation

◆ SystemBootstrapper() [1/3]

kcenon::common::bootstrap::SystemBootstrapper::SystemBootstrapper ( )
default

Default constructor.

Creates an uninitialized bootstrapper. Call configuration methods and then initialize() to start the system.

◆ ~SystemBootstrapper()

kcenon::common::bootstrap::SystemBootstrapper::~SystemBootstrapper ( )
inline

Destructor with automatic shutdown.

Automatically calls shutdown() if the bootstrapper was initialized. This ensures proper cleanup even if shutdown() is not called explicitly.

Definition at line 304 of file system_bootstrapper.h.

References initialized_, and shutdown().

Here is the call graph for this function:

◆ SystemBootstrapper() [2/3]

kcenon::common::bootstrap::SystemBootstrapper::SystemBootstrapper ( const SystemBootstrapper & )
delete

◆ SystemBootstrapper() [3/3]

kcenon::common::bootstrap::SystemBootstrapper::SystemBootstrapper ( SystemBootstrapper && other)
inlinenoexcept

Definition at line 310 of file system_bootstrapper.h.

311 : default_logger_factory_(std::move(other.default_logger_factory_))
312 , named_logger_factories_(std::move(other.named_logger_factories_))
313 , init_callbacks_(std::move(other.init_callbacks_))
314 , shutdown_callbacks_(std::move(other.shutdown_callbacks_))
315 , auto_freeze_logger_registry_(other.auto_freeze_logger_registry_)
316 , auto_freeze_service_container_(other.auto_freeze_service_container_)
317 , initialized_(other.initialized_) {
318 other.initialized_ = false; // Prevent double shutdown
319}
std::vector< std::function< void()> > init_callbacks_
std::vector< std::function< void()> > shutdown_callbacks_
std::vector< std::pair< std::string, interfaces::LoggerFactory > > named_logger_factories_

Member Function Documentation

◆ clear_loggers()

void kcenon::common::bootstrap::SystemBootstrapper::clear_loggers ( )
inlineprivate

Clear all loggers from GlobalLoggerRegistry.

Definition at line 520 of file system_bootstrapper.h.

520 {
522}
static GlobalLoggerRegistry & instance()
Get the singleton instance of GlobalLoggerRegistry.
void clear()
Clear all registered loggers and factories.

References kcenon::common::interfaces::GlobalLoggerRegistry::clear(), and kcenon::common::interfaces::GlobalLoggerRegistry::instance().

Referenced by register_loggers(), and shutdown().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ execute_init_callbacks()

void kcenon::common::bootstrap::SystemBootstrapper::execute_init_callbacks ( )
inlineprivate

Execute all initialization callbacks.

Definition at line 501 of file system_bootstrapper.h.

501 {
502 for (const auto& callback : init_callbacks_) {
503 if (callback) {
504 callback();
505 }
506 }
507}

References init_callbacks_.

Referenced by initialize().

Here is the caller graph for this function:

◆ execute_shutdown_callbacks()

void kcenon::common::bootstrap::SystemBootstrapper::execute_shutdown_callbacks ( )
inlineprivate

Execute all shutdown callbacks in reverse order.

Definition at line 509 of file system_bootstrapper.h.

509 {
510 // Execute in reverse order (LIFO)
511 for (auto it = shutdown_callbacks_.rbegin();
512 it != shutdown_callbacks_.rend();
513 ++it) {
514 if (*it) {
515 (*it)();
516 }
517 }
518}

References shutdown_callbacks_.

Referenced by shutdown().

Here is the caller graph for this function:

◆ initialize()

VoidResult kcenon::common::bootstrap::SystemBootstrapper::initialize ( )
inline

Initialize the system.

Performs the following steps in order:

  1. Validates that initialization hasn't already occurred
  2. Creates and registers the default logger (if configured)
  3. Creates and registers all named loggers
  4. Calls all initialization callbacks in registration order
  5. Marks the bootstrapper as initialized

If any step fails, the operation is rolled back and an error is returned.

Returns
VoidResult indicating success or error
Note
This method should only be called once. Subsequent calls return an error.

Definition at line 385 of file system_bootstrapper.h.

385 {
386 std::lock_guard<std::mutex> lock(state_mutex_);
387
388 if (initialized_) {
391 "SystemBootstrapper already initialized",
392 "bootstrap::SystemBootstrapper"
393 );
394 }
395
396 // Step 1: Register all loggers
397 auto result = register_loggers();
398 if (result.is_err()) {
399 return result;
400 }
401
402 // Step 2: Execute initialization callbacks
404
405 // Step 3: Freeze registries if auto-freeze is enabled
408 }
411 }
412
413 // Step 4: Mark as initialized
414 initialized_ = true;
415
416 return VoidResult::ok({});
417}
static Result< T > ok(U &&value)
Create a successful result with value (static factory)
Definition core.h:223
VoidResult register_loggers()
Register all loggers with GlobalLoggerRegistry.
void execute_init_callbacks()
Execute all initialization callbacks.
void freeze()
Freeze the container to prevent further registrations.
static service_container & global()
Get the global service container instance.
void freeze()
Freeze the registry to prevent further modifications.
constexpr int ALREADY_EXISTS
Definition compat.h:37
Result< T > make_error(int code, const std::string &message, const std::string &module="")
Create an error result with code and message.
Definition utilities.h:91

References kcenon::common::error_codes::ALREADY_EXISTS, auto_freeze_logger_registry_, auto_freeze_service_container_, execute_init_callbacks(), kcenon::common::di::service_container::freeze(), kcenon::common::interfaces::GlobalLoggerRegistry::freeze(), kcenon::common::di::service_container::global(), initialized_, kcenon::common::interfaces::GlobalLoggerRegistry::instance(), kcenon::common::make_error(), kcenon::common::Result< T >::ok(), register_loggers(), and state_mutex_.

Here is the call graph for this function:

◆ is_initialized()

bool kcenon::common::bootstrap::SystemBootstrapper::is_initialized ( ) const
inlinenoexcept

Check if the system is initialized.

Returns
true if initialize() has been called successfully

Definition at line 436 of file system_bootstrapper.h.

436 {
437 std::lock_guard<std::mutex> lock(state_mutex_);
438 return initialized_;
439}

References initialized_, and state_mutex_.

Referenced by reset().

Here is the caller graph for this function:

◆ on_initialize()

SystemBootstrapper & kcenon::common::bootstrap::SystemBootstrapper::on_initialize ( std::function< void()> callback)
inline

Register an initialization callback.

The callback will be invoked during initialize() after all loggers are registered. Multiple callbacks can be registered and will be called in the order of registration.

Parameters
callbackFunction to call during initialization
Returns
Reference to this bootstrapper for method chaining

Definition at line 361 of file system_bootstrapper.h.

362 {
363 if (callback) {
364 init_callbacks_.push_back(std::move(callback));
365 }
366 return *this;
367}

References init_callbacks_.

◆ on_shutdown()

SystemBootstrapper & kcenon::common::bootstrap::SystemBootstrapper::on_shutdown ( std::function< void()> callback)
inline

Register a shutdown callback.

The callback will be invoked during shutdown() before loggers are cleared. Multiple callbacks can be registered and will be called in reverse order of registration (LIFO).

Parameters
callbackFunction to call during shutdown
Returns
Reference to this bootstrapper for method chaining

Definition at line 369 of file system_bootstrapper.h.

370 {
371 if (callback) {
372 shutdown_callbacks_.push_back(std::move(callback));
373 }
374 return *this;
375}

References shutdown_callbacks_.

◆ operator=() [1/2]

SystemBootstrapper & kcenon::common::bootstrap::SystemBootstrapper::operator= ( const SystemBootstrapper & )
delete

◆ operator=() [2/2]

SystemBootstrapper & kcenon::common::bootstrap::SystemBootstrapper::operator= ( SystemBootstrapper && other)
inlinenoexcept

Definition at line 321 of file system_bootstrapper.h.

322 {
323 if (this != &other) {
324 // Shutdown current state if initialized
325 if (initialized_) {
326 shutdown();
327 }
328
329 default_logger_factory_ = std::move(other.default_logger_factory_);
330 named_logger_factories_ = std::move(other.named_logger_factories_);
331 init_callbacks_ = std::move(other.init_callbacks_);
332 shutdown_callbacks_ = std::move(other.shutdown_callbacks_);
333 auto_freeze_logger_registry_ = other.auto_freeze_logger_registry_;
334 auto_freeze_service_container_ = other.auto_freeze_service_container_;
335 initialized_ = other.initialized_;
336 other.initialized_ = false; // Prevent double shutdown
337 }
338 return *this;
339}

References auto_freeze_logger_registry_, auto_freeze_service_container_, default_logger_factory_, init_callbacks_, initialized_, named_logger_factories_, shutdown(), and shutdown_callbacks_.

Here is the call graph for this function:

◆ register_loggers()

VoidResult kcenon::common::bootstrap::SystemBootstrapper::register_loggers ( )
inlineprivate

Register all loggers with GlobalLoggerRegistry.

Returns
VoidResult indicating success or error

Definition at line 458 of file system_bootstrapper.h.

458 {
460
461 // Register default logger
463 auto logger = default_logger_factory_();
464 if (!logger) {
467 "Default logger factory returned null",
468 "bootstrap::SystemBootstrapper"
469 );
470 }
471 auto result = registry.set_default_logger(std::move(logger));
472 if (result.is_err()) {
473 return result;
474 }
475 }
476
477 // Register named loggers
478 for (const auto& [name, factory] : named_logger_factories_) {
479 if (!factory) {
480 continue; // Skip null factories
481 }
482 auto logger = factory();
483 if (!logger) {
486 "Logger factory for '" + name + "' returned null",
487 "bootstrap::SystemBootstrapper"
488 );
489 }
490 auto result = registry.register_logger(name, std::move(logger));
491 if (result.is_err()) {
492 // Clean up already registered loggers
494 return result;
495 }
496 }
497
498 return VoidResult::ok({});
499}
void clear_loggers()
Clear all loggers from GlobalLoggerRegistry.
constexpr int INTERNAL_ERROR
Definition compat.h:42

References clear_loggers(), default_logger_factory_, kcenon::common::interfaces::GlobalLoggerRegistry::instance(), kcenon::common::error_codes::INTERNAL_ERROR, kcenon::common::make_error(), named_logger_factories_, and kcenon::common::Result< T >::ok().

Referenced by initialize().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reset()

void kcenon::common::bootstrap::SystemBootstrapper::reset ( )
inline

Reset the bootstrapper to initial state.

Clears all registered factories, callbacks, and resets the initialized state. If currently initialized, shutdown() is called first.

Useful for testing or reconfiguration scenarios.

Definition at line 441 of file system_bootstrapper.h.

441 {
442 // Shutdown if initialized
443 if (is_initialized()) {
444 shutdown();
445 }
446
447 std::lock_guard<std::mutex> lock(state_mutex_);
448
449 // Clear all configuration
450 default_logger_factory_ = nullptr;
452 init_callbacks_.clear();
453 shutdown_callbacks_.clear();
456}
bool is_initialized() const noexcept
Check if the system is initialized.

References auto_freeze_logger_registry_, auto_freeze_service_container_, default_logger_factory_, init_callbacks_, is_initialized(), named_logger_factories_, shutdown(), shutdown_callbacks_, and state_mutex_.

Here is the call graph for this function:

◆ shutdown()

void kcenon::common::bootstrap::SystemBootstrapper::shutdown ( )
inline

Shutdown the system.

Performs the following steps in order:

  1. Validates that the system is initialized
  2. Calls all shutdown callbacks in reverse registration order
  3. Clears all loggers from GlobalLoggerRegistry
  4. Marks the bootstrapper as not initialized
Note
This method is idempotent; calling it multiple times after the first call has no effect.
The destructor calls this automatically if needed.

Definition at line 419 of file system_bootstrapper.h.

419 {
420 std::lock_guard<std::mutex> lock(state_mutex_);
421
422 if (!initialized_) {
423 return; // Already shutdown or never initialized
424 }
425
426 // Step 1: Execute shutdown callbacks in reverse order
428
429 // Step 2: Clear loggers from registry
431
432 // Step 3: Mark as not initialized
433 initialized_ = false;
434}
void execute_shutdown_callbacks()
Execute all shutdown callbacks in reverse order.

References clear_loggers(), execute_shutdown_callbacks(), initialized_, and state_mutex_.

Referenced by operator=(), reset(), and ~SystemBootstrapper().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ with_auto_freeze()

SystemBootstrapper & kcenon::common::bootstrap::SystemBootstrapper::with_auto_freeze ( bool freeze_logger_registry = true,
bool freeze_service_container = true )
inline

Enable automatic freezing of registries after initialization.

When enabled, the GlobalLoggerRegistry and service_container::global() will be frozen after all initialization callbacks complete. This prevents unauthorized modifications to loggers and services after system startup.

Parameters
freeze_logger_registryWhether to freeze GlobalLoggerRegistry (default: true)
freeze_service_containerWhether to freeze service_container (default: true)
Returns
Reference to this bootstrapper for method chaining
Note
This is a security feature to prevent audit log tampering and service hijacking after system initialization.
Freezing is a one-way operation and cannot be undone.
See also
Issue #206 for security requirements.
.with_default_logger(create_console_logger)
.with_auto_freeze() // Freeze after initialization
.initialize();
SystemBootstrapper()=default
Default constructor.

Definition at line 377 of file system_bootstrapper.h.

References auto_freeze_logger_registry_, and auto_freeze_service_container_.

◆ with_default_logger()

SystemBootstrapper & kcenon::common::bootstrap::SystemBootstrapper::with_default_logger ( interfaces::LoggerFactory factory)
inline

Register a factory for the default logger.

The factory function will be invoked during initialize() to create the default logger. The logger will be registered with GlobalLoggerRegistry and accessible via get_logger().

Parameters
factoryFactory function that creates the default logger
Returns
Reference to this bootstrapper for method chaining
Note
If called multiple times, only the last factory is used.

Definition at line 341 of file system_bootstrapper.h.

342 {
343 default_logger_factory_ = std::move(factory);
344 return *this;
345}

References default_logger_factory_.

◆ with_logger()

SystemBootstrapper & kcenon::common::bootstrap::SystemBootstrapper::with_logger ( const std::string & name,
interfaces::LoggerFactory factory )
inline

Register a factory for a named logger.

The factory function will be invoked during initialize() to create the named logger. The logger will be registered with GlobalLoggerRegistry and accessible via get_logger(name).

Parameters
nameLogger name (case-sensitive)
factoryFactory function that creates the logger
Returns
Reference to this bootstrapper for method chaining
Note
If the same name is registered multiple times, only the last factory is used.

Definition at line 347 of file system_bootstrapper.h.

349 {
350 // Check if name already exists and update, otherwise add
351 for (auto& entry : named_logger_factories_) {
352 if (entry.first == name) {
353 entry.second = std::move(factory);
354 return *this;
355 }
356 }
357 named_logger_factories_.emplace_back(name, std::move(factory));
358 return *this;
359}

References named_logger_factories_.

Member Data Documentation

◆ auto_freeze_logger_registry_

bool kcenon::common::bootstrap::SystemBootstrapper::auto_freeze_logger_registry_ = false
private

Definition at line 292 of file system_bootstrapper.h.

Referenced by initialize(), operator=(), reset(), and with_auto_freeze().

◆ auto_freeze_service_container_

bool kcenon::common::bootstrap::SystemBootstrapper::auto_freeze_service_container_ = false
private

Definition at line 293 of file system_bootstrapper.h.

Referenced by initialize(), operator=(), reset(), and with_auto_freeze().

◆ default_logger_factory_

interfaces::LoggerFactory kcenon::common::bootstrap::SystemBootstrapper::default_logger_factory_
private

Definition at line 286 of file system_bootstrapper.h.

Referenced by operator=(), register_loggers(), reset(), and with_default_logger().

◆ init_callbacks_

std::vector<std::function<void()> > kcenon::common::bootstrap::SystemBootstrapper::init_callbacks_
private

Definition at line 288 of file system_bootstrapper.h.

Referenced by execute_init_callbacks(), on_initialize(), operator=(), and reset().

◆ initialized_

bool kcenon::common::bootstrap::SystemBootstrapper::initialized_ = false
private

◆ named_logger_factories_

std::vector<std::pair<std::string, interfaces::LoggerFactory> > kcenon::common::bootstrap::SystemBootstrapper::named_logger_factories_
private

Definition at line 287 of file system_bootstrapper.h.

Referenced by operator=(), register_loggers(), reset(), and with_logger().

◆ shutdown_callbacks_

std::vector<std::function<void()> > kcenon::common::bootstrap::SystemBootstrapper::shutdown_callbacks_
private

Definition at line 289 of file system_bootstrapper.h.

Referenced by execute_shutdown_callbacks(), on_shutdown(), operator=(), and reset().

◆ state_mutex_

std::mutex kcenon::common::bootstrap::SystemBootstrapper::state_mutex_
mutableprivate

Definition at line 297 of file system_bootstrapper.h.

Referenced by initialize(), is_initialized(), reset(), and shutdown().


The documentation for this class was generated from the following file: