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 #undef LOG_DOMAIN
32 #define LOG_DOMAIN 0xD001400
33
34 #undef LOG_TAG
35 #define LOG_TAG "FramePainter"
36 #define LOGW(fmt, ...) HILOG_WARN(LOG_CORE, fmt, ##__VA_ARGS__)
37 #define LOGI(fmt, ...) HILOG_INFO(LOG_CORE, fmt, ##__VA_ARGS__)
38
FrameSaver()39 FrameSaver::FrameSaver()
40 {
41 struct stat saveDirectoryStat = {};
42 if (stat(saveDirectory, &saveDirectoryStat) && errno != ENOENT) {
43 LOGW("get stat '%{public}s' failed: %{public}s", saveDirectory, strerror(errno));
44 return;
45 }
46
47 if (errno == ENOENT) {
48 constexpr int directoryPermission = 0777; // drwxrwxrwx
49 if (mkdir(saveDirectory, directoryPermission)) {
50 LOGW("create directory '%{public}s' failed: %{public}s", saveDirectory, strerror(errno));
51 return;
52 }
53 }
54
55 std::stringstream ss;
56
57 char tmpPath[PATH_MAX] = {0};
58 ss << saveDirectory << "/" << GetRealPid() << ".log";
59 if (realpath(ss.str().c_str(), tmpPath) == nullptr) {
60 return;
61 }
62
63 ofs_.open(tmpPath, ofs_.out | ofs_.app);
64 }
65
~FrameSaver()66 FrameSaver::~FrameSaver()
67 {
68 ofs_.close();
69 }
70
SaveFrameEvent(const FrameEventType & type,int64_t timeNs)71 void FrameSaver::SaveFrameEvent(const FrameEventType &type, int64_t timeNs)
72 {
73 if (!ofs_.is_open()) {
74 LOGI("%{public}s %{public}s", GetNameByFrameEventType(type).c_str(), std::to_string(timeNs).c_str());
75 return;
76 }
77
78 ofs_ << GetNameByFrameEventType(type) << " " << timeNs << std::endl;
79 }
80 } // namespace Rosen
81 } // namespace OHOS
82