source: nscp/service/settings_client.hpp @ 9fdde88

0.4.2
Last change on this file since 9fdde88 was 9fdde88, checked in by Michael Medin <michael@…>, 11 months ago
  • Added plugin_id to settings API to allow generating documentation based on module names.
  • Added option --filter <module name> to filter generation based on module name. You can now do this to generate documentation for a given module. nscp settings --settings dummy --generate trac --filter ExternalScripts? --load-all --add-defaults --log error
  • Property mode set to 100644
File size: 6.9 KB
Line 
1#pragma once
2#include <settings/settings_core.hpp>
3#include <nsclient/logger.hpp>
4
5class NSClientT;
6namespace nsclient {
7        class settings_client {
8                bool started_;
9                NSClient* core_;
10                std::wstring log_;
11                bool default_;
12                bool load_all_;
13                std::wstring filter_;
14
15        public:
16                settings_client(NSClient* core, std::wstring log, bool update_defaults, bool load_all, std::wstring filter) : started_(false), core_(core), log_(log), default_(update_defaults), load_all_(load_all), filter_(filter) {
17                        startup();
18                }
19
20
21                ~settings_client() {
22                        terminate();
23                }
24
25                void startup() {
26                        if (started_)
27                                return;
28                        if (!core_->boot_init(log_)) {
29                                std::wcout << _T("boot::init failed") << std::endl;
30                                return;
31                        }
32                        if (load_all_)
33                                core_->preboot_load_all_plugin_files();
34
35                        if (!core_->boot_load_all_plugins()) {
36                                std::wcout << _T("boot::load_all_plugins failed!") << std::endl;
37                                return;
38                        }
39                        if (!core_->boot_start_plugins(false)) {
40                                std::wcout << _T("boot::start_plugins failed!") << std::endl;
41                                return;
42                        }
43                        if (default_) {
44                                settings_manager::get_core()->update_defaults();
45                        }
46                        started_ = true;
47                }
48
49                void terminate() {
50                        if (!started_)
51                                return;
52                        core_->stop_unload_plugins_pre();
53                        core_->stop_exit_pre();
54                        core_->stop_exit_post();
55                        started_ = false;
56                }
57
58                int migrate_from(std::wstring src) {
59                        try {
60                                debug_msg(_T("Migrating from: ") + src);
61                                settings_manager::get_core()->migrate_from(src);
62                                return 1;
63                        } catch (settings::settings_exception e) {
64                                error_msg(_T("Failed to initialize settings: ") + e.getError());
65                        } catch (...) {
66                                error_msg(_T("FATAL ERROR IN SETTINGS SUBSYTEM"));
67                        }
68                        return -1;
69                }
70                int migrate_to(std::wstring target) {
71                        try {
72                                debug_msg(_T("Migrating to: ") + target);
73                                settings_manager::get_core()->migrate_to(target);
74                                return 1;
75                        } catch (settings::settings_exception e) {
76                                error_msg(_T("Failed to initialize settings: ") + e.getError());
77                        } catch (...) {
78                                error_msg(_T("FATAL ERROR IN SETTINGS SUBSYTEM"));
79                        }
80                        return -1;
81                }
82
83                void dump_path(std::wstring root) {
84                        BOOST_FOREACH(std::wstring path, settings_manager::get_core()->get()->get_sections(root)) {
85                                if (!root.empty())
86                                        dump_path(root + _T("/") + path);
87                                else
88                                        dump_path(path);
89                        }
90                        BOOST_FOREACH(std::wstring key, settings_manager::get_core()->get()->get_keys(root)) {
91                                std::wcout << root << _T(".") << key << _T("=") << settings_manager::get_core()->get()->get_string(root, key) << std::endl;
92                        }
93                }
94
95
96                int generate(std::wstring target) {
97                        try {
98                                if (target == _T("settings") || target.empty()) {
99                                        settings_manager::get_core()->get()->save();
100                                } else if (target == _T("trac")) {
101                                        settings::string_list s = settings_manager::get_core()->get_reg_sections();
102                                        BOOST_FOREACH(std::wstring path, s) {
103
104                                                settings::settings_core::path_description desc = settings_manager::get_core()->get_registred_path(path);
105                                                std::wstring plugins;
106                                                bool include = filter_.empty();
107                                                BOOST_FOREACH(unsigned int i, desc.plugins) {
108                                                        std::wstring name = core_->get_plugin_module_name(i);
109                                                        if (name.find(filter_) != std::wstring::npos)
110                                                                include = true;
111                                                        if (!plugins.empty())
112                                                                plugins += _T(", ");
113                                                        plugins += name;
114                                                }
115
116                                                if (!include)
117                                                        continue;
118
119                                                std::wcout << _T("== ") << path << _T(" ==") << std::endl;
120                                                if (!desc.description.empty())
121                                                        std::wcout << desc.description << std::endl;
122                                                std::wcout << _T("'''Used by:''' ") << plugins << std::endl;
123                                                std::wcout << std::endl;
124                                                settings::string_list k = settings_manager::get_core()->get_reg_keys(path);
125                                                bool first = true;
126                                                BOOST_FOREACH(std::wstring key, k) {
127                                                        settings::settings_core::key_description desc = settings_manager::get_core()->get_registred_key(path, key);
128                                                        if (!desc.advanced) {
129                                                                if (first)
130                                                                        std::wcout << _T("'''Normal settings'''") << std::endl;
131                                                                first = false;
132                                                                strEx::replace(desc.description, _T("\n"), _T("\n|| || ||"));
133                                                                if (desc.defValue.empty())
134                                                                        desc.defValue = _T(" ");
135                                                                std::wcout << _T("||") << key << _T("||") << desc.defValue << _T("||") << desc.title << _T(": ") << desc.description << std::endl;
136                                                        }
137                                                }
138                                                first = true;
139                                                BOOST_FOREACH(std::wstring key, k) {
140                                                        settings::settings_core::key_description desc = settings_manager::get_core()->get_registred_key(path, key);
141                                                        if (desc.advanced) {
142                                                                if (first)
143                                                                        std::wcout << _T("'''Advanced settings'''") << std::endl;
144                                                                first = false;
145                                                                std::wcout << _T("||") << key << _T("||") << desc.defValue << _T("||") << desc.title << _T(": ") << desc.description << std::endl;
146                                                        }
147                                                }
148                                        }
149                                } else {
150                                        //settings_manager::get_core()->update_defaults();
151                                        settings_manager::get_core()->get()->save_to(target);
152                                }
153                                return 1;
154                        } catch (settings::settings_exception e) {
155                                error_msg(_T("Failed to initialize settings: ") + e.getError());
156                        } catch (NSPluginException &e) {
157                                error_msg(_T("Failed to load plugins: ") + to_wstring(e.what()));
158                        } catch (std::exception &e) {
159                                error_msg(_T("Failed to initialize settings: ") + to_wstring(e.what()));
160                        } catch (...) {
161                                error_msg(_T("FATAL ERROR IN SETTINGS SUBSYTEM"));
162                        }
163                        return -1;
164                }
165
166
167                void switch_context(std::wstring contect) {
168                        settings_manager::get_core()->set_primary(contect);
169                }
170
171                int set(std::wstring path, std::wstring key, std::wstring val) {
172                        settings::settings_core::key_type type = settings_manager::get_core()->get()->get_key_type(path, key);
173                        if (type == settings::settings_core::key_string) {
174                                settings_manager::get_core()->get()->set_string(path, key, val);
175                        } else if (type == settings::settings_core::key_integer) {
176                                settings_manager::get_core()->get()->set_int(path, key, strEx::stoi(val));
177                        } else if (type == settings::settings_core::key_bool) {
178                                settings_manager::get_core()->get()->set_bool(path, key, settings::settings_interface::string_to_bool(val));
179                        } else {
180                                error_msg(_T("Failed to set key (not found)"));
181                                return -1;
182                        }
183                        settings_manager::get_core()->get()->save();
184                        return 0;
185                }
186                int show(std::wstring path, std::wstring key) {
187                        std::wcout << settings_manager::get_core()->get()->get_string(path, key);
188                        return 0;
189                }
190                int list(std::wstring path) {
191                        try {
192                                dump_path(path);
193                        } catch (settings::settings_exception e) {
194                                error_msg(_T("Settings error: ") + e.getError());
195                        } catch (...) {
196                                error_msg(_T("FATAL ERROR IN SETTINGS SUBSYTEM"));
197                        }
198
199                        return 0;
200                }
201
202                void error_msg(std::wstring msg) {
203                        nsclient::logging::logger::get_logger()->error(_T("client"), __FILE__, __LINE__, msg.c_str());
204                }
205                void debug_msg(std::wstring msg) {
206                        nsclient::logging::logger::get_logger()->debug(_T("client"), __FILE__, __LINE__, msg.c_str());
207                }
208        };
209}
Note: See TracBrowser for help on using the repository browser.