blob: d0e2eaa77d4893b18a97ff219b09bdd76d5df3d5 [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 Cherry349b0c42020-01-08 14:47:42 -080053#undef DLSYM
54
55 return real_liblog_functions;
56 }();
57
58 return liblog_functions;
59}
60
61#else
62
63const std::optional<LibLogFunctions>& GetLibLogFunctions() {
64 static std::optional<LibLogFunctions> liblog_functions = []() -> std::optional<LibLogFunctions> {
65 return LibLogFunctions{
66 .__android_log_set_logger = __android_log_set_logger,
67 .__android_log_write_logger_data = __android_log_write_logger_data,
68 .__android_log_logd_logger = __android_log_logd_logger,
69 .__android_log_stderr_logger = __android_log_stderr_logger,
70 .__android_log_set_aborter = __android_log_set_aborter,
71 .__android_log_call_aborter = __android_log_call_aborter,
72 .__android_log_default_aborter = __android_log_default_aborter,
Tom Cherry0391a872020-01-16 15:58:02 -080073 .__android_log_set_minimum_priority = __android_log_set_minimum_priority,
74 .__android_log_get_minimum_priority = __android_log_get_minimum_priority,
Tom Cherry349b0c42020-01-08 14:47:42 -080075 };
76 }();
77 return liblog_functions;
78}
79
80#endif
81
82} // namespace base
83} // namespace android