• 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 "JsonReader.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 Json2::Value & value)51 void TraceTool::SendTraceData(const Json2::Value& value)
52 {
53     *(GetInstance().socket) << value.ToString();
54 }
55 
HandleTrace(const string msg) const56 void TraceTool::HandleTrace(const string msg) const
57 {
58     if (!isReady) {
59         ILOG("Trace pipe is not prepared");
60         return;
61     }
62     Json2::Value val = JsonReader::CreateObject();
63     val.Add("sid", "10007");
64     Json2::Value detail = JsonReader::CreateObject();
65     detail.Add("ProjectId", CommandParser::GetInstance().GetProjectID().c_str());
66     detail.Add("device", CommandParser::GetInstance().GetDeviceType().c_str());
67     detail.Add("time", TimeTool::GetTraceFormatTime().c_str());
68     val.Add("detail", detail);
69     val.Add("action", msg.c_str());
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 
GetTracePipeName() const86 string TraceTool::GetTracePipeName() const
87 {
88     return CommandParser::GetInstance().Value("ts");
89 }
90