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