• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #ifndef OHOS_ROSEN_SESSION_CHANGE_RECORDER_H
17 #define OHOS_ROSEN_SESSION_CHANGE_RECORDER_H
18 
19 #include <cstdint>
20 #include <queue>
21 #include <string>
22 #include <thread>
23 #include <unordered_map>
24 
25 #include "window_manager_hilog.h"
26 #include "wm_single_instance.h"
27 #include "ws_common.h"
28 
29 namespace OHOS::Rosen {
30 /**
31  * @brief Record type of session change
32  */
33 enum class RecordType : uint32_t {
34     RECORD_TYPE_BEGIN = 0,
35     SESSION_STATE_RECORD,
36     VISIBLE_RECORD,
37     PRIVACY_MODE,
38     ORIENTAION_RECORD,
39     EVENT_RECORD,
40     RECORD_TYPE_END,
41 };
42 
43 /**
44  * @brief Scene session change info
45  */
46 struct SceneSessionChangeInfo {
47     int32_t persistentId_ = INVALID_SESSION_ID;
48     std::string changeInfo_ = "";
49     WmsLogTag logTag_ = WmsLogTag::DEFAULT;
50     std::string time_ = "";
51 };
52 
53 class SessionChangeRecorder {
54 WM_DECLARE_SINGLE_INSTANCE_BASE(SessionChangeRecorder)
55 public:
56     /**
57      * @brief Record scene session change info.
58      *
59      * @param recordType The type of record info.
60      * @param changeInfo The detailed change info.
61      */
62     WSError RecordSceneSessionChange(RecordType recordType, SceneSessionChangeInfo& changeInfo);
63 
64     /**
65      * @brief Set record size
66      *
67      * @param recordType The type of record info.
68      * @param recordSize Set the size of records for each record type.
69      *                   The valid range is 1 to 10.
70      */
71     WSError SetRecordSize(RecordType recordType, uint32_t recordSize);
72 
73     void Init();
74     void GetSceneSessionNeedDumpInfo(const std::vector<std::string>& dumpParams, std::string& dumpInfo);
75     std::atomic<bool> stopLogFlag_ { false };
76     std::atomic<bool> isInitFlag_ { false };
77 
78 private:
79     SessionChangeRecorder() = default;
80     virtual ~SessionChangeRecorder();
81     void RecordLog(RecordType recordType, SceneSessionChangeInfo& changeInfo);
82     void PrintLog(std::unordered_map<RecordType, std::queue<SceneSessionChangeInfo>> sceneSessionChangeNeedDumpMap);
83     void RecordDump(RecordType recordType, SceneSessionChangeInfo& changeInfo);
84     std::string FormatDumpInfoToJsonString (uint32_t specifiedRecordType, int32_t specifiedWindowId,
85     std::unordered_map<RecordType, std::queue<SceneSessionChangeInfo>>& dumpMap);
86     void SimplifyDumpInfo(std::string& dumpInfo, std::string preCompressInfo);
87     int CompressString(const char* in_str, size_t in_len, std::string& out_str, int level);
88 
89     std::unordered_map<RecordType, std::queue<SceneSessionChangeInfo>> sceneSessionChangeNeedLogMap_;
90     std::unordered_map<RecordType, std::queue<SceneSessionChangeInfo>> sceneSessionChangeNeedDumpMap_;
91     std::unordered_map<RecordType, uint32_t> recordSizeMap_;
92     std::mutex sessionChangeRecorderMutex_;
93     uint32_t currentLogSize_ = 0;
94     std::thread mThread;
95 };
96 } // namespace OHOS::Rosen
97 #endif // OHOS_ROSEN_SESSION_CHANGE_RECORDER_H