debug_settings.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // ----------------------------------------------------------------------------
  2. // Copyright (C) 2002-2006 Marcin Kalicinski
  3. //
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // For more information, see www.boost.org
  9. // ----------------------------------------------------------------------------
  10. //[debug_settings_includes
  11. #include <boost/property_tree/ptree.hpp>
  12. #include <boost/property_tree/xml_parser.hpp>
  13. #include <boost/foreach.hpp>
  14. #include <string>
  15. #include <set>
  16. #include <exception>
  17. #include <iostream>
  18. namespace pt = boost::property_tree;
  19. //]
  20. //[debug_settings_data
  21. struct debug_settings
  22. {
  23. std::string m_file; // log filename
  24. int m_level; // debug level
  25. std::set<std::string> m_modules; // modules where logging is enabled
  26. void load(const std::string &filename);
  27. void save(const std::string &filename);
  28. };
  29. //]
  30. //[debug_settings_load
  31. void debug_settings::load(const std::string &filename)
  32. {
  33. // Create empty property tree object
  34. pt::ptree tree;
  35. // Parse the XML into the property tree.
  36. pt::read_xml(filename, tree);
  37. // Use the throwing version of get to find the debug filename.
  38. // If the path cannot be resolved, an exception is thrown.
  39. m_file = tree.get<std::string>("debug.filename");
  40. // Use the default-value version of get to find the debug level.
  41. // Note that the default value is used to deduce the target type.
  42. m_level = tree.get("debug.level", 0);
  43. // Use get_child to find the node containing the modules, and iterate over
  44. // its children. If the path cannot be resolved, get_child throws.
  45. // A C++11 for-range loop would also work.
  46. BOOST_FOREACH(pt::ptree::value_type &v, tree.get_child("debug.modules")) {
  47. // The data function is used to access the data stored in a node.
  48. m_modules.insert(v.second.data());
  49. }
  50. }
  51. //]
  52. //[debug_settings_save
  53. void debug_settings::save(const std::string &filename)
  54. {
  55. // Create an empty property tree object.
  56. pt::ptree tree;
  57. // Put the simple values into the tree. The integer is automatically
  58. // converted to a string. Note that the "debug" node is automatically
  59. // created if it doesn't exist.
  60. tree.put("debug.filename", m_file);
  61. tree.put("debug.level", m_level);
  62. // Add all the modules. Unlike put, which overwrites existing nodes, add
  63. // adds a new node at the lowest level, so the "modules" node will have
  64. // multiple "module" children.
  65. BOOST_FOREACH(const std::string &name, m_modules)
  66. tree.add("debug.modules.module", name);
  67. // Write property tree to XML file
  68. pt::write_xml(filename, tree);
  69. }
  70. //]
  71. int main()
  72. {
  73. try
  74. {
  75. debug_settings ds;
  76. ds.load("debug_settings.xml");
  77. ds.save("debug_settings_out.xml");
  78. std::cout << "Success\n";
  79. }
  80. catch (std::exception &e)
  81. {
  82. std::cout << "Error: " << e.what() << "\n";
  83. }
  84. return 0;
  85. }