• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "TraceTool.h"
17 
18 #include "CommandParser.h"
19 #include "PreviewerEngineLog.h"
20 #include "TimeTool.h"
21 
22 using namespace std;
23 
InitPipe()24 void TraceTool::InitPipe()
25 {
26     if (socket != nullptr) {
27         socket.reset();
28         ELOG("TraceTool::InitPipe socket is not null");
29     }
30 
31     socket = std::make_unique<LocalSocket>();
32     if (socket == nullptr) {
33         FLOG("TraceTool::Connect socket memory allocation failed!");
34     }
35     string name = GetTracePipeName();
36     if (!socket->ConnectToServer(socket->GetTracePipeName(name), LocalSocket::READ_WRITE)) {
37         ELOG("TraceTool::pipe connect failed");
38         return;
39     }
40     isReady = true;
41     ELOG("TraceTool::pipe connect successed");
42 }
43 
GetInstance()44 TraceTool& TraceTool::GetInstance()
45 {
46     static TraceTool instance;
47     return instance;
48 }
49 
SendTraceData(const Json::Value & value)50 void TraceTool::SendTraceData(const Json::Value& value)
51 {
52     Json::StreamWriterBuilder builder;
53     builder.settings_["indentation"] = "";
54     ostringstream osStream;
55     std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
56     writer->write(value, &osStream);
57     *(GetInstance().socket) << osStream.str();
58 }
59 
HandleTrace(const string msg) const60 void TraceTool::HandleTrace(const string msg) const
61 {
62     if (!isReady) {
63         ILOG("Trace pipe is not prepared");
64         return;
65     }
66     Json::Value val = GetBaseInfo();
67     val["action"] = msg;
68     SendTraceData(val);
69 }
70 
TraceTool()71 TraceTool::TraceTool() : socket(nullptr), isReady(false)
72 {
73     InitPipe();
74 }
75 
~TraceTool()76 TraceTool::~TraceTool()
77 {
78     if (socket != nullptr) {
79         socket->DisconnectFromServer();
80         socket = nullptr;
81     }
82 }
83 
GetBaseInfo() const84 Json::Value TraceTool::GetBaseInfo() const
85 {
86     Json::Value val;
87     val["sid"] = "10007";
88     val["detail"]["ProjectId"] = CommandParser::GetInstance().GetProjectID();
89     val["detail"]["device"] = CommandParser::GetInstance().GetDeviceType();
90     val["detail"]["time"] = TimeTool::GetTraceFormatTime();
91     return val;
92 }
93 
GetTracePipeName() const94 string TraceTool::GetTracePipeName() const
95 {
96     return CommandParser::GetInstance().Value("ts");
97 }
98