• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "executor/fd_output.h"
17 #include <unistd.h>
18 #include <fcntl.h>
19 
20 namespace OHOS {
21 namespace HiviewDFX {
22 const mode_t FDOutput::OPEN_ARGV = 0664;
23 
FDOutput()24 FDOutput::FDOutput() : fd_(-1)
25 {
26 }
27 
~FDOutput()28 FDOutput::~FDOutput()
29 {
30     if (fd_ >= 0) {
31         close(fd_);
32     }
33     fd_ = -1;
34 }
35 
PreExecute(const std::shared_ptr<DumperParameter> & parameter,StringMatrix dumpDatas)36 DumpStatus FDOutput::PreExecute(const std::shared_ptr<DumperParameter>& parameter,
37     StringMatrix dumpDatas)
38 {
39     if (dumpDatas.get()) {
40         dumpDatas_ = dumpDatas;
41     } else {
42         return DumpStatus::DUMP_FAIL;
43     }
44     ptrReqCtl_ = parameter->getClientCallback();
45     path_ = parameter->GetOutputFilePath();
46     if ((fd_ < 0) && (!path_.empty())) {
47         fd_ = open(path_.c_str(), O_WRONLY | O_CREAT | O_APPEND, OPEN_ARGV);
48         if (fd_ < 0) {
49             return DumpStatus::DUMP_FAIL;
50         }
51     }
52     return DumpStatus::DUMP_OK;
53 }
54 
Execute()55 DumpStatus FDOutput::Execute()
56 {
57     if ((ptrReqCtl_ != nullptr) && (dumpDatas_ != nullptr)) {
58         OutMethod();
59     }
60     return DumpStatus::DUMP_OK;
61 }
62 
AfterExecute()63 DumpStatus FDOutput::AfterExecute()
64 {
65     if (dumpDatas_ != nullptr) {
66         dumpDatas_->clear();
67     }
68     return DumpStatus::DUMP_OK;
69 }
70 
OutMethod()71 void FDOutput::OutMethod()
72 {
73     for (size_t i = 0; i < dumpDatas_->size(); i++) {
74         std::vector<std::string> line = dumpDatas_->at(i);
75         for (size_t j = 0; j < line.size(); j++) {
76             std::string str = line[j];
77             if ((i < dumpDatas_->size()) && (j == (line.size() - 1))) {
78                 NewLineMethod(str);
79             }
80             int rawParamFd = ptrReqCtl_->GetOutputFd();
81             if (rawParamFd > -1) {
82                 write(rawParamFd, str.c_str(), strlen(str.c_str()));
83                 fsync(rawParamFd);
84             }
85             if (fd_ > -1) {
86                 dprintf(fd_, "%s", str.c_str());
87             }
88         }
89     }
90 }
91 
NewLineMethod(std::string & str)92 void FDOutput::NewLineMethod(std::string &str)
93 {
94     if (str.find("\n") == std::string::npos) { // No line breaks
95         str = str + "\n";
96     }
97 }
98 } // namespace HiviewDFX
99 } // namespace OHOS
100