blob: 8d5917907c807ecd1c07d5c7bfade2e278b0184c [file] [log] [blame]
Tom Cherry349b0c42020-01-08 14:47:42 -08001/*
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
Jiyong Park2c608b32020-03-03 16:12:36 +090019#if defined(__ANDROID__)
20#if !defined(NO_LIBLOG_DLSYM) || defined(__ANDROID_APEX__)
21#define USE_DLSYM
22#endif
23#endif
24
25#ifdef USE_DLSYM
Tom Cherry349b0c42020-01-08 14:47:42 -080026#include <dlfcn.h>
27#endif
28
29namespace android {
30namespace base {
31
Jiyong Park2c608b32020-03-03 16:12:36 +090032#ifdef USE_DLSYM
Tom Cherry349b0c42020-01-08 14:47:42 -080033
34const std::optional<LibLogFunctions>& GetLibLogFunctions() {
35 static std::optional<LibLogFunctions> liblog_functions = []() -> std::optional<LibLogFunctions> {
36 void* liblog_handle = dlopen("liblog.so", RTLD_NOW);
37 if (liblog_handle == nullptr) {
38 return {};
39 }
40
41 LibLogFunctions real_liblog_functions = {};
42
43#define DLSYM(name) \
44 real_liblog_functions.name = \
45 reinterpret_cast<decltype(LibLogFunctions::name)>(dlsym(liblog_handle, #name)); \
46 if (real_liblog_functions.name == nullptr) { \
47 return {}; \
48 }
49
50 DLSYM(__android_log_set_logger)
51 DLSYM(__android_log_write_logger_data)
52 DLSYM(__android_log_logd_logger)
53 DLSYM(__android_log_stderr_logger)
54 DLSYM(__android_log_set_aborter)
55 DLSYM(__android_log_call_aborter)
56 DLSYM(__android_log_default_aborter)
Tom Cherry0391a872020-01-16 15:58:02 -080057 DLSYM(__android_log_set_minimum_priority);
58 DLSYM(__android_log_get_minimum_priority);
Tom Cherry69ee5dd2020-01-22 07:48:42 -080059 DLSYM(__android_log_set_default_tag);
Tom Cherry349b0c42020-01-08 14:47:42 -080060#undef DLSYM
61
62 return real_liblog_functions;
63 }();
64
65 return liblog_functions;
66}
67
68#else
69
70const std::optional<LibLogFunctions>& GetLibLogFunctions() {
71 static std::optional<LibLogFunctions> liblog_functions = []() -> std::optional<LibLogFunctions> {
72 return LibLogFunctions{
73 .__android_log_set_logger = __android_log_set_logger,
74 .__android_log_write_logger_data = __android_log_write_logger_data,
75 .__android_log_logd_logger = __android_log_logd_logger,
76 .__android_log_stderr_logger = __android_log_stderr_logger,
77 .__android_log_set_aborter = __android_log_set_aborter,
78 .__android_log_call_aborter = __android_log_call_aborter,
79 .__android_log_default_aborter = __android_log_default_aborter,
Tom Cherry0391a872020-01-16 15:58:02 -080080 .__android_log_set_minimum_priority = __android_log_set_minimum_priority,
81 .__android_log_get_minimum_priority = __android_log_get_minimum_priority,
Tom Cherry69ee5dd2020-01-22 07:48:42 -080082 .__android_log_set_default_tag = __android_log_set_default_tag,
Tom Cherry349b0c42020-01-08 14:47:42 -080083 };
84 }();
85 return liblog_functions;
86}
87
88#endif
89
90} // namespace base
91} // namespace android