blob: d5dfcd28be03b83f9088c0856c90fc13757d9531 [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
19#if defined(__ANDROID__) && !defined(NO_LIBLOG_DLSYM)
20#include <dlfcn.h>
21#endif
22
23namespace android {
24namespace base {
25
26#if defined(__ANDROID__) && !defined(NO_LIBLOG_DLSYM)
27
28const std::optional<LibLogFunctions>& GetLibLogFunctions() {
29 static std::optional<LibLogFunctions> liblog_functions = []() -> std::optional<LibLogFunctions> {
30 void* liblog_handle = dlopen("liblog.so", RTLD_NOW);
31 if (liblog_handle == nullptr) {
32 return {};
33 }
34
35 LibLogFunctions real_liblog_functions = {};
36
37#define DLSYM(name) \
38 real_liblog_functions.name = \
39 reinterpret_cast<decltype(LibLogFunctions::name)>(dlsym(liblog_handle, #name)); \
40 if (real_liblog_functions.name == nullptr) { \
41 return {}; \
42 }
43
44 DLSYM(__android_log_set_logger)
45 DLSYM(__android_log_write_logger_data)
46 DLSYM(__android_log_logd_logger)
47 DLSYM(__android_log_stderr_logger)
48 DLSYM(__android_log_set_aborter)
49 DLSYM(__android_log_call_aborter)
50 DLSYM(__android_log_default_aborter)
Tom Cherry0391a872020-01-16 15:58:02 -080051 DLSYM(__android_log_set_minimum_priority);
52 DLSYM(__android_log_get_minimum_priority);
Tom Cherry69ee5dd2020-01-22 07:48:42 -080053 DLSYM(__android_log_set_default_tag);
Tom Cherry349b0c42020-01-08 14:47:42 -080054#undef DLSYM
55
56 return real_liblog_functions;
57 }();
58
59 return liblog_functions;
60}
61
62#else
63
64const std::optional<LibLogFunctions>& GetLibLogFunctions() {
65 static std::optional<LibLogFunctions> liblog_functions = []() -> std::optional<LibLogFunctions> {
66 return LibLogFunctions{
67 .__android_log_set_logger = __android_log_set_logger,
68 .__android_log_write_logger_data = __android_log_write_logger_data,
69 .__android_log_logd_logger = __android_log_logd_logger,
70 .__android_log_stderr_logger = __android_log_stderr_logger,
71 .__android_log_set_aborter = __android_log_set_aborter,
72 .__android_log_call_aborter = __android_log_call_aborter,
73 .__android_log_default_aborter = __android_log_default_aborter,
Tom Cherry0391a872020-01-16 15:58:02 -080074 .__android_log_set_minimum_priority = __android_log_set_minimum_priority,
75 .__android_log_get_minimum_priority = __android_log_get_minimum_priority,
Tom Cherry69ee5dd2020-01-22 07:48:42 -080076 .__android_log_set_default_tag = __android_log_set_default_tag,
Tom Cherry349b0c42020-01-08 14:47:42 -080077 };
78 }();
79 return liblog_functions;
80}
81
82#endif
83
84} // namespace base
85} // namespace android