• 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     std::lock_guard<std::mutex> lock(mutex_);
40     if (dumpDatas != nullptr) {
41         dumpDatas_ = dumpDatas;
42     } else {
43         return DumpStatus::DUMP_FAIL;
44     }
45     ptrReqCtl_ = parameter->getClientCallback();
46     path_ = parameter->GetOutputFilePath();
47     if ((fd_ < 0) && (!path_.empty())) {
48         fd_ = open(path_.c_str(), O_WRONLY | O_CREAT | O_APPEND, OPEN_ARGV);
49         if (fd_ < 0) {
50             return DumpStatus::DUMP_FAIL;
51         }
52     }
53     return DumpStatus::DUMP_OK;
54 }
55 
Execute()56 DumpStatus FDOutput::Execute()
57 {
58     if ((ptrReqCtl_ != nullptr) && (dumpDatas_ != nullptr)) {
59         OutMethod();
60     } else {
61         DUMPER_HILOGE(MODULE_COMMON, "FDOutput Execute failed");
62     }
63     return DumpStatus::DUMP_OK;
64 }
65 
AfterExecute()66 DumpStatus FDOutput::AfterExecute()
67 {
68     std::lock_guard<std::mutex> lock(mutex_);
69     if (dumpDatas_ != nullptr) {
70         dumpDatas_->clear();
71     }
72     return DumpStatus::DUMP_OK;
73 }
74 
OutMethod()75 void FDOutput::OutMethod()
76 {
77     std::lock_guard<std::mutex> lock(mutex_);
78     for (size_t i = 0; i < dumpDatas_->size(); i++) {
79         std::vector<std::string> line = dumpDatas_->at(i);
80         for (size_t j = 0; j < line.size(); j++) {
81             std::string str = line[j];
82             if ((i < dumpDatas_->size()) && (j == (line.size() - 1))) {
83                 NewLineMethod(str);
84             }
85             WriteToFd(str);
86         }
87     }
88 }
89 
WriteToFd(std::string & str)90 void FDOutput::WriteToFd(std::string &str)
91 {
92     int rawParamFd = ptrReqCtl_->GetOutputFd();
93     if (rawParamFd > -1) {
94         if (write(rawParamFd, str.c_str(), strlen(str.c_str())) == -1) {
95             DUMPER_HILOGE(MODULE_COMMON, "write to rawParamFd failed, errno: %{public}d", errno);
96         }
97         if (fsync(rawParamFd) == -1) {
98             DUMPER_HILOGD(MODULE_COMMON, "fsync to rawParamFd failed, errno: %{public}d", errno);
99         }
100     }
101     if (fd_ > -1) {
102         if (dprintf(fd_, "%s", str.c_str()) == -1) {
103             DUMPER_HILOGE(MODULE_COMMON, "dprintf to fd_ failed, errno: %{public}d", errno);
104         }
105     }
106 }
107 
NewLineMethod(std::string & str)108 void FDOutput::NewLineMethod(std::string &str)
109 {
110     if (str.find("\n") == std::string::npos) { // No line breaks
111         str = str + "\n";
112     }
113 }
114 } // namespace HiviewDFX
115 } // namespace OHOS
116