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