main.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright Andrey Semashev 2007 - 2015.
  3. * Distributed under the Boost Software License, Version 1.0.
  4. * (See accompanying file LICENSE_1_0.txt or copy at
  5. * http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. /*!
  8. * \file main.cpp
  9. * \author Andrey Semashev
  10. * \date 26.04.2008
  11. *
  12. * \brief An example of initializing the library from a settings file.
  13. * See the library tutorial for expanded comments on this code.
  14. * It may also be worthwhile reading the Wiki requirements page:
  15. * http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl?Boost.Logging
  16. */
  17. // #define BOOST_ALL_DYN_LINK 1
  18. #include <exception>
  19. #include <iostream>
  20. #include <fstream>
  21. #include <boost/log/trivial.hpp>
  22. #include <boost/log/common.hpp>
  23. #include <boost/log/attributes.hpp>
  24. #include <boost/log/utility/setup/from_stream.hpp>
  25. namespace logging = boost::log;
  26. namespace attrs = boost::log::attributes;
  27. void try_logging()
  28. {
  29. BOOST_LOG_TRIVIAL(trace) << "This is a trace severity record";
  30. BOOST_LOG_TRIVIAL(debug) << "This is a debug severity record";
  31. BOOST_LOG_TRIVIAL(info) << "This is an info severity record";
  32. BOOST_LOG_TRIVIAL(warning) << "This is a warning severity record";
  33. BOOST_LOG_TRIVIAL(error) << "This is an error severity record";
  34. BOOST_LOG_TRIVIAL(fatal) << "This is a fatal severity record";
  35. }
  36. int main(int argc, char* argv[])
  37. {
  38. try
  39. {
  40. // Open the file
  41. std::ifstream settings("settings.txt");
  42. if (!settings.is_open())
  43. {
  44. std::cout << "Could not open settings.txt file" << std::endl;
  45. return 1;
  46. }
  47. // Read the settings and initialize logging library
  48. logging::init_from_stream(settings);
  49. // Add some attributes
  50. logging::core::get()->add_global_attribute("TimeStamp", attrs::local_clock());
  51. // Try logging
  52. try_logging();
  53. // Now enable tagging and try again
  54. BOOST_LOG_SCOPED_THREAD_TAG("Tag", "TAGGED");
  55. try_logging();
  56. return 0;
  57. }
  58. catch (std::exception& e)
  59. {
  60. std::cout << "FAILURE: " << e.what() << std::endl;
  61. return 1;
  62. }
  63. }