1 /*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "liblog_symbols.h"
18
19 #if defined(__ANDROID_SDK_VERSION__) && (__ANDROID_SDK_VERSION__ <= 29)
20 #define USE_DLSYM
21 #endif
22
23 #ifdef USE_DLSYM
24 #include <dlfcn.h>
25 #endif
26
27 namespace android {
28 namespace base {
29
30 #ifdef USE_DLSYM
31
GetLibLogFunctions()32 const std::optional<LibLogFunctions>& GetLibLogFunctions() {
33 static std::optional<LibLogFunctions> liblog_functions = []() -> std::optional<LibLogFunctions> {
34 void* liblog_handle = dlopen("liblog.so", RTLD_NOW);
35 if (liblog_handle == nullptr) {
36 return {};
37 }
38
39 LibLogFunctions real_liblog_functions = {};
40
41 #define DLSYM(name) \
42 real_liblog_functions.name = \
43 reinterpret_cast<decltype(LibLogFunctions::name)>(dlsym(liblog_handle, #name)); \
44 if (real_liblog_functions.name == nullptr) { \
45 return {}; \
46 }
47
48 DLSYM(__android_log_set_logger)
49 DLSYM(__android_log_write_log_message)
50 DLSYM(__android_log_logd_logger)
51 DLSYM(__android_log_stderr_logger)
52 DLSYM(__android_log_set_aborter)
53 DLSYM(__android_log_call_aborter)
54 DLSYM(__android_log_default_aborter)
55 DLSYM(__android_log_set_minimum_priority);
56 DLSYM(__android_log_get_minimum_priority);
57 DLSYM(__android_log_set_default_tag);
58 #undef DLSYM
59
60 return real_liblog_functions;
61 }();
62
63 return liblog_functions;
64 }
65
66 #else
67
68 const std::optional<LibLogFunctions>& GetLibLogFunctions() {
69 static std::optional<LibLogFunctions> liblog_functions = []() -> std::optional<LibLogFunctions> {
70 return LibLogFunctions{
71 .__android_log_set_logger = __android_log_set_logger,
72 .__android_log_write_log_message = __android_log_write_log_message,
73 .__android_log_logd_logger = __android_log_logd_logger,
74 .__android_log_stderr_logger = __android_log_stderr_logger,
75 .__android_log_set_aborter = __android_log_set_aborter,
76 .__android_log_call_aborter = __android_log_call_aborter,
77 .__android_log_default_aborter = __android_log_default_aborter,
78 .__android_log_set_minimum_priority = __android_log_set_minimum_priority,
79 .__android_log_get_minimum_priority = __android_log_get_minimum_priority,
80 .__android_log_set_default_tag = __android_log_set_default_tag,
81 };
82 }();
83 return liblog_functions;
84 }
85
86 #endif
87
88 } // namespace base
89 } // namespace android
90