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 <cstdint>
19 
20 #include "hilog_common.h"
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     header->len = sizeof(HilogMsg) + tagLen + fmtLen;
40     header->tagLen = tagLen;
41 
42     iovec vec[3];
43     vec[0].iov_base = header;                                              // 0 : index of hos log header
44     vec[0].iov_len = sizeof(HilogMsg);                                     // 0 : index of hos log header
45     vec[1].iov_base = reinterpret_cast<void*>(const_cast<char*>(tag));     // 1 : index of log tag
46     vec[1].iov_len = tagLen;                                               // 1 : index of log tag
47     vec[2].iov_base = reinterpret_cast<void*>(const_cast<char*>(fmt));     // 2 : index of log content
48     vec[2].iov_len = fmtLen;                                               // 2 : index of log content
49     ret = WriteV(vec, 3);                                                  // 3 : written size of vector
50     if (ret < 0) {
51         Connect();
52         ret = WriteV(vec, 3);                // 3 : written size of vector
53     }
54 
55     return ret;
56 }
57 } // namespace HiviewDFX
58 } // namespace OHOS
59