1 /*
2 * Copyright (c) 2021 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_input_socket_client.h"
17
18 #include <sys/time.h>
19 #include <cstring>
20 #include <iostream>
21
22 namespace OHOS {
23 namespace HiviewDFX {
24 static HilogInputSocketClient g_hilogInputSocketClient;
HilogWriteLogMessage(HilogMsg * header,const char * tag,uint16_t tagLen,const char * fmt,uint16_t fmtLen)25 extern "C" int HilogWriteLogMessage(HilogMsg *header, const char *tag, uint16_t tagLen, const char *fmt,
26 uint16_t fmtLen)
27 {
28 return g_hilogInputSocketClient.WriteLogMessage(header, tag, tagLen, fmt, fmtLen);
29 }
30
WriteLogMessage(HilogMsg * header,const char * tag,uint16_t tagLen,const char * fmt,uint16_t fmtLen)31 int HilogInputSocketClient::WriteLogMessage(HilogMsg *header, const char *tag, uint16_t tagLen, const char *fmt,
32 uint16_t fmtLen)
33 {
34 int ret = CheckSocket();
35 if (ret < 0) {
36 return ret;
37 }
38
39 struct timeval tv = {0};
40 gettimeofday(&tv, nullptr);
41 header->tv_sec = static_cast<uint32_t>(tv.tv_sec);
42 header->tv_nsec = static_cast<uint32_t>(tv.tv_usec * 1000); // 1000 : usec convert to nsec
43 header->len = sizeof(HilogMsg) + tagLen + fmtLen;
44 header->tag_len = tagLen;
45
46 iovec vec[3];
47 vec[0].iov_base = header; // 0 : index of hos log header
48 vec[0].iov_len = sizeof(HilogMsg); // 0 : index of hos log header
49 vec[1].iov_base = (void*)tag; // 1 : index of log tag
50 vec[1].iov_len = tagLen; // 1 : index of log tag
51 vec[2].iov_base = (void*)fmt; // 2 : index of log content
52 vec[2].iov_len = fmtLen; // 2 : index of log content
53 ret = WriteV(vec, 3); // 3 : written size of vector
54 if (ret < 0) {
55 Connect();
56 ret = WriteV(vec, 3); // 3 : written size of vector
57 }
58
59 return ret;
60 }
61 } // namespace HiviewDFX
62 } // namespace OHOS
63