Demonstrates configuration loading and hot-reload with config_watcher.
Demonstrates configuration loading and hot-reload with config_watcher.Shows unified_config structure, config_watcher file monitoring with change callbacks, and config snapshot history.
#include <cstring>
#include <chrono>
#include <fstream>
#include <iostream>
#include <string>
#include <thread>
{
std::cout << "=== Config Watcher Example ===\n\n";
const std::string config_path = "example_config.yaml";
{
std::ofstream f(config_path);
f << "thread:\n"
<< " pool_size: 4\n"
<< " queue_type: adaptive\n"
<< "logger:\n"
<< " level: info\n"
<< "monitoring:\n"
<< " enabled: true\n"
<< " metrics_interval: 30\n";
}
std::cout << "1. Creating config watcher for '" << config_path << "'...\n";
{
std::cout << "\n [Change detected]\n";
});
watcher.
on_error([](
const std::string& error)
{ std::cerr << " [Config error] " << error << "\n"; });
std::cout << "\n2. Starting file watcher...\n";
auto start_result = watcher.
start();
if (start_result.is_ok())
{
std::cout <<
" Watcher running = " << (watcher.
is_running() ?
"true" :
"false") <<
"\n";
}
else
{
std::cout << " Watcher start skipped (platform may not support file watching in this "
"environment)\n";
}
std::cout << "\n3. Modifying config file...\n";
{
std::ofstream f(config_path);
f << "thread:\n"
<< " pool_size: 8\n"
<< " queue_type: lock_free\n"
<< "logger:\n"
<< " level: debug\n"
<< "monitoring:\n"
<< " enabled: true\n"
<< " metrics_interval: 10\n";
}
std::this_thread::sleep_for(std::chrono::milliseconds(500));
std::cout << "\n4. Manual reload...\n";
auto reload_result = watcher.
reload();
std::cout << " Reload: " << (reload_result.is_ok() ? "success" : "skipped") << "\n";
std::cout << "\n5. Stopping watcher...\n";
std::cout <<
" Watcher running = " << (watcher.
is_running() ?
"true" :
"false") <<
"\n";
std::remove(config_path.c_str());
std::cout << "\nDone.\n";
return 0;
}
Monitors configuration files for changes and supports hot-reload.
bool is_running() const
Check if the watcher is currently running.
VoidResult reload()
Manually trigger a configuration reload.
VoidResult start()
Start watching the configuration file for changes.
void on_change(change_callback callback)
Register a callback for configuration changes.
void stop()
Stop watching the configuration file.
void on_error(error_callback callback)
Register a callback for reload errors.
Configuration hot-reload support with file system watching.
size_t pool_size
Number of worker threads (default: hardware concurrency)
Root configuration structure for the unified system.
thread_config thread
Thread system configuration.
Unified configuration schema for the entire system.