log.hpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright (C) 2012 Vicente J. Botet Escriba
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_THREAD_DETAIL_LOG_HPP
  6. #define BOOST_THREAD_DETAIL_LOG_HPP
  7. #include <boost/thread/detail/config.hpp>
  8. #if defined BOOST_THREAD_USES_LOG
  9. #include <boost/thread/recursive_mutex.hpp>
  10. #include <boost/thread/lock_guard.hpp>
  11. #if defined BOOST_THREAD_USES_LOG_THREAD_ID
  12. #include <boost/thread/thread.hpp>
  13. #endif
  14. #include <iostream>
  15. namespace boost
  16. {
  17. namespace thread_detail
  18. {
  19. inline boost::recursive_mutex& terminal_mutex()
  20. {
  21. static boost::recursive_mutex mtx;
  22. return mtx;
  23. }
  24. }
  25. }
  26. #if defined BOOST_THREAD_USES_LOG_THREAD_ID
  27. #define BOOST_THREAD_LOG \
  28. { \
  29. boost::lock_guard<boost::recursive_mutex> _lk_(boost::thread_detail::terminal_mutex()); \
  30. std::cout << boost::this_thread::get_id() << " - "<<__FILE__<<"["<<__LINE__<<"] " <<std::dec
  31. #else
  32. #define BOOST_THREAD_LOG \
  33. { \
  34. boost::lock_guard<boost::recursive_mutex> _lk_(boost::thread_detail::terminal_mutex()); \
  35. std::cout << __FILE__<<"["<<__LINE__<<"] " <<std::dec
  36. #endif
  37. #define BOOST_THREAD_END_LOG \
  38. std::dec << std::endl; \
  39. }
  40. #else
  41. namespace boost
  42. {
  43. namespace thread_detail
  44. {
  45. struct dummy_stream_t
  46. {
  47. };
  48. template <typename T>
  49. inline dummy_stream_t const& operator<<(dummy_stream_t const& os, T)
  50. {
  51. return os;
  52. }
  53. inline dummy_stream_t const& operator<<(dummy_stream_t const& os, dummy_stream_t const&)
  54. {
  55. return os;
  56. }
  57. BOOST_CONSTEXPR_OR_CONST dummy_stream_t dummy_stream = {};
  58. }
  59. }
  60. #ifdef BOOST_MSVC
  61. #define BOOST_THREAD_LOG \
  62. __pragma(warning(suppress:4127)) /* conditional expression is constant */ \
  63. if (true) {} else boost::thread_detail::dummy_stream
  64. #else
  65. #define BOOST_THREAD_LOG if (true) {} else boost::thread_detail::dummy_stream
  66. #endif
  67. #define BOOST_THREAD_END_LOG boost::thread_detail::dummy_stream
  68. #endif
  69. #define BOOST_THREAD_TRACE BOOST_THREAD_LOG << BOOST_THREAD_END_LOG
  70. #ifdef BOOST_MSVC
  71. #define BOOST_DETAIL_THREAD_LOG \
  72. __pragma(warning(suppress:4127)) /* conditional expression is constant */ \
  73. if (false) {} else std::cout << std::endl << __FILE__ << "[" << __LINE__ << "]"
  74. #else
  75. #define BOOST_DETAIL_THREAD_LOG \
  76. if (false) {} else std::cout << std::endl << __FILE__ << "[" << __LINE__ << "]"
  77. #endif
  78. #endif // header