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 #ifndef HIVIEW_PLUGIN_EVENT_LOG_COLLECTOR_H 16 #define HIVIEW_PLUGIN_EVENT_LOG_COLLECTOR_H 17 18 #include <ctime> 19 #include <map> 20 #include <memory> 21 #include <string> 22 #include <unordered_map> 23 #include <mutex> 24 25 #include "event.h" 26 #include "event_loop.h" 27 #include "log_store_ex.h" 28 #include "logger.h" 29 #include "plugin.h" 30 #include "sys_event.h" 31 32 #include "active_key_event.h" 33 #include "event_logger_config.h" 34 #include "event_thread_pool.h" 35 36 namespace OHOS { 37 namespace HiviewDFX { 38 struct BinderInfo { 39 int client; 40 int server; 41 unsigned long wait; 42 }; 43 44 class EventLogger : public EventListener, public Plugin { 45 public: EventLogger()46 EventLogger() : logStore_(std::make_shared<LogStoreEx>(LOGGER_EVENT_LOG_PATH, false)), 47 startTime_(time(nullptr)) {}; ~EventLogger()48 ~EventLogger() {}; 49 bool OnEvent(std::shared_ptr<Event> &event) override; 50 void OnLoad() override; 51 void OnUnload() override; 52 bool IsInterestedPipelineEvent(std::shared_ptr<Event> event) override; 53 std::string GetListenerName() override; 54 void OnUnorderedEvent(const Event& msg) override; 55 private: 56 static const inline std::string LOGGER_EVENT_LOG_PATH = "/data/log/eventlog"; 57 static const inline std::string MONITOR_STACK_LOG_PATH = "/data/log/faultlog/temp"; 58 static const inline std::string LONG_PRESS = "LONG_PRESS"; 59 static const inline std::string AP_S_PRESS6S = "AP_S_PRESS6S"; 60 static const inline std::string REBOOT_REASON = "reboot_reason"; 61 static const inline std::string NORMAL_RESET_TYPE = "normal_reset_type"; 62 static const inline std::string PATTERN_WITHOUT_SPACE = "\\s*=\\s*([^ \\n]*)"; 63 static const inline std::string DOMAIN_LONGPRESS = "KERNEL_VENDOR"; 64 static const inline std::string STRINGID_LONGPRESS = "COM_LONG_PRESS"; 65 static const inline std::string MONITOR_STACK_FLIE_NAME[] = { 66 "jsstack", 67 }; 68 static const inline std::string MONITOR_LOG_PATH[] = { 69 MONITOR_STACK_LOG_PATH, 70 }; 71 static constexpr int EVENT_MAX_ID = 1000000; 72 static constexpr int MAX_FILE_NUM = 500; 73 static constexpr int MAX_FOLDER_SIZE = 50 * 1024 * 1024; 74 75 std::shared_ptr<LogStoreEx> logStore_; 76 uint64_t startTime_; 77 std::unordered_map<std::string, std::time_t> eventTagTime_; 78 std::unordered_map<int, std::string> fileMap_; 79 std::unordered_map<std::string, EventLoggerConfig::EventLoggerConfigData> eventLoggerConfig_; 80 std::shared_ptr<EventLoop> threadLoop_ = nullptr; 81 int const maxEventPoolCount = 5; 82 std::shared_ptr<EventThreadPool> eventPool_; 83 std::mutex intervalMutex_; 84 std::unique_ptr<ActiveKeyEvent> activeKeyEvent_; 85 std::string cmdlinePath_ = "/proc/cmdline"; 86 std::string cmdlineContent_ = ""; 87 std::vector<std::string> rebootReasons_; 88 89 void StartLogCollect(std::shared_ptr<SysEvent> event); 90 int Getfile(std::shared_ptr<SysEvent> event, std::string& logFile); 91 bool JudgmentRateLimiting(std::shared_ptr<SysEvent> event); 92 bool WriteCommonHead(int fd, std::shared_ptr<SysEvent> event); 93 bool WriteFreezeJsonInfo(int fd, int jsonFd, std::shared_ptr<SysEvent> event); 94 bool UpdateDB(std::shared_ptr<SysEvent> event, std::string logFile); 95 void CreateAndPublishEvent(std::string& dirPath, std::string& fileName); 96 bool IsHandleAppfreeze(std::shared_ptr<SysEvent> event); 97 void CheckEventOnContinue(std::shared_ptr<SysEvent> event); 98 bool CanProcessRebootEvent(const Event& event); 99 void ProcessRebootEvent(); 100 std::string GetRebootReason() const; 101 void GetCmdlineContent(); 102 void GetRebootReasonConfig(); 103 bool GetMatchString(const std::string& src, std::string& dst, const std::string& pattern) const; 104 }; 105 } // namespace HiviewDFX 106 } // namespace OHOS 107 #endif // HIVIEW_PLUGIN_EVENT_LOG_COLLECTOR_H 108