| 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 process_id.cpp |
| 9 | * \author Andrey Semashev |
| 10 | * \date 12.09.2009 |
| 11 | * |
| 12 | * \brief This header is the Boost.Log library implementation, see the library documentation |
| 13 | * at http://www.boost.org/doc/libs/release/libs/log/doc/html/index.html. |
| 14 | */ |
| 15 | |
| 16 | #include <boost/log/detail/config.hpp> |
| 17 | #include <iostream> |
| 18 | #include <boost/log/detail/process_id.hpp> |
| 19 | #include "id_formatting.hpp" |
| 20 | |
| 21 | #if defined(BOOST_WINDOWS) |
| 22 | |
| 23 | #include <windows.h> |
| 24 | #include <boost/log/detail/header.hpp> |
| 25 | |
| 26 | namespace boost { |
| 27 | |
| 28 | BOOST_LOG_OPEN_NAMESPACE |
| 29 | |
| 30 | namespace aux { |
| 31 | |
| 32 | enum { pid_size = sizeof(GetCurrentProcessId()) }; |
| 33 | |
| 34 | namespace this_process { |
| 35 | |
| 36 | //! The function returns current process identifier |
| 37 | BOOST_LOG_API process::id get_id() |
| 38 | { |
| 39 | return process::id(GetCurrentProcessId()); |
| 40 | } |
| 41 | |
| 42 | } // namespace this_process |
| 43 | |
| 44 | } // namespace aux |
| 45 | |
| 46 | BOOST_LOG_CLOSE_NAMESPACE // namespace log |
| 47 | |
| 48 | } // namespace boost |
| 49 | |
| 50 | #else // defined(BOOST_WINDOWS) |
| 51 | |
| 52 | #include <unistd.h> |
| 53 | #include <boost/log/detail/header.hpp> |
| 54 | |
| 55 | namespace boost { |
| 56 | |
| 57 | BOOST_LOG_OPEN_NAMESPACE |
| 58 | |
| 59 | namespace aux { |
| 60 | |
| 61 | namespace this_process { |
| 62 | |
| 63 | //! The function returns current process identifier |
| 64 | BOOST_LOG_API process::id get_id() |
| 65 | { |
| 66 | // According to POSIX, pid_t should always be an integer type: |
| 67 | // http://pubs.opengroup.org/onlinepubs/009695399/basedefs/sys/types.h.html |
| 68 | return process::id(getpid()); |
| 69 | } |
| 70 | |
| 71 | } // namespace this_process |
| 72 | |
| 73 | enum { pid_size = sizeof(pid_t) }; |
| 74 | |
| 75 | } // namespace aux |
| 76 | |
| 77 | BOOST_LOG_CLOSE_NAMESPACE // namespace log |
| 78 | |
| 79 | } // namespace boost |
| 80 | |
| 81 | #endif // defined(BOOST_WINDOWS) |
| 82 | |
| 83 | |
| 84 | namespace boost { |
| 85 | |
| 86 | BOOST_LOG_OPEN_NAMESPACE |
| 87 | |
| 88 | namespace aux { |
| 89 | |
| 90 | template< typename CharT, typename TraitsT > |
| 91 | BOOST_LOG_API std::basic_ostream< CharT, TraitsT >& |
| 92 | operator<< (std::basic_ostream< CharT, TraitsT >& strm, process::id const& pid) |
| 93 | { |
| 94 | if (strm.good()) |
| 95 | { |
| 96 | CharT buf[pid_size * 2 + 3]; // 2 chars per byte + 3 chars for the leading 0x and terminating zero |
| 97 | format_id< pid_size >(buf, sizeof(buf) / sizeof(*buf), pid.native_id(), (strm.flags() & std::ios_base::uppercase) != 0); |
| 98 | |
| 99 | strm << buf; |
| 100 | } |
| 101 | |
| 102 | return strm; |
| 103 | } |
| 104 | |
| 105 | #if defined(BOOST_LOG_USE_CHAR) |
| 106 | template BOOST_LOG_API |
| 107 | std::basic_ostream< char, std::char_traits< char > >& |
| 108 | operator<< (std::basic_ostream< char, std::char_traits< char > >& strm, process::id const& pid); |
| 109 | #endif // defined(BOOST_LOG_USE_CHAR) |
| 110 | |
| 111 | #if defined(BOOST_LOG_USE_WCHAR_T) |
| 112 | template BOOST_LOG_API |
| 113 | std::basic_ostream< wchar_t, std::char_traits< wchar_t > >& |
| 114 | operator<< (std::basic_ostream< wchar_t, std::char_traits< wchar_t > >& strm, process::id const& pid); |
| 115 | #endif // defined(BOOST_LOG_USE_WCHAR_T) |
| 116 | |
| 117 | } // namespace aux |
| 118 | |
| 119 | BOOST_LOG_CLOSE_NAMESPACE // namespace log |
| 120 | |
| 121 | } // namespace boost |
| 122 | |
| 123 | #include <boost/log/detail/footer.hpp> |
| 124 | |