/src/logging-log4cxx/src/main/cpp/system.cpp
Line | Count | Source |
1 | | /* |
2 | | * Licensed to the Apache Software Foundation (ASF) under one or more |
3 | | * contributor license agreements. See the NOTICE file distributed with |
4 | | * this work for additional information regarding copyright ownership. |
5 | | * The ASF licenses this file to You under the Apache License, Version 2.0 |
6 | | * (the "License"); you may not use this file except in compliance with |
7 | | * the License. You may obtain a copy of the License at |
8 | | * |
9 | | * http://www.apache.org/licenses/LICENSE-2.0 |
10 | | * |
11 | | * Unless required by applicable law or agreed to in writing, software |
12 | | * distributed under the License is distributed on an "AS IS" BASIS, |
13 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 | | * See the License for the specific language governing permissions and |
15 | | * limitations under the License. |
16 | | */ |
17 | | |
18 | | #include <log4cxx/logstring.h> |
19 | | #include <log4cxx/helpers/system.h> |
20 | | #include <log4cxx/helpers/filesystempath.h> |
21 | | |
22 | | #include <log4cxx/helpers/transcoder.h> |
23 | | #include <log4cxx/helpers/pool.h> |
24 | | #include <log4cxx/helpers/properties.h> |
25 | | #include <log4cxx/helpers/loglog.h> |
26 | | #include <log4cxx/helpers/stringhelper.h> |
27 | | #include <apr_file_io.h> |
28 | | #include <apr_user.h> |
29 | | #include <apr_env.h> |
30 | | |
31 | | #ifdef _WIN32 |
32 | | #include <windows.h> |
33 | | #elif __APPLE__ |
34 | | #include <mach-o/dyld.h> |
35 | | #elif (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 500) || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) |
36 | | #include <unistd.h> // getpid |
37 | | #endif |
38 | | #include <sstream> |
39 | | |
40 | | using namespace LOG4CXX_NS; |
41 | | using namespace LOG4CXX_NS::helpers; |
42 | | |
43 | | |
44 | | LogString System::getProperty(const LogString& lkey) |
45 | 114k | { |
46 | 114k | if (lkey.empty()) |
47 | 0 | { |
48 | 0 | throw IllegalArgumentException(LOG4CXX_STR("key is empty")); |
49 | 0 | } |
50 | | |
51 | 114k | LogString rv; |
52 | | |
53 | 114k | if (lkey == LOG4CXX_STR("java.io.tmpdir")) |
54 | 455 | { |
55 | 455 | Pool p; |
56 | 455 | const char* dir = NULL; |
57 | 455 | apr_status_t stat = apr_temp_dir_get(&dir, p.getAPRPool()); |
58 | | |
59 | 455 | if (stat == APR_SUCCESS) |
60 | 455 | { |
61 | 455 | Transcoder::decode(dir, rv); |
62 | 455 | } |
63 | | |
64 | 455 | return rv; |
65 | 455 | } |
66 | | |
67 | 113k | if (lkey == LOG4CXX_STR("user.dir")) |
68 | 321 | { |
69 | 321 | Pool p; |
70 | 321 | char* dir = NULL; |
71 | 321 | apr_status_t stat = apr_filepath_get(&dir, APR_FILEPATH_NATIVE, |
72 | 321 | p.getAPRPool()); |
73 | | |
74 | 321 | if (stat == APR_SUCCESS) |
75 | 321 | { |
76 | 321 | Transcoder::decode(dir, rv); |
77 | 321 | } |
78 | | |
79 | 321 | return rv; |
80 | 321 | } |
81 | | |
82 | 113k | #if APR_HAS_USER |
83 | | |
84 | 113k | if (lkey == LOG4CXX_STR("user.home") || lkey == LOG4CXX_STR("user.name")) |
85 | 678 | { |
86 | 678 | Pool pool; |
87 | 678 | apr_uid_t userid; |
88 | 678 | apr_gid_t groupid; |
89 | 678 | apr_pool_t* p = pool.getAPRPool(); |
90 | 678 | apr_status_t stat = apr_uid_current(&userid, &groupid, p); |
91 | | |
92 | 678 | if (stat == APR_SUCCESS) |
93 | 678 | { |
94 | 678 | char* username = NULL; |
95 | 678 | stat = apr_uid_name_get(&username, userid, p); |
96 | | |
97 | 678 | if (stat == APR_SUCCESS) |
98 | 678 | { |
99 | 678 | if (lkey == LOG4CXX_STR("user.name")) |
100 | 492 | { |
101 | 492 | Transcoder::decode(username, rv); |
102 | 492 | } |
103 | 186 | else |
104 | 186 | { |
105 | 186 | char* dirname = NULL; |
106 | 186 | stat = apr_uid_homepath_get(&dirname, username, p); |
107 | | |
108 | 186 | if (stat == APR_SUCCESS) |
109 | 186 | { |
110 | 186 | Transcoder::decode(dirname, rv); |
111 | 186 | } |
112 | 186 | } |
113 | 678 | } |
114 | 678 | } |
115 | | |
116 | 678 | return rv; |
117 | 678 | } |
118 | | |
119 | 112k | #endif |
120 | | |
121 | 112k | LOG4CXX_ENCODE_CHAR(key, lkey); |
122 | 112k | Pool p; |
123 | 112k | char* value = NULL; |
124 | 112k | apr_status_t stat = apr_env_get(&value, key.c_str(), |
125 | 112k | p.getAPRPool()); |
126 | | |
127 | 112k | if (stat == APR_SUCCESS) |
128 | 88.0k | { |
129 | 88.0k | Transcoder::decode((const char*) value, rv); |
130 | 88.0k | } |
131 | | |
132 | 112k | return rv; |
133 | 113k | } log4cxx::helpers::System::getProperty(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) Line | Count | Source | 45 | 92.6k | { | 46 | 92.6k | if (lkey.empty()) | 47 | 0 | { | 48 | 0 | throw IllegalArgumentException(LOG4CXX_STR("key is empty")); | 49 | 0 | } | 50 | | | 51 | 92.6k | LogString rv; | 52 | | | 53 | 92.6k | if (lkey == LOG4CXX_STR("java.io.tmpdir")) | 54 | 454 | { | 55 | 454 | Pool p; | 56 | 454 | const char* dir = NULL; | 57 | 454 | apr_status_t stat = apr_temp_dir_get(&dir, p.getAPRPool()); | 58 | | | 59 | 454 | if (stat == APR_SUCCESS) | 60 | 454 | { | 61 | 454 | Transcoder::decode(dir, rv); | 62 | 454 | } | 63 | | | 64 | 454 | return rv; | 65 | 454 | } | 66 | | | 67 | 92.2k | if (lkey == LOG4CXX_STR("user.dir")) | 68 | 321 | { | 69 | 321 | Pool p; | 70 | 321 | char* dir = NULL; | 71 | 321 | apr_status_t stat = apr_filepath_get(&dir, APR_FILEPATH_NATIVE, | 72 | 321 | p.getAPRPool()); | 73 | | | 74 | 321 | if (stat == APR_SUCCESS) | 75 | 321 | { | 76 | 321 | Transcoder::decode(dir, rv); | 77 | 321 | } | 78 | | | 79 | 321 | return rv; | 80 | 321 | } | 81 | | | 82 | 91.9k | #if APR_HAS_USER | 83 | | | 84 | 91.9k | if (lkey == LOG4CXX_STR("user.home") || lkey == LOG4CXX_STR("user.name")) | 85 | 673 | { | 86 | 673 | Pool pool; | 87 | 673 | apr_uid_t userid; | 88 | 673 | apr_gid_t groupid; | 89 | 673 | apr_pool_t* p = pool.getAPRPool(); | 90 | 673 | apr_status_t stat = apr_uid_current(&userid, &groupid, p); | 91 | | | 92 | 673 | if (stat == APR_SUCCESS) | 93 | 673 | { | 94 | 673 | char* username = NULL; | 95 | 673 | stat = apr_uid_name_get(&username, userid, p); | 96 | | | 97 | 673 | if (stat == APR_SUCCESS) | 98 | 673 | { | 99 | 673 | if (lkey == LOG4CXX_STR("user.name")) | 100 | 490 | { | 101 | 490 | Transcoder::decode(username, rv); | 102 | 490 | } | 103 | 183 | else | 104 | 183 | { | 105 | 183 | char* dirname = NULL; | 106 | 183 | stat = apr_uid_homepath_get(&dirname, username, p); | 107 | | | 108 | 183 | if (stat == APR_SUCCESS) | 109 | 183 | { | 110 | 183 | Transcoder::decode(dirname, rv); | 111 | 183 | } | 112 | 183 | } | 113 | 673 | } | 114 | 673 | } | 115 | | | 116 | 673 | return rv; | 117 | 673 | } | 118 | | | 119 | 91.2k | #endif | 120 | | | 121 | 91.2k | LOG4CXX_ENCODE_CHAR(key, lkey); | 122 | 91.2k | Pool p; | 123 | 91.2k | char* value = NULL; | 124 | 91.2k | apr_status_t stat = apr_env_get(&value, key.c_str(), | 125 | 91.2k | p.getAPRPool()); | 126 | | | 127 | 91.2k | if (stat == APR_SUCCESS) | 128 | 71.0k | { | 129 | 71.0k | Transcoder::decode((const char*) value, rv); | 130 | 71.0k | } | 131 | | | 132 | 91.2k | return rv; | 133 | 91.9k | } |
log4cxx::helpers::System::getProperty(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&) Line | Count | Source | 45 | 21.3k | { | 46 | 21.3k | if (lkey.empty()) | 47 | 0 | { | 48 | 0 | throw IllegalArgumentException(LOG4CXX_STR("key is empty")); | 49 | 0 | } | 50 | | | 51 | 21.3k | LogString rv; | 52 | | | 53 | 21.3k | if (lkey == LOG4CXX_STR("java.io.tmpdir")) | 54 | 1 | { | 55 | 1 | Pool p; | 56 | 1 | const char* dir = NULL; | 57 | 1 | apr_status_t stat = apr_temp_dir_get(&dir, p.getAPRPool()); | 58 | | | 59 | 1 | if (stat == APR_SUCCESS) | 60 | 1 | { | 61 | 1 | Transcoder::decode(dir, rv); | 62 | 1 | } | 63 | | | 64 | 1 | return rv; | 65 | 1 | } | 66 | | | 67 | 21.3k | if (lkey == LOG4CXX_STR("user.dir")) | 68 | 0 | { | 69 | 0 | Pool p; | 70 | 0 | char* dir = NULL; | 71 | 0 | apr_status_t stat = apr_filepath_get(&dir, APR_FILEPATH_NATIVE, | 72 | 0 | p.getAPRPool()); | 73 | |
| 74 | 0 | if (stat == APR_SUCCESS) | 75 | 0 | { | 76 | 0 | Transcoder::decode(dir, rv); | 77 | 0 | } | 78 | |
| 79 | 0 | return rv; | 80 | 0 | } | 81 | | | 82 | 21.3k | #if APR_HAS_USER | 83 | | | 84 | 21.3k | if (lkey == LOG4CXX_STR("user.home") || lkey == LOG4CXX_STR("user.name")) | 85 | 5 | { | 86 | 5 | Pool pool; | 87 | 5 | apr_uid_t userid; | 88 | 5 | apr_gid_t groupid; | 89 | 5 | apr_pool_t* p = pool.getAPRPool(); | 90 | 5 | apr_status_t stat = apr_uid_current(&userid, &groupid, p); | 91 | | | 92 | 5 | if (stat == APR_SUCCESS) | 93 | 5 | { | 94 | 5 | char* username = NULL; | 95 | 5 | stat = apr_uid_name_get(&username, userid, p); | 96 | | | 97 | 5 | if (stat == APR_SUCCESS) | 98 | 5 | { | 99 | 5 | if (lkey == LOG4CXX_STR("user.name")) | 100 | 2 | { | 101 | 2 | Transcoder::decode(username, rv); | 102 | 2 | } | 103 | 3 | else | 104 | 3 | { | 105 | 3 | char* dirname = NULL; | 106 | 3 | stat = apr_uid_homepath_get(&dirname, username, p); | 107 | | | 108 | 3 | if (stat == APR_SUCCESS) | 109 | 3 | { | 110 | 3 | Transcoder::decode(dirname, rv); | 111 | 3 | } | 112 | 3 | } | 113 | 5 | } | 114 | 5 | } | 115 | | | 116 | 5 | return rv; | 117 | 5 | } | 118 | | | 119 | 21.3k | #endif | 120 | | | 121 | 21.3k | LOG4CXX_ENCODE_CHAR(key, lkey); | 122 | 21.3k | Pool p; | 123 | 21.3k | char* value = NULL; | 124 | 21.3k | apr_status_t stat = apr_env_get(&value, key.c_str(), | 125 | 21.3k | p.getAPRPool()); | 126 | | | 127 | 21.3k | if (stat == APR_SUCCESS) | 128 | 17.0k | { | 129 | 17.0k | Transcoder::decode((const char*) value, rv); | 130 | 17.0k | } | 131 | | | 132 | 21.3k | return rv; | 133 | 21.3k | } |
|
134 | | |
135 | | void System::addProgramFilePathComponents(Properties& props) |
136 | 10 | { |
137 | | // Find the executable file name |
138 | 10 | static const int bufSize = 4096; |
139 | 10 | char buf[bufSize+1] = {0}, pathSepar = '/'; |
140 | | #if defined(_WIN32) |
141 | | if (0 == GetModuleFileName(NULL, buf, bufSize)) |
142 | | { |
143 | | LogString lsErrorCode; |
144 | | StringHelper::toString((int)GetLastError(), lsErrorCode); |
145 | | LogLog::warn(LOG4CXX_STR("GetModuleFileName error ") + lsErrorCode); |
146 | | return; |
147 | | } |
148 | | pathSepar = '\\'; |
149 | | #elif defined(__APPLE__) |
150 | | uint32_t bufCount = bufSize; |
151 | | if (0 != _NSGetExecutablePath(buf, &bufCount)) |
152 | | { |
153 | | LogLog::warn(LOG4CXX_STR("_NSGetExecutablePath failed")); |
154 | | return; |
155 | | } |
156 | | #elif (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 500) || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) |
157 | | int bufCount = 0; |
158 | 10 | std::ostringstream exeLink; |
159 | 10 | exeLink << "/proc/" << getpid() << "/exe"; |
160 | 10 | if ((bufCount = readlink(exeLink.str().c_str(), buf, bufSize)) <= 0) |
161 | 0 | { |
162 | 0 | LOG4CXX_DECODE_CHAR(lsExeLink, exeLink.str()); |
163 | 0 | LogLog::warn(LOG4CXX_STR("Failed to read ") + lsExeLink); |
164 | 0 | return; |
165 | 0 | } |
166 | 10 | if (bufSize < bufCount) |
167 | 0 | buf[bufSize] = 0; |
168 | 10 | else |
169 | 10 | buf[bufCount] = 0; |
170 | | #else |
171 | | LogLog::warn(LOG4CXX_STR("Unable to determine the name of the executable file on this system")); |
172 | | return; |
173 | | #endif |
174 | | |
175 | | // Add the path to the properties |
176 | 10 | std::string programFileName(buf); |
177 | 10 | if (programFileName.empty()) |
178 | 0 | { |
179 | 0 | LogLog::warn(LOG4CXX_STR("Current executable's file name is empty")); |
180 | 0 | return; |
181 | 0 | } |
182 | | |
183 | 10 | LOG4CXX_DECODE_CHAR(lsProgramFileName, programFileName); |
184 | 10 | LogString prefix{ LOG4CXX_STR("PROGRAM_FILE_PATH") }; |
185 | 10 | props.setProperty(prefix, lsProgramFileName); |
186 | | |
187 | 10 | #if LOG4CXX_HAS_FILESYSTEM_PATH |
188 | | // Add the path components to the properties |
189 | 10 | prefix += '.'; |
190 | 10 | FilesystemPath programPath(programFileName); |
191 | | #if LOG4CXX_LOGCHAR_IS_WCHAR |
192 | | auto root_name = programPath.root_name().wstring(); |
193 | 5 | if (!root_name.empty()) |
194 | 0 | props.setProperty(prefix + LOG4CXX_STR("ROOT_NAME"), root_name); |
195 | | auto root_directory = programPath.root_directory().wstring(); |
196 | 5 | props.setProperty(prefix + LOG4CXX_STR("ROOT_DIRECTORY"),root_directory); |
197 | | auto root_path = programPath.root_path().wstring(); |
198 | 5 | props.setProperty(prefix + LOG4CXX_STR("ROOT_PATH"), root_path); |
199 | | auto relative_path = programPath.relative_path().wstring(); |
200 | 5 | props.setProperty(prefix + LOG4CXX_STR("RELATIVE_PATH"), relative_path); |
201 | | auto parent_path = programPath.parent_path().wstring(); |
202 | 5 | props.setProperty(prefix + LOG4CXX_STR("PARENT_PATH"), parent_path); |
203 | | auto filename = programPath.filename().wstring(); |
204 | 5 | props.setProperty(prefix + LOG4CXX_STR("FILENAME"), filename); |
205 | | auto stem = programPath.stem().wstring(); |
206 | 5 | props.setProperty(prefix + LOG4CXX_STR("STEM"), stem); |
207 | | auto extension = programPath.extension().wstring(); |
208 | 5 | if (!extension.empty()) |
209 | 0 | props.setProperty(prefix + LOG4CXX_STR("EXTENSION"), extension); |
210 | | #else |
211 | 5 | LOG4CXX_DECODE_CHAR(root_name, programPath.root_name().string()); |
212 | 5 | if (!root_name.empty()) |
213 | 0 | props.setProperty(prefix + LOG4CXX_STR("ROOT_NAME"), root_name); |
214 | 5 | LOG4CXX_DECODE_CHAR(root_directory, programPath.root_directory().string()); |
215 | 5 | props.setProperty(prefix + LOG4CXX_STR("ROOT_DIRECTORY"),root_directory); |
216 | 5 | LOG4CXX_DECODE_CHAR(root_path, programPath.root_path().string()); |
217 | 5 | props.setProperty(prefix + LOG4CXX_STR("ROOT_PATH"), root_path); |
218 | 5 | LOG4CXX_DECODE_CHAR(relative_path, programPath.relative_path().string()); |
219 | 5 | props.setProperty(prefix + LOG4CXX_STR("RELATIVE_PATH"), relative_path); |
220 | 5 | LOG4CXX_DECODE_CHAR(parent_path, programPath.parent_path().string()); |
221 | 5 | props.setProperty(prefix + LOG4CXX_STR("PARENT_PATH"), parent_path); |
222 | 5 | LOG4CXX_DECODE_CHAR(filename, programPath.filename().string()); |
223 | 5 | props.setProperty(prefix + LOG4CXX_STR("FILENAME"), filename); |
224 | 5 | LOG4CXX_DECODE_CHAR(stem, programPath.stem().string()); |
225 | 5 | props.setProperty(prefix + LOG4CXX_STR("STEM"), stem); |
226 | 5 | LOG4CXX_DECODE_CHAR(extension, programPath.extension().string()); |
227 | 5 | if (!extension.empty()) |
228 | 0 | props.setProperty(prefix + LOG4CXX_STR("EXTENSION"), extension); |
229 | | #endif |
230 | 10 | #endif // LOG4CXX_HAS_FILESYSTEM_PATH |
231 | 10 | } log4cxx::helpers::System::addProgramFilePathComponents(log4cxx::helpers::Properties&) Line | Count | Source | 136 | 5 | { | 137 | | // Find the executable file name | 138 | 5 | static const int bufSize = 4096; | 139 | 5 | char buf[bufSize+1] = {0}, pathSepar = '/'; | 140 | | #if defined(_WIN32) | 141 | | if (0 == GetModuleFileName(NULL, buf, bufSize)) | 142 | | { | 143 | | LogString lsErrorCode; | 144 | | StringHelper::toString((int)GetLastError(), lsErrorCode); | 145 | | LogLog::warn(LOG4CXX_STR("GetModuleFileName error ") + lsErrorCode); | 146 | | return; | 147 | | } | 148 | | pathSepar = '\\'; | 149 | | #elif defined(__APPLE__) | 150 | | uint32_t bufCount = bufSize; | 151 | | if (0 != _NSGetExecutablePath(buf, &bufCount)) | 152 | | { | 153 | | LogLog::warn(LOG4CXX_STR("_NSGetExecutablePath failed")); | 154 | | return; | 155 | | } | 156 | | #elif (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 500) || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) | 157 | | int bufCount = 0; | 158 | 5 | std::ostringstream exeLink; | 159 | 5 | exeLink << "/proc/" << getpid() << "/exe"; | 160 | 5 | if ((bufCount = readlink(exeLink.str().c_str(), buf, bufSize)) <= 0) | 161 | 0 | { | 162 | 0 | LOG4CXX_DECODE_CHAR(lsExeLink, exeLink.str()); | 163 | 0 | LogLog::warn(LOG4CXX_STR("Failed to read ") + lsExeLink); | 164 | 0 | return; | 165 | 0 | } | 166 | 5 | if (bufSize < bufCount) | 167 | 0 | buf[bufSize] = 0; | 168 | 5 | else | 169 | 5 | buf[bufCount] = 0; | 170 | | #else | 171 | | LogLog::warn(LOG4CXX_STR("Unable to determine the name of the executable file on this system")); | 172 | | return; | 173 | | #endif | 174 | | | 175 | | // Add the path to the properties | 176 | 5 | std::string programFileName(buf); | 177 | 5 | if (programFileName.empty()) | 178 | 0 | { | 179 | 0 | LogLog::warn(LOG4CXX_STR("Current executable's file name is empty")); | 180 | 0 | return; | 181 | 0 | } | 182 | | | 183 | 5 | LOG4CXX_DECODE_CHAR(lsProgramFileName, programFileName); | 184 | 5 | LogString prefix{ LOG4CXX_STR("PROGRAM_FILE_PATH") }; | 185 | 5 | props.setProperty(prefix, lsProgramFileName); | 186 | | | 187 | 5 | #if LOG4CXX_HAS_FILESYSTEM_PATH | 188 | | // Add the path components to the properties | 189 | 5 | prefix += '.'; | 190 | 5 | FilesystemPath programPath(programFileName); | 191 | | #if LOG4CXX_LOGCHAR_IS_WCHAR | 192 | | auto root_name = programPath.root_name().wstring(); | 193 | | if (!root_name.empty()) | 194 | | props.setProperty(prefix + LOG4CXX_STR("ROOT_NAME"), root_name); | 195 | | auto root_directory = programPath.root_directory().wstring(); | 196 | | props.setProperty(prefix + LOG4CXX_STR("ROOT_DIRECTORY"),root_directory); | 197 | | auto root_path = programPath.root_path().wstring(); | 198 | | props.setProperty(prefix + LOG4CXX_STR("ROOT_PATH"), root_path); | 199 | | auto relative_path = programPath.relative_path().wstring(); | 200 | | props.setProperty(prefix + LOG4CXX_STR("RELATIVE_PATH"), relative_path); | 201 | | auto parent_path = programPath.parent_path().wstring(); | 202 | | props.setProperty(prefix + LOG4CXX_STR("PARENT_PATH"), parent_path); | 203 | | auto filename = programPath.filename().wstring(); | 204 | | props.setProperty(prefix + LOG4CXX_STR("FILENAME"), filename); | 205 | | auto stem = programPath.stem().wstring(); | 206 | | props.setProperty(prefix + LOG4CXX_STR("STEM"), stem); | 207 | | auto extension = programPath.extension().wstring(); | 208 | | if (!extension.empty()) | 209 | | props.setProperty(prefix + LOG4CXX_STR("EXTENSION"), extension); | 210 | | #else | 211 | 5 | LOG4CXX_DECODE_CHAR(root_name, programPath.root_name().string()); | 212 | 5 | if (!root_name.empty()) | 213 | 0 | props.setProperty(prefix + LOG4CXX_STR("ROOT_NAME"), root_name); | 214 | 5 | LOG4CXX_DECODE_CHAR(root_directory, programPath.root_directory().string()); | 215 | 5 | props.setProperty(prefix + LOG4CXX_STR("ROOT_DIRECTORY"),root_directory); | 216 | 5 | LOG4CXX_DECODE_CHAR(root_path, programPath.root_path().string()); | 217 | 5 | props.setProperty(prefix + LOG4CXX_STR("ROOT_PATH"), root_path); | 218 | 5 | LOG4CXX_DECODE_CHAR(relative_path, programPath.relative_path().string()); | 219 | 5 | props.setProperty(prefix + LOG4CXX_STR("RELATIVE_PATH"), relative_path); | 220 | 5 | LOG4CXX_DECODE_CHAR(parent_path, programPath.parent_path().string()); | 221 | 5 | props.setProperty(prefix + LOG4CXX_STR("PARENT_PATH"), parent_path); | 222 | 5 | LOG4CXX_DECODE_CHAR(filename, programPath.filename().string()); | 223 | 5 | props.setProperty(prefix + LOG4CXX_STR("FILENAME"), filename); | 224 | 5 | LOG4CXX_DECODE_CHAR(stem, programPath.stem().string()); | 225 | 5 | props.setProperty(prefix + LOG4CXX_STR("STEM"), stem); | 226 | 5 | LOG4CXX_DECODE_CHAR(extension, programPath.extension().string()); | 227 | 5 | if (!extension.empty()) | 228 | 0 | props.setProperty(prefix + LOG4CXX_STR("EXTENSION"), extension); | 229 | 5 | #endif | 230 | 5 | #endif // LOG4CXX_HAS_FILESYSTEM_PATH | 231 | 5 | } |
log4cxx::helpers::System::addProgramFilePathComponents(log4cxx::helpers::Properties&) Line | Count | Source | 136 | 5 | { | 137 | | // Find the executable file name | 138 | 5 | static const int bufSize = 4096; | 139 | 5 | char buf[bufSize+1] = {0}, pathSepar = '/'; | 140 | | #if defined(_WIN32) | 141 | | if (0 == GetModuleFileName(NULL, buf, bufSize)) | 142 | | { | 143 | | LogString lsErrorCode; | 144 | | StringHelper::toString((int)GetLastError(), lsErrorCode); | 145 | | LogLog::warn(LOG4CXX_STR("GetModuleFileName error ") + lsErrorCode); | 146 | | return; | 147 | | } | 148 | | pathSepar = '\\'; | 149 | | #elif defined(__APPLE__) | 150 | | uint32_t bufCount = bufSize; | 151 | | if (0 != _NSGetExecutablePath(buf, &bufCount)) | 152 | | { | 153 | | LogLog::warn(LOG4CXX_STR("_NSGetExecutablePath failed")); | 154 | | return; | 155 | | } | 156 | | #elif (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 500) || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) | 157 | | int bufCount = 0; | 158 | 5 | std::ostringstream exeLink; | 159 | 5 | exeLink << "/proc/" << getpid() << "/exe"; | 160 | 5 | if ((bufCount = readlink(exeLink.str().c_str(), buf, bufSize)) <= 0) | 161 | 0 | { | 162 | 0 | LOG4CXX_DECODE_CHAR(lsExeLink, exeLink.str()); | 163 | 0 | LogLog::warn(LOG4CXX_STR("Failed to read ") + lsExeLink); | 164 | 0 | return; | 165 | 0 | } | 166 | 5 | if (bufSize < bufCount) | 167 | 0 | buf[bufSize] = 0; | 168 | 5 | else | 169 | 5 | buf[bufCount] = 0; | 170 | | #else | 171 | | LogLog::warn(LOG4CXX_STR("Unable to determine the name of the executable file on this system")); | 172 | | return; | 173 | | #endif | 174 | | | 175 | | // Add the path to the properties | 176 | 5 | std::string programFileName(buf); | 177 | 5 | if (programFileName.empty()) | 178 | 0 | { | 179 | 0 | LogLog::warn(LOG4CXX_STR("Current executable's file name is empty")); | 180 | 0 | return; | 181 | 0 | } | 182 | | | 183 | 5 | LOG4CXX_DECODE_CHAR(lsProgramFileName, programFileName); | 184 | 5 | LogString prefix{ LOG4CXX_STR("PROGRAM_FILE_PATH") }; | 185 | 5 | props.setProperty(prefix, lsProgramFileName); | 186 | | | 187 | 5 | #if LOG4CXX_HAS_FILESYSTEM_PATH | 188 | | // Add the path components to the properties | 189 | 5 | prefix += '.'; | 190 | 5 | FilesystemPath programPath(programFileName); | 191 | 5 | #if LOG4CXX_LOGCHAR_IS_WCHAR | 192 | 5 | auto root_name = programPath.root_name().wstring(); | 193 | 5 | if (!root_name.empty()) | 194 | 0 | props.setProperty(prefix + LOG4CXX_STR("ROOT_NAME"), root_name); | 195 | 5 | auto root_directory = programPath.root_directory().wstring(); | 196 | 5 | props.setProperty(prefix + LOG4CXX_STR("ROOT_DIRECTORY"),root_directory); | 197 | 5 | auto root_path = programPath.root_path().wstring(); | 198 | 5 | props.setProperty(prefix + LOG4CXX_STR("ROOT_PATH"), root_path); | 199 | 5 | auto relative_path = programPath.relative_path().wstring(); | 200 | 5 | props.setProperty(prefix + LOG4CXX_STR("RELATIVE_PATH"), relative_path); | 201 | 5 | auto parent_path = programPath.parent_path().wstring(); | 202 | 5 | props.setProperty(prefix + LOG4CXX_STR("PARENT_PATH"), parent_path); | 203 | 5 | auto filename = programPath.filename().wstring(); | 204 | 5 | props.setProperty(prefix + LOG4CXX_STR("FILENAME"), filename); | 205 | 5 | auto stem = programPath.stem().wstring(); | 206 | 5 | props.setProperty(prefix + LOG4CXX_STR("STEM"), stem); | 207 | 5 | auto extension = programPath.extension().wstring(); | 208 | 5 | if (!extension.empty()) | 209 | 0 | props.setProperty(prefix + LOG4CXX_STR("EXTENSION"), extension); | 210 | | #else | 211 | | LOG4CXX_DECODE_CHAR(root_name, programPath.root_name().string()); | 212 | | if (!root_name.empty()) | 213 | | props.setProperty(prefix + LOG4CXX_STR("ROOT_NAME"), root_name); | 214 | | LOG4CXX_DECODE_CHAR(root_directory, programPath.root_directory().string()); | 215 | | props.setProperty(prefix + LOG4CXX_STR("ROOT_DIRECTORY"),root_directory); | 216 | | LOG4CXX_DECODE_CHAR(root_path, programPath.root_path().string()); | 217 | | props.setProperty(prefix + LOG4CXX_STR("ROOT_PATH"), root_path); | 218 | | LOG4CXX_DECODE_CHAR(relative_path, programPath.relative_path().string()); | 219 | | props.setProperty(prefix + LOG4CXX_STR("RELATIVE_PATH"), relative_path); | 220 | | LOG4CXX_DECODE_CHAR(parent_path, programPath.parent_path().string()); | 221 | | props.setProperty(prefix + LOG4CXX_STR("PARENT_PATH"), parent_path); | 222 | | LOG4CXX_DECODE_CHAR(filename, programPath.filename().string()); | 223 | | props.setProperty(prefix + LOG4CXX_STR("FILENAME"), filename); | 224 | | LOG4CXX_DECODE_CHAR(stem, programPath.stem().string()); | 225 | | props.setProperty(prefix + LOG4CXX_STR("STEM"), stem); | 226 | | LOG4CXX_DECODE_CHAR(extension, programPath.extension().string()); | 227 | | if (!extension.empty()) | 228 | | props.setProperty(prefix + LOG4CXX_STR("EXTENSION"), extension); | 229 | | #endif | 230 | 5 | #endif // LOG4CXX_HAS_FILESYSTEM_PATH | 231 | 5 | } |
|