sinks_debugger.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. #include <boost/smart_ptr/shared_ptr.hpp>
  8. #include <boost/log/core.hpp>
  9. #include <boost/log/expressions/predicates/is_debugger_present.hpp>
  10. #include <boost/log/sinks/sync_frontend.hpp>
  11. #include <boost/log/sinks/debug_output_backend.hpp>
  12. #include <boost/log/sources/logger.hpp>
  13. #include <boost/log/sources/record_ostream.hpp>
  14. #if defined(BOOST_WINDOWS)
  15. namespace logging = boost::log;
  16. namespace expr = boost::log::expressions;
  17. namespace src = boost::log::sources;
  18. namespace sinks = boost::log::sinks;
  19. //[ example_sinks_debugger
  20. // Complete sink type
  21. typedef sinks::synchronous_sink< sinks::debug_output_backend > sink_t;
  22. void init_logging()
  23. {
  24. boost::shared_ptr< logging::core > core = logging::core::get();
  25. // Create the sink. The backend requires synchronization in the frontend.
  26. boost::shared_ptr< sink_t > sink(new sink_t());
  27. // Set the special filter to the frontend
  28. // in order to skip the sink when no debugger is available
  29. sink->set_filter(expr::is_debugger_present());
  30. core->add_sink(sink);
  31. }
  32. //]
  33. int main(int, char*[])
  34. {
  35. init_logging();
  36. src::logger lg;
  37. BOOST_LOG(lg) << "Hello world!";
  38. return 0;
  39. }
  40. #else
  41. int main(int, char*[])
  42. {
  43. return 0;
  44. }
  45. #endif