Common System 0.2.0
Common interfaces and patterns for system integration
Loading...
Searching...
No Matches
config_watcher_example.cpp File Reference
#include <cstring>
#include <kcenon/common/config/unified_config.h>
#include <kcenon/common/config/config_watcher.h>
#include <chrono>
#include <fstream>
#include <iostream>
#include <string>
#include <thread>
Include dependency graph for config_watcher_example.cpp:

Go to the source code of this file.

Functions

int main ()
 

Function Documentation

◆ main()

int main ( )

Definition at line 29 of file config_watcher_example.cpp.

30{
31 std::cout << "=== Config Watcher Example ===\n\n";
32
33 // Create a sample config file
34 const std::string config_path = "example_config.yaml";
35 {
36 std::ofstream f(config_path);
37 f << "thread:\n"
38 << " pool_size: 4\n"
39 << " queue_type: adaptive\n"
40 << "logger:\n"
41 << " level: info\n"
42 << "monitoring:\n"
43 << " enabled: true\n"
44 << " metrics_interval: 30\n";
45 }
46
47 // Create watcher with history of 5 snapshots
48 std::cout << "1. Creating config watcher for '" << config_path << "'...\n";
49 config_watcher watcher(config_path, 5);
50
51 // Register change callback
52 watcher.on_change(
53 [](const unified_config& old_cfg, const unified_config& new_cfg)
54 {
55 std::cout << "\n [Change detected]\n";
56 std::cout << " Old pool_size: " << old_cfg.thread.pool_size << "\n";
57 std::cout << " New pool_size: " << new_cfg.thread.pool_size << "\n";
58 });
59
60 // Register error callback
61 watcher.on_error([](const std::string& error)
62 { std::cerr << " [Config error] " << error << "\n"; });
63
64 // Start watching
65 std::cout << "\n2. Starting file watcher...\n";
66 auto start_result = watcher.start();
67 if (start_result.is_ok())
68 {
69 std::cout << " Watcher running = " << (watcher.is_running() ? "true" : "false") << "\n";
70 }
71 else
72 {
73 std::cout << " Watcher start skipped (platform may not support file watching in this "
74 "environment)\n";
75 }
76
77 // Simulate a config change
78 std::cout << "\n3. Modifying config file...\n";
79 {
80 std::ofstream f(config_path);
81 f << "thread:\n"
82 << " pool_size: 8\n"
83 << " queue_type: lock_free\n"
84 << "logger:\n"
85 << " level: debug\n"
86 << "monitoring:\n"
87 << " enabled: true\n"
88 << " metrics_interval: 10\n";
89 }
90
91 // Give the watcher time to detect the change
92 std::this_thread::sleep_for(std::chrono::milliseconds(500));
93
94 // Manual reload
95 std::cout << "\n4. Manual reload...\n";
96 auto reload_result = watcher.reload();
97 std::cout << " Reload: " << (reload_result.is_ok() ? "success" : "skipped") << "\n";
98
99 // Stop watching
100 std::cout << "\n5. Stopping watcher...\n";
101 watcher.stop();
102 std::cout << " Watcher running = " << (watcher.is_running() ? "true" : "false") << "\n";
103
104 // Cleanup
105 std::remove(config_path.c_str());
106
107 std::cout << "\nDone.\n";
108 return 0;
109}
Monitors configuration files for changes and supports hot-reload.
size_t pool_size
Number of worker threads (default: hardware concurrency)
Root configuration structure for the unified system.
thread_config thread
Thread system configuration.

References kcenon::common::config::config_watcher::is_running(), kcenon::common::config::config_watcher::on_change(), kcenon::common::config::config_watcher::on_error(), kcenon::common::config::thread_config::pool_size, kcenon::common::config::config_watcher::reload(), kcenon::common::config::config_watcher::start(), kcenon::common::config::config_watcher::stop(), and kcenon::common::config::unified_config::thread.

Here is the call graph for this function: