1 /*
2 * Copyright (c) 2023 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_base/log_base.h"
17 #include <hilog_base.h>
18 #include <vsnprintf_s_p.h>
19
20 #include <errno.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <sys/socket.h>
24 #include <sys/uio.h>
25 #include <sys/un.h>
26 #include <time.h>
27 #include <unistd.h>
28
29 #define LOG_LEN 3
30 #define ERROR_FD 2
31
32 static const int SOCKET_TYPE = SOCK_DGRAM | SOCK_NONBLOCK | SOCK_CLOEXEC;
33 static const struct sockaddr_un SOCKET_ADDR = {AF_UNIX, SOCKET_FILE_DIR INPUT_SOCKET_NAME};
34
SendMessage(HilogMsg * header,const char * tag,uint16_t tagLen,const char * fmt,uint16_t fmtLen)35 static int SendMessage(HilogMsg *header, const char *tag, uint16_t tagLen, const char *fmt, uint16_t fmtLen)
36 {
37 // The hilogbase interface cannot has mutex, so need to re-open and connect to the socketof the hilogd
38 // server each time you write logs. Althougth there is some overhead, you can only do this.
39 int socketFd = TEMP_FAILURE_RETRY(socket(AF_UNIX, SOCKET_TYPE, 0));
40 if (socketFd < 0) {
41 dprintf(ERROR_FD, "HiLogBase: Can't create socket! Errno: %d\n", errno);
42 return socketFd;
43 }
44
45 long int result =
46 TEMP_FAILURE_RETRY(connect(socketFd, (const struct sockaddr *)(&SOCKET_ADDR), sizeof(SOCKET_ADDR)));
47 if (result < 0) {
48 dprintf(ERROR_FD, "HiLogBase: Can't connect to server. Errno: %d\n", errno);
49 if (socketFd >= 0) {
50 close(socketFd);
51 }
52 return result;
53 }
54
55 struct timespec ts = {0};
56 (void)clock_gettime(CLOCK_REALTIME, &ts);
57 struct timespec ts_mono = {0};
58 (void)clock_gettime(CLOCK_MONOTONIC, &ts_mono);
59 header->tv_sec = (uint32_t)(ts.tv_sec);
60 header->tv_nsec = (uint32_t)(ts.tv_nsec);
61 header->mono_sec = (uint32_t)(ts_mono.tv_sec);
62 header->len = sizeof(HilogMsg) + tagLen + fmtLen;
63 header->tagLen = tagLen;
64
65 struct iovec vec[LOG_LEN] = {0};
66 vec[0].iov_base = header; // 0 : index of hos log header
67 vec[0].iov_len = sizeof(HilogMsg); // 0 : index of hos log header
68 vec[1].iov_base = (void *)((char *)(tag)); // 1 : index of log tag
69 vec[1].iov_len = tagLen; // 1 : index of log tag
70 vec[2].iov_base = (void *)((char *)(fmt)); // 2 : index of log content
71 vec[2].iov_len = fmtLen; // 2 : index of log content
72 int ret = TEMP_FAILURE_RETRY(writev(socketFd, vec, LOG_LEN));
73 if (socketFd >= 0) {
74 close(socketFd);
75 }
76 return ret;
77 }
78
HiLogBasePrintArgs(const LogType type,const LogLevel level,const unsigned int domain,const char * tag,const char * fmt,va_list ap)79 int HiLogBasePrintArgs(
80 const LogType type, const LogLevel level, const unsigned int domain, const char *tag, const char *fmt, va_list ap)
81 {
82 char buf[MAX_LOG_LEN] = {0};
83
84 vsnprintfp_s(buf, MAX_LOG_LEN, MAX_LOG_LEN - 1, true, fmt, ap);
85
86 size_t tagLen = strnlen(tag, MAX_TAG_LEN - 1);
87 size_t logLen = strnlen(buf, MAX_LOG_LEN - 1);
88 HilogMsg header = {0};
89 header.type = type;
90 header.level = level;
91 #ifndef __RECV_MSG_WITH_UCRED_
92 header.pid = getprocpid();
93 #endif
94 header.tid = (uint32_t)(gettid());
95 header.domain = domain;
96
97 return SendMessage(&header, tag, tagLen + 1, buf, logLen + 1);
98 }
99
HiLogBasePrint(LogType type,LogLevel level,unsigned int domain,const char * tag,const char * fmt,...)100 int HiLogBasePrint(LogType type, LogLevel level, unsigned int domain, const char *tag, const char *fmt, ...)
101 {
102 if (!HiLogBaseIsLoggable(domain, tag, level)) {
103 return -1;
104 }
105
106 int ret;
107 va_list ap;
108 va_start(ap, fmt);
109 ret = HiLogBasePrintArgs(type, level, domain, tag, fmt, ap);
110 va_end(ap);
111 return ret;
112 }
113
HiLogBaseIsLoggable(unsigned int domain,const char * tag,LogLevel level)114 bool HiLogBaseIsLoggable(unsigned int domain, const char *tag, LogLevel level)
115 {
116 if ((level <= LOG_LEVEL_MIN) || (level >= LOG_LEVEL_MAX) || tag == NULL) {
117 return false;
118 }
119 return true;
120 }
121