• 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     } else {
60         DUMPER_HILOGE(MODULE_COMMON, "FDOutput Execute end");
61     }
62     return DumpStatus::DUMP_OK;
63 }
64 
AfterExecute()65 DumpStatus FDOutput::AfterExecute()
66 {
67     if (dumpDatas_ != nullptr) {
68         dumpDatas_->clear();
69     }
70     return DumpStatus::DUMP_OK;
71 }
72 
OutMethod()73 void FDOutput::OutMethod()
74 {
75     for (size_t i = 0; i < dumpDatas_->size(); i++) {
76         std::vector<std::string> line = dumpDatas_->at(i);
77         for (size_t j = 0; j < line.size(); j++) {
78             std::string str = line[j];
79             if ((i < dumpDatas_->size()) && (j == (line.size() - 1))) {
80                 NewLineMethod(str);
81             }
82             int rawParamFd = ptrReqCtl_->GetOutputFd();
83             if (rawParamFd > -1) {
84                 write(rawParamFd, str.c_str(), strlen(str.c_str()));
85                 fsync(rawParamFd);
86             }
87             if (fd_ > -1) {
88                 dprintf(fd_, "%s", str.c_str());
89             }
90         }
91     }
92 }
93 
NewLineMethod(std::string & str)94 void FDOutput::NewLineMethod(std::string &str)
95 {
96     if (str.find("\n") == std::string::npos) { // No line breaks
97         str = str + "\n";
98     }
99 }
100 } // namespace HiviewDFX
101 } // namespace OHOS
102