blob: 9fd81b461c58e3aebe65bb3c6776750b98789e9c [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)
51#undef DLSYM
52
53 return real_liblog_functions;
54 }();
55
56 return liblog_functions;
57}
58
59#else
60
61const std::optional<LibLogFunctions>& GetLibLogFunctions() {
62 static std::optional<LibLogFunctions> liblog_functions = []() -> std::optional<LibLogFunctions> {
63 return LibLogFunctions{
64 .__android_log_set_logger = __android_log_set_logger,
65 .__android_log_write_logger_data = __android_log_write_logger_data,
66 .__android_log_logd_logger = __android_log_logd_logger,
67 .__android_log_stderr_logger = __android_log_stderr_logger,
68 .__android_log_set_aborter = __android_log_set_aborter,
69 .__android_log_call_aborter = __android_log_call_aborter,
70 .__android_log_default_aborter = __android_log_default_aborter,
71 };
72 }();
73 return liblog_functions;
74}
75
76#endif
77
78} // namespace base
79} // namespace android