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 "log_controller.h"
17
18 #include <algorithm>
19 #include <cstring>
20 #include <cstdio>
21 #include <iostream>
22 #include <regex>
23 #include <securec.h>
24 #include <sstream>
25 #include <vector>
26 #include "hilog/log.h"
27 #include "hilog_common.h"
28 #include "hilogtool_msg.h"
29 #include "seq_packet_socket_client.h"
30 #include "properties.h"
31 #include "log_display.h"
32
33 namespace OHOS {
34 namespace HiviewDFX {
35 using namespace std;
36
37 const int LOG_PERSIST_FILE_SIZE = 4 * ONE_MB;
38 const int LOG_PERSIST_FILE_NUM = 10;
39 const uint32_t DEFAULT_JOBID = 1;
40 const uint32_t DEFAULT_KMSG_JOBID = 2;
SetMsgHead(MessageHeader * msgHeader,const uint8_t msgCmd,const uint16_t msgLen)41 void SetMsgHead(MessageHeader* msgHeader, const uint8_t msgCmd, const uint16_t msgLen)
42 {
43 if (!msgHeader) {
44 return;
45 }
46 msgHeader->version = 0;
47 msgHeader->msgType = msgCmd;
48 msgHeader->msgLen = msgLen;
49 }
50
Split(const std::string & src,const std::string & separator,std::vector<std::string> & dest)51 void Split(const std::string& src, const std::string& separator, std::vector<std::string>& dest)
52 {
53 string str = src;
54 string substring;
55 string::size_type start = 0;
56 string::size_type index;
57 dest.clear();
58 index = str.find_first_of(separator, start);
59 if (index == string::npos) {
60 dest.push_back(str);
61 return;
62 }
63 do {
64 substring = str.substr(start, index - start);
65 dest.push_back(substring);
66 start = index + separator.size();
67 index = str.find(separator, start);
68 if (start == string::npos) {
69 break;
70 }
71 } while (index != string::npos);
72 substring = str.substr(start);
73 dest.emplace_back(substring);
74 }
75
GetLogType(const string & logTypeStr)76 uint16_t GetLogType(const string& logTypeStr)
77 {
78 uint16_t logType;
79 if (logTypeStr == "init") {
80 logType = LOG_INIT;
81 } else if (logTypeStr == "core") {
82 logType = LOG_CORE;
83 } else if (logTypeStr == "app") {
84 logType = LOG_APP;
85 } else if (logTypeStr == "kmsg") {
86 logType = LOG_KMSG;
87 } else {
88 return 0xffff;
89 }
90 return logType;
91 }
92
GetBuffSize(const string & buffSizeStr)93 uint64_t GetBuffSize(const string& buffSizeStr)
94 {
95 uint64_t index = buffSizeStr.size() - 1;
96 long int buffSize;
97 std::regex reg("[0-9]+[bBkKmMgGtT]?");
98 if (!std::regex_match(buffSizeStr, reg)) {
99 std::cout << ParseErrorCode(ERR_BUFF_SIZE_INVALID) << std::endl;
100 exit(-1);
101 }
102 if (buffSizeStr[index] == 'b' || buffSizeStr[index] == 'B') {
103 buffSize = stol(buffSizeStr.substr(0, index));
104 } else if (buffSizeStr[index] == 'k' || buffSizeStr[index] == 'K') {
105 buffSize = stol(buffSizeStr.substr(0, index)) * ONE_KB;
106 } else if (buffSizeStr[index] == 'm' || buffSizeStr[index] == 'M') {
107 buffSize = stol(buffSizeStr.substr(0, index)) * ONE_MB;
108 } else if (buffSizeStr[index] == 'g' || buffSizeStr[index] == 'G') {
109 buffSize = stol(buffSizeStr.substr(0, index)) * ONE_GB;
110 } else if (buffSizeStr[index] == 't' || buffSizeStr[index] == 'T') {
111 buffSize = stol(buffSizeStr.substr(0, index)) * ONE_TB;
112 } else {
113 buffSize = stol(buffSizeStr.substr(0, index + 1));
114 }
115 return static_cast<uint64_t>(buffSize);
116 }
117
GetCompressAlg(const std::string & pressAlg)118 uint16_t GetCompressAlg(const std::string& pressAlg)
119 {
120 if (pressAlg == "none") {
121 return COMPRESS_TYPE_NONE;
122 } else if (pressAlg == "zlib") {
123 return COMPRESS_TYPE_ZLIB;
124 } else if (pressAlg == "zstd") {
125 return COMPRESS_TYPE_ZSTD;
126 }
127 return COMPRESS_TYPE_ZLIB;
128 }
129
GetLogLevel(const std::string & logLevelStr,std::string & logLevel)130 uint16_t GetLogLevel(const std::string& logLevelStr, std::string& logLevel)
131 {
132 if (logLevelStr == "debug" || logLevelStr == "DEBUG" || logLevelStr == "d" || logLevelStr == "D") {
133 logLevel = "D";
134 return LOG_DEBUG;
135 } else if (logLevelStr == "info" || logLevelStr == "INFO" || logLevelStr == "i" || logLevelStr == "I") {
136 logLevel = "I";
137 return LOG_INFO;
138 } else if (logLevelStr == "warn" || logLevelStr == "WARN" || logLevelStr == "w" || logLevelStr == "W") {
139 logLevel = "W";
140 return LOG_WARN;
141 } else if (logLevelStr == "error" || logLevelStr == "ERROR" || logLevelStr == "e" || logLevelStr == "E") {
142 logLevel = "E";
143 return LOG_ERROR;
144 } else if (logLevelStr == "fatal" || logLevelStr == "FATAL" || logLevelStr == "f" || logLevelStr == "F") {
145 logLevel = "F";
146 return LOG_FATAL;
147 }
148 return 0xffff;
149 }
150
SetDefaultLogType(const std::string & logTypeStr)151 string SetDefaultLogType(const std::string& logTypeStr)
152 {
153 string logType;
154 if (logTypeStr == "") {
155 logType = "core app";
156 } else if (logTypeStr == "all") {
157 logType = "core app init";
158 } else {
159 logType = logTypeStr;
160 }
161 return logType;
162 }
NextRequestOp(SeqPacketSocketClient & controller,uint16_t sendId)163 void NextRequestOp(SeqPacketSocketClient& controller, uint16_t sendId)
164 {
165 NextRequest nextRequest = {{0}};
166 SetMsgHead(&nextRequest.header, NEXT_REQUEST, sizeof(NextRequest)-sizeof(MessageHeader));
167 nextRequest.sendId = sendId;
168 controller.WriteAll((char*)&nextRequest, sizeof(NextRequest));
169 }
170
LogQueryRequestOp(SeqPacketSocketClient & controller,const HilogArgs * context)171 void LogQueryRequestOp(SeqPacketSocketClient& controller, const HilogArgs* context)
172 {
173 LogQueryRequest logQueryRequest = {{0}};
174 logQueryRequest.levels = context->levels;
175 logQueryRequest.types = context->types;
176 logQueryRequest.nPid = context->nPid;
177 logQueryRequest.nDomain = context->nDomain;
178 logQueryRequest.nTag = context->nTag;
179 for (int i = 0; i < context->nPid; i++) {
180 std::istringstream(context->pids[i]) >> std::dec >> logQueryRequest.pids[i];
181 }
182 for (int i = 0; i < context->nDomain; i++) {
183 std::istringstream(context->domains[i]) >> std::hex >> logQueryRequest.domains[i];
184 }
185 for (int i = 0; i < context->nTag; i++) {
186 if (strncpy_s(logQueryRequest.tags[i], MAX_TAG_LEN,
187 context->tags[i].c_str(), context->tags[i].length())) {
188 continue;
189 }
190 }
191 logQueryRequest.noLevels = context->noLevels;
192 logQueryRequest.noTypes = context->noTypes;
193 logQueryRequest.nNoPid = context->nNoPid;
194 logQueryRequest.nNoDomain = context->nNoDomain;
195 logQueryRequest.nNoTag = context->nNoTag;
196 for (int i = 0; i < context->nNoPid; i++) {
197 std::istringstream(context->noPids[i]) >> std::dec >> logQueryRequest.noPids[i];
198 }
199 for (int i = 0; i < context->nNoDomain; i++) {
200 std::istringstream(context->noDomains[i]) >> std::hex >> logQueryRequest.noDomains[i];
201 }
202 for (int i = 0; i < context->nNoTag; i++) {
203 if (strncpy_s(logQueryRequest.noTags[i], MAX_TAG_LEN,
204 context->noTags[i].c_str(), context->noTags[i].length())) {
205 continue;
206 }
207 }
208 SetMsgHead(&logQueryRequest.header, LOG_QUERY_REQUEST, sizeof(LogQueryRequest)-sizeof(MessageHeader));
209 logQueryRequest.header.version = 0;
210 controller.WriteAll((char*)&logQueryRequest, sizeof(LogQueryRequest));
211 }
212
LogQueryResponseOp(SeqPacketSocketClient & controller,char * recvBuffer,uint32_t bufLen,const HilogArgs * context,uint32_t format)213 void LogQueryResponseOp(SeqPacketSocketClient& controller, char* recvBuffer, uint32_t bufLen,
214 const HilogArgs* context, uint32_t format)
215 {
216 static std::vector<string> tailBuffer;
217 LogQueryResponse* rsp = reinterpret_cast<LogQueryResponse*>(recvBuffer);
218 HilogDataMessage* data = &(rsp->data);
219 if (data->sendId != SENDIDN) {
220 HilogShowLog(format, data, context, tailBuffer);
221 }
222 NextRequestOp(controller, SENDIDA);
223 while(1) {
224 std::fill_n(recvBuffer, bufLen, 0);
225 if (controller.RecvMsg(recvBuffer, bufLen) == 0) {
226 fprintf(stderr, "Unexpected EOF ");
227 HilogPrintError(errno);
228 exit(1);
229 return;
230 }
231 MessageHeader* msgHeader = &(rsp->header);
232 if (msgHeader->msgType == NEXT_RESPONSE) {
233 switch (data->sendId) {
234 case SENDIDN:
235 if (context->noBlockMode) {
236 uint16_t i = context->tailLines;
237 while (i-- && !tailBuffer.empty()) {
238 cout << tailBuffer.back() << endl;
239 tailBuffer.pop_back();
240 }
241 NextRequestOp(controller, SENDIDN);
242 exit(1);
243 }
244 break;
245 case SENDIDA:
246 HilogShowLog(format, data, context, tailBuffer);
247 NextRequestOp(controller, SENDIDA);
248 break;
249 default:
250 NextRequestOp(controller, SENDIDA);
251 break;
252 }
253 }
254 }
255 }
BufferSizeOp(SeqPacketSocketClient & controller,uint8_t msgCmd,const std::string & logTypeStr,const std::string & buffSizeStr)256 int32_t BufferSizeOp(SeqPacketSocketClient& controller, uint8_t msgCmd, const std::string& logTypeStr,
257 const std::string& buffSizeStr)
258 {
259 char msgToSend[MSG_MAX_LEN] = {0};
260 vector<string> vecLogType;
261 uint32_t logTypeNum;
262 uint32_t iter;
263 string logType = SetDefaultLogType(logTypeStr);
264 Split(logType, " ", vecLogType);
265 logTypeNum = vecLogType.size();
266 switch (msgCmd) {
267 case MC_REQ_BUFFER_SIZE: {
268 BufferSizeRequest* pBuffSizeReq = reinterpret_cast<BufferSizeRequest*>(msgToSend);
269 BuffSizeMsg* pBuffSizeMsg = reinterpret_cast<BuffSizeMsg*>(&pBuffSizeReq->buffSizeMsg);
270 if (logTypeNum * sizeof(BuffSizeMsg) + sizeof(MessageHeader) > MSG_MAX_LEN) {
271 return RET_FAIL;
272 }
273 for (iter = 0; iter < logTypeNum; iter++) {
274 pBuffSizeMsg->logType = GetLogType(vecLogType[iter]);
275 if (pBuffSizeMsg->logType == 0xffff) {
276 cout << ParseErrorCode(ERR_LOG_TYPE_INVALID) << endl;
277 return RET_FAIL;
278 }
279 pBuffSizeMsg++;
280 }
281 SetMsgHead(&pBuffSizeReq->msgHeader, msgCmd, sizeof(BuffSizeMsg) * logTypeNum);
282 controller.WriteAll(msgToSend, sizeof(MessageHeader) + sizeof(BuffSizeMsg) * logTypeNum);
283 break;
284 }
285
286 case MC_REQ_BUFFER_RESIZE: {
287 BufferResizeRequest* pBuffResizeReq = reinterpret_cast<BufferResizeRequest*>(msgToSend);
288 BuffResizeMsg* pBuffResizeMsg = reinterpret_cast<BuffResizeMsg*>(&pBuffResizeReq->buffResizeMsg);
289 if (logTypeNum * sizeof(BuffResizeMsg) + sizeof(MessageHeader) > MSG_MAX_LEN) {
290 return RET_FAIL;
291 }
292 for (iter = 0; iter < logTypeNum; iter++) {
293 pBuffResizeMsg->logType = GetLogType(vecLogType[iter]);
294 if (pBuffResizeMsg->logType == 0xffff) {
295 cout << ParseErrorCode(ERR_LOG_TYPE_INVALID) << endl;
296 return RET_FAIL;
297 }
298 pBuffResizeMsg->buffSize = GetBuffSize(buffSizeStr);
299 pBuffResizeMsg++;
300 }
301 SetMsgHead(&pBuffResizeReq->msgHeader, msgCmd, sizeof(BuffResizeMsg) * logTypeNum);
302 controller.WriteAll(msgToSend, sizeof(MessageHeader) + sizeof(BuffResizeMsg) * logTypeNum);
303 break;
304 }
305
306 default:
307 break;
308 }
309 return RET_SUCCESS;
310 }
311
StatisticInfoOp(SeqPacketSocketClient & controller,uint8_t msgCmd,const std::string & logTypeStr,const std::string & domainStr)312 int32_t StatisticInfoOp(SeqPacketSocketClient& controller, uint8_t msgCmd,
313 const std::string& logTypeStr, const std::string& domainStr)
314 {
315 if ((logTypeStr != "" && domainStr != "") || (logTypeStr == "" && domainStr == "")) {
316 return RET_FAIL;
317 }
318 uint16_t logType = GetLogType(logTypeStr);
319 uint32_t domain;
320 if (domainStr == "") {
321 domain = 0xffffffff;
322 if (logType == 0xffff) {
323 cout << ParseErrorCode(ERR_LOG_TYPE_INVALID) << endl;
324 return RET_FAIL;
325 }
326 } else {
327 std::istringstream(domainStr) >> domain;
328 if (domain == 0 || domain > DOMAIN_MAX_SCOPE) {
329 cout << ParseErrorCode(ERR_DOMAIN_INVALID) << endl;
330 return RET_FAIL;
331 }
332 }
333 switch (msgCmd) {
334 case MC_REQ_STATISTIC_INFO_QUERY: {
335 StatisticInfoQueryRequest staInfoQueryReq = {{0}};
336 staInfoQueryReq.logType = logType;
337 staInfoQueryReq.domain = domain;
338 SetMsgHead(&staInfoQueryReq.msgHeader, msgCmd, sizeof(StatisticInfoQueryRequest) - sizeof(MessageHeader));
339 controller.WriteAll((char*)&staInfoQueryReq, sizeof(StatisticInfoQueryRequest));
340 break;
341 }
342 case MC_REQ_STATISTIC_INFO_CLEAR: {
343 StatisticInfoClearRequest staInfoClearReq = {{0}};
344 staInfoClearReq.logType = logType;
345 staInfoClearReq.domain = domain;
346 SetMsgHead(&staInfoClearReq.msgHeader, msgCmd, sizeof(StatisticInfoClearRequest) - sizeof(MessageHeader));
347 controller.WriteAll((char*)&staInfoClearReq, sizeof(StatisticInfoClearRequest));
348 break;
349 }
350 default:
351 break;
352 }
353 return RET_SUCCESS;
354 }
355
LogClearOp(SeqPacketSocketClient & controller,uint8_t msgCmd,const std::string & logTypeStr)356 int32_t LogClearOp(SeqPacketSocketClient& controller, uint8_t msgCmd, const std::string& logTypeStr)
357 {
358 char msgToSend[MSG_MAX_LEN] = {0};
359 vector<string> vecLogType;
360 uint32_t logTypeNum;
361 uint32_t iter;
362 string logType = SetDefaultLogType(logTypeStr);
363 Split(logType, " ", vecLogType);
364 logTypeNum = vecLogType.size();
365 LogClearRequest* pLogClearReq = reinterpret_cast<LogClearRequest*>(msgToSend);
366 LogClearMsg* pLogClearMsg = reinterpret_cast<LogClearMsg*>(&pLogClearReq->logClearMsg);
367 if (!pLogClearMsg) {
368 cout << ParseErrorCode(ERR_MEM_ALLOC_FAIL) << endl;
369 return RET_FAIL;
370 }
371 if (logTypeNum * sizeof(LogClearMsg) + sizeof(MessageHeader) > MSG_MAX_LEN) {
372 cout << ParseErrorCode(ERR_MSG_LEN_INVALID) << endl;
373 return RET_FAIL;
374 }
375 for (iter = 0; iter < logTypeNum; iter++) {
376 pLogClearMsg->logType = GetLogType(vecLogType[iter]);
377 if (pLogClearMsg->logType == 0xffff) {
378 cout << ParseErrorCode(ERR_LOG_TYPE_INVALID) << endl;
379 return RET_FAIL;
380 }
381 pLogClearMsg++;
382 }
383 SetMsgHead(&pLogClearReq->msgHeader, msgCmd, sizeof(LogClearMsg) * logTypeNum);
384 controller.WriteAll(msgToSend, sizeof(LogClearMsg) * logTypeNum + sizeof(MessageHeader));
385 return RET_SUCCESS;
386 }
387
LogPersistOp(SeqPacketSocketClient & controller,uint8_t msgCmd,LogPersistParam * logPersistParam)388 int32_t LogPersistOp(SeqPacketSocketClient& controller, uint8_t msgCmd, LogPersistParam* logPersistParam)
389 {
390 char msgToSend[MSG_MAX_LEN] = {0};
391 vector<string> vecLogType;
392 vector<string> vecJobId;
393 uint32_t logTypeNum;
394 uint32_t jobIdNum;
395 uint32_t iter;
396 int ret = 0;
397 uint32_t fileSizeDefault = LOG_PERSIST_FILE_SIZE;
398 uint32_t fileNumDefault = LOG_PERSIST_FILE_NUM;
399 string logType = SetDefaultLogType(logPersistParam->logTypeStr);
400 Split(logType, " ", vecLogType);
401 Split(logPersistParam->jobIdStr, " ", vecJobId);
402 logTypeNum = vecLogType.size();
403 jobIdNum = vecJobId.size();
404 switch (msgCmd) {
405 case MC_REQ_LOG_PERSIST_START: {
406 LogPersistStartRequest* pLogPersistStartReq = reinterpret_cast<LogPersistStartRequest*>(msgToSend);
407 LogPersistStartMsg* pLogPersistStartMsg =
408 reinterpret_cast<LogPersistStartMsg*>(&pLogPersistStartReq->logPersistStartMsg);
409 if (sizeof(LogPersistStartRequest) > MSG_MAX_LEN) {
410 cout << ParseErrorCode(ERR_MSG_LEN_INVALID) << endl;
411 return RET_FAIL;
412 }
413 for (iter = 0; iter < logTypeNum; iter++) {
414 uint16_t tmpType = GetLogType(vecLogType[iter]);
415 if (tmpType == 0xffff) {
416 cout << ParseErrorCode(ERR_LOG_TYPE_INVALID) << endl;
417 return RET_FAIL;
418 }
419 pLogPersistStartMsg->logType = (0b01 << tmpType) | pLogPersistStartMsg->logType;
420 }
421 if (pLogPersistStartMsg->logType == (0b01 << LOG_KMSG)) {
422 pLogPersistStartMsg->jobId = (logPersistParam->jobIdStr == "") ? DEFAULT_KMSG_JOBID
423 : static_cast<uint32_t>(stoi(logPersistParam->jobIdStr));
424 } else {
425 pLogPersistStartMsg->jobId = (logPersistParam->jobIdStr == "") ? DEFAULT_JOBID
426 : static_cast<uint32_t>(stoi(logPersistParam->jobIdStr));
427 }
428 if (pLogPersistStartMsg->jobId <= 0) {
429 cout << ParseErrorCode(ERR_LOG_PERSIST_JOBID_INVALID) << endl;
430 return RET_FAIL;
431 }
432 pLogPersistStartMsg->compressAlg = (logPersistParam->compressAlgStr == "") ? COMPRESS_TYPE_ZLIB :
433 GetCompressAlg(logPersistParam->compressAlgStr);
434 pLogPersistStartMsg->fileSize = (logPersistParam->fileSizeStr == "") ? fileSizeDefault : GetBuffSize(
435 logPersistParam->fileSizeStr);
436 pLogPersistStartMsg->fileNum = (logPersistParam->fileNumStr == "") ? fileNumDefault
437 : static_cast<uint32_t>(stoi(logPersistParam->fileNumStr));
438 if (logPersistParam->fileNameStr.size() > FILE_PATH_MAX_LEN) {
439 cout << ParseErrorCode(ERR_LOG_PERSIST_FILE_NAME_INVALID) << endl;
440 return RET_FAIL;
441 }
442 if (logPersistParam->fileNameStr != " ") {
443 ret += strcpy_s(pLogPersistStartMsg->filePath, FILE_PATH_MAX_LEN, logPersistParam->fileNameStr.c_str());
444 }
445 SetMsgHead(&pLogPersistStartReq->msgHeader, msgCmd, sizeof(LogPersistStartRequest));
446 controller.WriteAll(msgToSend, sizeof(LogPersistStartRequest));
447 break;
448 }
449
450 case MC_REQ_LOG_PERSIST_STOP: {
451 LogPersistStopRequest* pLogPersistStopReq =
452 reinterpret_cast<LogPersistStopRequest*>(msgToSend);
453 LogPersistStopMsg* pLogPersistStopMsg =
454 reinterpret_cast<LogPersistStopMsg*>(&pLogPersistStopReq->logPersistStopMsg);
455 if (logPersistParam->jobIdStr == "") {
456 pLogPersistStopMsg->jobId = JOB_ID_ALL;
457 SetMsgHead(&pLogPersistStopReq->msgHeader, msgCmd, sizeof(LogPersistStopMsg));
458 controller.WriteAll(msgToSend, sizeof(LogPersistStopMsg) + sizeof(MessageHeader));
459 break;
460 }
461 if (jobIdNum * sizeof(LogPersistStopMsg) + sizeof(MessageHeader) > MSG_MAX_LEN) {
462 cout << ParseErrorCode(ERR_MSG_LEN_INVALID) << endl;
463 return RET_FAIL;
464 }
465 for (iter = 0; iter < jobIdNum; iter++) {
466 pLogPersistStopMsg->jobId = static_cast<uint32_t>(stoi(vecJobId[iter]));
467 pLogPersistStopMsg++;
468 }
469 SetMsgHead(&pLogPersistStopReq->msgHeader, msgCmd, sizeof(LogPersistStopMsg) * jobIdNum);
470 controller.WriteAll(msgToSend, sizeof(LogPersistStopMsg) * jobIdNum + sizeof(MessageHeader));
471 break;
472 }
473
474 case MC_REQ_LOG_PERSIST_QUERY: {
475 LogPersistQueryRequest* pLogPersistQueryReq =
476 reinterpret_cast<LogPersistQueryRequest*>(msgToSend);
477 LogPersistQueryMsg* pLogPersistQueryMsg =
478 reinterpret_cast<LogPersistQueryMsg*>(&pLogPersistQueryReq->logPersistQueryMsg);
479
480 for (iter = 0; iter < logTypeNum; iter++) {
481 uint16_t tmpType = GetLogType(vecLogType[iter]);
482 if (tmpType == 0xffff) {
483 cout << ParseErrorCode(ERR_LOG_TYPE_INVALID) << endl;
484 return RET_FAIL;
485 }
486 pLogPersistQueryMsg->logType = (0b01 << tmpType) | pLogPersistQueryMsg->logType;
487 }
488 SetMsgHead(&pLogPersistQueryReq->msgHeader, msgCmd, sizeof(LogPersistQueryMsg));
489 controller.WriteAll(msgToSend, sizeof(LogPersistQueryRequest));
490 break;
491 }
492
493 default:
494 break;
495 }
496
497 if (ret) {
498 return RET_FAIL;
499 }
500
501 return RET_SUCCESS;
502 }
503
SetPropertiesOp(SeqPacketSocketClient & controller,uint8_t operationType,SetPropertyParam * propertyParm)504 int32_t SetPropertiesOp(SeqPacketSocketClient& controller, uint8_t operationType, SetPropertyParam* propertyParm)
505 {
506 vector<string> vecDomain;
507 vector<string> vecTag;
508 uint32_t domainNum, tagNum;
509 uint32_t iter;
510 string key, value;
511 Split(propertyParm->domainStr, " ", vecDomain);
512 Split(propertyParm->tagStr, " ", vecTag);
513 domainNum = vecDomain.size();
514 tagNum = vecTag.size();
515 switch (operationType) {
516 case OT_PRIVATE_SWITCH:
517 key = GetPropertyName(PROP_PRIVATE);
518 if (propertyParm->privateSwitchStr == "on") {
519 PropertySet(key.c_str(), "true");
520 cout << "hilog private formatter is enabled" << endl;
521 } else if (propertyParm->privateSwitchStr == "off") {
522 PropertySet(key.c_str(), "false");
523 cout << "hilog private formatter is disabled" << endl;
524 } else {
525 cout << ParseErrorCode(ERR_PRIVATE_SWITCH_VALUE_INVALID) << endl;
526 return RET_FAIL;
527 }
528 break;
529 case OT_KMSG_SWITCH:
530 key = GetPropertyName(PROP_KMSG);
531 if (propertyParm->kmsgSwitchStr == "on") {
532 PropertySet(key.c_str(), "true");
533 std::cout << "hilog will store kmsg log" << std::endl;
534 } else if (propertyParm->kmsgSwitchStr == "off") {
535 PropertySet(key.c_str(), "false");
536 std::cout << "hilog will not store kmsg log" << std::endl;
537 } else {
538 std::cout << ParseErrorCode(ERR_KMSG_SWITCH_VALUE_INVALID) << std::endl;
539 return RET_FAIL;
540 }
541 break;
542 case OT_LOG_LEVEL:
543 if (propertyParm->tagStr != "" && propertyParm->domainStr != "") {
544 return RET_FAIL;
545 } else if (propertyParm->domainStr != "") { // by domain
546 std::string keyPre = GetPropertyName(PROP_DOMAIN_LOG_LEVEL);
547 for (iter = 0; iter < domainNum; iter++) {
548 key = keyPre + vecDomain[iter];
549 if (GetLogLevel(propertyParm->logLevelStr, value) == 0xffff) {
550 continue;
551 }
552 PropertySet(key.c_str(), value.c_str());
553 cout << "domain " << vecDomain[iter] << " level is set to " << propertyParm->logLevelStr << endl;
554 }
555 } else if (propertyParm->tagStr != "") { // by tag
556 std::string keyPre = GetPropertyName(PROP_TAG_LOG_LEVEL);
557 for (iter = 0; iter < tagNum; iter++) {
558 key = keyPre + vecTag[iter];
559 if (GetLogLevel(propertyParm->logLevelStr, value) == 0xffff) {
560 continue;
561 }
562 PropertySet(key.c_str(), value.c_str());
563 cout << "tag " << vecTag[iter] << " level is set to " << propertyParm->logLevelStr << endl;
564 }
565 } else {
566 key = GetPropertyName(PROP_GLOBAL_LOG_LEVEL);
567 if (GetLogLevel(propertyParm->logLevelStr, value) == 0xffff) {
568 return RET_FAIL;
569 }
570 PropertySet(key.c_str(), value.c_str());
571 cout << "global log level is set to " << propertyParm->logLevelStr << endl;
572 }
573 break;
574
575 case OT_FLOW_SWITCH:
576 if (propertyParm->flowSwitchStr == "pidon") {
577 key = GetPropertyName(PROP_PROCESS_FLOWCTRL);
578 PropertySet(key.c_str(), "true");
579 cout << "flow control by process is enabled" << endl;
580 } else if (propertyParm->flowSwitchStr == "pidoff") {
581 key = GetPropertyName(PROP_PROCESS_FLOWCTRL);
582 PropertySet(key.c_str(), "false");
583 cout << "flow control by process is disabled" << endl;
584 } else if (propertyParm->flowSwitchStr == "domainon") {
585 key = GetPropertyName(PROP_DOMAIN_FLOWCTRL);
586 PropertySet(key.c_str(), "true");
587 cout << "flow control by domain is enabled" << endl;
588 } else if (propertyParm->flowSwitchStr == "domainoff") {
589 key = GetPropertyName(PROP_DOMAIN_FLOWCTRL);
590 PropertySet(key.c_str(), "false");
591 cout << "flow control by domain is disabled" << endl;
592 } else {
593 cout << ParseErrorCode(ERR_FLOWCTRL_SWITCH_VALUE_INVALID) << endl;
594 return RET_FAIL;
595 }
596 break;
597
598 default:
599 break;
600 }
601 return RET_SUCCESS;
602 }
603
604
MultiQuerySplit(const std::string & src,const char & delim,std::vector<std::string> & vecSplit)605 int MultiQuerySplit(const std::string& src, const char& delim, std::vector<std::string>& vecSplit)
606 {
607 auto srcSize = src.length();
608 string::size_type findPos = 0;
609 string::size_type getPos = 0;
610 vecSplit.clear();
611
612 while (getPos < srcSize) {
613 findPos = src.find(delim, findPos);
614 if (string::npos == findPos) {
615 if (getPos < srcSize) {
616 vecSplit.push_back(src.substr(getPos, srcSize - getPos));
617 return 0;
618 }
619 } else if (findPos == getPos) {
620 vecSplit.push_back(std::string(""));
621 } else {
622 vecSplit.push_back(src.substr(getPos, findPos - getPos));
623 }
624 getPos = ++findPos;
625 if (getPos == srcSize) {
626 vecSplit.push_back(std::string(""));
627 return 0;
628 }
629 }
630 return 0;
631 }
632 } // namespace HiviewDFX
633 } // namespace OHOS
634