1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <hilog_adapter.h>
17 #include <fcntl.h>
18 #include <dlfcn.h>
19 #include <unistd.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <pthread.h>
23
24 #include "musl_log.h"
25
26 #define PATH_MAX 256
27 #define DFX_LOG_LIB "libasan_logger.z.so"
28 #define DFX_LOG_INTERFACE "WriteGwpAsanLog"
29 static void (*g_dfxLogPtr)(char*, size_t);
30 static void* g_dfxLibHandler = NULL;
31 static pthread_mutex_t g_muslLogMutex = PTHREAD_MUTEX_INITIALIZER;
32
ohos_dfx_log(const char *str)33 int ohos_dfx_log(const char *str)
34 {
35 if (g_dfxLogPtr != NULL) {
36 g_dfxLogPtr(str, strlen(str));
37 return 0;
38 }
39
40 pthread_mutex_lock(&g_muslLogMutex);
41 if (g_dfxLogPtr != NULL) {
42 g_dfxLogPtr(str, strlen(str));
43 pthread_mutex_unlock(&g_muslLogMutex);
44 return 0;
45 }
46 if (g_dfxLibHandler == NULL) {
47 g_dfxLibHandler = dlopen(DFX_LOG_LIB, RTLD_LAZY);
48 if (g_dfxLibHandler == NULL) {
49 MUSL_LOGE("[musl_log] dlopen %{public}s failed!\n", DFX_LOG_LIB);
50 pthread_mutex_unlock(&g_muslLogMutex);
51 return 0;
52 }
53 }
54 if (g_dfxLogPtr == NULL) {
55 *(void **)(&g_dfxLogPtr) = dlsym(g_dfxLibHandler, DFX_LOG_INTERFACE);
56 if (g_dfxLogPtr != NULL) {
57 g_dfxLogPtr(str, strlen(str));
58 } else {
59 MUSL_LOGE("[musl_log] dlsym %{public}s, failed!\n", DFX_LOG_INTERFACE);
60 }
61 }
62 pthread_mutex_unlock(&g_muslLogMutex);
63
64 return 0;
65 }
66
musl_log(const char *fmt, ...)67 int musl_log(const char *fmt, ...)
68 {
69 int ret;
70 va_list ap;
71 va_start(ap, fmt);
72 ret = HiLogAdapterPrintArgs(MUSL_LOG_TYPE, LOG_INFO, MUSL_LOG_DOMAIN, MUSL_LOG_TAG, fmt, ap);
73 va_end(ap);
74 return ret;
75 }
76