BitShares-Core  4.0.0
BitShares blockchain implementation and command-line interface software
config_util.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 Lubos Ilcik, and contributors.
3  *
4  * The MIT License
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
27 
28 #include <fc/reflect/variant.hpp>
29 #include <fc/string.hpp>
32 #include <fc/log/file_appender.hpp>
33 #include <fc/log/logger_config.hpp>
34 
35 #include <boost/property_tree/ptree.hpp>
36 #include <boost/property_tree/ini_parser.hpp>
37 #include <boost/algorithm/string/predicate.hpp>
38 #include <boost/algorithm/string/split.hpp>
39 #include <boost/algorithm/string/split.hpp>
40 #include <boost/algorithm/string.hpp>
41 
42 #include <fstream>
43 
44 namespace bpo = boost::program_options;
45 
46 namespace graphene { namespace app { namespace detail {
47 
49 {
50 public:
51  deduplicator() : modifier(nullptr) {}
52 
53  deduplicator(const boost::shared_ptr<bpo::option_description> (*mod_fn)(const boost::shared_ptr<bpo::option_description>&))
54  : modifier(mod_fn) {}
55 
56  const boost::shared_ptr<bpo::option_description> next(const boost::shared_ptr<bpo::option_description>& o)
57  {
58  const std::string name = o->long_name();
59  if( seen.find( name ) != seen.end() )
60  return nullptr;
61  seen.insert(name);
62  return modifier ? modifier(o) : o;
63  }
64 
65 private:
66  boost::container::flat_set<std::string> seen;
67  const boost::shared_ptr<bpo::option_description> (*modifier)(const boost::shared_ptr<bpo::option_description>&);
68 };
69 
70 } } } // graphene::app::detail
71 
72 // Currently, you can only specify the filenames and logging levels, which
73 // are all most users would want to change. At a later time, options can
74 // be added to control rotation intervals, compression, and other seldom-
75 // used features
76 static void write_default_logging_config_to_stream(std::ostream& out)
77 {
78  out << "# declare an appender named \"stderr\" that writes messages to the console\n"
79  "[log.console_appender.stderr]\n"
80  "stream=std_error\n\n"
81  "# declare an appender named \"default\" that writes messages to default.log\n"
82  "[log.file_appender.default]\n"
83  "# filename can be absolute or relative to this config file\n"
84  "filename=logs/default/default.log\n"
85  "# Rotate log every ? minutes, if leave out default to 60\n"
86  "rotation_interval=60\n"
87  "# how long will logs be kept (in days), if leave out default to 1\n"
88  "rotation_limit=7\n\n"
89  "# declare an appender named \"p2p\" that writes messages to p2p.log\n"
90  "[log.file_appender.p2p]\n"
91  "# filename can be absolute or relative to this config file\n"
92  "filename=logs/p2p/p2p.log\n"
93  "# Rotate log every ? minutes, if leave out default to 60\n"
94  "rotation_interval=60\n"
95  "# how long will logs be kept (in days), if leave out default to 1\n"
96  "rotation_limit=7\n\n"
97  "# declare an appender named \"rpc\" that writes messages to rpc.log\n"
98  "[log.file_appender.rpc]\n"
99  "# filename can be absolute or relative to this config file\n"
100  "filename=logs/rpc/rpc.log\n"
101  "# Rotate log every ? minutes, if leave out default to 60\n"
102  "rotation_interval=60\n"
103  "# how long will logs be kept (in days), if leave out default to 1\n"
104  "rotation_limit=7\n\n"
105  "# route any messages logged to the default logger to the \"stderr\" appender and\n"
106  "# \"default\" appender we declared above, if they are info level or higher\n"
107  "[logger.default]\n"
108  "level=info\n"
109  "appenders=stderr,default\n\n"
110  "# route messages sent to the \"p2p\" logger to the \"p2p\" appender declared above\n"
111  "[logger.p2p]\n"
112  "level=warn\n"
113  "appenders=p2p\n\n"
114  "# route messages sent to the \"rpc\" logger to the \"rpc\" appender declared above\n"
115  "[logger.rpc]\n"
116  "level=error\n"
117  "appenders=rpc\n\n";
118 }
119 
120 // logging config is too complicated to be parsed by boost::program_options,
121 // so we do it by hand
122 static fc::optional<fc::logging_config> load_logging_config_from_ini_file(const fc::path& config_ini_filename)
123 {
124  try
125  {
126  fc::logging_config logging_config;
127  bool found_logging_config = false;
128 
129  boost::property_tree::ptree config_ini_tree;
130  boost::property_tree::ini_parser::read_ini(config_ini_filename.preferred_string().c_str(), config_ini_tree);
131  for (const auto& section : config_ini_tree)
132  {
133  const std::string& section_name = section.first;
134  const boost::property_tree::ptree& section_tree = section.second;
135 
136  const std::string console_appender_section_prefix = "log.console_appender.";
137  const std::string file_appender_section_prefix = "log.file_appender.";
138  const std::string logger_section_prefix = "logger.";
139 
140  if (boost::starts_with(section_name, console_appender_section_prefix))
141  {
142  std::string console_appender_name = section_name.substr(console_appender_section_prefix.length());
143  std::string stream_name = section_tree.get<std::string>("stream");
144 
145  // construct a default console appender config here
146  // stdout/stderr will be taken from ini file, everything else hard-coded here
147  fc::console_appender::config console_appender_config;
148  console_appender_config.level_colors.emplace_back(
151  console_appender_config.level_colors.emplace_back(
154  console_appender_config.level_colors.emplace_back(
157  console_appender_config.stream = fc::variant(stream_name).as<fc::console_appender::stream::type>(GRAPHENE_MAX_NESTED_OBJECTS);
158  logging_config.appenders.push_back(fc::appender_config(console_appender_name, "console", fc::variant(console_appender_config, GRAPHENE_MAX_NESTED_OBJECTS)));
159  found_logging_config = true;
160  }
161  else if (boost::starts_with(section_name, file_appender_section_prefix))
162  {
163  std::string file_appender_name = section_name.substr(file_appender_section_prefix.length());
164  fc::path file_name = section_tree.get<std::string>("filename");
165  if (file_name.is_relative())
166  file_name = fc::absolute(config_ini_filename).parent_path() / file_name;
167 
168  int interval = section_tree.get_optional<int>("rotation_interval").get_value_or(60);
169  int limit = section_tree.get_optional<int>("rotation_limit").get_value_or(1);
170 
171  // construct a default file appender config here
172  // filename will be taken from ini file, everything else hard-coded here
173  fc::file_appender::config file_appender_config;
174  file_appender_config.filename = file_name;
175  file_appender_config.flush = true;
176  file_appender_config.rotate = true;
177  file_appender_config.rotation_interval = fc::minutes(interval);
178  file_appender_config.rotation_limit = fc::days(limit);
179  logging_config.appenders.push_back(fc::appender_config(file_appender_name, "file", fc::variant(file_appender_config, GRAPHENE_MAX_NESTED_OBJECTS)));
180  found_logging_config = true;
181  }
182  else if (boost::starts_with(section_name, logger_section_prefix))
183  {
184  std::string logger_name = section_name.substr(logger_section_prefix.length());
185  std::string level_string = section_tree.get<std::string>("level");
186  std::string appenders_string = section_tree.get<std::string>("appenders");
187  fc::logger_config logger_config(logger_name);
188  logger_config.level = fc::variant(level_string).as<fc::log_level>(5);
189  boost::split(logger_config.appenders, appenders_string,
190  boost::is_any_of(" ,"),
191  boost::token_compress_on);
192  logging_config.loggers.push_back(logger_config);
193  found_logging_config = true;
194  }
195  }
196  if (found_logging_config)
197  return logging_config;
198  else
200  }
201  FC_RETHROW_EXCEPTIONS(warn, "")
202 }
203 
204 static const boost::shared_ptr<bpo::option_description> new_option_description( const std::string& name, const bpo::value_semantic* value, const std::string& description )
205 {
206  bpo::options_description helper("");
207  helper.add_options()( name.c_str(), value, description.c_str() );
208  return helper.options()[0];
209 }
210 
211 
212 static void load_config_file(const fc::path& config_ini_path, const bpo::options_description& cfg_options,
213  bpo::variables_map& options )
214 {
216  bpo::options_description unique_options("BitShares Witness Node");
217  for( const boost::shared_ptr<bpo::option_description> opt : cfg_options.options() )
218  {
219  const boost::shared_ptr<bpo::option_description> od = dedup.next(opt);
220  if( !od ) continue;
221  unique_options.add( od );
222  }
223 
224  // get the basic options
225  bpo::store(bpo::parse_config_file<char>(config_ini_path.preferred_string().c_str(),
226  unique_options, true), options);
227 }
228 
229 static bool load_logging_config_file(const fc::path& config_ini_path)
230 {
231  // try to get logging options from the config file.
232  try
233  {
234  fc::optional<fc::logging_config> logging_config = load_logging_config_from_ini_file(config_ini_path);
235  if (logging_config)
236  {
237  fc::configure_logging(*logging_config);
238  return true;
239  }
240  }
241  catch (const fc::exception& ex)
242  {
243  wlog("Error parsing logging config from logging config file ${config}, using default config", ("config", config_ini_path.preferred_string()));
244  }
245  return false;
246 }
247 
248 static void create_new_config_file(const fc::path& config_ini_path, const fc::path& data_dir,
249  const bpo::options_description& cfg_options )
250 {
251  ilog("Writing new config file at ${path}", ("path", config_ini_path));
252  if( !fc::exists(data_dir) )
253  fc::create_directories(data_dir);
254 
255  auto modify_option_defaults = [](const boost::shared_ptr<bpo::option_description>& o) -> const boost::shared_ptr<bpo::option_description> {
256  const std::string& name = o->long_name();
257  if( name == "partial-operations" )
258  return new_option_description(name, bpo::value<bool>()->default_value(true), o->description() );
259  if( name == "max-ops-per-account" )
260  return new_option_description(name, bpo::value<int>()->default_value(100), o->description() );
261  return o;
262  };
263  graphene::app::detail::deduplicator dedup(modify_option_defaults);
264  std::ofstream out_cfg(config_ini_path.preferred_string());
265  std::string plugin_header_surrounding( 78, '=' );
266  for( const boost::shared_ptr<bpo::option_description> opt : cfg_options.options() )
267  {
268  const boost::shared_ptr<bpo::option_description> od = dedup.next(opt);
269  if( !od ) continue;
270 
271  if( od->long_name().find("plugin-cfg-header-") == 0 ) // it's a plugin header
272  {
273  out_cfg << "\n";
274  out_cfg << "# " << plugin_header_surrounding << "\n";
275  out_cfg << "# " << od->description() << "\n";
276  out_cfg << "# " << plugin_header_surrounding << "\n";
277  out_cfg << "\n";
278  continue;
279  }
280 
281  if( !od->description().empty() )
282  out_cfg << "# " << od->description() << "\n";
283  boost::any store;
284  if( !od->semantic()->apply_default(store) )
285  out_cfg << "# " << od->long_name() << " = \n";
286  else {
287  auto example = od->format_parameter();
288  if( example.empty() )
289  // This is a boolean switch
290  out_cfg << od->long_name() << " = " << "false\n";
291  else {
292  // The string is formatted "arg (=<interesting part>)"
293  example.erase(0, 6);
294  example.erase(example.length()-1);
295  out_cfg << od->long_name() << " = " << example << "\n";
296  }
297  }
298  out_cfg << "\n";
299  }
300 
301  out_cfg << "\n"
302  << "# " << plugin_header_surrounding << "\n"
303  << "# logging options\n"
304  << "# " << plugin_header_surrounding << "\n"
305  << "#\n"
306  << "# Logging configuration is loaded from logging.ini by default.\n"
307  << "# If logging.ini exists, logging configuration added in this file will be ignored.\n";
308  out_cfg.close();
309 }
310 
311 static void create_logging_config_file(const fc::path& config_ini_path, const fc::path& data_dir)
312 {
313  ilog("Writing new config file at ${path}", ("path", config_ini_path));
314  if (!exists(data_dir))
315  {
316  create_directories(data_dir);
317  }
318 
319  std::ofstream out_cfg(config_ini_path.preferred_string());
320  write_default_logging_config_to_stream(out_cfg);
321  out_cfg.close();
322 }
323 
324 namespace graphene { namespace app {
325 
326  void load_configuration_options(const fc::path& data_dir, const bpo::options_description& cfg_options, bpo::variables_map& options)
327  {
328  const auto config_ini_path = data_dir / "config.ini";
329  const auto logging_ini_path = data_dir / "logging.ini";
330 
331  if(!exists(config_ini_path) && fc::exists(logging_ini_path))
332  {
333  // this is an uncommon case
334  create_new_config_file(config_ini_path, data_dir, cfg_options);
335  }
336  else if(!exists(config_ini_path))
337  {
338  // create default config.ini and logging.ini
339  create_new_config_file(config_ini_path, data_dir, cfg_options);
340  create_logging_config_file(logging_ini_path, data_dir);
341  }
342 
343  // load witness node configuration
344  load_config_file(config_ini_path, cfg_options, options);
345 
346  // load logging configuration
347  if (fc::exists(logging_ini_path))
348  {
349  load_logging_config_file(logging_ini_path);
350  }
351  else
352  {
353  // this is the legacy config.ini case
354  load_logging_config_file(config_ini_path);
355  }
356  }
357 
358 } } // graphene::app
std::vector< logger_config > loggers
bool exists(const path &p)
Definition: filesystem.cpp:209
microseconds minutes(int64_t m)
Definition: time.hpp:36
T as(uint32_t max_depth) const
Definition: variant.hpp:336
#define GRAPHENE_MAX_NESTED_OBJECTS
Definition: config.hpp:31
Definition: api.cpp:56
std::vector< level_color > level_colors
Used to generate a useful error report when an exception is thrown.At each level in the stack where t...
Definition: exception.hpp:56
void create_directories(const path &p)
Definition: filesystem.cpp:210
#define FC_RETHROW_EXCEPTIONS(LOG_LEVEL, FORMAT,...)
Catchs all exception&#39;s, std::exceptions, and ... and rethrows them after appending the provided log m...
Definition: exception.hpp:463
#define wlog(FORMAT,...)
Definition: logger.hpp:123
deduplicator(const boost::shared_ptr< bpo::option_description >(*mod_fn)(const boost::shared_ptr< bpo::option_description > &))
Definition: config_util.cpp:53
provides stack-based nullable value similar to boost::optional
Definition: optional.hpp:20
void load_configuration_options(const fc::path &data_dir, const bpo::options_description &cfg_options, bpo::variables_map &options)
microseconds days(int64_t d)
Definition: time.hpp:38
#define ilog(FORMAT,...)
Definition: logger.hpp:117
bool is_relative() const
Definition: filesystem.cpp:166
const boost::shared_ptr< bpo::option_description > next(const boost::shared_ptr< bpo::option_description > &o)
Definition: config_util.cpp:56
void configure_logging(const fc::path &log_config)
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object&#39;s.
Definition: variant.hpp:198
std::vector< appender_config > appenders
Defines exception&#39;s used by fc.
std::string preferred_string() const
Definition: filesystem.cpp:99
fc::path parent_path() const
Definition: filesystem.cpp:163
wraps boost::filesystem::path to provide platform independent path manipulation.
Definition: filesystem.hpp:28
path absolute(const path &p)
Definition: filesystem.cpp:341