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