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 24 #include "event.h" 25 #include "event_loop.h" 26 #include "log_store_ex.h" 27 #include "logger.h" 28 #include "plugin.h" 29 #include "sys_event.h" 30 31 #include "event_logger_config.h" 32 #include "event_thread_pool.h" 33 namespace OHOS { 34 namespace HiviewDFX { 35 struct BinderInfo { 36 int client; 37 int server; 38 unsigned long wait; 39 }; 40 41 class EventLogger : public Plugin, public FileDescriptorEventCallback { 42 public: EventLogger()43 EventLogger() : logStore_(std::make_unique<LogStoreEx>(LOGGER_EVENT_LOG_PATH, true)), 44 startTime_(time(nullptr)), 45 inotifyFd_(0) {}; ~EventLogger()46 ~EventLogger() {}; 47 bool OnEvent(std::shared_ptr<Event> &event) override; 48 void OnLoad() override; 49 void OnUnload() override; 50 bool IsInterestedPipelineEvent(std::shared_ptr<Event> event) override; 51 bool OnFileDescriptorEvent(int fd, int type) override; 52 int32_t GetPollFd() override; 53 int32_t GetPollType() override; 54 private: 55 static const inline std::string LOGGER_EVENT_LOG_PATH = "/data/log/eventlog"; 56 static const inline std::string MONITOR_STACK_LOG_PATH = "/data/log/faultlog/temp"; 57 static const inline std::string MONITOR_STACK_FLIE_NAME[] = { 58 "jsstack", 59 }; 60 static const inline std::string MONITOR_LOG_PATH[] = { 61 MONITOR_STACK_LOG_PATH, 62 }; 63 static constexpr int EVENT_MAX_ID = 1000000; 64 static constexpr int MAX_FILE_NUM = 500; 65 static constexpr int MAX_FOLDER_SIZE = 50 * 1024 * 1024; 66 67 std::unique_ptr<LogStoreEx> logStore_; 68 uint64_t startTime_; 69 std::unordered_map<std::string, std::time_t> eventTagTime_; 70 int inotifyFd_; 71 std::unordered_map<int, std::string> fileMap_; 72 std::unordered_map<std::string, EventLoggerConfig::EventLoggerConfigData> eventLoggerConfig_; 73 int const maxEventPoolCount = 5; 74 std::unique_ptr<EventThreadPool> eventPool_; 75 76 void StartLogCollect(std::shared_ptr<SysEvent> event); 77 bool JudgmentRateLimiting(std::shared_ptr<SysEvent> event); 78 bool WriteCommonHead(int fd, std::shared_ptr<SysEvent> event); 79 bool PostEvent(std::shared_ptr<SysEvent> event); 80 bool UpdateDB(std::shared_ptr<SysEvent> event, std::string logFile); 81 void CreateAndPublishEvent(std::string& dirPath, std::string& fileName); 82 }; 83 } // namespace HiviewDFX 84 } // namespace OHOS 85 #endif // HIVIEW_PLUGIN_EVENT_LOG_COLLECTOR_H 86