• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_name.cpp
9  * \author Andrey Semashev
10  * \date   29.07.2012
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  * The code in this file is based on information on this page:
16  *
17  * http://stackoverflow.com/questions/1023306/finding-current-executables-path-without-proc-self-exe
18  */
19 
20 #include <boost/log/detail/config.hpp>
21 #include <climits> // PATH_MAX
22 #include <boost/log/attributes/current_process_name.hpp>
23 #include <boost/filesystem/path.hpp>
24 
25 #ifndef PATH_MAX
26 #define PATH_MAX 1024
27 #endif
28 
29 #if defined(BOOST_WINDOWS)
30 
31 #include <windows.h>
32 #include <boost/log/detail/header.hpp>
33 
34 namespace boost {
35 
36 BOOST_LOG_OPEN_NAMESPACE
37 
38 namespace aux {
39 
40 //! The function returns the current process name
get_process_name()41 BOOST_LOG_API std::string get_process_name()
42 {
43     std::wstring buf;
44     buf.resize(PATH_MAX);
45     do
46     {
47         unsigned int len = GetModuleFileNameW(NULL, &buf[0], static_cast< unsigned int >(buf.size()));
48         if (len < buf.size())
49         {
50             buf.resize(len);
51             break;
52         }
53 
54         buf.resize(buf.size() * 2);
55     }
56     while (buf.size() < 65536);
57 
58     return filesystem::path(buf).filename().string();
59 }
60 
61 } // namespace aux
62 
63 BOOST_LOG_CLOSE_NAMESPACE // namespace log
64 
65 } // namespace boost
66 
67 #include <boost/log/detail/footer.hpp>
68 
69 #elif defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__)
70 
71 #include <cstring>
72 #include <mach-o/dyld.h>
73 #include <boost/cstdint.hpp>
74 #include <boost/log/detail/header.hpp>
75 
76 namespace boost {
77 
78 BOOST_LOG_OPEN_NAMESPACE
79 
80 namespace aux {
81 
82 //! The function returns the current process name
get_process_name()83 BOOST_LOG_API std::string get_process_name()
84 {
85     std::string buf;
86     buf.resize(PATH_MAX);
87     while (true)
88     {
89         uint32_t size = static_cast< uint32_t >(buf.size());
90         if (_NSGetExecutablePath(&buf[0], &size) == 0)
91         {
92             buf.resize(std::strlen(&buf[0]));
93             break;
94         }
95 
96         buf.resize(size);
97     }
98 
99     return filesystem::path(buf).filename().string();
100 }
101 
102 } // namespace aux
103 
104 BOOST_LOG_CLOSE_NAMESPACE // namespace log
105 
106 } // namespace boost
107 
108 #include <boost/log/detail/footer.hpp>
109 
110 #elif defined(__FreeBSD__)
111 
112 #include <stddef.h>
113 #include <unistd.h>
114 #include <sys/types.h>
115 #include <sys/sysctl.h>
116 #include <boost/lexical_cast.hpp>
117 #include <boost/filesystem/operations.hpp>
118 #include <boost/log/detail/header.hpp>
119 
120 namespace boost {
121 
122 BOOST_LOG_OPEN_NAMESPACE
123 
124 namespace aux {
125 
126 //! The function returns the current process name
get_process_name()127 BOOST_LOG_API std::string get_process_name()
128 {
129 #if defined(KERN_PROC_PATHNAME)
130     int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
131     char buf[PATH_MAX] = {};
132     size_t cb = sizeof(buf);
133     if (sysctl(mib, 4, buf, &cb, NULL, 0) == 0)
134         return filesystem::path(buf).filename().string();
135 #endif
136 
137     if (filesystem::exists("/proc/curproc/file"))
138         return filesystem::read_symlink("/proc/curproc/file").filename().string();
139 
140     return boost::lexical_cast< std::string >(getpid());
141 }
142 
143 } // namespace aux
144 
145 BOOST_LOG_CLOSE_NAMESPACE // namespace log
146 
147 } // namespace boost
148 
149 #include <boost/log/detail/footer.hpp>
150 
151 #else
152 
153 #include <unistd.h>
154 #include <boost/lexical_cast.hpp>
155 #include <boost/filesystem/operations.hpp>
156 #include <boost/log/detail/header.hpp>
157 
158 namespace boost {
159 
160 BOOST_LOG_OPEN_NAMESPACE
161 
162 namespace aux {
163 
164 //! The function returns the current process name
get_process_name()165 BOOST_LOG_API std::string get_process_name()
166 {
167     if (filesystem::exists("/proc/self/exe"))
168         return filesystem::read_symlink("/proc/self/exe").filename().string();
169 
170     if (filesystem::exists("/proc/curproc/file"))
171         return filesystem::read_symlink("/proc/curproc/file").filename().string();
172 
173     if (filesystem::exists("/proc/curproc/exe"))
174         return filesystem::read_symlink("/proc/curproc/exe").filename().string();
175 
176     return boost::lexical_cast< std::string >(getpid());
177 }
178 
179 } // namespace aux
180 
181 BOOST_LOG_CLOSE_NAMESPACE // namespace log
182 
183 } // namespace boost
184 
185 #include <boost/log/detail/footer.hpp>
186 
187 #endif
188