1570af302Sopenharmony_ci/* 2570af302Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 3570af302Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4570af302Sopenharmony_ci * you may not use this file except in compliance with the License. 5570af302Sopenharmony_ci * You may obtain a copy of the License at 6570af302Sopenharmony_ci * 7570af302Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8570af302Sopenharmony_ci * 9570af302Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10570af302Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11570af302Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12570af302Sopenharmony_ci * See the License for the specific language governing permissions and 13570af302Sopenharmony_ci * limitations under the License. 14570af302Sopenharmony_ci */ 15570af302Sopenharmony_ci 16570af302Sopenharmony_ci#include <hilog_adapter.h> 17570af302Sopenharmony_ci#include <fcntl.h> 18570af302Sopenharmony_ci#include <dlfcn.h> 19570af302Sopenharmony_ci#include <unistd.h> 20570af302Sopenharmony_ci#include <stdio.h> 21570af302Sopenharmony_ci#include <string.h> 22570af302Sopenharmony_ci#include <pthread.h> 23570af302Sopenharmony_ci 24570af302Sopenharmony_ci#include "musl_log.h" 25570af302Sopenharmony_ci 26570af302Sopenharmony_ci#define PATH_MAX 256 27570af302Sopenharmony_ci#define DFX_LOG_LIB "libasan_logger.z.so" 28570af302Sopenharmony_ci#define DFX_LOG_INTERFACE "WriteGwpAsanLog" 29570af302Sopenharmony_cistatic void (*g_dfxLogPtr)(char*, size_t); 30570af302Sopenharmony_cistatic void* g_dfxLibHandler = NULL; 31570af302Sopenharmony_cistatic pthread_mutex_t g_muslLogMutex = PTHREAD_MUTEX_INITIALIZER; 32570af302Sopenharmony_ci 33570af302Sopenharmony_ciint ohos_dfx_log(const char *str) 34570af302Sopenharmony_ci{ 35570af302Sopenharmony_ci if (g_dfxLogPtr != NULL) { 36570af302Sopenharmony_ci g_dfxLogPtr(str, strlen(str)); 37570af302Sopenharmony_ci return 0; 38570af302Sopenharmony_ci } 39570af302Sopenharmony_ci 40570af302Sopenharmony_ci pthread_mutex_lock(&g_muslLogMutex); 41570af302Sopenharmony_ci if (g_dfxLogPtr != NULL) { 42570af302Sopenharmony_ci g_dfxLogPtr(str, strlen(str)); 43570af302Sopenharmony_ci pthread_mutex_unlock(&g_muslLogMutex); 44570af302Sopenharmony_ci return 0; 45570af302Sopenharmony_ci } 46570af302Sopenharmony_ci if (g_dfxLibHandler == NULL) { 47570af302Sopenharmony_ci g_dfxLibHandler = dlopen(DFX_LOG_LIB, RTLD_LAZY); 48570af302Sopenharmony_ci if (g_dfxLibHandler == NULL) { 49570af302Sopenharmony_ci MUSL_LOGE("[musl_log] dlopen %{public}s failed!\n", DFX_LOG_LIB); 50570af302Sopenharmony_ci pthread_mutex_unlock(&g_muslLogMutex); 51570af302Sopenharmony_ci return 0; 52570af302Sopenharmony_ci } 53570af302Sopenharmony_ci } 54570af302Sopenharmony_ci if (g_dfxLogPtr == NULL) { 55570af302Sopenharmony_ci *(void **)(&g_dfxLogPtr) = dlsym(g_dfxLibHandler, DFX_LOG_INTERFACE); 56570af302Sopenharmony_ci if (g_dfxLogPtr != NULL) { 57570af302Sopenharmony_ci g_dfxLogPtr(str, strlen(str)); 58570af302Sopenharmony_ci } else { 59570af302Sopenharmony_ci MUSL_LOGE("[musl_log] dlsym %{public}s, failed!\n", DFX_LOG_INTERFACE); 60570af302Sopenharmony_ci } 61570af302Sopenharmony_ci } 62570af302Sopenharmony_ci pthread_mutex_unlock(&g_muslLogMutex); 63570af302Sopenharmony_ci 64570af302Sopenharmony_ci return 0; 65570af302Sopenharmony_ci} 66570af302Sopenharmony_ci 67570af302Sopenharmony_ciint musl_log(const char *fmt, ...) 68570af302Sopenharmony_ci{ 69570af302Sopenharmony_ci int ret; 70570af302Sopenharmony_ci va_list ap; 71570af302Sopenharmony_ci va_start(ap, fmt); 72570af302Sopenharmony_ci ret = HiLogAdapterPrintArgs(MUSL_LOG_TYPE, LOG_INFO, MUSL_LOG_DOMAIN, MUSL_LOG_TAG, fmt, ap); 73570af302Sopenharmony_ci va_end(ap); 74570af302Sopenharmony_ci return ret; 75570af302Sopenharmony_ci} 76