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 
6 #ifndef BOOST_THREAD_DETAIL_LOG_HPP
7 #define BOOST_THREAD_DETAIL_LOG_HPP
8 
9 #include <boost/thread/detail/config.hpp>
10 #if defined BOOST_THREAD_USES_LOG
11 #include <boost/thread/recursive_mutex.hpp>
12 #include <boost/thread/lock_guard.hpp>
13 #if defined BOOST_THREAD_USES_LOG_THREAD_ID
14 #include <boost/thread/thread.hpp>
15 #endif
16 #include <iostream>
17 
18 namespace boost
19 {
20   namespace thread_detail
21   {
terminal_mutex()22     inline boost::recursive_mutex& terminal_mutex()
23     {
24       static boost::recursive_mutex mtx;
25       return mtx;
26     }
27 
28   }
29 }
30 #if defined BOOST_THREAD_USES_LOG_THREAD_ID
31 
32 #define BOOST_THREAD_LOG \
33   { \
34     boost::lock_guard<boost::recursive_mutex> _lk_(boost::thread_detail::terminal_mutex()); \
35     std::cout << boost::this_thread::get_id() << " - "<<__FILE__<<"["<<__LINE__<<"] " <<std::dec
36 #else
37 
38 #define BOOST_THREAD_LOG \
39 { \
40   boost::lock_guard<boost::recursive_mutex> _lk_(boost::thread_detail::terminal_mutex()); \
41   std::cout << __FILE__<<"["<<__LINE__<<"] " <<std::dec
42 
43 #endif
44 #define BOOST_THREAD_END_LOG \
45     std::dec << std::endl; \
46   }
47 
48 #else
49 
50 namespace boost
51 {
52   namespace thread_detail
53   {
54     struct dummy_stream_t
55     {
56     };
57 
58     template <typename T>
operator <<(dummy_stream_t const & os,T)59     inline dummy_stream_t const& operator<<(dummy_stream_t const& os, T)
60     {
61       return os;
62     }
63 
operator <<(dummy_stream_t const & os,dummy_stream_t const &)64     inline dummy_stream_t const& operator<<(dummy_stream_t const& os, dummy_stream_t const&)
65     {
66       return os;
67     }
68 
69 
70     BOOST_CONSTEXPR_OR_CONST dummy_stream_t dummy_stream = {};
71 
72   }
73 }
74 
75 #define BOOST_THREAD_LOG if (true) {} else boost::thread_detail::dummy_stream
76 #define BOOST_THREAD_END_LOG boost::thread_detail::dummy_stream
77 
78 #endif
79 
80 #define BOOST_THREAD_TRACE BOOST_THREAD_LOG << BOOST_THREAD_END_LOG
81 
82 
83 #endif // header
84