Tom Cherry | 349b0c4 | 2020-01-08 14:47:42 -0800 | [diff] [blame] | 1 | /* |
| 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 Han | 2d95413 | 2020-03-17 03:43:03 +0900 | [diff] [blame] | 19 | #if defined(__ANDROID_SDK_VERSION__) && (__ANDROID_SDK_VERSION__ <= 29) |
Jiyong Park | 2c608b3 | 2020-03-03 16:12:36 +0900 | [diff] [blame] | 20 | #define USE_DLSYM |
| 21 | #endif |
Jiyong Park | 2c608b3 | 2020-03-03 16:12:36 +0900 | [diff] [blame] | 22 | |
| 23 | #ifdef USE_DLSYM |
Tom Cherry | 349b0c4 | 2020-01-08 14:47:42 -0800 | [diff] [blame] | 24 | #include <dlfcn.h> |
| 25 | #endif |
| 26 | |
| 27 | namespace android { |
| 28 | namespace base { |
| 29 | |
Jiyong Park | 2c608b3 | 2020-03-03 16:12:36 +0900 | [diff] [blame] | 30 | #ifdef USE_DLSYM |
Tom Cherry | 349b0c4 | 2020-01-08 14:47:42 -0800 | [diff] [blame] | 31 | |
| 32 | const 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 Cherry | ebf43ad | 2020-03-11 11:07:13 -0700 | [diff] [blame^] | 49 | DLSYM(__android_log_write_log_message) |
Tom Cherry | 349b0c4 | 2020-01-08 14:47:42 -0800 | [diff] [blame] | 50 | 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 Cherry | 0391a87 | 2020-01-16 15:58:02 -0800 | [diff] [blame] | 55 | DLSYM(__android_log_set_minimum_priority); |
| 56 | DLSYM(__android_log_get_minimum_priority); |
Tom Cherry | 69ee5dd | 2020-01-22 07:48:42 -0800 | [diff] [blame] | 57 | DLSYM(__android_log_set_default_tag); |
Tom Cherry | 349b0c4 | 2020-01-08 14:47:42 -0800 | [diff] [blame] | 58 | #undef DLSYM |
| 59 | |
| 60 | return real_liblog_functions; |
| 61 | }(); |
| 62 | |
| 63 | return liblog_functions; |
| 64 | } |
| 65 | |
| 66 | #else |
| 67 | |
| 68 | const 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 Cherry | ebf43ad | 2020-03-11 11:07:13 -0700 | [diff] [blame^] | 72 | .__android_log_write_log_message = __android_log_write_log_message, |
Tom Cherry | 349b0c4 | 2020-01-08 14:47:42 -0800 | [diff] [blame] | 73 | .__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 Cherry | 0391a87 | 2020-01-16 15:58:02 -0800 | [diff] [blame] | 78 | .__android_log_set_minimum_priority = __android_log_set_minimum_priority, |
| 79 | .__android_log_get_minimum_priority = __android_log_get_minimum_priority, |
Tom Cherry | 69ee5dd | 2020-01-22 07:48:42 -0800 | [diff] [blame] | 80 | .__android_log_set_default_tag = __android_log_set_default_tag, |
Tom Cherry | 349b0c4 | 2020-01-08 14:47:42 -0800 | [diff] [blame] | 81 | }; |
| 82 | }(); |
| 83 | return liblog_functions; |
| 84 | } |
| 85 | |
| 86 | #endif |
| 87 | |
| 88 | } // namespace base |
| 89 | } // namespace android |