blob: 1f4b69b536215f371a3f2cd112d6ce18481de305 [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
Jooyung Han2d954132020-03-17 03:43:03 +090019#if defined(__ANDROID_SDK_VERSION__) && (__ANDROID_SDK_VERSION__ <= 29)
Jiyong Park2c608b32020-03-03 16:12:36 +090020#define USE_DLSYM
21#endif
Jiyong Park2c608b32020-03-03 16:12:36 +090022
23#ifdef USE_DLSYM
Tom Cherry349b0c42020-01-08 14:47:42 -080024#include <dlfcn.h>
25#endif
26
27namespace android {
28namespace base {
29
Jiyong Park2c608b32020-03-03 16:12:36 +090030#ifdef USE_DLSYM
Tom Cherry349b0c42020-01-08 14:47:42 -080031
32const 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)
Tom Cherryebf43ad2020-03-11 11:07:13 -070049 DLSYM(__android_log_write_log_message)
Tom Cherry349b0c42020-01-08 14:47:42 -080050 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)
Tom Cherry0391a872020-01-16 15:58:02 -080055 DLSYM(__android_log_set_minimum_priority);
56 DLSYM(__android_log_get_minimum_priority);
Tom Cherry69ee5dd2020-01-22 07:48:42 -080057 DLSYM(__android_log_set_default_tag);
Tom Cherry349b0c42020-01-08 14:47:42 -080058#undef DLSYM
59
60 return real_liblog_functions;
61 }();
62
63 return liblog_functions;
64}
65
66#else
67
68const 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,
Tom Cherryebf43ad2020-03-11 11:07:13 -070072 .__android_log_write_log_message = __android_log_write_log_message,
Tom Cherry349b0c42020-01-08 14:47:42 -080073 .__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,
Tom Cherry0391a872020-01-16 15:58:02 -080078 .__android_log_set_minimum_priority = __android_log_set_minimum_priority,
79 .__android_log_get_minimum_priority = __android_log_get_minimum_priority,
Tom Cherry69ee5dd2020-01-22 07:48:42 -080080 .__android_log_set_default_tag = __android_log_set_default_tag,
Tom Cherry349b0c42020-01-08 14:47:42 -080081 };
82 }();
83 return liblog_functions;
84}
85
86#endif
87
88} // namespace base
89} // namespace android