• 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 "frame_saver.h"
17 
18 #include <map>
19 #include <sstream>
20 #include <string>
21 #include <sys/stat.h>
22 #include <unistd.h>
23 
24 #include "hilog/log.h"
25 
26 #include "frame_info.h"
27 #include "sandbox_utils.h"
28 
29 namespace OHOS {
30 namespace Rosen {
31 namespace {
32 constexpr ::OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, 0xD001400, "FrameSaver" };
33 } // namespace
34 
FrameSaver()35 FrameSaver::FrameSaver()
36 {
37     struct stat saveDirectoryStat = {};
38     if (stat(saveDirectory, &saveDirectoryStat) && errno != ENOENT) {
39         ::OHOS::HiviewDFX::HiLog::Warn(LABEL,
40             "get stat '%{public}s' failed: %{public}s",
41             saveDirectory, strerror(errno));
42         return;
43     }
44 
45     if (errno == ENOENT) {
46         constexpr int directoryPermission = 0777; // drwxrwxrwx
47         if (mkdir(saveDirectory, directoryPermission)) {
48             ::OHOS::HiviewDFX::HiLog::Warn(LABEL,
49                 "create directory '%{public}s' failed: %{public}s",
50                 saveDirectory, strerror(errno));
51             return;
52         }
53     }
54 
55     std::stringstream ss;
56     ss << saveDirectory << "/" << GetRealPid() << ".log";
57     ofs_.open(ss.str(), ofs_.out | ofs_.app);
58 }
59 
~FrameSaver()60 FrameSaver::~FrameSaver()
61 {
62     ofs_.close();
63 }
64 
SaveFrameEvent(const FrameEventType & type,int64_t timeNs)65 void FrameSaver::SaveFrameEvent(const FrameEventType &type, int64_t timeNs)
66 {
67     if (!ofs_.is_open()) {
68         ::OHOS::HiviewDFX::HiLog::Info(LABEL, "%{public}s %{public}s",
69             GetNameByFrameEventType(type).c_str(), std::to_string(timeNs).c_str());
70         return;
71     }
72 
73     ofs_ << GetNameByFrameEventType(type) << " " << timeNs << std::endl;
74 }
75 } // namespace Rosen
76 } // namespace OHOS
77